Every attempt checks that the identity that started the task and the identity the workflow declares may both run it, recording a denial for either. A workflow that declares no identity is refused rather than running as its starter or the root. A workflow.call callee is started by its caller's declared identity instead of inheriting the caller's row. The workflow settings panel marks the identity as required and offers the agent identities.
141 lines
6.2 KiB
Go
141 lines
6.2 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"
|
|
root.Notify.Webhook = "https://example.test/notify"
|
|
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 || got.Notify != root.Notify {
|
|
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")
|
|
}
|
|
}
|
|
}
|