fix(webui): refuse API requests until the access chain loads

A State Store that was unreachable at startup left the access chain nil,
and authorize treated nil as no chain at all: every request passed the
credential check, no principal or org reached the context, the State Store
defaulted each call to org-sys, and the instance-only surfaces were served.
The dashboard stayed open until a restart.

Build a chain with no engine when the first load is Unavailable; it refuses
every request with 503 (or the notice page for a browser) until the first
load succeeds, and Run keeps retrying so the dashboard recovers without a
restart. A denial is not recorded, because nothing was evaluated.
This commit is contained in:
sam committed 2026-10-10 21:55:02 +11:00
1 parent 8ad4c431f8
commit bccb265980
7 files changed
+189 -23

No files matched your search

+4 -3
View File
@@ -35,10 +35,11 @@ func newReadinessRegistry(o Options, tasks storecontract.TaskStore, chat messagi
return health.NewRegistry(probes...)
}
// problemSource keeps a missing chain a nil source, which the probe reports
// as not wired.
// problemSource reports the chain's invalid policies; a chain that has not
// loaded yet is reported as not wired, so readiness is degraded while requests
// are refused.
func problemSource(chain *infraaccess.Live) readiness.PolicyProblems {
if chain == nil {
if chain == nil || !chain.Ready() {
return nil
}
return chain
+10 -9
View File
@@ -27,16 +27,17 @@ import (
"github.com/samcharles93/archie-core/internal/webui"
)
// buildAccessChain builds the live access engine over the stored policies.
// An unreachable State Store returns nil and logs a warning.
// buildAccessChain builds the live access engine over the stored policies. A
// State Store that is not up yet yields a chain with no engine: it refuses
// every request until the first load succeeds and keeps retrying, so a slow
// database or a bad start order closes the dashboard rather than opening it.
func buildAccessChain(ctx context.Context, store *staterpc.Client, log *slog.Logger) (*infraaccess.Live, error) {
live, err := infraaccess.NewLive(ctx, store.ListPolicies, log)
if status.Code(err) == codes.Unavailable {
log.Warn("access chain unavailable; the credential check is the gate", "err", err)
return nil, nil
}
if err != nil {
return nil, err
live := infraaccess.NewPending(store.ListPolicies, log)
if err := live.Reload(ctx); err != nil {
if status.Code(err) != codes.Unavailable {
return nil, err
}
log.Warn("access policies unavailable; the dashboard refuses API requests until they load", "err", err)
}
go live.Run(ctx, applystatus.RestampInterval)
return live, nil
+4
View File
@@ -181,4 +181,8 @@ var (
// ErrPolicyLockout is returned when a change would leave an org with no
// owner who may manage its policies.
ErrPolicyLockout = errors.New("access: change leaves no owner able to manage policies")
// ErrChainUnavailable reports that no policy engine has loaded yet: the
// request was refused without evaluating any policy, so it is an
// availability failure, not a denial.
ErrChainUnavailable = errors.New("access: policy chain not loaded")
)
+50 -7
View File
@@ -28,6 +28,16 @@ func NewLive(ctx context.Context, load func(context.Context) ([]access.Policy, e
return l, nil
}
// NewPending returns a chain with no engine: Run loads it, retrying until the
// store answers. A chain that is not ready refuses every request, so a process
// that starts before its State Store is closed rather than opened.
func NewPending(load func(context.Context) ([]access.Policy, error), log *slog.Logger) *Live {
return &Live{load: load, log: log}
}
// Ready reports whether an engine has loaded.
func (l *Live) Ready() bool { return l.engine.Load() != nil }
// Reload rebuilds the engine from the store now.
func (l *Live) Reload(ctx context.Context) error {
stored, err := l.load(ctx)
@@ -46,29 +56,62 @@ func (l *Live) Reload(ctx context.Context) error {
return nil
}
// Run reloads on every interval until ctx ends.
// Run reloads on every interval until ctx ends. A chain with no engine yet
// tries at once and keeps trying, so the first load does not wait an interval
// after the State Store returns.
func (l *Live) Run(ctx context.Context, interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
l.reload(ctx)
select {
case <-ctx.Done():
return
case <-ticker.C:
if err := l.Reload(ctx); err != nil && ctx.Err() == nil {
l.log.Warn("access policies not reloaded; keeping the last chain", "err", err)
}
}
}
}
func (l *Live) reload(ctx context.Context) {
err := l.Reload(ctx)
if err == nil || ctx.Err() != nil {
return
}
if l.Ready() {
l.log.Warn("access policies not reloaded; keeping the last chain", "err", err)
return
}
l.log.Warn("access policies not loaded; requests are refused until they load", "err", err)
}
func (l *Live) Authorize(p access.Principal, a access.Action, r access.Resource, c access.Context) access.Decision {
return l.engine.Load().Authorize(p, a, r, c)
engine := l.engine.Load()
if engine == nil {
return notLoaded()
}
return engine.Authorize(p, a, r, c)
}
func (l *Live) AuthorizeDelivery(orgID org.OrgID, sourcePath, addr string) access.Decision {
return l.engine.Load().AuthorizeDelivery(orgID, sourcePath, addr)
engine := l.engine.Load()
if engine == nil {
return notLoaded()
}
return engine.AuthorizeDelivery(orgID, sourcePath, addr)
}
// notLoaded is the refusal of a chain with no engine: nothing was evaluated.
func notLoaded() access.Decision {
decision := access.DeniedAt(access.LevelInstance, nil)
decision.Err = access.ErrChainUnavailable
return decision
}
// Problems reports the current engine's invalid policies.
func (l *Live) Problems() []Problem { return l.engine.Load().Problems() }
func (l *Live) Problems() []Problem {
engine := l.engine.Load()
if engine == nil {
return nil
}
return engine.Problems()
}
@@ -0,0 +1,52 @@
package access
import (
"context"
"errors"
"log/slog"
"testing"
"github.com/samcharles93/archie-core/internal/domain/access"
)
// TestPendingChainRefusesUntilLoaded pins that a chain built before its store
// is up refuses every request until the first load succeeds, then allows.
func TestPendingChainRefusesUntilLoaded(t *testing.T) {
loaded := false
live := NewPending(func(context.Context) ([]access.Policy, error) {
if !loaded {
return nil, errors.New("store down")
}
return nil, nil
}, slog.New(slog.DiscardHandler))
request := func() access.Decision {
return live.Authorize(access.SharedTokenOwner(), access.ActionRead,
access.Resource{Kind: access.KindDashboard}, access.Context{})
}
if decision := request(); decision.Allowed || !errors.Is(decision.Err, access.ErrChainUnavailable) {
t.Fatalf("before load = %+v, want the not-loaded refusal", decision)
}
if live.Ready() {
t.Fatal("Ready before load = true, want false")
}
if got := live.Problems(); got != nil {
t.Fatalf("Problems before load = %v, want nil", got)
}
if decision := live.AuthorizeDelivery("acme", "sources/x", "10.0.0.1"); decision.Allowed ||
!errors.Is(decision.Err, access.ErrChainUnavailable) {
t.Fatalf("delivery before load = %+v, want the not-loaded refusal", decision)
}
loaded = true
if err := live.Reload(t.Context()); err != nil {
t.Fatal(err)
}
if !live.Ready() {
t.Fatal("Ready after load = false, want true")
}
if decision := request(); !decision.Allowed {
t.Fatalf("after load = %+v, want allowed", decision)
}
}
+25 -4
View File
@@ -2,6 +2,7 @@ package webui
import (
"context"
"errors"
"net/http"
"strings"
@@ -10,13 +11,14 @@ import (
"github.com/samcharles93/archie-core/internal/domain/org"
)
// authorize wraps h with the policy chain. Optional: a server with no
// Authorizer keeps the credential check as the whole gate, which is the
// documented behaviour of an install that has not built the chain.
// authorize wraps h with the policy chain. A chain that is absent or has not
// loaded refuses the request: the credential check alone is never the gate
// while orgs exist, so a State Store outage closes the dashboard rather than
// letting every request through as the system org.
func (s *Server) authorize(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if s.Access == nil {
h.ServeHTTP(w, r)
s.refuseUngated(w, r)
return
}
principal, err := s.requestPrincipal(r.Context())
@@ -36,6 +38,12 @@ func (s *Server) authorize(h http.Handler) http.Handler {
resource := access.Resource{Kind: kind, ID: id, Org: principal.Org}
decision := s.Access.Authorize(principal, action, resource, access.Context{})
if !decision.Allowed {
if errors.Is(decision.Err, access.ErrChainUnavailable) {
// Nothing was evaluated: the chain has not loaded. This is an
// availability failure, not a denial, so none is recorded.
s.refuseUngated(w, r)
return
}
s.recordDenial(r.Context(), principal, action, resource, decision)
if wantsDocument(r) {
s.authPage(w, "You do not have permission to do that.")
@@ -52,6 +60,19 @@ func (s *Server) authorize(h http.Handler) http.Handler {
})
}
// refuseUngated answers a request the dashboard cannot authorize because no
// policy chain is available. It serves nothing ungated: without a chain the
// credential check would be the whole gate and every call would act as the
// system org.
func (s *Server) refuseUngated(w http.ResponseWriter, r *http.Request) {
if wantsDocument(r) {
s.authPage(w, "The access policy chain is not available yet.")
return
}
w.Header().Set("Cache-Control", "no-store")
http.Error(w, "access policies are not available", http.StatusServiceUnavailable)
}
// requestPrincipal assembles the principal a request acts as. With no acting
// identity (the shared token), it is the org-sys owner person when the upgrade
// created one; with an acting identity, it is the identity's org and
+44
View File
@@ -45,6 +45,16 @@ func (allowAll) Authorize(access.Principal, access.Action, access.Resource, acce
return access.Allowed()
}
// notReadyChain is an authorizer whose engine has not loaded: every decision
// is the not-loaded refusal, which no policy evaluated.
type notReadyChain struct{}
func (notReadyChain) Authorize(access.Principal, access.Action, access.Resource, access.Context) access.Decision {
decision := access.DeniedAt(access.LevelInstance, nil)
decision.Err = access.ErrChainUnavailable
return decision
}
type recordingTokens struct{ owner identity.IdentityID }
// AddPersonalToken mirrors the State Store's rule: only a signed-in person's
@@ -123,6 +133,40 @@ func TestSharedTokenOperatorPostsPersonalToken(t *testing.T) {
}
}
// TestAuthorizeRefusesWithoutAChain pins the gate for a server with no chain
// and for a chain that has not loaded: the request is refused rather than
// served as the system org, and nothing is written.
func TestAuthorizeRefusesWithoutAChain(t *testing.T) {
tests := []struct {
name string
srv *Server
}{
{"no chain", &Server{}},
{"chain not loaded", &Server{Access: notReadyChain{}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tokens := &recordingTokens{}
tt.srv.Token = "shared"
tt.srv.Principals = stubPrincipals(true)
tt.srv.PersonalTokens = tokens
handler := tt.srv.requireToken(tt.srv.authorize(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
t.Fatal("the handler must not be reached")
})))
rec := httptest.NewRecorder()
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/tokens", nil)
req.Header.Set("Authorization", "Bearer shared")
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want 503", rec.Code)
}
if tokens.owner != "" {
t.Fatalf("token owner = %q, want no write", tokens.owner)
}
})
}
}
func TestAdministrationRoutes(t *testing.T) {
tests := []struct {
method, path string