refactor(webui): extract the access policies and denials module (#1778)
Quality / check (push) Canceled after 1m5s
deploy / gate (push) Successful in 4m35s
deploy / archied-image (push) Successful in 2m49s
deploy / agent-image (push) Successful in 12s
deploy / dist-zip (push) Skipped

This commit is contained in:
sam committed 2026-11-03 08:33:46 +11:00
1 parent 369e5038ca
commit c5f4541cbf
5 files changed
+35 -20

No files matched your search

@@ -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
@@ -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)
}
@@ -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
}
+1 -4
View File
@@ -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)