Skip to content

fix(infra): trust the ENVIRONMENT subject on the apply role, not the ref - #356

Merged
athvin merged 1 commit into
mainfrom
bm/infra-apply-oidc-subject
Oct 30, 2026
Merged

athvin merged 1 commit into
mainfrom
bm/infra-apply-oidc-subject

Conversation

@athvin

@athvin athvin commented Oct 30, 2026

Copy link
Copy Markdown
Owner

The first unattended apply failed all three envs in under three minutes:

##[error]Could not assume role with OIDC:
Not authorized to perform sts:AssumeRoleWithWebIdentity

The role exists — I created it in the bootstrap apply. The cause is the OIDC
sub claim. A job that references a GitHub Environment gets
…:environment:<name> instead of …:ref:refs/heads/main, and each apply job
sets environment: infra-apply-<env>. So a ref-pinned StringEquals could
never match it.

The diagnosis is a clean contrast rather than a guess: plan and apply use
the same provider, the same repo and the same branch, and differ only in that
plan sets no environment. plan succeeded; apply could not assume.

Now trusting the three environment subjects explicitly.

This is not weaker than the ref condition

Each infra-apply-* environment carries
deployment_branch_policy.protected_branches = true, so only a protected
branch — main — can deploy to it, and GitHub enforces that before minting the
token. The main-only property moves from the claim to the environment; it does
not disappear. Listing the three names rather than globbing :environment:*
keeps a future environment from silently inheriting apply rights.

The other two roles are correct as they stand — checked, not assumed

er-ci-deploy-dev and the sccache pair are used by jobs that set no
environment, so their ref-based subjects are right. Verified rather than
reasoned: images.yml's deploy-dev has no environment: key, and sccache's
write role demonstrably works — the first main run after #350 reported

Cache hits rate   100.00 %
Cache write errors     0

which also closes out the read/write split: PRs get write errors and succeed,
main writes cleanly.

One more human apply, and it is the last one

This changes the apply role's own trust policy, so it cannot be applied by the
role it fixes. envs/core needs one more human apply; after that CI owns every
apply including this file's future changes.

Co-Authored-By: Claude Opus 5 (1M context) [email protected]

🤖 Generated with Claude Code

The first unattended apply failed all three envs in under three minutes:

    ##[error]Could not assume role with OIDC:
    Not authorized to perform sts:AssumeRoleWithWebIdentity

The role exists — I created it in the bootstrap apply. The cause is the OIDC
`sub` claim. A job that references a GitHub Environment gets
`…:environment:<name>` instead of `…:ref:refs/heads/main`, and each apply job
sets `environment: infra-apply-<env>`. So a ref-pinned `StringEquals` could
never match it.

The diagnosis is a clean contrast rather than a guess: `plan` and `apply` use
the same provider, the same repo and the same branch, and differ only in that
`plan` sets no environment. `plan` succeeded; `apply` could not assume.

Now trusting the three environment subjects explicitly.

## This is not weaker than the ref condition

Each `infra-apply-*` environment carries
`deployment_branch_policy.protected_branches = true`, so only a protected
branch — `main` — can deploy to it, and GitHub enforces that before minting the
token. The main-only property moves from the claim to the environment; it does
not disappear. Listing the three names rather than globbing `:environment:*`
keeps a future environment from silently inheriting apply rights.

## The other two roles are correct as they stand — checked, not assumed

`er-ci-deploy-dev` and the sccache pair are used by jobs that set no
environment, so their ref-based subjects are right. Verified rather than
reasoned: `images.yml`'s `deploy-dev` has no `environment:` key, and sccache's
write role demonstrably works — the first `main` run after #350 reported

    Cache hits rate   100.00 %
    Cache write errors     0

which also closes out the read/write split: PRs get write errors and succeed,
`main` writes cleanly.

## One more human apply, and it is the last one

