Files
archie/internal/infrastructure/access/live_test.go
T
sam 5c922038f9 fix(access): check the policy version before each decision
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.
2026-10-25 16:21:01 +11:00

145 lines
4.8 KiB
Go

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(t.Context(), func(context.Context) (int64, error) {
return 1, nil
}, 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)
}
}
// fakePolicyStore is a State Store stub: the policy version, the policy list,
// and the read failures a policy write can land between.
type fakePolicyStore struct {
version int64
policies []access.Policy
versionErr error
listErr error
}
func (s *fakePolicyStore) PoliciesVersion(context.Context) (int64, error) {
if s.versionErr != nil {
return 0, s.versionErr
}
return s.version, nil
}
func (s *fakePolicyStore) ListPolicies(context.Context) ([]access.Policy, error) {
if s.listErr != nil {
return nil, s.listErr
}
return s.policies, nil
}
// TestLiveAppliesAPolicyEditBeforeTheNextDecision pins the ordering guarantee a
// policy edit gets: the next decision after a write returns is decided by the
// edited chain, and a store failure in between refuses rather than standing on
// the chain the process happens to hold.
func TestLiveAppliesAPolicyEditBeforeTheNextDecision(t *testing.T) {
// The process starts with no stored policies, which allow everything, so
// the forbid is the edit under test.
forbidAll := access.Policy{ID: "forbid-all", Level: access.LevelInstance, Text: `forbid(principal, action, resource);`}
storeDown := errors.New("state store down")
request := func(live *Live) access.Decision {
return live.Authorize(access.SharedTokenOwner(), access.ActionRead,
access.Resource{Kind: access.KindDashboard}, access.Context{})
}
tests := []struct {
name string
write func(store *fakePolicyStore)
wantAllowed bool
wantUnavailable bool
}{
{
name: "an unchanged version keeps the chain",
write: func(store *fakePolicyStore) { store.policies = []access.Policy{forbidAll} },
wantAllowed: true,
},
{
name: "a bumped version applies the forbid",
write: func(store *fakePolicyStore) { store.policies = []access.Policy{forbidAll}; store.version++ },
},
{
name: "a failed version read refuses",
write: func(store *fakePolicyStore) {
store.policies = []access.Policy{forbidAll}
store.version++
store.versionErr = storeDown
},
wantUnavailable: true,
},
{
// The chain held is stamped below the version, so it may predate
// the edit: keeping it is how a forbid survives a store blip.
name: "a failed list read after a bump refuses instead of keeping the old chain",
write: func(store *fakePolicyStore) { store.version++; store.listErr = storeDown },
wantUnavailable: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &fakePolicyStore{version: 1}
live := NewPending(t.Context(), store.PoliciesVersion, store.ListPolicies, slog.New(slog.DiscardHandler))
if err := live.Reload(t.Context()); err != nil {
t.Fatal(err)
}
if decision := request(live); !decision.Allowed {
t.Fatalf("before the edit = %+v, want allowed so the edit is what refuses", decision)
}
tt.write(store)
decision := request(live)
if decision.Allowed != tt.wantAllowed {
t.Fatalf("after the edit = %+v, want allowed %v", decision, tt.wantAllowed)
}
if unavailable := errors.Is(decision.Err, access.ErrChainUnavailable); unavailable != tt.wantUnavailable {
t.Fatalf("after the edit = %+v, want unavailable %v", decision, tt.wantUnavailable)
}
})
}
}