Files
archie/internal/daemon/workflow_identity_test.go
T

178 lines
7.6 KiB
Go

package daemon
import (
"context"
"log/slog"
"reflect"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow"
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/taskstate"
)
type identityStore struct {
storecontract.TaskStore
parked bool
}
func (*identityStore) Update(context.Context, *workflow.Task) error { return nil }
func (s *identityStore) ParkTask(context.Context, int64, string, string, taskstate.ParkClass) error {
s.parked = true
return nil
}
type principalOf struct{}
func (principalOf) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) {
return access.Principal{IdentityID: id}, nil
}
// runAllowed permits run only to the identities it lists.
type runAllowed map[identity.IdentityID]bool
func (r runAllowed) Authorize(p access.Principal, _ access.Action, _ access.Resource, _ access.Context) access.Decision {
if r[p.IdentityID] {
return access.Allowed()
}
return access.DeniedAt(access.LevelOrg, nil)
}
func (runAllowed) Reload(context.Context) error { return nil }
type enablementOf workflowtask.WorkflowEnablement
func (e enablementOf) WorkflowEnablement(context.Context) (workflowtask.WorkflowEnablement, error) {
return workflowtask.WorkflowEnablement(e), nil
}
// denials records the principals refused.
type denials []identity.IdentityID
func (d *denials) RecordDenial(_ context.Context, denial access.Denial) error {
*d = append(*d, denial.Principal)
return nil
}
// TestWorkflowIdentity pins that a run acts as the identity its workflow
// declares only when both the identity that started it and the declared one
// may run the workflow, that a refusal is recorded, and that a workflow
// naming no identity is refused rather than inheriting one. A called
// workflow is started by its caller's declared identity and runs as its own.
func TestWorkflowIdentity(t *testing.T) {
const deploy = "id: deploy\nidentity: deployer\n"
const callee = "id: deploy\nidentity: bot\n"
tests := []struct {
name string
yaml string
starter string
allowed runAllowed
wantIdentity string
wantParked bool
wantDenied []identity.IdentityID
disabled bool
}{
{"allowed dispatch runs as the workflow's identity", deploy, "", runAllowed{"root": true, "id-deployer": true}, "deployer", false, nil, false},
{"refused invocation is parked", deploy, "", runAllowed{"id-deployer": true}, "", true, []identity.IdentityID{"root"}, false},
{"refused execution is parked", deploy, "", runAllowed{"root": true}, "", true, []identity.IdentityID{"id-deployer"}, false},
{"called workflow runs as its own identity, not its caller's", callee, "deployer", runAllowed{"id-deployer": true, "id-bot": true}, "bot", false, nil, false},
{"caller refused the called workflow is parked", callee, "deployer", runAllowed{"id-bot": true}, "", true, []identity.IdentityID{"id-deployer"}, false},
{"workflow naming the root runs as the root", "id: deploy\nidentity: root\n", "bot", runAllowed{"root": true, "id-bot": true}, "", false, nil, false},
{"no declared identity is refused", "id: deploy\n", "bot", runAllowed{"root": true, "id-bot": true}, "", true, nil, false},
{"unconfigured identity is parked", "id: deploy\nidentity: ghost\n", "", runAllowed{"root": true}, "", true, nil, false},
{"disabled workflow is parked", deploy, "bot", runAllowed{"id-bot": true, "id-deployer": true}, "", true, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &identityStore{}
var denied denials
d := &Daemon{
Store: store, Log: slog.New(slog.DiscardHandler), RootIdentityID: "root",
Access: tt.allowed, Principals: principalOf{}, Denials: &denied,
Identities: []*IdentityRunner{{ID: "id-deployer", Name: "deployer"}, {ID: "id-bot", Name: "bot"}},
}
if tt.disabled {
d.WorkflowEnablement = enablementOf(workflowtask.WorkflowEnablement{}.SetEnabled("", "deploy", false))
}
task := &workflow.Task{
ID: 1, Identity: tt.starter, Workflow: "deploy",
WorkflowDefinitionYAML: tt.yaml, WorkflowDefinitionDigest: workflow.DigestDefinition(tt.yaml),
}
ok := d.adoptWorkflowIdentity(context.Background(), task)
if ok == tt.wantParked || store.parked != tt.wantParked || (ok && task.Identity != tt.wantIdentity) {
t.Fatalf("ok %v parked %v identity %q, want parked %v identity %q", ok, store.parked, task.Identity, tt.wantParked, tt.wantIdentity)
}
if !reflect.DeepEqual([]identity.IdentityID(denied), tt.wantDenied) {
t.Fatalf("denied %v, want %v", denied, tt.wantDenied)
}
})
}
}
func TestIdentityTasksUseLiveSettings(t *testing.T) {
root := config.Config{Models: map[string]string{"builder": "old/model"}, Providers: map[string]config.Provider{"old": {}}, Repos: []config.Repo{{Owner: "acme", Name: "app", Base: "main"}}}
id := config.IdentityConfig{Name: "bot", BotUser: "bot", BotEmail: "bot@example.test", Forge: config.Forge{Type: "github"}, Repos: []config.Repo{{Owner: "acme", Name: "app", Base: "stale"}}}
d := &Daemon{Cfg: config.NewHolder(root), Identities: []*IdentityRunner{{Name: "bot", Cfg: id, Repos: id.Repos}}}
task := &workflow.Task{Identity: "bot", Owner: "acme", Repo: "app"}
for _, model := range []string{"old/model", "new/model"} {
root.Models = map[string]string{"builder": model}
root.Budgets.MaxSteps++
root.Dispatch.Trigger = "label"
d.Cfg.Set(root)
got := d.configFor(task)
if !reflect.DeepEqual(got.Models, root.Models) || !reflect.DeepEqual(got.Providers, root.Providers) || got.Budgets != root.Budgets || got.Dispatch != root.Dispatch {
t.Fatalf("identity settings replaced the current runtime settings: %+v", got)
}
repo, ok := d.repoFor(task)
if !ok || repo.Base != "main" {
t.Fatalf("repository = %+v, found %v; want current base main", repo, ok)
}
if got.BotUser != id.BotUser || got.Forge != id.Forge {
t.Fatal("identity lost its forge account")
}
}
}
// signedOnly permits run only for a verified signature, as a policy
// conditioned on context.signature does.
type signedOnly struct{ runAllowed }
func (s signedOnly) Authorize(p access.Principal, a access.Action, r access.Resource, c access.Context) access.Decision {
if c.Signature != "valid" {
return access.DeniedAt(access.LevelOrg, nil)
}
return s.runAllowed.Authorize(p, a, r, c)
}
// TestWorkflowRunCarriesEventContext pins that the run-time re-check sees the
// signature the binding's capture carried, so a signature-conditioned policy
// that admitted the dispatch admits the task, and an unsigned event stays
// refused.
func TestWorkflowRunCarriesEventContext(t *testing.T) {
const deploy = "id: deploy\nidentity: deployer\n"
for _, tt := range []struct {
signature string
want bool
}{{"valid", true}, {"unsigned", false}, {"", false}} {
t.Run(tt.signature, func(t *testing.T) {
d := &Daemon{
Store: &identityStore{}, Log: slog.New(slog.DiscardHandler), RootIdentityID: "root",
Access: signedOnly{runAllowed{"root": true, "id-deployer": true}},
Principals: principalOf{}, Identities: []*IdentityRunner{{ID: "id-deployer", Name: "deployer"}},
}
task := &workflow.Task{
ID: 1, Workflow: "deploy", BindingSignature: tt.signature,
WorkflowDefinitionYAML: deploy, WorkflowDefinitionDigest: workflow.DigestDefinition(deploy),
}
if got := d.adoptWorkflowIdentity(context.Background(), task); got != tt.want {
t.Fatalf("admitted %v, want %v", got, tt.want)
}
})
}
}