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) } }) } }