Two halves of one boundary: a credential resolves to an identity, and an action records which identity acted and whose authority it used. resource server. internal/infrastructure/oidc verifies a provider-issued bearer token against the provider's published keys, using an OSS OIDC library: archie compares no signature, validates no claim and rotates no key itself, holds no client secret and issues no session, so a compromised archie cannot mint an identity. Audience is required rather than optional -- a token minted for another service must not authenticate here -- and discovery happens at construction, so an unreachable provider is a startup failure rather than a request-time surprise. identity.Authenticate is the single place a credential becomes an identity, and it refuses in four distinguishable ways: no credential presented, a credential that did not verify (wrong key, expired, wrong issuer, wrong audience, malformed), a verified subject with no identity bound to it, and an identity that may not act. The order is deliberate: verification happens before any field of the token is used, so nothing an unverified token carries can select an identity. A suspended identity is refused even though its credential verified, because suspension is archie's decision and must not depend on the provider withdrawing a token. The binding is the second bridge beside the legacy-name alias: identity_subjects keys (issuer, subject) so the same subject string from another issuer is a different caller, and rebinding moves a subject so two identities can never claim one provider subject. The contract Authenticate needs is SubjectResolver, narrower than Repository, so a consumer that only authenticates does not take the whole identity CRUD surface -- and staterpc.Client, which asserts Repository, does not need a new RPC to satisfy it. attribution. taskactions now records two identities per action: the acting identity and the authorising principal, both on the event row as new columns added through the existing migration table. The rule that the bug turned on is enforced by the event kind describing the actor: a person's approval is human_approved, an agent's is agent_approved, and an action with no verified actor is task_approved, a task-level event that credits nobody. An agent acting under a person's standing approval records itself as the actor and that person as the principal, so "approved by came from me" is answerable from the record rather than asserted. Scope and actor are separated: scope decides which task a caller may touch and is never promoted into an actor, because the dashboard's credential is a shared token and possession of it is not evidence that a person was at the keyboard. That last point is a deliberate fidelity trade, and the reason the transport is not wired: the dashboard action path passes no actor yet, so dashboard approvals are now recorded as task_approved rather than claimed as a human's. Recording nothing false is the requirement; naming the human verifiably needs the dashboard to authenticate a token and resolve it, which needs two new state-store RPCs (ResolveIdentitySubject, BindIdentitySubject) -- the next piece, not a rewrite: the verifier is a one-method interface, so introspection would be a second implementation of it. Verified: task check exit 0; focused runs recorded in the handoff. Opaque authelia_at_ tokens were inspected rather than assumed and cannot be verified against public keys; infra is configuring access_token_signed_response_alg RS256 on the archie client, with audience https://archie.catlow.cloud and issuer https://auth.catlow.cloud.
212 lines
7.6 KiB
Go
212 lines
7.6 KiB
Go
package taskactions
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
|
)
|
|
|
|
type fakeStore struct {
|
|
task *Task
|
|
events []events.Event
|
|
}
|
|
|
|
func (f *fakeStore) TaskByID(context.Context, int64) (*Task, error) { return f.task, nil }
|
|
func (f *fakeStore) Transition(context.Context, int64, string, string, string) error {
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStore) Requeue(context.Context, int64, string, string) error { return nil }
|
|
func (f *fakeStore) RetryTask(context.Context, int64, string, string) error {
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStore) ArchiveTask(_ context.Context, _ int64, _ string, e events.Event) (int64, error) {
|
|
f.events = append(f.events, e)
|
|
return int64(len(f.events)), nil
|
|
}
|
|
|
|
func (f *fakeStore) InsertEvent(_ context.Context, e events.Event) (int64, error) {
|
|
f.events = append(f.events, e)
|
|
return int64(len(f.events)), nil
|
|
}
|
|
|
|
func (f *fakeStore) last() events.Event {
|
|
if len(f.events) == 0 {
|
|
return events.Event{}
|
|
}
|
|
return f.events[len(f.events)-1]
|
|
}
|
|
|
|
const (
|
|
humanID = identity.IdentityID("20000000-0000-5000-8000-000000000001")
|
|
agentID = identity.IdentityID("20000000-0000-5000-8000-000000000002")
|
|
)
|
|
|
|
func humanActor() Actor {
|
|
return ActorFor(identity.Identity{ID: humanID, Kind: identity.KindUser, Lifecycle: identity.LifecycleActive})
|
|
}
|
|
|
|
func agentActor() Actor {
|
|
return ActorFor(identity.Identity{ID: agentID, Kind: identity.KindBot, Lifecycle: identity.LifecycleActive})
|
|
}
|
|
|
|
// TestEventKindDescribesTheActor is the rule that the bug turned on: an agent's
|
|
// approval was written down as a human's. The kind names the actor, so an
|
|
// agent's action can never be read as a person's, and an action with no verified
|
|
// actor is recorded as a task-level event that credits nobody.
|
|
func TestEventKindDescribesTheActor(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
action taskstate.Action
|
|
actor Actor
|
|
wantKind string
|
|
}{
|
|
{"a person approves", taskstate.ActionApprove, humanActor(), events.KindHumanApproved},
|
|
{"an agent approves", taskstate.ActionApprove, agentActor(), events.KindAgentApproved},
|
|
{"a person rejects", taskstate.ActionReject, humanActor(), events.KindHumanRejected},
|
|
{"an agent rejects", taskstate.ActionReject, agentActor(), events.KindAgentRejected},
|
|
{"an unattributed approval", taskstate.ActionApprove, Actor{}, events.KindTaskApproved},
|
|
{"an unattributed rejection", taskstate.ActionReject, Actor{}, events.KindTaskRejected},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: "waiting_human"}}
|
|
service := Service{Store: store}
|
|
|
|
if err := service.Apply(context.Background(), nil, tc.actor, 7, tc.action); err != nil {
|
|
t.Fatalf("Apply() error = %v", err)
|
|
}
|
|
got := store.last()
|
|
if got.Kind != tc.wantKind {
|
|
t.Fatalf("event kind = %q, want %q", got.Kind, tc.wantKind)
|
|
}
|
|
if tc.actor.Human() && got.Kind == events.KindAgentApproved {
|
|
t.Fatal("a person's action was recorded as an agent's")
|
|
}
|
|
if tc.actor.Attributed() && !tc.actor.Human() && got.Kind == events.KindHumanApproved {
|
|
t.Fatal("an agent's action was recorded as a human approval")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestActorAndPrincipalAreRecordedSeparately is the two-attribution requirement:
|
|
// an agent acting under a person's standing authority records both, so "approved
|
|
// by came from me" is answerable from the record.
|
|
func TestActorAndPrincipalAreRecordedSeparately(t *testing.T) {
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: "waiting_human"}}
|
|
service := Service{Store: store}
|
|
|
|
actor := agentActor().AuthorisedBy(humanID)
|
|
if err := service.Apply(context.Background(), nil, actor, 7, taskstate.ActionApprove); err != nil {
|
|
t.Fatalf("Apply() error = %v", err)
|
|
}
|
|
|
|
got := store.last()
|
|
if got.ActorID != string(agentID) {
|
|
t.Fatalf("actor = %q, want %q", got.ActorID, agentID)
|
|
}
|
|
if got.ActorKind != string(identity.KindBot) {
|
|
t.Fatalf("actor kind = %q, want %q", got.ActorKind, identity.KindBot)
|
|
}
|
|
if got.PrincipalID != string(humanID) {
|
|
t.Fatalf("principal = %q, want %q", got.PrincipalID, humanID)
|
|
}
|
|
if got.Kind != events.KindAgentApproved {
|
|
t.Fatalf("event kind = %q, want %q", got.Kind, events.KindAgentApproved)
|
|
}
|
|
}
|
|
|
|
// TestUnattributedActionClaimsNoApprover: with no verified actor the record must
|
|
// not credit the actor's own authority, and must not name a human.
|
|
func TestUnattributedActionClaimsNoApprover(t *testing.T) {
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: "waiting_human"}}
|
|
service := Service{Store: store}
|
|
|
|
if err := service.Apply(context.Background(), nil, Actor{}, 7, taskstate.ActionApprove); err != nil {
|
|
t.Fatalf("Apply() error = %v", err)
|
|
}
|
|
|
|
got := store.last()
|
|
if got.ActorID != "" || got.ActorKind != "" || got.PrincipalID != "" {
|
|
t.Fatalf("unattributed action recorded an actor: %+v", got)
|
|
}
|
|
if got.Kind == events.KindHumanApproved || got.Kind == events.KindAgentApproved {
|
|
t.Fatalf("unattributed action claimed an actor through its kind: %q", got.Kind)
|
|
}
|
|
}
|
|
|
|
// TestEveryActionCarriesItsAttribution: no action path may record an event that
|
|
// skipped the attribution, including the ones that also close a forge issue.
|
|
func TestEveryActionCarriesItsAttribution(t *testing.T) {
|
|
actions := []struct {
|
|
action taskstate.Action
|
|
status string
|
|
}{
|
|
{taskstate.ActionApprove, "waiting_human"},
|
|
{taskstate.ActionReject, "waiting_human"},
|
|
{taskstate.ActionRetry, "parked"},
|
|
{taskstate.ActionStop, "running"},
|
|
{taskstate.ActionCancel, "queued"},
|
|
{taskstate.ActionAbandon, "parked"},
|
|
}
|
|
|
|
for _, tc := range actions {
|
|
t.Run(string(tc.action), func(t *testing.T) {
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: tc.status}}
|
|
service := Service{
|
|
Store: store,
|
|
CancelTask: func(int64) bool { return true },
|
|
}
|
|
if err := service.Apply(context.Background(), nil, agentActor(), 7, tc.action); err != nil {
|
|
t.Fatalf("Apply(%s) error = %v", tc.action, err)
|
|
}
|
|
got := store.last()
|
|
if got.ActorID != string(agentID) || got.ActorKind != string(identity.KindBot) {
|
|
t.Fatalf("%s recorded without its actor: %+v", tc.action, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestScopeStillLimitsATaskButIsNotTheActor: scope answers which task a caller
|
|
// may touch. It is not an actor, and passing one must not attribute the action
|
|
// to it.
|
|
func TestScopeStillLimitsATaskButIsNotTheActor(t *testing.T) {
|
|
other := "sam"
|
|
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: "waiting_human", Identity: "archie"}}
|
|
service := Service{Store: store}
|
|
|
|
if err := service.Apply(context.Background(), &other, agentActor(), 7, taskstate.ActionApprove); err == nil {
|
|
t.Fatal("Apply() allowed an identity to act on another identity's task")
|
|
}
|
|
if len(store.events) != 0 {
|
|
t.Fatalf("a refused action recorded %d events", len(store.events))
|
|
}
|
|
|
|
if err := service.Apply(context.Background(), &other, agentActor(), 7, taskstate.ActionApprove); err == nil {
|
|
t.Fatal("Apply() allowed a scoped caller to act on a foreign task")
|
|
}
|
|
}
|
|
|
|
func TestActorForCarriesTheKindAndLeavesTheActionUnattributedWithoutAPrincipal(t *testing.T) {
|
|
actor := agentActor()
|
|
if actor.Principal != "" {
|
|
t.Fatalf("ActorFor() set a principal: %q", actor.Principal)
|
|
}
|
|
if !actor.Attributed() {
|
|
t.Fatal("ActorFor() produced an unattributed actor for a real identity")
|
|
}
|
|
if (Actor{}).Attributed() {
|
|
t.Fatal("the zero Actor reported itself as attributed")
|
|
}
|
|
if (Actor{}).Human() {
|
|
t.Fatal("the zero Actor reported itself as a human")
|
|
}
|
|
}
|