diff --git a/internal/webui/api_access_denials.go b/internal/webui/access_denials.go similarity index 81% rename from internal/webui/api_access_denials.go rename to internal/webui/access_denials.go index 32ffff63b..d70947702 100644 --- a/internal/webui/api_access_denials.go +++ b/internal/webui/access_denials.go @@ -13,8 +13,8 @@ const defaultDenialsListLimit = 100 // first. The route's read_audit grant limits it to the org's admins and // owners, and the store scopes the list by the credential's org, so no // request field chooses the org an operator reads. -func (s *Server) handleListDenials(w http.ResponseWriter, r *http.Request) { - if s.DenialLister == nil { +func (a *accessAPI) handleListDenials(w http.ResponseWriter, r *http.Request) { + if a.denials == nil { http.Error(w, "denial records unavailable", http.StatusServiceUnavailable) return } @@ -22,7 +22,7 @@ func (s *Server) handleListDenials(w http.ResponseWriter, r *http.Request) { if err != nil || limit <= 0 || limit > 1000 { limit = defaultDenialsListLimit } - denials, err := s.DenialLister.ListDenials(r.Context(), policyOrg(r), int(limit)) + denials, err := a.denials.ListDenials(r.Context(), policyOrg(r), int(limit)) if err != nil { writeControlPlaneError(w, err) return diff --git a/internal/webui/api_access_denials_test.go b/internal/webui/access_denials_test.go similarity index 98% rename from internal/webui/api_access_denials_test.go rename to internal/webui/access_denials_test.go index d488f4d70..b7367e397 100644 --- a/internal/webui/api_access_denials_test.go +++ b/internal/webui/access_denials_test.go @@ -95,7 +95,7 @@ func TestHandleListDenialsScopesToCallersOrg(t *testing.T) { ctx := org.WithOrg(access.WithPrincipal(t.Context(), principal), tt.caller) req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/access/denials"+tt.query, nil) rec := httptest.NewRecorder() - srv.handleListDenials(rec, req) + newAccessAPI(srv).handleListDenials(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d, want 200 body=%s", rec.Code, rec.Body.String()) } @@ -129,7 +129,7 @@ func TestHandleListDenialsUnwired(t *testing.T) { srv := &Server{} req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/access/denials", nil) rec := httptest.NewRecorder() - srv.handleListDenials(rec, req) + newAccessAPI(srv).handleListDenials(rec, req) if rec.Code != http.StatusServiceUnavailable { t.Fatalf("status = %d, want 503", rec.Code) } diff --git a/internal/webui/api_access_policies.go b/internal/webui/access_policies.go similarity index 70% rename from internal/webui/api_access_policies.go rename to internal/webui/access_policies.go index 6be089ba1..5776e6521 100644 --- a/internal/webui/api_access_policies.go +++ b/internal/webui/access_policies.go @@ -9,6 +9,24 @@ import ( "github.com/samcharles93/archie-core/internal/domain/org" ) +// accessAPI owns the /api/access routes: the stored policy chain and the +// recorded denials. Either store may be nil; its routes then answer 503. +type accessAPI struct { + policies access.PolicyStore + denials access.DenialStore +} + +func newAccessAPI(s *Server) *accessAPI { + return &accessAPI{policies: s.Policies, denials: s.DenialLister} +} + +func (a *accessAPI) register(mux *http.ServeMux) { + mux.HandleFunc("GET /api/access/policies", a.handleListPolicies) + mux.HandleFunc("PUT /api/access/policies", a.handlePutPolicy) + mux.HandleFunc("DELETE /api/access/policies", a.handleDeletePolicy) + mux.HandleFunc("GET /api/access/denials", a.handleListDenials) +} + // policyOrg is the org a policy request acts in: the caller's. func policyOrg(r *http.Request) org.OrgID { if principal, ok := access.PrincipalFromContext(r.Context()); ok { @@ -19,12 +37,12 @@ func policyOrg(r *http.Request) org.OrgID { // handleListPolicies lists the caller's org policies, and the instance // policies when the caller is the instance owner. -func (s *Server) handleListPolicies(w http.ResponseWriter, r *http.Request) { - if s.Policies == nil { +func (a *accessAPI) handleListPolicies(w http.ResponseWriter, r *http.Request) { + if a.policies == nil { http.Error(w, "policies unavailable", http.StatusServiceUnavailable) return } - policies, err := s.Policies.ListPolicies(r.Context()) + policies, err := a.policies.ListPolicies(r.Context()) if err != nil { writeControlPlaneError(w, err) return @@ -39,12 +57,12 @@ func (s *Server) handleListPolicies(w http.ResponseWriter, r *http.Request) { writeJSON(w, map[string]any{"policies": out, "shipped": shippedPolicyIDs()}) } -func (s *Server) handlePutPolicy(w http.ResponseWriter, r *http.Request) { - p, ok := s.decodePolicy(w, r) +func (a *accessAPI) handlePutPolicy(w http.ResponseWriter, r *http.Request) { + p, ok := a.decodePolicy(w, r) if !ok { return } - version, err := s.Policies.PutPolicy(r.Context(), p) + version, err := a.policies.PutPolicy(r.Context(), p) if err != nil { writePolicyError(w, err) return @@ -52,12 +70,12 @@ func (s *Server) handlePutPolicy(w http.ResponseWriter, r *http.Request) { writeJSON(w, map[string]int64{"version": version}) } -func (s *Server) handleDeletePolicy(w http.ResponseWriter, r *http.Request) { - p, ok := s.decodePolicy(w, r) +func (a *accessAPI) handleDeletePolicy(w http.ResponseWriter, r *http.Request) { + p, ok := a.decodePolicy(w, r) if !ok { return } - if err := s.Policies.DeletePolicy(r.Context(), p); err != nil { + if err := a.policies.DeletePolicy(r.Context(), p); err != nil { writePolicyError(w, err) return } @@ -66,8 +84,8 @@ func (s *Server) handleDeletePolicy(w http.ResponseWriter, r *http.Request) { // decodePolicy reads a policy and pins it to the caller's org: no request // field chooses the org, and instance policies are not edited here. -func (s *Server) decodePolicy(w http.ResponseWriter, r *http.Request) (access.Policy, bool) { - if s.Policies == nil { +func (a *accessAPI) decodePolicy(w http.ResponseWriter, r *http.Request) (access.Policy, bool) { + if a.policies == nil { http.Error(w, "policies unavailable", http.StatusServiceUnavailable) return access.Policy{}, false } diff --git a/internal/webui/api_access_policies_test.go b/internal/webui/access_policies_test.go similarity index 100% rename from internal/webui/api_access_policies_test.go rename to internal/webui/access_policies_test.go diff --git a/internal/webui/server.go b/internal/webui/server.go index 72a5fd4b7..cef7e743b 100644 --- a/internal/webui/server.go +++ b/internal/webui/server.go @@ -360,13 +360,10 @@ func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) { mux.HandleFunc("GET /api/control-plane/catalog", s.handleControlPlaneCatalog) mux.HandleFunc("GET /api/control-plane/apply-status", instanceOnly(s.handleApplyStatus)) mux.HandleFunc("GET /api/services", instanceOnly(s.handleServices)) - mux.HandleFunc("GET /api/access/policies", s.handleListPolicies) - mux.HandleFunc("PUT /api/access/policies", s.handlePutPolicy) - mux.HandleFunc("DELETE /api/access/policies", s.handleDeletePolicy) - mux.HandleFunc("GET /api/access/denials", s.handleListDenials) mux.HandleFunc("GET /api/control-plane/resources/{kind}", s.handleControlPlaneQuery) mux.HandleFunc("GET /api/control-plane/resources/{kind}/history", s.handleControlPlaneHistory) mux.HandleFunc("POST /api/control-plane/resources/{kind}/commands/{command}", s.handleControlPlaneCommand) + newAccessAPI(s).register(mux) newOrgsAPI(s).register(mux) newIdentitiesAPI(s).register(mux) mux.HandleFunc("PUT /api/orgs/current", s.handleOrgSelect)