Skip to content

fix(pool): one inline policy per queue on the shared worker roles (calque#226) - #231

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/226-per-queue-grants
Nov 16, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/226-per-queue-grants

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #226. Part of the Leave-no-trace epic spore-host/spore-host#752.

Root cause

Every pool's workers run under calque-pool-worker, and every fleet run's under calque-fleet-worker. Both are shared role names. ProvisionWorkers, ScaleWorkers and ProvisionFleetWorkers passed their one-queue policy to spawn's CreateOrGetInstanceProfile as InlinePolicyJSON. In spawn v0.131.0 (the version calque pins), createIAMRole writes that policy for a new role and updateInlinePolicy writes it for an existing one. Both call PutRolePolicy under the fixed name spawn-scoped-policy, and PutRolePolicy replaces the whole document. So pool create --model b removed pool a's queue grant, and pool a's workers started getting AccessDenied on ReceiveMessage. Two fleet runs raced the same way.

Relation to #167

#167 found the same PutRolePolicy-replaces behaviour on the real-run path and fixed it with one role per bucket (roleNameForBucket). That approach doesn't carry over here. Pools and fleet runs would need a role and instance profile per model or run id, and nothing reaps those. Instead the role stays shared and each queue gets its own inline policy.

Fix

  • Per-queue grant. Each queue's grant is named q- plus the first 12 hex characters of sha256(queue ARN) (QueueGrantPolicyName). It is deterministic, so provisioning and teardown agree on the name without storing any state.
  • No shared policy from spawn. CreateOrGetInstanceProfile is now called with no policy. spawn still creates the role and profile and attaches SSM and spored-baseline-policy, but writes no shared policy. calque then writes the queue's grant with PutRolePolicy (internal/pool/iam.go).
  • Removal.
    • calque pool delete removes that pool's grant after its queue is deleted. If the queue delete fails, the grant is kept so a retry is consistent.
    • The fleet run's cleanup removes the run's grant, and so does its provisioning-failure path. A fleet run is one-shot, so this happens even if the queue delete failed.
    • DeleteRolePolicy only ever targets that one q-* name. calque never touches the role's other inline policies.
  • Size limit. If a grant would push the role past IAM's 10,240-byte aggregate inline-policy limit, provisioning fails with ErrRolePolicyBudget and a message saying what to do. It does not fall back to a broader grant. Measured: a grant is about 700 bytes and spawn's baseline about 1.3 KB, so roughly 12 concurrent pools or fleet runs fit per role.
  • Retries. Throttling and ConcurrentModification are retried, since concurrent pools now write different policies on one role at the same time.
  • Migration. A leftover spawn-scoped-policy is logged and left in place. It is deleted in two cases: when the one queue it grants is torn down, or when a newly written per-queue grant is identical to it. In both cases nothing can still depend on it. Not handled: a legacy policy whose queue an older calque already deleted stays in place, because offline we can't tell it apart from a live legacy pool in another region or account. The docs say so. There is also a small read-then-delete window if an older calque writes the legacy policy at the same moment.

New permissions for the calque caller

  • iam:DeleteRolePolicy: required for pool and fleet teardown.
  • iam:GetRolePolicy and iam:ListRolePolicies: best-effort, used only for legacy migration and the size-limit message. Without them the policy is logged and left in place.

docs/guide/getting-started.md is updated. Its existing list already omitted iam:GetRole and iam:GetInstanceProfile, which spawn's helper calls. That gap predates this PR and is left as it was.

Tests (internal/pool/iam_test.go)

The tests use a fake IAM with real PutRolePolicy replace-by-name semantics and the aggregate size cap. A fake CreateOrGetInstanceProfile models spawn v0.131.0, writing InlinePolicyJSON as spawn-scoped-policy.

  • TestEnsureWorkerProfile_ConcurrentPoolsKeepBothGrants
  • TestDeletePool_KeepsOtherPoolsGrant, and TestDeletePool_QueueDeleteFailsKeepsGrant
  • TestFleetCleanup_RemovesOnlyItsOwnPolicy (also checks idempotence)
  • TestQueueGrantPolicyName_Deterministic, with the value pinned and checked against shasum
  • TestPutQueueGrant_SizeLimitIsSurfaced
  • TestPutQueueGrant_RetriesConcurrentModification
  • TestLegacySharedPolicy, with 4 cases

Revert check: I put the original call back (InlinePolicyJSON passed through to spawn, no per-queue write, no legacy handling). With that, the concurrent-pools test fails with pool bert lost its queue grant: role policies = [spawn-scoped-policy spored-baseline-policy]. The delete-keeps-other, fleet-cleanup and size-limit tests fail too. The original code called the concrete *spawnaws.Client with no seam, so the revert was done through the new seam.

Gates

All pass: gofmt -l . (clean), go vet ./..., go build ./..., go test -count=1 ./... (parse contract tests ran with uv, 0 skips), go test -race on pool and cmd/calque, golangci-lint run (0 issues; local v2.15.0, CI pins v2.12.2), govulncheck (0 affecting), uvx ruff check . (no Python changed), and semgrep scan --config=auto --error on the changed Go files (exit 0). Nothing was run against AWS.

…lque#226)

calque-pool-worker and calque-fleet-worker are shared, account-wide roles.
Each pool or fleet provisioning call handed its one-queue policy to spawn's
CreateOrGetInstanceProfile as InlinePolicyJSON. In spawn v0.131.0 that is
written with PutRolePolicy under the fixed name "spawn-scoped-policy"
(createIAMRole for a new role, updateInlinePolicy for an existing one), and
PutRolePolicy replaces the document. So `pool create --model b` removed
pool a's queue grant and pool a's workers lost access to their queue; two
fleet runs raced the same way.

calque#167 fixed this class for real runs with a role per bucket. That does
not fit pools and fleet runs: a role and instance profile per run id would
accumulate with nothing reaping them. Instead the role stays shared and each
queue gets its own inline policy, q-<first 12 hex of sha256(queue ARN)>:

- CreateOrGetInstanceProfile is now called with no policy, so spawn creates
  the role and profile and attaches SSM and spored-baseline-policy but writes
  no shared policy. calque then PutRolePolicy's the queue's own grant.
- `calque pool delete` removes that pool's grant once its queue is gone (kept
  if the queue delete fails, so a retry is consistent). The fleet run's
  cleanup, and its provisioning-failure path, remove the run's grant.
  DeleteRolePolicy only ever targets the one q-* name; other inline policies
  are never touched.
