Repository navigation
feat(ec2): enforce the 50-tag-per-resource limit (#469) - #479
Merged
Merged
Conversation
Substrate accepted any number of tags on a resource, so a consumer that hit
real EC2's 50-tag ceiling had no way to reach the TagLimitExceeded branch --
and a test asserting the rejection passed against substrate while the same
code failed against AWS.
CreateTags and every tag-on-create path (RunInstances, CreateFleet,
CreateImage, CreateNatGateway) now refuse a resource that would carry more
than 50 user tags: TagLimitExceeded, HTTP 400.
Two rules make this less like arithmetic than it looks, and both are
modelled in one expression -- the count is over the post-merge key set,
excluding reserved keys:
- Tags with the aws: prefix do not count against the limit. This is
load-bearing rather than pedantry, because substrate stamps
aws:ec2:fleet-id on every fleet instance (#443): a counter that
included reserved keys would refuse a fleet launch whose template
names the full 50 user tags, which real EC2 accepts.
- Overwriting an existing key at the limit succeeds, since a key already
present adds nothing to the set. Written as len(existing)+len(incoming)
it would fail instead; getmoto/moto#8151 reports real AWS permitting
it.
As with reserved keys, the whole request is refused before anything is
modified. A resource ID naming nothing is not counted against, because the
apply step ignores it -- checking it would refuse a request real EC2 accepts
as a no-op. ec2TaggableStateKey is factored out of applyTagsToResource so
the limit is counted against exactly the resources the apply step will
modify.
Provenance is split, and the weaker half is marked as such: the code is a
doc citation (EC2's client-error table lists TagLimitExceeded), while the
wire message is moto's, from a reimplementation rather than a capture.
Four mutations were confirmed to fail the new tests: counting
len(existing)+len(incoming); counting reserved keys; interleaving the check
with the apply loop; and moving the tag-on-create check after the launch.
Closes #469
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
ec2TaggableStateKey is what makes the limit check and the apply step agree on which IDs resolve, and only its instance and NAT-gateway arms were exercised. A resource type the apply step tags but the check could not resolve would be tagged past the limit, silently. The new table walks all eight ID prefixes substrate recognises. It asserts on the rejection rather than a tag read-back because, apart from instances and NAT gateways, substrate's Describe operations for these resources emit no tagSet -- the rejection is the observation available. The second test pins the other side: an ID the apply step ignores is not counted against either, in both the ways an ID goes unresolved (a prefix substrate does not tag, and a taggable prefix naming nothing). Counting one would refuse a request real EC2 accepts as a no-op, trading a silent-success infidelity for a spurious-rejection one.
golangci-lint enforces US spellings in Go source; the repo's markdown deliberately uses British forms, which is why this only bites in .go files.
Merged
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.
Substrate accepted any number of tags on a resource, so a consumer that hit real EC2's
50-tag ceiling had no way to reach the
TagLimitExceededbranch — and, worse, a testasserting the rejection passed against substrate while the same code failed against AWS.
CreateTagsand every tag-on-create path (RunInstances,CreateFleet,CreateImage,CreateNatGateway) now refuse a resource that would carry more than 50 user tags:TagLimitExceeded, HTTP 400. From the tagging documentation's restrictions: "Maximumnumber of tags per resource – 50".
The counting rule
Two documented rules make this less like arithmetic than it looks, and both fall out of
one expression — the count is over the post-merge key set, excluding reserved keys:
aws:prefix do not count ("Tags with theaws:prefix do notcount against your tags per resource limit"). This is load-bearing rather than
pedantry: substrate stamps
aws:ec2:fleet-idon every fleet instance (EC2: CreateFleet does not apply the aws:ec2:fleet-id tag, so a fleet's instances can't be found #443), so acounter that included reserved keys would refuse a fleet launch whose template names
the full 50 user tags — a launch real EC2 accepts. A fleet instance holds 51 tags
legally.
adds nothing to the set. Written as
len(existing) + len(incoming)it would failinstead, and real AWS permits it — getmoto/moto#8151
reports exactly that case.
This also closes the companion gap v0.85.0 recorded as vacuous: the reserved-tag
exemption now has a limit to be exempt from.
Ordering
As with reserved keys (#468), the whole request is refused before anything is modified —
a
CreateTagsnaming one instance with room and one at the limit tags neither.CreateTagsaccepts up to 1000 resource IDs and a partial apply is a state real EC2never produces.
A resource ID that names nothing is not counted against, because the apply step
ignores it: checking it would refuse a request real EC2 accepts as a no-op.
ec2TaggableStateKeyis factored out ofapplyTagsToResourceso the limit is countedagainst exactly the resources the apply step will modify, rather than the two drifting.
Provenance — split, and the weaker half marked
The code is a doc citation, unlike #452's: EC2's client-error table lists
TagLimitExceededas "You've reached the limit on the number of tags that you canassign to the specified resource." The wire message is published nowhere, so the
wording is moto's — a reimplementation rather than a capture. SDKs dispatch on
Error.Code, so the documented half is the one a consumer's error branch turns on.docs/services.mdand the CHANGELOG both say which half is which.Two documented restrictions still unenforced and tracked separately: a tag key's maximum
of 128 Unicode characters and a value's maximum of 256.
Tests
Six new tests in
emulator/ec2_tags_test.go:key7on a 50-tag instance succeeds and the value changes; addingkey51is refused — the two outcomes side by side in one table. The overwrite half fails
under naive arithmetic.
aws:exemption asserted on a real fleet instance: it carriesaws:ec2:fleet-idand still takes 50 user tags (51 total), while the 51st user tagis still refused, so the exemption did not raise the ceiling.
on
CreateNatGatewayand onCreateImagerejected with no resource left behind.CreateFleetnaming the full 50 instance tags still launches, and the stamp survives.Four mutations confirmed to fail these tests, then restored:
len(existing) + len(incoming)OverwriteAtLimitSucceedsaws:keysTagLimit_ExemptsReservedKeysLimitRejectionIsAllOrNothingTagOnCreate_EnforcesTagLimit(the rollback assertion)Verification
From the repo root:
gofmt -l .,go build ./...,go vet ./...,golangci-lint run ./...(0 issues),make test(race),make docs-reference docs-reference-check docs-versions,go test -C test/e2e ./...— all pass. The fullEC2 suite including the fleet tests is green, which is the #443 regression gate.
Closes #469