Repository navigation
fix(iam): an attach validates the shape of its PolicyArn (#499) - #647
Merged
Merged
Conversation
AttachUserPolicy, AttachRolePolicy and AttachGroupPolicy appended whatever ARN they were given, so a bare policy name or a bucket ARN produced a success that GetPolicy then contradicted with NoSuchEntity. All three now refuse a malformed policy ARN with InvalidInput 400, built from the model's accountIdType/policyPathType/policyNameType patterns. Existence is not required: substrate bundles 52 of ~1,200 AWS managed policies, so refusing an unresolvable ARN would fail every attach of the other ~1,150 where AWS succeeds. A well-formed ARN that resolves nowhere succeeds and logs at WARN, with different messages for an unbundled AWS managed policy and for a customer-managed ARN never created. The warning runs after the entity lookup, so a NoSuchEntity attach does not also warn.
5 tasks
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #499
AttachUserPolicy,AttachRolePolicyandAttachGroupPolicyappended whateverPolicyArnthey were given to the entity's list unconditionally. A consumer could attach a bare policy name from the console, or an S3 bucket ARN, get a200, and only discover the mistake later throughGetPolicyansweringNoSuchEntityfor an ARN the attach had just accepted — two operations disagreeing about the same string.The decision
#499 listed four paths and left the choice to be recorded. This ships (1) + (2): refuse a malformed ARN, warn on a well-formed one that resolves nowhere. The reasoning lives in
emulator/iam_policy_arn.go's header so it is readable next to the code it explains.Shape is refused.
InvalidInput(400) — the code the model declares for all three operations — for an ARN that does not match a policy ARN. The pattern is built from the model's ownaccountIdType(\d{12}),policyPathTypeandpolicyNameType([\w+=,.@-]+) rather than by eye, and thearnType20–2048 length bound is checked first so a 4 KB body is refused on its size.Existence is deliberately not required. Substrate bundles 52 of roughly 1,200 AWS managed policies. Refusing an ARN that resolves nowhere would refuse every attach of the other ~1,150 — attaching
AmazonAthenaFullAccesswould hard-fail where AWS succeeds. That trades a confusing success for a wrong failure, and the wrong failure is worse: it breaks working consumer code rather than merely failing to catch a typo. So a well-formed unresolvable ARN succeeds and logs atWARNnaming it, with a different message for an unbundled AWS managed policy (the expected case) than for a customer-managed ARN noCreatePolicyever created (a likelier real mistake).The partition stays permissive.
[a-z0-9-]+coversaws,aws-cn,aws-us-gov, theaws-isovariants and whatever follows; refusing an unknown partition would refuse an ARN that is well-formed for a region substrate does not model.All three operations, not two.
AttachGroupPolicyships in this same release, so applying the check to only the two pre-existing handlers would recreate the asymmetry #499 reports inside a brand-new operation.Ordering. The shape check precedes authorization — it needs no state, and a malformed request is invalid whatever the caller may do. The existence warning runs after the entity lookup, so an attach that is going to fail with
NoSuchEntitydoes not also warn about a policy nothing was attaching.Paths not taken, recorded on the issue: seeding all ~1,200 ARNs without documents (3) would make
GetPolicyresolve policies whose documents are empty; a seedable strict mode (4) would be a second behaviour for the same call with no consumer asking for it yet.Tests
emulator/iam_policy_arn_test.go, tables driving all three operations:GetPolicyresolves is never refused and never warns — IAM: AttachRolePolicy/AttachUserPolicy accept a PolicyArn that GetPolicy cannot resolve #499's hard acceptance criterion, asserted for a bundled ARN and a created one. Warning about a policy the emulator holds would train a consumer to ignore the warning.InvalidInput400: nine cases, each a mistake a consumer actually makes (bare name, wrong service,role/instead ofpolicy/, a region where IAM has none, slash-terminated with no name, 11- and 13-digit accounts).AmazonAthenaFullAccess(real, unbundled, asserted unbundled so the test fails loudly if it ever gets added) succeeds with exactly one warning naming both the operation and the ARN.NoSuchEntitywith no warning at all.ListAttachedUserPolicies, so the warning cannot quietly become a refusal.policyNameTypecharset.Compatibility
A fixture attaching a made-up, non-ARN-shaped
PolicyArnstarts failing withInvalidInput. One attaching a real-but-unbundled AWS managed ARN keeps working, and now says so in the log.Verification
gofmt -l .clean;go build ./...;go vet ./...;golangci-lint run ./...→0 issues.;make test(race) green. Coverage:iamValidatePolicyARN100%,iamPolicyARNIsAWSManaged100%,iamWarnUnresolvedPolicyARN91.7%; total 82.6%.