Files
archie/internal/daemon/workflow_identity_test.go
T
sam caf560bdfa Feat/operator alerts (#1635)
* feat(state): durable operator alerts owned by the State Store

Alerts dedup on org+kind+subject, resolve themselves when a condition
clears, and are raised, resolved, dismissed and listed through the state
contract. Task transitions raise needs-human and failed alerts in the same
transaction; the store checks service health and prunes closed alerts.
Each raise and resolve queues a webhook delivery whose attempts are kept.

* feat(notify): deliver operator alerts to the webhook; raise config and release alerts

notify.webhook now receives raised and resolved alerts above a severity
threshold with HMAC-signed payloads, bounded retry and every attempt recorded
on the alert. archied raises model alias, provider and credential-binding
alerts from the effective configuration, an update alert from the release
channel, and a credential-rejected alert from the egress resolver. The
dashboard API lists and dismisses the caller's org's alerts.

* feat(ui): alert bell in the top bar and webhook alert settings

The bell lists the org's open alerts by severity with dismiss and a link per
alert, and refreshes from the event stream. The task settings page gains the
webhook severity threshold and signing secret. ui/dist rebuilt.
2026-10-12 03:30:46 +11:00

140 lines
6.1 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")
}
}
}