fix(observability-proxy): require namespace access on application roots

The application roots asked only for the application subresource. Their
pod and PVC branches select by namespace and name across the whole
lookup window, so a caller granted only application "a" received the
older series of application "b" whenever "b" had used one of the same
pod or PVC names. The lineage labels the scope rests on can also be set
by whoever creates an unowned pod.

Until the scope carries application identity on every branch, the
application roots also require read access to the namespace root, the
same review the namespace root asks. Every tenant role that reads
applications already holds it, so no current grant loses access; a
grant narrowed by resourceNames alone is refused instead of trusted.

Assisted-by: LLM
Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
ZverGuy committed 2026-11-10 04:03:24 +07:00
1 parent dd7116ea45
commit c4593d4c3e
6 files changed
+75 -21

No files matched your search

+6 -5
View File
@@ -76,11 +76,12 @@ func nameBranch(ns, label string, names []string) string {
// stamped (cAdvisor, kubelet volume stats). Every branch carries the
// namespace, so their OR stays inside it.
//
// Within the namespace, both the identity and the pod and PVC lists come
// from lineage labels on pods and PVCs. They separate applications for a
// grant narrowed by resourceNames only while whoever creates a pod or PVC
// cannot choose those labels; the lineage webhook keeps them as set on an
// object it finds no owner for, and the logs scope rests on the same.
// Within the namespace this narrows, it does not isolate: the identity and
// the pod and PVC lists come from lineage labels that the lineage webhook
// keeps as set on an object it finds no owner for, and the pod and PVC
// branches match by name, so a name another application held within the
// lookup window pulls that application's series in. Route.Reviews makes
// the application roots require read access to the namespace root too.
func applicationSelectors(ns, kind, name string, pods, pvcs []string) []string {
out := []string{identitySelector(ns, seriesKindLabel, seriesNameLabel, kind, name)}
for _, b := range []string{nameBranch(ns, "pod", pods), nameBranch(ns, "persistentvolumeclaim", pvcs)} {
+15
View File
@@ -133,6 +133,21 @@ func ParseRoute(escapedPath string) (Route, *HTTPError) {
// ResourceAttributes is the single SubjectAccessReview a route requires. It
// depends on the path only, so a POST query authorizes exactly like a GET.
// Reviews lists every access a request on the route needs. An application
// root also needs read access to its namespace root: its pod and PVC
// branches match by name across the window, so a name another application
// held earlier reaches that application's series, and a grant narrowed by
// resourceNames would read more than it names.
func (r Route) Reviews() []authorizationv1.ResourceAttributes {
switch r.Kind {
case ApplicationLog:
return []authorizationv1.ResourceAttributes{Route{Kind: NamespaceLogs, Namespace: r.Namespace}.ResourceAttributes(), r.ResourceAttributes()}
case ApplicationMetrics:
return []authorizationv1.ResourceAttributes{Route{Kind: NamespaceMetrics, Namespace: r.Namespace}.ResourceAttributes(), r.ResourceAttributes()}
}
return []authorizationv1.ResourceAttributes{r.ResourceAttributes()}
}
func (r Route) ResourceAttributes() authorizationv1.ResourceAttributes {
switch r.Kind {
case NamespaceLogs:
+24
View File
@@ -18,6 +18,7 @@ package observabilityproxy
import (
"net/http"
"slices"
"testing"
authorizationv1 "k8s.io/api/authorization/v1"
@@ -130,3 +131,26 @@ func TestRouteResourceAttributes(t *testing.T) {
}
}
}
func TestRouteReviews(t *testing.T) {
nsLogs := authorizationv1.ResourceAttributes{Verb: "list", Group: "cozystack.io", Resource: "logs", Namespace: "a"}
nsMetrics := authorizationv1.ResourceAttributes{Verb: "list", Group: "cozystack.io", Resource: "metrics", Namespace: "a"}
tests := []struct {
route Route
want []authorizationv1.ResourceAttributes
}{
{Route{Kind: NamespaceLogs, Namespace: "a"}, []authorizationv1.ResourceAttributes{nsLogs}},
{Route{Kind: NamespaceMetrics, Namespace: "a"}, []authorizationv1.ResourceAttributes{nsMetrics}},
{Route{Kind: ApplicationLog, Namespace: "a", Plural: "postgreses", Name: "db"}, []authorizationv1.ResourceAttributes{
nsLogs, {Verb: "get", Group: "apps.cozystack.io", Resource: "postgreses", Subresource: "log", Name: "db", Namespace: "a"},
}},
{Route{Kind: ApplicationMetrics, Namespace: "a", Plural: "postgreses", Name: "db"}, []authorizationv1.ResourceAttributes{
nsMetrics, {Verb: "get", Group: "apps.cozystack.io", Resource: "postgreses", Subresource: "metrics", Name: "db", Namespace: "a"},
}},
}
for _, tt := range tests {
if got := tt.route.Reviews(); !slices.Equal(got, tt.want) {
t.Errorf("%v.Reviews() = %+v, want %+v", tt.route.Kind, got, tt.want)
}
}
}
+9 -7
View File
@@ -451,13 +451,15 @@ func (s *Server) authorize(r *http.Request, route Route) (string, *HTTPError) {
slog.Error("token review failed", "error", err)
return "", errorf(http.StatusServiceUnavailable, "authentication unavailable")
}
allowed, err := s.auth.Authorize(ctx, user, route.ResourceAttributes())
if err != nil {
slog.Error("subject access review failed", "error", err)
return "", errorf(http.StatusServiceUnavailable, "authorization unavailable")
}
if !allowed {
return "", errorf(http.StatusForbidden, "forbidden")
for _, attrs := range route.Reviews() {
allowed, err := s.auth.Authorize(ctx, user, attrs)
if err != nil {
slog.Error("subject access review failed", "error", err)
return "", errorf(http.StatusServiceUnavailable, "authorization unavailable")
}
if !allowed {
return "", errorf(http.StatusForbidden, "forbidden")
}
}
return user.Username, nil
}
+20 -8
View File
@@ -31,11 +31,18 @@ import (
const appRoot = "/apis/apps.cozystack.io/v1alpha1/namespaces/tenant-a/postgreses/"
// appPolicy lets alice read every postgres in tenant-a, as the view role's
// apps.cozystack.io wildcard does, and bob only "a", as a grant narrowed
// with resourceNames does.
// appPolicy lets alice read every postgres in tenant-a and the namespace
// roots, as the view role does. bob and carol may read only postgres "a",
// as a grant narrowed with resourceNames does; carol may also read the
// namespace roots.
func appPolicy(user string, a *authorizationv1.ResourceAttributes) bool {
if a.Namespace != "tenant-a" || a.Verb != "get" || a.Group != "apps.cozystack.io" || a.Resource != "postgreses" ||
if a.Namespace != "tenant-a" {
return false
}
if a.Verb == "list" && a.Group == "cozystack.io" && (a.Resource == "logs" || a.Resource == "metrics") && a.Name == "" && a.Subresource == "" {
return user == "alice" || user == "carol"
}
if a.Verb != "get" || a.Group != "apps.cozystack.io" || a.Resource != "postgreses" ||
(a.Subresource != "log" && a.Subresource != "metrics") {
return false
}
@@ -270,7 +277,10 @@ func TestServerApplicationMetricsBranches(t *testing.T) {
}
// A grant narrowed to application "a" by resourceNames must not reach "b"
// through any endpoint of either root.
// through any endpoint of either root. Without read access to the
// namespace roots it does not reach "a" either: the pod and PVC branches
// match by name, and a name another application used earlier in the
// window would pull its series in.
func TestServerApplicationNarrowedGrant(t *testing.T) {
h := newPolicyHarness(t, Config{}, appMetricsUpstream([]string{"a-0"}, nil, http.StatusOK), appPolicy, nil)
paths := []string{
@@ -288,15 +298,17 @@ func TestServerApplicationNarrowedGrant(t *testing.T) {
"log/select/logsql/facets",
}
for _, p := range paths {
if got := h.status(http.MethodGet, appRoot+"b/"+p, "bob-token"); got != http.StatusForbidden {
t.Errorf("b/%s answered %d, want 403", p, got)
for _, denied := range []struct{ app, token string }{{"b", "bob-token"}, {"b", "carol-token"}, {"a", "bob-token"}} {
if got := h.status(http.MethodGet, appRoot+denied.app+"/"+p, denied.token); got != http.StatusForbidden {
t.Errorf("%s on %s/%s answered %d, want 403", denied.token, denied.app, p, got)
}
}
}
if n := len(h.requests()); n != 0 {
t.Fatalf("upstream saw %d requests for a denied application", n)
}
for _, p := range []string{"metrics/api/v1/labels", "log/select/logsql/query"} {
if got := h.status(http.MethodGet, appRoot+"a/"+p, "bob-token"); got != http.StatusOK {
if got := h.status(http.MethodGet, appRoot+"a/"+p, "carol-token"); got != http.StatusOK {
t.Errorf("a/%s answered %d, want 200", p, got)
}
}
+1 -1
View File
@@ -64,7 +64,7 @@ func newHarness(t *testing.T, cfg Config, handler http.HandlerFunc) *harness {
// and plural-to-kind mapping; nil kinds knows only postgreses.
func newPolicyHarness(t *testing.T, cfg Config, handler http.HandlerFunc, allow func(string, *authorizationv1.ResourceAttributes) bool, kinds KindLookup) *harness {
h := &harness{t: t}
h.kube = newFakeKube(map[string]string{"good": "alice", "bob-token": "bob"}, allow)
h.kube = newFakeKube(map[string]string{"good": "alice", "bob-token": "bob", "carol-token": "carol"}, allow)
if kinds == nil {
kinds = newTestKinds(t, true, appDefinition("postgres", "Postgres", "postgreses"))
}