package governance import ( "context" "encoding/json/v2" "errors" "log/slog" "reflect" "strings" "testing" "time" ) type fixedDelivery struct{ decision Decision } func (f fixedDelivery) AuthorizeDelivery(context.Context, OrgID, string, string) Decision { return f.decision } type fixedAuthorizer struct{ decision Decision } func (f fixedAuthorizer) Authorize(context.Context, Principal, Action, Resource, AccessContext) Decision { return f.decision } type brokenLog struct{} func (brokenLog) RecordDecision(context.Context, DecisionRecord) error { return errors.New("database down") } // TestEveryDecisionIsRecorded guards the audit trail: an allowed decision and a // refused one are each stored with the principal, action, resource and deciding // policies, whether the request came through the chain or the delivery // check. The request's signature is a credential and is never stored. func TestEveryDecisionIsRecorded(t *testing.T) { const signature = "sha256=feedface" quiet := slog.New(slog.DiscardHandler) workflow := Resource{Kind: KindWorkflow, ID: "triage", Org: DefaultOrgID} ask := func(ctx context.Context, d Decision, rec DecisionRecorder) { Recording(fixedAuthorizer{d}, rec, quiet).Authorize(ctx, SharedTokenOwner(), ActionRun, workflow, AccessContext{Signature: signature}) } deliver := func(ctx context.Context, d Decision, rec DecisionRecorder) { RecordingDelivery(fixedDelivery{d}, rec, quiet).AuthorizeDelivery(ctx, DefaultOrgID, "alerts", "203.0.113.9") } chain := DecisionRecord{Principal: SystemID, Org: DefaultOrgID, Action: ActionRun, Kind: KindWorkflow, ResourceID: "triage", Level: LevelOrg} delivery := DecisionRecord{Principal: SenderID, Org: DefaultOrgID, Action: ActionDeliver, Kind: KindSource, ResourceID: "alerts", Level: LevelInstance} with := func(d DecisionRecord, outcome Outcome, policy string) DecisionRecord { d.Outcome, d.Policies = outcome, []string{policy} return d } tests := []struct { name string run func(context.Context, Decision, DecisionRecorder) in Decision want DecisionRecord }{ {"an allowed request", ask, AllowedAt(LevelOrg, []string{"org-owner"}), with(chain, OutcomeAllowed, "org-owner")}, {"a refused request", ask, DeniedAt(LevelOrg, []string{"org-viewer"}), with(chain, OutcomeDenied, "org-viewer")}, {"an allowed delivery", deliver, AllowedAt(LevelInstance, []string{"deliver-any"}), with(delivery, OutcomeAllowed, "deliver-any")}, {"a refused delivery", deliver, DeniedAt(LevelInstance, []string{"deliver-none"}), with(delivery, OutcomeDenied, "deliver-none")}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { log := &decisionLog{} tt.run(t.Context(), tt.in, log) if len(log.recorded) != 1 { t.Fatalf("records = %d, want 1", len(log.recorded)) } got := log.recorded[0] stored, err := json.Marshal(got) if err != nil { t.Fatal(err) } if strings.Contains(string(stored), signature) { t.Fatalf("record %s holds the request signature", stored) } got.At, got.Count = time.Time{}, 0 if !reflect.DeepEqual(got, tt.want) { t.Fatalf("recorded %+v, want %+v", got, tt.want) } }) } } // TestAllowedDecisionNeedsItsRecord guards the audit guarantee: a request the // chain allowed is refused as unavailable when its record cannot be written, // while a refusal stays a refusal. func TestAllowedDecisionNeedsItsRecord(t *testing.T) { tests := []struct { name string in Decision allowed bool wantErr error }{ {name: "an allowed request", in: AllowedAt(LevelOrg, []string{"org-owner"}), wantErr: ErrChainUnavailable}, {name: "a refused request", in: DeniedAt(LevelOrg, nil)}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { a := Recording(fixedAuthorizer{tt.in}, brokenLog{}, slog.New(slog.DiscardHandler)) d := a.Authorize(t.Context(), SharedTokenOwner(), ActionRun, Resource{Kind: KindWorkflow, Org: DefaultOrgID}, AccessContext{}) if d.Allowed != tt.allowed || !errors.Is(d.Err, tt.wantErr) { t.Fatalf("decision = %+v, want allowed %v, error %v", d, tt.allowed, tt.wantErr) } }) } }