diff --git a/RELEASING.md b/RELEASING.md index 96d9b3c3d..1bc3d9288 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -208,19 +208,31 @@ curl -X POST "$CANARY_URL/api/identities//tokens" \ -d '{"name":"ci-canary","days":90}' # -> {"id":"","token":"archie_pat_..."} -- store the token value as the CANARY_TOKEN secret -# 3. Install the grant, scoped to that identity. The policy's org is pinned -# to the caller's org (org-sys for the instance admin), so the body needs -# no org field. +# 3. Install the grant pair, scoped to that identity. The policies' org is +# pinned to the caller's org (org-sys for the instance admin), so the +# bodies need no org field. The permit alone is not enough: service +# accounts created through the API are assigned the developer role, so a +# matching forbid (installed second) negates everything outside the +# permit's surface for that identity. curl -X PUT "$CANARY_URL/api/access/policies" -H "Authorization: Bearer $ADMIN" \ -H 'Content-Type: application/json' \ -d '{"id":"org-canary-grant","level":"org","text":"permit(principal, action, resource) when {\n resource.org == principal.org &&\n principal == Archie::Identity::\"\" &&\n (\n (action == Archie::Action::\"read\" && [\"dashboard\", \"task\"].contains(resource.kind)) ||\n (action == Archie::Action::\"read\" && resource == Archie::Object::\"update-settings\") ||\n (action == Archie::Action::\"update\" && resource == Archie::Object::\"update\") ||\n (action == Archie::Action::\"manage_identities\" && resource == Archie::Object::\"update-settings\")\n )\n};"}' ``` -The policy text is exactly `access.CanaryGrantPolicy(org-sys, )` -from `internal/domain/access/canary.go`; the curl body above is that text with +The second policy forbids the canary identity from everything the permit does +not name, so its developer role cannot widen the grant: + +```bash +curl -X PUT "$CANARY_URL/api/access/policies" -H "Authorization: Bearer $ADMIN" \ + -H 'Content-Type: application/json' \ + -d '{"id":"org-canary-grant-forbid","level":"org","text":"forbid(principal, action, resource) when {\\n resource.org == principal.org &&\\n principal == Archie::Identity::\\\"\\\" &&\\n !(\\n (action == Archie::Action::\\\"read\\\" && [\\\"dashboard\\\", \\\"task\\\"].contains(resource.kind)) ||\\n (action == Archie::Action::\\\"read\\\" && resource == Archie::Object::\\\"update-settings\\\") ||\\n (action == Archie::Action::\\\"update\\\" && resource == Archie::Object::\\\"update\\\") ||\\n (action == Archie::Action::\\\"manage_identities\\\" && resource == Archie::Object::\\\"update-settings\\\")\\n )\\n};"}' +``` + +The policy texts are exactly `access.CanaryGrantPolicy` and +`access.CanaryGrantForbidPolicy` from `internal/domain/access/canary.go` with the identity id filled in. The canary needs no other rights: `GET /health/detailed` is unauthenticated, and every other call it makes is covered -by the grant. +by the grant pair. The runner must reach `CANARY_URL`. Without a canary a release stays on `next` until promoted by hand, under the same refusals: diff --git a/internal/domain/access/canary.go b/internal/domain/access/canary.go index 9423e93ed..4fcb24a88 100644 --- a/internal/domain/access/canary.go +++ b/internal/domain/access/canary.go @@ -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} +} diff --git a/internal/domain/access/canary_test.go b/internal/domain/access/canary_test.go index fc05e09ea..fe9f4fae3 100644 --- a/internal/domain/access/canary_test.go +++ b/internal/domain/access/canary_test.go @@ -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) + } +} diff --git a/internal/infrastructure/access/engine_test.go b/internal/infrastructure/access/engine_test.go index 4cb1a69d3..874580c5f 100644 --- a/internal/infrastructure/access/engine_test.go +++ b/internal/infrastructure/access/engine_test.go @@ -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()) diff --git a/tools/canary-rehearse.sh b/tools/canary-rehearse.sh index aee66e977..33f62b9a0 100755 --- a/tools/canary-rehearse.sh +++ b/tools/canary-rehearse.sh @@ -7,6 +7,9 @@ # archie-ui) with its own config, state dir and ports. The loop's commands -- # release canary start, release canary status, release canary check and # release canary promote -- are then run against that host, each step printed. +# The loop authenticates with a scoped service-account token carrying only +# the org-canary-grant policy (#1727), and a step proves that token is denied +# everything outside the grant. # The local host has no systemd, so once the install has switched the binary # the script restarts archied itself, the way an unmanaged host's operator # would. @@ -41,6 +44,7 @@ trap cleanup EXIT INT TERM die() { echo "canary-rehearse: $*" >&2 + keep=1 exit 1 } @@ -269,6 +273,60 @@ resp=$(api POST /api/control-plane/resources/update-settings/commands/replace \ "{\"value\":$value,\"expected_version\":$ver}") printf '%s' "$resp" | grep -q '"resource"' || die "could not set update-settings on the local host: $resp" +# --- the scoped canary grant (#1727): a service account, not instance admin -- +# Create a service_account identity, mint a token for it, install the +# org-canary-grant policy, then prove the token can read the signals but +# nothing else. +echo +echo "== creating the canary service account and installing the scoped grant" +canary_id=$(api POST /api/identities '{"display_name":"canary","kind":"service_account"}' | py 'd["id"]') || + die "could not create the canary service account" +[ -n "$canary_id" ] || die "identity create printed no id" +scoped=$(api POST "/api/identities/$canary_id/tokens" '{"name":"canary","days":7}' | py 'd["token"]') || + die "could not mint a token for the canary identity" +[ -n "$scoped" ] || die "token create printed no token" +grant_policy() { # id prefix permit|forbid -> policy JSON on stdout + python3 -c 'import json, sys +who, effect, identity = sys.argv[1], sys.argv[2], sys.argv[3] +actions = """( + (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") + )""" +body = """ resource.org == principal.org && + principal == Archie::Identity::"%s" && + %s%s""" % (identity, "!" if effect == "forbid" else "", actions) +print(json.dumps({"id": "org-canary-grant" + ("-forbid" if effect == "forbid" else ""), + "level": "org", "org_id": "org-sys", + "text": "%s(principal, action, resource) when {\n%s\n};" % (effect, body)}))' "$1" "$2" "$canary_id" +} +for effect in permit forbid; do + resp=$(api PUT /api/access/policies "$(grant_policy grant "$effect")") + printf '%s' "$resp" | grep -q '"version"' || die "could not install the canary grant $effect policy: $resp" +done + +bearer() { # path -> curl with the scoped token, prints the status code + curl -sS --max-time 15 -o /dev/null -w '%{http_code}' \ + -H "Authorization: Bearer $scoped" "$base$1" +} +bearer_body() { # path -> curl with the scoped token, prints the body + curl -sS --max-time 15 -H "Authorization: Bearer $scoped" "$base$1" +} +# The policy chain reloads asynchronously; allow a few seconds for the grant. +# The negative probe can run now; the positive one must wait for the canary +# loop to switch the channel to next (a missing stable tag is a 502 from the +# update check, not an authorization answer). +i=0 +code=000 +while :; do + code=$(bearer /api/access/policies) + [ "$code" != "200" ] && break + i=$((i + 1)); [ "$i" -gt 45 ] && die "the scoped canary token can read the policies API: the grant is not scoped" + sleep 1 +done +echo "scoped token reads /api/access/policies: $code (denied, as intended)" + # --- the canary loop, each step printed --------------------------------------- failed=0 @@ -287,7 +345,9 @@ run_step() { # description command... fi } -export ARCHIE_CANARY_TOKEN=$token +# The canary loop runs with the scoped service-account token, not the admin +# one: that is the point of #1727. +export ARCHIE_CANARY_TOKEN=$scoped echo echo "== release canary start -- point the host at next and install $release_version" @@ -326,6 +386,18 @@ else keep=1 fi +# Now that the canary follows next, the scoped token's positive probe: the +# policy chain reloads asynchronously, so allow a few seconds. +i=0 +code=000 +while :; do + code=$(bearer /api/chat/update) + [ "$code" = "200" ] && break + i=$((i + 1)); [ "$i" -gt 45 ] && die "the scoped canary token never got read access to /api/chat/update (last status $code: $(bearer_body /api/chat/update))" + sleep 1 +done +echo "scoped token reads /api/chat/update: 200" + run_step "release canary status -- what the canary runs and how the window is going" \ timeout 120 "$bin/archied" release canary status -dashboard "$base" \ -source "127.0.0.1:$reg_port/archie" -key "$pub"