Files
sam 062336db91 refactor: rename denial records to access decisions
- rename governance.access_denials table, indexes and API types via migration 00061
- expose /api/access/decisions and decisions payload; keep audit auth unchanged
- rename UI store, page and route from denials to decisions
2026-11-23 02:03:35 +11:00

108 lines
4.5 KiB
Go

package governance
import "slices"
const (
roleOwner = string(RoleOwner)
roleAdmin = string(RoleAdmin)
roleDeveloper = string(RoleDeveloper)
roleViewer = string(RoleViewer)
roleWorkload = string(RoleWorkload)
)
// Shipped policy IDs overwritten by seeding and org policy resets.
const (
PolicyOrgRead = "org-role-read"
PolicyOrgEdit = "org-role-edit"
PolicyOrgAdmin = "org-role-admin"
PolicyOrgOwner = "org-role-owner"
// PolicyOrgWorkload grants the principal background work acts as.
PolicyOrgWorkload = "org-workload"
// PolicyOrgSelfService lets every member manage their own account,
// conversations and notes.
PolicyOrgSelfService = "org-self-service"
PolicyCrossOrgID = "instance-cross-org"
)
const orgReadPolicy = `permit(principal, action, resource) when {
resource.org == principal.org &&
["` + roleViewer + `", "` + roleDeveloper + `", "` + roleAdmin + `", "` + roleOwner + `"].contains(principal.role) &&
action in [Archie::Action::"read", Archie::Action::"read_logs"]
};`
const orgEditPolicy = `permit(principal, action, resource) when {
resource.org == principal.org &&
["` + roleDeveloper + `", "` + roleAdmin + `", "` + roleOwner + `"].contains(principal.role) &&
action in [Archie::Action::"create", Archie::Action::"update", Archie::Action::"run"]
};`
const orgAdminPolicy = `permit(principal, action, resource) when {
resource.org == principal.org &&
["` + roleAdmin + `", "` + roleOwner + `"].contains(principal.role) &&
(action != Archie::Action::"delete" || resource.kind != "org") &&
(resource.kind != "member" || resource.state != "` + roleOwner + `")
};`
const orgOwnerPolicy = `permit(principal, action, resource) when {
resource.org == principal.org &&
principal.role == "` + roleOwner + `"
};`
// orgWorkloadPolicy lets trigger dispatch, run workers and delivery workers do
// the work they were queued to do and nothing else.
const orgWorkloadPolicy = `permit(principal, action, resource) when {
resource.org == principal.org &&
principal.role == "` + roleWorkload + `" &&
action in [Archie::Action::"read", Archie::Action::"run", Archie::Action::"deliver"]
};`
// orgSelfServicePolicy lets any member use the account, conversation and note
// routes, whose handlers scope every row to the caller.
const orgSelfServicePolicy = `permit(principal, action, resource) when {
resource.org == principal.org &&
["` + roleViewer + `", "` + roleDeveloper + `", "` + roleAdmin + `", "` + roleOwner + `"].contains(principal.role) &&
["` + string(KindAccount) + `", "` + string(KindChatSession) + `", "` + string(KindNote) + `"].contains(resource.kind) &&
action in [Archie::Action::"read", Archie::Action::"create", Archie::Action::"update", Archie::Action::"delete"]
};`
// ShippedRolePolicies returns the four shipped role policies for one org.
func ShippedRolePolicies(orgID OrgID) []Policy {
return []Policy{
{ID: PolicyOrgRead, Level: LevelOrg, OrgID: orgID, Text: orgReadPolicy},
{ID: PolicyOrgEdit, Level: LevelOrg, OrgID: orgID, Text: orgEditPolicy},
{ID: PolicyOrgAdmin, Level: LevelOrg, OrgID: orgID, Text: orgAdminPolicy},
{ID: PolicyOrgOwner, Level: LevelOrg, OrgID: orgID, Text: orgOwnerPolicy},
}
}
// ShippedWorkloadPolicy returns the shipped policy for the workload principal.
func ShippedWorkloadPolicy(orgID OrgID) Policy {
return Policy{ID: PolicyOrgWorkload, Level: LevelOrg, OrgID: orgID, Text: orgWorkloadPolicy}
}
// ShippedSelfServicePolicy returns the shipped policy for self-service routes.
func ShippedSelfServicePolicy(orgID OrgID) Policy {
return Policy{ID: PolicyOrgSelfService, Level: LevelOrg, OrgID: orgID, Text: orgSelfServicePolicy}
}
// shippedExtraPolicies are the shipped policies added after the role
// policies. Each is seeded on its own, so an org seeded before it existed
// still receives it.
func shippedExtraPolicies(orgID OrgID) []Policy {
return []Policy{ShippedWorkloadPolicy(orgID), ShippedSelfServicePolicy(orgID)}
}
// ShippedOrgPolicies returns every policy shipped for one org.
func ShippedOrgPolicies(orgID OrgID) []Policy {
return slices.Clip(append(ShippedRolePolicies(orgID), shippedExtraPolicies(orgID)...))
}
// CrossOrgForbidID is the ID reported in decision records for cross-org refusals.
const CrossOrgForbidID = PolicyCrossOrgID
// CrossOrgForbidText describes the non-removable cross-org forbid enforced
// structurally by the engine before Cedar evaluation.
const CrossOrgForbidText = `forbid(principal, action, resource) when {
resource.org != principal.org;
};`