diff --git a/internal/server/authz.go b/internal/server/authz.go index 6b794a5..dc6d4ab 100644 --- a/internal/server/authz.go +++ b/internal/server/authz.go @@ -153,6 +153,12 @@ func (s *Server) requireAuthz(next http.Handler) http.Handler { if p.IsUser() && isIntakeUserPath(r.URL.Path) { group = "intake.user" } + // A CLI that signs people in (group licences: werkpilot-cli, + // firmwerk-cli) may also send their feedback (werkpilot#86) without + // another grant; the handler takes user tokens only. + if group == "feedback" && p.IsUser() && !s.Authz.Allowed(p.ClientID, group) && s.Authz.Allowed(p.ClientID, "licences") { + group = "licences" + } if !s.Authz.Allowed(p.ClientID, group) { s.Log.Warn("client not authorized for route group", "azp", p.ClientID, "group", group, "path", r.URL.Path) writeError(w, http.StatusForbidden, "FORBIDDEN_CLIENT", "client "+p.ClientID+" may not call "+group) diff --git a/internal/server/authz_test.go b/internal/server/authz_test.go index 3cf1a33..8914bd9 100644 --- a/internal/server/authz_test.go +++ b/internal/server/authz_test.go @@ -30,7 +30,8 @@ func TestAuthzRules(t *testing.T) { {"portal-svc", "GET", "/v1/internal/connections/c1", false}, {"portal-svc", "POST", "/v1/tenants/t1/connections", true}, {"pruefwerk-svc", "POST", "/v1/tenants/t1/connection-invites", false}, - // Feedback (werkpilot#86): its own group, not licences or usage. + // Feedback (werkpilot#86): its own group. User tokens from clients + // with licences also pass (requireAuthz, feedback_test.go). {"werkpilot-cli", "POST", "/v1/feedback", true}, {"firmwerk-cli", "POST", "/v1/feedback", false}, {"portal-svc", "POST", "/v1/feedback", false}, diff --git a/internal/server/feedback_test.go b/internal/server/feedback_test.go index 6c504ee..c6e4ce0 100644 --- a/internal/server/feedback_test.go +++ b/internal/server/feedback_test.go @@ -72,8 +72,8 @@ type feedbackFixture struct { now time.Time } -// newFeedbackFixture: the deployed AUTHZ_RULES shape for the CLIs, with -// feedback granted to werkpilot-cli only, and principals by name. +// newFeedbackFixture: the deployed AUTHZ_RULES shape (the CLIs hold +// licences and usage, not feedback), and principals by name. func newFeedbackFixture(t *testing.T) *feedbackFixture { t.Helper() mem := store.NewMemory() @@ -88,15 +88,18 @@ func newFeedbackFixture(t *testing.T) *feedbackFixture { return &authn.Principal{Subject: sub, ClientID: client, TenantID: tenantID, Email: email, OrgRoles: []string{"USER"}} } as := map[string]*authn.Principal{ - "user": cli("werkpilot-cli", "u-1", acme.ID, "dev@acme.test"), - "colleague": cli("werkpilot-cli", "u-2", acme.ID, ""), - "stranger": cli("werkpilot-cli", "u-x", other.ID, "x@other.test"), - "firmwerk": cli("firmwerk-cli", "u-1", acme.ID, "dev@acme.test"), - "service": {Subject: "svc-portal", ClientID: "portal-svc"}, + "user": cli("werkpilot-cli", "u-1", acme.ID, "dev@acme.test"), + "colleague": cli("werkpilot-cli", "u-2", acme.ID, ""), + "stranger": cli("werkpilot-cli", "u-x", other.ID, "x@other.test"), + "firmwerk": cli("firmwerk-cli", "u-1", acme.ID, "dev@acme.test"), + "service": {Subject: "svc-portal", ClientID: "portal-svc"}, + "portal": cli("portal-prod", "u-1", acme.ID, "dev@acme.test"), + "granted": cli("other-cli", "u-1", acme.ID, "dev@acme.test"), + "firmwerk-svc": {Subject: "svc-fw", ClientID: "firmwerk-svc-prod"}, } f.s = &Server{ Log: slog.New(slog.NewTextHandler(f.logs, nil)), Store: mem, Keycloak: keycloak.NewMock(), - Authz: ParseAuthzRules("portal-svc:*;werkpilot-cli:licences,usage,feedback;firmwerk-cli:licences,usage"), + Authz: ParseAuthzRules("portal-svc:*;werkpilot-cli:licences,usage;firmwerk-cli:licences,usage;portal-prod:role-dataset.user,intake.user;other-cli:feedback;firmwerk-svc-prod:licences,usage"), Cfg: &config.Config{FeedbackGiteaURL: f.gitea.srv.URL + "/", FeedbackGiteaRepo: "platform/werkpilot-feedback", FeedbackGiteaToken: feedbackToken}, Now: func() time.Time { return f.now }, @@ -284,18 +287,27 @@ func TestFeedback_tenantComesFromThePrincipal(t *testing.T) { } } -func TestFeedback_needsAUserTokenAndTheGroup(t *testing.T) { +// The CLIs reach the route with the grants they already hold in the +// deployed AUTHZ_RULES (licences, usage): a user token from a client with +// licences counts as holding feedback. Other clients need the group itself. +func TestFeedback_whoReachesIt(t *testing.T) { f := newFeedbackFixture(t) + for _, as := range []string{"user", "firmwerk", "granted"} { + if code, out, raw := f.post(t, as, feedbackJSON(nil)); code != http.StatusCreated || out["id"] != float64(42) { + t.Errorf("%q: %d %s", as, code, raw) + } + } for as, want := range map[string]string{ - "service": "user_token_required", - "": "user_token_required", // AUTH_ENABLED=false: no principal, no tenant - "firmwerk": "FORBIDDEN_CLIENT", + "service": "user_token_required", + "": "user_token_required", // AUTH_ENABLED=false: no principal, no tenant + "portal": "FORBIDDEN_CLIENT", // a user token, but its client holds no licences + "firmwerk-svc": "FORBIDDEN_CLIENT", // licences, but a service token } { if code, out, raw := f.post(t, as, feedbackJSON(nil)); code != http.StatusForbidden || out["error"] != want { t.Errorf("%q: %d %s", as, code, raw) } } - if n := len(f.gitea.sent()); n != 0 { + if n := len(f.gitea.sent()); n != 3 { t.Fatalf("gitea called %d times", n) } } diff --git a/openapi.yaml b/openapi.yaml index 12ea6d6..ee7b2b7 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -2646,7 +2646,8 @@ paths: block (tenant slug and id, the user's subject and e-mail, product, version, device, kind, time received in UTC), a `---` rule, then `body` verbatim. Only the issue number is returned. Route group - `feedback`. At most 10 reports per person and hour. Fields are + `feedback`; a user token from a client holding `licences` (the + CLIs) passes without it. At most 10 reports per person and hour. Fields are trimmed; in title, product, version and device, line breaks and other control characters become spaces. Over-long fields are refused, not cut. @@ -2675,7 +2676,7 @@ paths: properties: id: { type: integer, description: "The issue number in the feedback repo." } "400": { $ref: "#/components/responses/BadRequest" } - "403": { description: "Not a user token (user_token_required), or the client lacks the feedback group." } + "403": { description: "Not a user token (user_token_required), or the client holds neither feedback nor licences (FORBIDDEN_CLIENT)." } "413": { description: "body exceeds 200000 bytes (too_large)." } "429": { description: "More than 10 reports from this person in the last hour (rate_limited)." } "502": { description: "Gitea did not create the issue (feedback_failed)." }