Skip to content

fix(iam): don't emit an empty-Resource write statement for read-only S3 - #677

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/669-readonly-s3-policy
Oct 4, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/669-readonly-s3-policy

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #669. Diagnosis confirmed exactly as filed, including the line number.

Reproduced first

taskStagingPolicy (cmd/task.go) appended its s3:PutObject statement unconditionally. On the launch path outputBuckets is empty and resultsBucket is "", and dedupeBuckets drops empty strings, so --s3-read my-inputs alone produced:

{"Effect":"Allow","Action":["s3:PutObject"],"Resource":[]}

IAM rejects the whole document for an empty Resource, not just that statement, so the instance profile could not be created and the launch died with MalformedPolicyDocument before reaching EC2.

A read-only grant now contains no write statement at all — rather than an unscoped one, which would have been worse than the bug.

Two things beyond the report

--s3-read-write alone was broken the same way. With only Resources.S3ReadWrite set, writeB is empty too, so the Snakemake S3-storage plugin's shape hit the identical failure. Covered by a test.

Guarding the write statement alone would have swapped one invalid document for another. With no buckets at all, stmts becomes empty and the result is {"Version":"2012-10-17","Statement":[]} — MalformedPolicyDocument just as surely. The function now returns "" when it has nothing to grant, and the policy is simply not attached: both PutRolePolicy call sites in pkg/aws/iam.go (:687, :744) were already guarded on InlinePolicyJSON != "", so no caller changes were needed.

Why this survived so long

There was already a test calling exactly this case — TestS3FlagsAreAdditiveWithIAMPolicy does taskStagingPolicy([]string{"b"}, nil, "", nil), which is #669 verbatim. It asserted only that the document starts with {"Version":"2012-10-17", and elsewhere the suite asserted json.Valid. Both pass for a document IAM refuses: "Resource":[] is perfectly good JSON.

So the new assertIAMAcceptable helper checks the two shapes IAM rejects that json.Valid accepts — an empty Resource, and an empty Statement — and I strengthened that pre-existing test in place to use it, rather than leaving a test that calls the broken case and declares it fine. That is the same class as the "_comment" incident earlier in this cycle: a policy document no compiler or test could reach.

Verification

Every new assertion confirmed to fail against the old code first, reporting the real symptom — the Resource":[] statement printed in full, not a compile error. All pre-existing S3/IAM/task tests still pass unchanged.

Gates clean locally before pushing: gofmt -l empty, go vet ./..., go test -short ./... (0 failures), make check-docs, changelog gate, semgrep scan --config=auto --error . → 0 findings.

Not verified against live IAM. The assertions encode IAM's documented rejection rules rather than observing them, so if you want certainty that the read-only document is now accepted, a aws iam create-role + put-role-policy against the rendered JSON is the check — worth doing before the release tag since the failure mode is a hard launch failure.

taskStagingPolicy appended s3:PutObject unconditionally, so `--s3-read
bucket` with no --s3-write shipped "Resource":[] and IAM rejected the
whole document — failing the launch before it reached EC2. Same shape
broke --s3-read-write on its own (the Snakemake S3-storage case).

An empty "Statement" is equally malformed, so with nothing to grant the
function now returns "" and the policy is not attached.

Fixes #669
@codecov

codecov Bot commented Oct 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 855399a into main Oct 4, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/669-readonly-s3-policy branch October 4, 2026 16:58
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.

--s3-read without --s3-write fails launch: MalformedPolicyDocument (empty PutObject Resource)

1 participant