The canary now authenticates as a dedicated service-account identity in the
system org whose only grant is access.CalaryGrantPolicy: read the signal
endpoints (chat update, alerts, logs, tasks), read and write the
update-settings resource, and trigger an update install. CANARY_TOKEN no
longer carries instance admin; DEPLOY_TOKEN stays CI-only.
- authorize: POST /api/chat/update/install and /defer map to
update on Object::"update" (dashboard) instead of run on any workflow,
so the grant can cover installs without granting workflow runs
- accounts: active service-account identities may authenticate with bearer
tokens (CanAct), not just users
- new POST /api/identities/{id}/tokens (behind manage_identities) mints a
token for a named non-user identity; new AccountService RPC
BindIdentityPersonalToken backs it remotely, refusing user-kind targets,
with the caller recorded as audit actor
- engine tests pin the grant's exact allow/deny surface
RELEASING.md documents the one-time setup. task check passes on this tree.
185 lines
8.4 KiB
Go
185 lines
8.4 KiB
Go
package access
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
)
|
|
|
|
// TestInvalidInstancePolicy pins that an instance policy that no longer
|
|
// compiles closes the instance without locking the operator out: only an
|
|
// instance admin working on the policies passes, so the policy can be
|
|
// repaired from the dashboard.
|
|
func TestInvalidInstancePolicy(t *testing.T) {
|
|
broken := access.Policy{ID: "broken", Level: access.LevelInstance, Text: `permit(principal, action == Archie::Action::"nope", resource);`}
|
|
policies := append(access.ShippedOrgPolicies(org.DefaultOrgID), access.ShippedOrgPolicies("acme")...)
|
|
engine := New(append(policies, broken))
|
|
if len(engine.Problems()) != 1 {
|
|
t.Fatalf("problems = %v, want the broken instance policy", engine.Problems())
|
|
}
|
|
|
|
owner := func(id identity.IdentityID, o org.OrgID) access.Principal {
|
|
return access.Principal{IdentityID: id, Org: o, Memberships: []org.Membership{{IdentityID: id, OrgID: o, Role: org.RoleOwner}}}
|
|
}
|
|
admin := owner("admin", org.DefaultOrgID)
|
|
tests := []struct {
|
|
name string
|
|
principal access.Principal
|
|
action access.Action
|
|
kind access.ResourceKind
|
|
allowed bool
|
|
}{
|
|
{"instance admin edits policies", admin, access.ActionManagePolicies, access.KindPolicy, true},
|
|
{"instance admin reads policies", admin, access.ActionRead, access.KindPolicy, true},
|
|
{"instance admin reads anything else", admin, access.ActionRead, access.KindDashboard, false},
|
|
{"another org's owner edits policies", owner("acme-owner", "acme"), access.ActionManagePolicies, access.KindPolicy, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := engine.Authorize(tt.principal, tt.action, access.Resource{Kind: tt.kind}, access.Context{})
|
|
if got.Allowed != tt.allowed {
|
|
t.Fatalf("allowed = %v, want %v (%+v)", got.Allowed, tt.allowed, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestReadAuditRoleBoundary pins that the audit feed is an administrator's
|
|
// read: the dashboard's broad read grant (viewer and developer) must not open
|
|
// the instance's settings, access and identity history.
|
|
func TestReadAuditRoleBoundary(t *testing.T) {
|
|
const auditOrg = org.OrgID("acme")
|
|
engine := New(access.ShippedOrgPolicies(auditOrg))
|
|
tests := []struct {
|
|
role org.Role
|
|
allowed bool
|
|
}{
|
|
{org.RoleOwner, true},
|
|
{org.RoleAdmin, true},
|
|
{org.RoleDeveloper, false},
|
|
{org.RoleViewer, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(string(tt.role), func(t *testing.T) {
|
|
p := access.Principal{
|
|
IdentityID: "person", Org: auditOrg,
|
|
Memberships: []org.Membership{{IdentityID: "person", OrgID: auditOrg, Role: tt.role}},
|
|
}
|
|
got := engine.Authorize(p, access.ActionReadAudit, access.Resource{Kind: access.KindAudit, Org: auditOrg}, access.Context{})
|
|
if got.Allowed != tt.allowed {
|
|
t.Fatalf("allowed = %v, want %v (%+v)", got.Allowed, tt.allowed, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestWorkspaceEnvironment pins that a policy can read the environment of
|
|
// the workspace a resource is in: it validates on save and decides runs in
|
|
// that workspace only.
|
|
func TestWorkspaceEnvironment(t *testing.T) {
|
|
frozen := access.Policy{
|
|
ID: "no-prod-runs", Level: access.LevelOrg, OrgID: "acme",
|
|
Text: `forbid(principal, action == Archie::Action::"run", resource) when { resource.workspace.environment == "production" };`,
|
|
}
|
|
engine := New(append(access.ShippedOrgPolicies("acme"), frozen))
|
|
if err := engine.Validate(frozen); err != nil {
|
|
t.Fatalf("validate: %v", err)
|
|
}
|
|
dev := access.Principal{
|
|
IdentityID: "dev", Org: "acme",
|
|
Memberships: []org.Membership{{IdentityID: "dev", OrgID: "acme", Role: org.RoleDeveloper}},
|
|
Workspaces: []org.Workspace{{ID: "live", OrgID: "acme", Environment: "production"}, {ID: "stage", OrgID: "acme", Environment: "staging"}},
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
action access.Action
|
|
workspace org.WorkspaceID
|
|
allowed bool
|
|
}{
|
|
{"run in a production workspace", access.ActionRun, "live", false},
|
|
{"other actions in it", access.ActionUpdate, "live", true},
|
|
{"run in another environment", access.ActionRun, "stage", true},
|
|
{"run on a record with no workspace", access.ActionRun, "", true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
resource := access.Resource{Kind: access.KindWorkflow, ID: "triage", Org: "acme", Workspace: tt.workspace}
|
|
if got := engine.Authorize(dev, tt.action, resource, access.Context{}); got.Allowed != tt.allowed {
|
|
t.Fatalf("allowed = %v, want %v (%+v)", got.Allowed, tt.allowed, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCanaryGrant pins the scoped canary grant: the one identity it names, with
|
|
// no role, may read the signal surface, read and write update-settings, and
|
|
// trigger an install -- and nothing else, neither for it nor for anyone else.
|
|
func TestCanaryGrant(t *testing.T) {
|
|
const canaryID = identity.IdentityID("canary-svc")
|
|
grant := access.CanaryGrantPolicy(org.DefaultOrgID, string(canaryID))
|
|
engine := New(append(access.ShippedOrgPolicies(org.DefaultOrgID), grant))
|
|
if len(engine.Problems()) != 0 {
|
|
t.Fatalf("problems = %v, want the canary grant to compile", engine.Problems())
|
|
}
|
|
canary := access.Principal{IdentityID: canaryID, Org: org.DefaultOrgID}
|
|
other := access.Principal{IdentityID: "someone-else", Org: org.DefaultOrgID}
|
|
at := func(kind access.ResourceKind, id string) access.Resource {
|
|
return access.Resource{Kind: kind, ID: id, Org: org.DefaultOrgID}
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
principal access.Principal
|
|
action access.Action
|
|
resource access.Resource
|
|
allowed bool
|
|
}{
|
|
{"read the dashboard", canary, access.ActionRead, at(access.KindDashboard, ""), true},
|
|
{"read tasks", canary, access.ActionRead, at(access.KindTask, ""), true},
|
|
{"read one task", canary, access.ActionRead, at(access.KindTask, "7"), true},
|
|
{"read update-settings", canary, access.ActionRead, at(access.KindPolicy, "update-settings"), true},
|
|
{"install an update", canary, access.ActionUpdate, at(access.KindDashboard, "update"), true},
|
|
{"replace update-settings", canary, access.ActionManageIdentities, at(access.KindPolicy, "update-settings"), true},
|
|
{"read a secret", canary, access.ActionReadSecret, at(access.KindSecret, "github-token"), false},
|
|
{"delete a task", canary, access.ActionDelete, at(access.KindTask, "7"), false},
|
|
{"manage policies", canary, access.ActionManagePolicies, at(access.KindPolicy, "org-role-read"), false},
|
|
{"run a workflow", canary, access.ActionRun, at(access.KindWorkflow, "triage"), false},
|
|
{"create anything", canary, access.ActionCreate, at(access.KindTask, ""), false},
|
|
{"update another object", canary, access.ActionUpdate, at(access.KindDashboard, "other"), false},
|
|
{"read another object", canary, access.ActionRead, at(access.KindPolicy, "other-policy"), false},
|
|
{"manage identities on another object", canary, access.ActionManageIdentities, at(access.KindIdentity, "someone-else"), false},
|
|
{"read logs", canary, access.ActionReadLogs, at(access.KindTask, "7"), false},
|
|
{"read the audit feed", canary, access.ActionReadAudit, at(access.KindAudit, ""), false},
|
|
{"a resource of another org", canary, access.ActionRead, access.Resource{Kind: access.KindDashboard, Org: "acme"}, false},
|
|
{"another identity reads the dashboard", other, access.ActionRead, at(access.KindDashboard, ""), false},
|
|
{"another identity installs an update", other, access.ActionUpdate, at(access.KindDashboard, "update"), false},
|
|
{"another identity replaces update-settings", other, access.ActionManageIdentities, at(access.KindPolicy, "update-settings"), false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := engine.Authorize(tt.principal, tt.action, tt.resource, access.Context{})
|
|
if got.Allowed != tt.allowed {
|
|
t.Fatalf("allowed = %v, want %v (%+v)", got.Allowed, tt.allowed, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCanaryGrantValidates pins that the canary grant passes the domain's
|
|
// structural check and the engine's schema validation, so it can be installed
|
|
// through the policy store.
|
|
func TestCanaryGrantValidates(t *testing.T) {
|
|
grant := access.CanaryGrantPolicy(org.DefaultOrgID, "canary-svc")
|
|
if err := grant.Validate(); err != nil {
|
|
t.Fatalf("Validate: %v", err)
|
|
}
|
|
engine := New([]access.Policy{grant})
|
|
if err := engine.Validate(grant); err != nil {
|
|
t.Fatalf("engine Validate: %v", err)
|
|
}
|
|
if len(engine.Problems()) != 0 {
|
|
t.Fatalf("problems = %v, want none", engine.Problems())
|
|
}
|
|
}
|