Files
sam c5f4541cbf
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
refactor(webui): extract the access policies and denials module (#1778)
2026-11-03 08:33:46 +11:00

134 lines
4.2 KiB
Go

package webui
import (
"encoding/json"
"errors"
"net/http"
"github.com/samcharles93/archie-core/internal/domain/access"
"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 {
return principal.Org
}
return access.SharedTokenOwner().Org
}
// handleListPolicies lists the caller's org policies, and the instance
// policies when the caller is the instance owner.
func (a *accessAPI) handleListPolicies(w http.ResponseWriter, r *http.Request) {
if a.policies == nil {
http.Error(w, "policies unavailable", http.StatusServiceUnavailable)
return
}
policies, err := a.policies.ListPolicies(r.Context())
if err != nil {
writeControlPlaneError(w, err)
return
}
orgID := policyOrg(r)
out := []access.Policy{}
for _, p := range policies {
if (p.Level == access.LevelInstance && instanceOwner(r)) || (p.Level != access.LevelInstance && p.OrgID == orgID) {
out = append(out, p)
}
}
writeJSON(w, map[string]any{"policies": out, "shipped": shippedPolicyIDs()})
}
func (a *accessAPI) handlePutPolicy(w http.ResponseWriter, r *http.Request) {
p, ok := a.decodePolicy(w, r)
if !ok {
return
}
version, err := a.policies.PutPolicy(r.Context(), p)
if err != nil {
writePolicyError(w, err)
return
}
writeJSON(w, map[string]int64{"version": version})
}
func (a *accessAPI) handleDeletePolicy(w http.ResponseWriter, r *http.Request) {
p, ok := a.decodePolicy(w, r)
if !ok {
return
}
if err := a.policies.DeletePolicy(r.Context(), p); err != nil {
writePolicyError(w, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// 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 (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
}
var p access.Policy
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 64<<10)).Decode(&p); err != nil {
http.Error(w, "invalid policy: "+err.Error(), http.StatusBadRequest)
return access.Policy{}, false
}
if p.Level == access.LevelInstance {
// Instance policies span orgs; only the instance owner edits them.
if !instanceOwner(r) {
http.Error(w, "instance policies are edited by the instance owner", http.StatusForbidden)
return access.Policy{}, false
}
p.OrgID, p.WorkspaceID, p.ObjectKind, p.ObjectID = "", "", "", ""
return p, true
}
p.OrgID = policyOrg(r)
return p, true
}
func writePolicyError(w http.ResponseWriter, err error) {
if errors.Is(err, access.ErrPolicyLockout) {
http.Error(w, err.Error(), http.StatusConflict)
return
}
writeControlPlaneError(w, err)
}
func shippedPolicyIDs() []string {
return []string{access.PolicyOrgRead, access.PolicyOrgEdit, access.PolicyOrgAdmin, access.PolicyOrgOwner}
}
// instanceOwner reports whether the request acts as the instance owner: a
// principal holding the owner role of the default org. The shared token acts
// as the org-sys operator person, and the system fallback is an owner of
// org-sys too, so both answer true; an owner of any other org, or a member of
// org-sys with a lesser role, does not.
func instanceOwner(r *http.Request) bool {
principal, ok := access.PrincipalFromContext(r.Context())
if !ok {
principal = access.SharedTokenOwner()
}
return principal.InstanceAdmin()
}