fix(access): forbid clause scopes the canary grant against its developer role (#1727)
Service accounts created through the identities API are assigned the developer role in the request's org, whose shipped policy permits read and write across the org -- the permit alone did not scope the canary. Install a matching org-canary-grant-forbid policy (access.CanaryGrantForbidPolicy) that negates exactly the permit's surface for the canary identity; a forbid wins within the level, so the role can no longer widen access. Engine tests pin the surface with the principal carrying the developer role, and the rehearsal now proves it live: the scoped token gets 403 on /api/access/policies and 200 on /api/chat/update, and the full canary loop runs on it. task check passes; task canary:rehearse green (REHEARSE_EXIT=0).
This commit is contained in:
5 files changed
+180
-29
No files matched your search
@@ -8,22 +8,52 @@ import "github.com/samcharles93/archie-core/internal/domain/org"
|
||||
// installed by the operator from CanaryGrantPolicy's text.
|
||||
const CanaryGrantID = "org-canary-grant"
|
||||
|
||||
// CanaryGrantPolicy is the grant a CI canary carries instead of instance
|
||||
// admin: exactly one named identity may read the dashboard's signal surface
|
||||
// (chat update, alerts, logs, tasks), read and write the update-settings
|
||||
// resource, and trigger an update install or deferral. Every clause is
|
||||
// guarded by the resource belonging to the principal's own org, which the
|
||||
// engine enforces structurally anyway.
|
||||
func CanaryGrantPolicy(orgID org.OrgID, identityID string) Policy {
|
||||
text := `permit(principal, action, resource) when {
|
||||
resource.org == principal.org &&
|
||||
principal == Archie::Identity::"` + identityID + `" &&
|
||||
(
|
||||
// CanaryGrantForbidID is the matching forbid's ID: the grant only scopes the
|
||||
// canary if everything else it could reach is forbidden, because the identity
|
||||
// may also carry a role (API-created service accounts are assigned developer)
|
||||
// whose shipped policy permits broadly within the org. A forbid wins within
|
||||
// the level, so the pair scopes the identity regardless of its role.
|
||||
const CanaryGrantForbidID = "org-canary-grant-forbid"
|
||||
|
||||
// canaryGrantActions is the exact action surface the canary needs, as one
|
||||
// Cedar condition shared by the grant's permit and its forbid.
|
||||
func canaryGrantActions() string {
|
||||
return `(
|
||||
(action == Archie::Action::"read" && ["dashboard", "task"].contains(resource.kind)) ||
|
||||
(action == Archie::Action::"read" && resource == Archie::Object::"update-settings") ||
|
||||
(action == Archie::Action::"update" && resource == Archie::Object::"update") ||
|
||||
(action == Archie::Action::"manage_identities" && resource == Archie::Object::"update-settings")
|
||||
)
|
||||
)`
|
||||
}
|
||||
|
||||
func canaryPrincipal(identityID string) string {
|
||||
return `principal == Archie::Identity::"` + identityID + `"`
|
||||
}
|
||||
|
||||
// CanaryGrantPolicy is the grant a CI canary carries instead of instance
|
||||
// admin: exactly one named identity may read the dashboard's signal surface
|
||||
// (chat update, alerts, logs, tasks), read and write the update-settings
|
||||
// resource, and trigger an update install or deferral. Install it together
|
||||
// with CanaryGrantForbidPolicy, which forbids the same identity from
|
||||
// everything else. Every clause is guarded by the resource belonging to the
|
||||
// principal's own org, which the engine enforces structurally anyway.
|
||||
func CanaryGrantPolicy(orgID org.OrgID, identityID string) Policy {
|
||||
text := `permit(principal, action, resource) when {
|
||||
resource.org == principal.org &&
|
||||
` + canaryPrincipal(identityID) + ` &&
|
||||
` + canaryGrantActions() + `
|
||||
};`
|
||||
return Policy{ID: CanaryGrantID, Level: LevelOrg, OrgID: orgID, Text: text}
|
||||
}
|
||||
|
||||
// CanaryGrantForbidPolicy forbids the canary identity from everything outside
|
||||
// CanaryGrantPolicy's surface, so a role assigned to the identity (service
|
||||
// accounts created through the API carry developer) cannot widen it.
|
||||
func CanaryGrantForbidPolicy(orgID org.OrgID, identityID string) Policy {
|
||||
text := `forbid(principal, action, resource) when {
|
||||
resource.org == principal.org &&
|
||||
` + canaryPrincipal(identityID) + ` &&
|
||||
!` + canaryGrantActions() + `
|
||||
};`
|
||||
return Policy{ID: CanaryGrantForbidID, Level: LevelOrg, OrgID: orgID, Text: text}
|
||||
}
|
||||
@@ -26,3 +26,29 @@ func TestCanaryGrantPolicy(t *testing.T) {
|
||||
t.Fatalf("grant for another identity must name that identity only:\n%s", other.Text)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCanaryGrantForbidPolicy pins the forbid that keeps a role assigned to
|
||||
// the canary identity from widening the grant: it names the same identity and
|
||||
// negates exactly the permit's action surface.
|
||||
func TestCanaryGrantForbidPolicy(t *testing.T) {
|
||||
permit := CanaryGrantPolicy(org.DefaultOrgID, "canary-svc")
|
||||
forbid := CanaryGrantForbidPolicy(org.DefaultOrgID, "canary-svc")
|
||||
if err := forbid.Validate(); err != nil {
|
||||
t.Fatalf("Validate: %v", err)
|
||||
}
|
||||
if forbid.ID != CanaryGrantForbidID || forbid.Level != LevelOrg || forbid.OrgID != org.DefaultOrgID {
|
||||
t.Fatalf("forbid = %+v, want ID %q at org level in %q", forbid, CanaryGrantForbidID, org.DefaultOrgID)
|
||||
}
|
||||
if !strings.HasPrefix(forbid.Text, "forbid(") {
|
||||
t.Fatalf("forbid text must be a forbid:\n%s", forbid.Text)
|
||||
}
|
||||
if !strings.Contains(forbid.Text, `principal == Archie::Identity::"canary-svc"`) {
|
||||
t.Fatalf("forbid does not name the canary identity:\n%s", forbid.Text)
|
||||
}
|
||||
// The forbid must negate the permit's surface, not the permit itself.
|
||||
permitBody := strings.TrimSuffix(strings.SplitN(permit.Text, "when {", 2)[1], "};")
|
||||
forbidBody := strings.TrimSuffix(strings.SplitN(forbid.Text, "when {", 2)[1], "};")
|
||||
if !strings.Contains(forbidBody, "!"+canaryGrantActions()) {
|
||||
t.Fatalf("forbid does not negate the grant's action surface:\npermit:%s\nforbid:%s", permitBody, forbidBody)
|
||||
}
|
||||
}
|
||||
@@ -113,17 +113,23 @@ func TestWorkspaceEnvironment(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
// TestCanaryGrant pins the scoped canary grant: the one identity it names may
|
||||
// read the signal surface, read and write update-settings, and trigger an
|
||||
// install -- and nothing else, neither for it nor for anyone else. The canary
|
||||
// principal carries the developer role (API-created service accounts are
|
||||
// assigned it); the grant's forbid must keep the role from widening access.
|
||||
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))
|
||||
forbid := access.CanaryGrantForbidPolicy(org.DefaultOrgID, string(canaryID))
|
||||
engine := New(append(access.ShippedOrgPolicies(org.DefaultOrgID), grant, forbid))
|
||||
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}
|
||||
canary := access.Principal{
|
||||
IdentityID: canaryID, Org: org.DefaultOrgID,
|
||||
Memberships: []org.Membership{{IdentityID: canaryID, OrgID: org.DefaultOrgID, Role: org.RoleDeveloper}},
|
||||
}
|
||||
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}
|
||||
@@ -171,12 +177,17 @@ func TestCanaryGrant(t *testing.T) {
|
||||
// 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)
|
||||
forbid := access.CanaryGrantForbidPolicy(org.DefaultOrgID, "canary-svc")
|
||||
for _, p := range []access.Policy{grant, forbid} {
|
||||
if err := p.Validate(); err != nil {
|
||||
t.Fatalf("Validate %s: %v", p.ID, err)
|
||||
}
|
||||
}
|
||||
engine := New([]access.Policy{grant})
|
||||
if err := engine.Validate(grant); err != nil {
|
||||
t.Fatalf("engine Validate: %v", err)
|
||||
engine := New([]access.Policy{grant, forbid})
|
||||
for _, p := range []access.Policy{grant, forbid} {
|
||||
if err := engine.Validate(p); err != nil {
|
||||
t.Fatalf("engine Validate %s: %v", p.ID, err)
|
||||
}
|
||||
}
|
||||
if len(engine.Problems()) != 0 {
|
||||
t.Fatalf("problems = %v, want none", engine.Problems())
|
||||
|
||||
Reference in new issue
Block a user