- A grant that would exceed IAM's 10,240-byte aggregate inline-policy limit
  fails with ErrRolePolicyBudget and a message saying what to do. No fallback
  to a broader grant.
- Throttling and ConcurrentModification are retried (concurrent pools now
  write different policies on one role at the same time).
- Migration: a leftover spawn-scoped-policy is logged and left in place. It
  is deleted when the one queue it grants is torn down, or when a newly
  written per-queue grant is identical to it. A legacy policy whose queue an
  older calque already deleted is not removed automatically (documented).

New caller permissions: iam:DeleteRolePolicy (required for teardown);
iam:GetRolePolicy and iam:ListRolePolicies (best-effort: legacy migration and
the size-limit message). docs/guide/getting-started.md updated.

Tests use a fake IAM with real PutRolePolicy replace semantics and a fake
CreateOrGetInstanceProfile modelling spawn v0.131.0. Reverting the grant step
to the original call (InlinePolicyJSON through spawn) fails the concurrent
pools, delete-keeps-other, fleet-cleanup and size-limit tests.

Fixes #226
@scttfrdmn
scttfrdmn marked this pull request as ready for review November 16, 2026 19:35
@scttfrdmn
scttfrdmn merged commit 5f031db into main Nov 16, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/226-per-queue-grants branch November 16, 2026 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shared worker roles: concurrent pools and fleets overwrite each other's queue grants

1 participant