archied, archie-ui and the tool gateway re-read the stored policies on a 30s ticker, so a forbid written seconds earlier did not stop a run. One instance-wide revision now moves inside the transaction of every policy write, and a decision reads it first and rebuilds the engine only when it moved. A version read that fails, or a policy list that fails after a bump, refuses with ErrChainUnavailable instead of standing on the last chain, so three call sites now treat unavailable as availability rather than denial: dispatch leaves the capture for the next cycle, a task start parks transient, and the tool gateway reports the tool as unavailable.
116 lines
3.5 KiB
Go
116 lines
3.5 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"
|
|
)
|
|
|
|
// 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 (s *Server) handleListPolicies(w http.ResponseWriter, r *http.Request) {
|
|
if s.Policies == nil {
|
|
http.Error(w, "policies unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
policies, err := s.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 (s *Server) handlePutPolicy(w http.ResponseWriter, r *http.Request) {
|
|
p, ok := s.decodePolicy(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
version, err := s.Policies.PutPolicy(r.Context(), p)
|
|
if err != nil {
|
|
writePolicyError(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, map[string]int64{"version": version})
|
|
}
|
|
|
|
func (s *Server) handleDeletePolicy(w http.ResponseWriter, r *http.Request) {
|
|
p, ok := s.decodePolicy(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := s.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 (s *Server) decodePolicy(w http.ResponseWriter, r *http.Request) (access.Policy, bool) {
|
|
if s.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()
|
|
}
|