Operations register through httpapi.Register with Public, Authorized or AdminAuthorized, and the guard asks the policy chain for the declared action and resource kind before the handler runs. An undeclared operation and an undecidable chain are refused. The workload principal, its shipped policy, a self-service policy and Recording for denials are added in governance.
108 lines
4.5 KiB
Go
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 denial 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;
|
|
};`
|