Skip to content

refactor(#1609): tagging providers, with CloudWatch, EC2 and S3 as pilots - #1673

Merged
scttfrdmn merged 3 commits into
mainfrom
refactor/v0132-tagging-providers
Nov 24, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
refactor/v0132-tagging-providers

Conversation

@scttfrdmn

@scttfrdmn scttfrdmn commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Part of #1609 — this is part 3 (tagging behind a plugin interface), framework plus three pilots. The other services are tracked in #1672 (v0.137.0, five batches plus retiring the direct paths). Part 1 (#1667) has merged; this is rebased onto it. Part 2 (the comment rule, gates and trims) is W3's PR.

Rebased onto main at bcff881 (after W1 #1668 and W4 #1670).

  • Framework (emulator/tagging_providers.go): two unexported optional interfaces.
    • taggableResourceLister.listTaggedResources(ctx, reqCtx) serves GetResources.
    • resourceTagger.tagResourceByARN(reqCtx, arn, add, remove, quota) serves TagResources and UntagResources. resourceTagger.tagRecord(ctx, key, add, remove, quota) is what mergeResourceTags calls, so CloudFormation's aws:cloudformation:* stamps and stack-tag propagation reach the same writer. Neither method refuses an aws: key.
    • Implementers are found through PluginRegistry, the way ResetPlugins and RunClockDriven find theirs. The type assertion runs under the read lock, and the lock is released before any plugin is called.
  • Preference and fallback: each of the four entry points prefers an implementer and otherwise runs today's path.
    • scanAllResources runs a scanPlan. It asks an owner's lister once, in place of its first descriptor, and drops that owner's other descriptors.
    • tagResources and untagResources go through tagARN. It calls the owner's tagResourceByARN when there is one, and otherwise calls resolveARN and then mergeResourceTags. A resolution failure from either path is wrapped as taggingResolveError, so tagFailure still sends it through tagResolveFailure. Every published code and status is unchanged.
    • mergeResourceTags takes the registry, so cfnStampResourceTags and cfnPropagateStackTags pass d.registry, and it delegates to the namespace owner's tagRecord.
    • taggingCheckTagQuota now runs only on the fallback path. Each pilot checks its own quota inside tagRecord.
  • Pilots. Each pilot's scan, resolve, merge and quota code moves into its own tag file as state-taking functions. The tagging plugin's direct path calls the same functions, so nothing is duplicated.
    • CloudWatch (cloudwatch_tags.go): routeAlarmTags' RouteRequest and the mergeTags wrapper are gone. Writes run under CloudWatchPlugin.mu, with TagResource's merge, cwCheckTags quota and tag-history stamp, and UntagResource's removal. The MissingParameter that a TagResources with no tags used to get from the routed UntagResource is kept.
    • EC2 (ec2_tags.go): instances, Elastic IPs, security groups, launch templates and volumes, in one lister. A new EC2Plugin.tagMu is taken by CreateTags, DeleteTags and tagRecord. The 50-tag quota (taggingEC2QuotaRefusal) moves here.
    • S3 (s3_tagging.go): bucket tag writes hold the bucket's slot in S3Plugin.keyLocks under the empty object key (lockBucketTags). PutBucketTagging, DeleteBucketTagging and tagRecord take it.
  • Docs: docs/reference/tagging.md says which services list and tag their own resources, what the lock orders, and the aws: refusal. docs/reference/monitoring.md updates its alarm-tag note.
  • CHANGELOG: one line under Changed.
  • Routed operations: the catalog is unchanged at 1,128.

Review fixes (second commit, 19491cc)

  • CloudFormation's EC2 writes take EC2's tag lock. The stamp (stampCFNResourceTags) and stack-tag propagation (cfnPropagateEC2StackTags) now go through ec2ApplyTagsThroughOwner → EC2Plugin.applyTagsByID, which holds tagMu. It does not use tagRecord: CloudFormation addresses EC2 resources by ID across all sixteen ID shapes, with the upsert semantics of ec2ApplyTagsToResource, and tagRecord's merge covers only the five types the tagging API scans.
  • aws: keys are refused on the public path (Compatibility). TagResources and UntagResources refuse an aws: key for every resource.
    • EC2 applies ec2CheckTagRules to the keys it adds and removes, and answers InvalidParameterValue.
    • S3 applies s3CheckTagSet, PutBucketTagging's rules extracted to return an AWSError, and answers InvalidTag.
    • CloudWatch, and every service on the direct path, answer InvalidParameterException/400. The message is substrate's own; the source is the Tagging AWS Resources guide.
    • tagRecord keeps the allowance, so CloudFormation's stamps still land.
  • S3 enforces its bucket limit. With quota enforced, tagRecord holds the merged set to 50 tags, with aws: keys not counted, and answers InvalidTag.
  • Alarms without a registry. cwMergeAlarmTags takes the state, and mergeResourceTags uses it on the direct path. An existing alarm now merges there, and a missing one answers 400 (Fixed: it was 500 before).
  • Alarm merges apply additions and removals together, and then cwCheckTags.
  • Lock comments now say the lock orders tag writes only. Per-record locking is EC2, S3: per-record locks so tag writes and other writers of a record cannot lose updates #1674, filed on v0.137.0 with area: tagging, area: ec2 and area: s3.

Review fixes, round 2 (3471653)

  • One answer on both paths. When no EC2 or S3 plugin is registered, the direct path now applies the owners' own key rules (ec2TagKeyRefusal, s3TagKeyRefusal) and S3's bucket limit (a new s3 arm in taggingCheckTagQuota).
  • One reserved-prefix rule, taggingReservedKey. It is case-sensitive, following EC2's Tag restrictions ("Tag keys and values are case-sensitive"; "The aws: prefix is reserved for AWS use"), which S3's tagging pages defer to. It also matches the observed EC2 behaviour, which accepts AWS:foo. ECS's Tag shape reserves every case of the prefix, but that is ECS's own rule.
    • Compatibility: S3 now accepts an AWS:-prefixed key as an ordinary tag. One row of s3_upload_tagging_test.go that expected the opposite is removed.
  • Answer order is the same on both paths: an unresolvable ARN first, then a key the rules refuse, then a missing resource.
  • S3's 50-tag limit is one rule: user tags only, with aws: tags not counted. It is cited on s3BucketTagQuotaRefusal. PutBucketTagging counts its whole set because a caller's set cannot contain a reserved key.
  • Tests: AnswersMatchTheDirectPath now also covers an aws: key added and removed, an uppercase AWS: key, an over-long key, a reserved key on a missing resource, and a resource filled to 50 tags followed by a 51st.

Tests

  • The existing black-box tagging tests pass unchanged. The only existing test file touched is export_test.go, which gains three accessors and passes nil for the new registry parameter in MergeResourceTagsForTest.
  • New tests in tagging_providers_test.go:
    • ThePilotsImplementBothInterfaces: in a default registry, the listers and taggers are exactly ec2, monitoring and s3.
    • EachPilotAnswersItsScanOnceAndNoDirectReadRuns: with a registry, each pilot's plan step is lister exactly once and is never direct. Without one, each is direct, and Lambda is direct either way.
    • EachPilotWritesItsOwnTags: tag writes for each pilot's ARNs go to its own tagger, and to none without a registry.
    • AnswersMatchTheDirectPath: one script runs against both environments, and every response body must be byte-identical. That covers every FailedResourcesMap, every GetResources page with ResourcesPerPage=1 and its token, the type-filtered scans, and an existing alarm.
    • ReservedKeysAreRefused, with and without a registry, and OwnerKeyRulesApply.
    • AlarmMergesAddAndRemoveTogether.
    • CFNStackTagsDoNotRaceCreateTags. With the lock bypassed it failed 19 runs out of 20.
    • QuotasAreTheOwnersAnswer: EC2 answers TagLimitExceeded, CloudWatch InvalidParameterValue and S3 InvalidTag, including S3's merged-set count. A refused merge writes nothing, and an empty write is MissingParameter.
  • Race tests run TagResources concurrently with each pilot's own tag write: EC2 CreateTags, CloudWatch TagResource, and S3 merges then PutBucketTagging. With tagRecord's lock removed, the EC2 and S3 tests failed 20 runs out of 20 before the lock was restored.
  • Coverage: the package is at 88.6%. The new functions are mostly 85–100%. The uncovered lines are the unreachable invalid-ARN branch in each tagResourceByARN and taggingResolveError.Error.

Gates (local)

  • make lint: 0 issues
  • make test (race)
  • make e2e
  • SUBSTRATE_IAC=1 make iac-e2e
  • Both drift sets
  • nondeterminism-check, docs-versions, docs-counts-check, cfn-schema-check
  • VitePress build, then rm -rf docs/node_modules

Follow-ups filed: #1672 (the remaining services) and #1674 (per-record locks).

@scttfrdmn scttfrdmn added this to the v0.132.0 milestone Nov 24, 2026
@scttfrdmn scttfrdmn added type: chore Build, tooling, dependency updates area: tagging labels Nov 24, 2026
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.25397% with 34 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/ec2_tags.go 91.81% 13 Missing and 1 partial ⚠️
emulator/cloudwatch_tags.go 86.36% 9 Missing ⚠️
emulator/s3_tagging.go 92.78% 7 Missing ⚠️
emulator/tagging_providers.go 95.71% 3 Missing ⚠️
emulator/cfn_stack_tag_propagation.go 83.33% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

…lots

Plugins that own tagged resources can answer the Resource Groups Tagging API
through two unexported optional interfaces in tagging_providers.go:
taggableResourceLister (GetResources) and resourceTagger (TagResources,
UntagResources and CloudFormation's stamps through mergeResourceTags), found
through the registry the way ResetPlugins and RunClockDriven find theirs. The
tagging plugin prefers an implementer and falls back to its direct paths.

CloudWatch (replacing routeAlarmTags' RouteRequest), EC2 and S3 implement
both. Their scan, resolve, merge and quota code moves to cloudwatch_tags.go,
ec2_tags.go and s3_tagging.go, and their tag writes run under the lock their
own tag operations hold: CloudWatch's mu, a new EC2 tagMu taken by
CreateTags/DeleteTags, and the bucket's slot in S3's key locks taken by
PutBucketTagging/DeleteBucketTagging.
- CloudFormation's EC2 stamp and stack-tag propagation go through the EC2
  plugin's tag lock (ec2ApplyTagsThroughOwner, EC2Plugin.applyTagsByID).
- TagResources/UntagResources refuse aws: keys: EC2 and S3 apply their own
  key rules (ec2CheckTagRules, s3CheckTagSet), every other service gets the
  tagging API's InvalidParameterException. tagRecord keeps the allowance for
  internal stamps.
- S3 tagRecord enforces the 50-tag bucket limit, aws: keys uncounted.
- Alarms have a state-taking merge (cwMergeAlarmTags) the direct path uses,
  and it merges additions and removals together before cwCheckTags.
- The lock comments say what is serialized and no more; per-record locking
  is #1674.
- AnswersMatchTheDirectPath diffs every response body, pages and tokens
  included; new tests for reserved keys, owner key rules, the S3 limit, the
  alarm merge and a CFN-propagation-vs-CreateTags race.
@scttfrdmn
scttfrdmn force-pushed the refactor/v0132-tagging-providers branch from 19491cc to 74701d8 Compare November 24, 2026 03:35
- The tagging plugin's direct path applies EC2's and S3's own key rules
  (ec2TagKeyRefusal, s3TagKeyRefusal) and S3's bucket limit
  (taggingCheckTagQuota's new s3 arm), so a resource gets one answer
  whether or not its plugin is registered.
- One reserved-prefix rule, taggingReservedKey, case-sensitive per EC2's tag
  restrictions, which S3's tagging pages defer to. S3 no longer refuses
  AWS:-prefixed keys.
- Order: an unresolvable ARN, then a refused key, then a missing resource,
  on both paths.
- S3's 50-tag rule is one rule, user tags only, cited on
  s3BucketTagQuotaRefusal.
- AnswersMatchTheDirectPath covers reserved, uppercase, over-long and
  over-50 keys and a reserved key on a missing resource.
@scttfrdmn
scttfrdmn merged commit 37ce954 into main Nov 24, 2026
20 checks passed
@scttfrdmn
scttfrdmn deleted the refactor/v0132-tagging-providers branch November 24, 2026 04:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: tagging type: chore Build, tooling, dependency updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant