Files
archie/internal/domain/access/canary_test.go
T
sam 2f8076d7f8
Quality / check (push) Successful in 3m37s
deploy / gate (push) Successful in 4m45s
deploy / archied-image (push) Successful in 2m53s
deploy / agent-image (push) Successful in 10s
deploy / dist-zip (push) Skipped
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).
2026-11-02 06:23:38 +11:00

55 lines
2.4 KiB
Go

package access
import (
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/domain/org"
)
// TestCanaryGrantPolicy pins the shape of the canary grant: it names exactly
// the one identity it is built for, scoped to the org it serves, so a token
// carrying it cannot act as anyone else or reach another org.
func TestCanaryGrantPolicy(t *testing.T) {
p := CanaryGrantPolicy(org.DefaultOrgID, "canary-svc")
if err := p.Validate(); err != nil {
t.Fatalf("Validate: %v", err)
}
if p.ID != CanaryGrantID || p.Level != LevelOrg || p.OrgID != org.DefaultOrgID {
t.Fatalf("policy = %+v, want ID %q at org level in %q", p, CanaryGrantID, org.DefaultOrgID)
}
if !strings.Contains(p.Text, `principal == Archie::Identity::"canary-svc"`) {
t.Fatalf("text does not name the canary identity:\n%s", p.Text)
}
other := CanaryGrantPolicy(org.DefaultOrgID, "another")
if strings.Contains(other.Text, "canary-svc") || !strings.Contains(other.Text, `Archie::Identity::"another"`) {
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)
}
}