diff --git a/internal/observabilityproxy/appscope.go b/internal/observabilityproxy/appscope.go index e259831da..55b51afe6 100644 --- a/internal/observabilityproxy/appscope.go +++ b/internal/observabilityproxy/appscope.go @@ -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)} { diff --git a/internal/observabilityproxy/route.go b/internal/observabilityproxy/route.go index 56ee9e496..1baff4578 100644 --- a/internal/observabilityproxy/route.go +++ b/internal/observabilityproxy/route.go @@ -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: diff --git a/internal/observabilityproxy/route_test.go b/internal/observabilityproxy/route_test.go index f34311e0f..a8e85b377 100644 --- a/internal/observabilityproxy/route_test.go +++ b/internal/observabilityproxy/route_test.go @@ -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) + } + } +} diff --git a/internal/observabilityproxy/server.go b/internal/observabilityproxy/server.go index fd74491b5..cf873af72 100644 --- a/internal/observabilityproxy/server.go +++ b/internal/observabilityproxy/server.go @@ -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 } diff --git a/internal/observabilityproxy/server_app_test.go b/internal/observabilityproxy/server_app_test.go index 6d353d00f..a767e7f9e 100644 --- a/internal/observabilityproxy/server_app_test.go +++ b/internal/observabilityproxy/server_app_test.go @@ -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) } } diff --git a/internal/observabilityproxy/server_test.go b/internal/observabilityproxy/server_test.go index 168c402fe..ce2ba669b 100644 --- a/internal/observabilityproxy/server_test.go +++ b/internal/observabilityproxy/server_test.go @@ -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")) }