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.
53 lines
1.5 KiB
Go
53 lines
1.5 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(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)
|
|
}
|
|
}
|