This changes the apply role's own trust policy, so it cannot be applied by the
role it fixes. `envs/core` needs one more human apply; after that CI owns every
apply including this file's future changes.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@athvin
athvin merged commit 88af6c0 into main Oct 30, 2026
22 checks passed
@athvin
athvin deleted the bm/infra-apply-oidc-subject branch October 30, 2026 05:21
athvin added a commit that referenced this pull request Nov 1, 2026
… red on it (#368)

#367's `apply (core)` failed and `main` is red. The drift role was never
created, so nothing partially applied and nothing needs cleaning up — the
next apply retries from a clean state.

AWS rejected the trust policy:

  MalformedPolicyDocument: Trust policy with trusted principal
  …oidc-provider/token.actions.githubusercontent.com must evaluate, using
  StringEquals, StringLike or StringEqualsIgnoreCase,
  …:sub or …:job_workflow_ref which is not scoped to all.

I had written `job_workflow_ref` as `StringLike` on
`*/.github/workflows/drift.yml@refs/heads/main`, with the leading wildcard
deliberately hedging whether that claim carries the customised immutable
prefix this repo's `sub` uses. Two things I had wrong:

- a pattern that STARTS with `*` is "scoped to all" to that validator, and
- it refuses on any such condition rather than being satisfied by the exact
  `sub` StringEquals sitting beside it, which is what I assumed the
  message's "sub **or** job_workflow_ref" wording allowed.

So the uncertainty has to be resolved rather than wildcarded. Both
candidate formats are now listed as literals under `StringEquals`, which
ORs them: the documented `OWNER/REPO/.github/workflows/FILE@REF`, and the
same claim with the immutable ids in case it carries them. That is strictly
better than the wildcard was — no pattern to widen, and if GitHub ever
changes the format the role stops being assumable LOUDLY at
`AssumeRoleWithWebIdentity` rather than quietly matching more than intended.

Worth recording why the second condition not pinning the repository costs
nothing: `sub` already pins owner and repository by numeric id, immutably.
`job_workflow_ref` only has to answer "which workflow file, on which ref".

§14.2 and the infra-platform skill described the wildcard form, so both are
corrected, and the skill now states the rule as AWS enforces it rather than
as a preference — an exact match is not a style choice here, a
wildcard-leading one fails `CreateRole`.

This is the fourth bug in this series that only a real apply could find,
after the environment-vs-ref `sub` in #356, the two reads `ReadOnlyAccess`
omits in #354, and `sts:TagSession` on a chained role in #363. The pattern
is consistent: IAM trust policies and OIDC claims are not verifiable by
`terraform validate`, and every one of them has cost exactly one red run.

Co-authored-by: bmoore813 <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
athvin added a commit that referenced this pull request Nov 1, 2026
…y noise (#369)

* fix(infra): AWS refuses a wildcard-leading job_workflow_ref — main is red on it

#367's `apply (core)` failed and `main` is red. The drift role was never
created, so nothing partially applied and nothing needs cleaning up — the
next apply retries from a clean state.

AWS rejected the trust policy:

  MalformedPolicyDocument: Trust policy with trusted principal
  …oidc-provider/token.actions.githubusercontent.com must evaluate, using
  StringEquals, StringLike or StringEqualsIgnoreCase,
  …:sub or …:job_workflow_ref which is not scoped to all.

I had written `job_workflow_ref` as `StringLike` on
`*/.github/workflows/drift.yml@refs/heads/main`, with the leading wildcard
deliberately hedging whether that claim carries the customised immutable
prefix this repo's `sub` uses. Two things I had wrong:

- a pattern that STARTS with `*` is "scoped to all" to that validator, and
- it refuses on any such condition rather than being satisfied by the exact
  `sub` StringEquals sitting beside it, which is what I assumed the
  message's "sub **or** job_workflow_ref" wording allowed.

So the uncertainty has to be resolved rather than wildcarded. Both
candidate formats are now listed as literals under `StringEquals`, which
ORs them: the documented `OWNER/REPO/.github/workflows/FILE@REF`, and the
same claim with the immutable ids in case it carries them. That is strictly
better than the wildcard was — no pattern to widen, and if GitHub ever
changes the format the role stops being assumable LOUDLY at
`AssumeRoleWithWebIdentity` rather than quietly matching more than intended.

Worth recording why the second condition not pinning the repository costs
nothing: `sub` already pins owner and repository by numeric id, immutably.
`job_workflow_ref` only has to answer "which workflow file, on which ref".

§14.2 and the infra-platform skill described the wildcard form, so both are
corrected, and the skill now states the rule as AWS enforces it rather than
as a preference — an exact match is not a style choice here, a
wildcard-leading one fails `CreateRole`.

This is the fourth bug in this series that only a real apply could find,
after the environment-vs-ref `sub` in #356, the two reads `ReadOnlyAccess`
omits in #354, and `sts:TagSession` on a chained role in #363. The pattern
is consistent: IAM trust policies and OIDC claims are not verifiable by
`terraform validate`, and every one of them has cost exactly one red run.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* fix(infra): the first drift run measured the signal, and it was mostly noise

`drift.yml` ran for the first time (41280636155) and failed on all four
stacks. The trust policy worked — `Authenticated as assumedRoleId
AROA3TP35ULXVJ57LM6EF` — so #368 was right. Everything after it was wrong,
in three separate ways, and none of them was findable without running it.

**1. `-detailed-exitcode` is not a drift signal.** `dev` and `prod` both
printed "found no differences" and both exited 2. A refresh-only plan has
something to persist almost by construction. Treating 2 as drift would have
turned the schedule red every single day — the alarm fatigue I argued
against in the very commit that introduced it.

**2. The text sentinel is not one either.** "Objects have changed outside of
Terraform" fired on `core` and `state-backend`. Neither was drift:

    state-backend  aws_kms_key.state  — `tags` appearing as {} where state
                   holds null
                   aws_s3_bucket.state — `tags`, plus `policy` reporting what
                   a SEPARATE aws_s3_bucket_policy resource manages
    core           aws_iam_role.ci_tf_drift — `managed_policy_arns` reporting
                   what a SEPARATE aws_iam_role_policy_attachment manages

Same shape each time: an attribute the provider computes from something
Terraform manages elsewhere, or a null/empty normalisation. And
`state-backend` makes it permanent rather than transient — CI plans that
stack and never applies it, so nothing reconciles its state snapshot and it
would have reported drift forever.

So drift is now decided PER ATTRIBUTE by `scripts/ci/tf-drift-classify.sh`,
from the plan JSON, against an ignore list where every entry was observed in
that run. Extend it the same way — from evidence — never pre-emptively,
because each entry is a place real drift can hide. The ignored entries are
still printed, so they do not vanish.

`policy` is ignored only on `aws_s3_bucket`, not globally: a real change to
an `aws_iam_role_policy` document must stay loud. There is a test for
exactly that.

Nothing from the JSON is printed but addresses and attribute NAMES — the
same rule the apply tier follows, for the same reason. `driftplan` and
`plan.json` carry values in cleartext and there is deliberately no upload
step in the workflow.

**3. A third missing read: `bcm-data-exports:ListTagsForResource`.**
Refreshing the CUR export reads its tags as a separate call and
`ReadOnlyAccess` omits that too. #354 found `GetExport` by failing on it and
then switched the plan tier to `-refresh=false` — so until this workflow
existed nothing had ever COMPLETED a full refresh, and this call had never
been reached. A fourth may appear with the next resource type.

Also recorded, because "read-only role" reads safer than it is: the drift
role's `er/*` secret scope reaches `er/dev/shared/builder`, the
never-expiring AdministratorAccess key. It does not widen anything —
reaching this role takes a merge to `main`, and anyone who can merge can
already use `er-ci-tf-apply`, which is admin outright — so the grant sits
inside an existing boundary. The policy is deliberately not contorted to
exclude it, because the dev stack stores that key in a
`secretsmanager_secret_version` and excluding it would fail every dev
refresh. The thing to fix is the key.

The helper suite is now 41 assertions, and the new ten are mutation-checked:
emptying the ignore list, ignoring `policy` globally, making the classifier
unable to report drift, and letting a parse failure pass each turn it red.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: bmoore813 <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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.

2 participants