fix(ec2): keep tags in the region of the resource they label (#5973)

* fix(ec2): keep tags in the region of the resource they label

DescribeTags listed every region's tags because the tag store was keyed by
the bare resource id. Tags are now stored per region, so DescribeTags,
DeleteTags, CreateTags and the aws:ResourceTag IAM condition only see the
request region's tags. Each side of a cross-region transit gateway or VPC
peering connection keeps its own tags in its own region.

Tags stored by bare id are moved under their resource's region at startup,
for every account; a tag whose resource no region holds is dropped.

DescribeTags now reports image, volume, snapshot, network-acl,
vpc-peering-connection and spot-instances-request instead of "unknown".
Tags given when a volume or peering connection is created reach the store,
and DescribeVolumes, DescribeSnapshots and DescribeSpotInstanceRequests show
later CreateTags and DeleteTags changes.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(ec2): remove tags with their resource and keep node and catalog AMI tags on upgrade

Deleting a volume (directly or as a delete-on-termination volume of a
terminated instance), a snapshot with its image or a VPC peering connection,
and pruning a terminated instance, now removes the resource's tags, both
peering sides included.

The startup migration no longer drops tags it cannot place in a store: a
catalog AMI's tags move to the default region, and an instance's are kept
until its EKS node is found in a region, which then claims them.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(ec2): file a cluster node's legacy tags under its own region

A region-less findInstanceById reaches the EKS provider, which answers from any region, so the claim moved the node's legacy tags under a null region where DescribeTags never finds them. The claim now uses the instance's own region, and with no region known it reads the legacy tags in place for a regional lookup to claim.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(ec2): pass the region in the query GET resource-tag stub

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

---------

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-19 19:13:38 -04:00
1 parent 1b89d5da39
commit 1eb402d623
9 files changed
+783 -82

No files matched your search

@@ -0,0 +1,74 @@
package com.floci.test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.regions.RegionMetadata;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.CreateTagsRequest;
import software.amazon.awssdk.services.ec2.model.CreateVpcRequest;
import software.amazon.awssdk.services.ec2.model.DeleteVpcRequest;
import software.amazon.awssdk.services.ec2.model.DescribeTagsRequest;
import software.amazon.awssdk.services.ec2.model.Filter;
import software.amazon.awssdk.services.ec2.model.Tag;
import software.amazon.awssdk.services.ec2.model.TagDescription;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@DisplayName("EC2 tags are regional")
class Ec2TagRegionTest {
@Test
@DisplayName("DescribeTags lists only the tags of resources in the client's region")
void describeTagsListsOnlyTheClientRegionsTags() {
Region other = otherRegionInThePartition();
try (Ec2Client home = TestFixtures.ec2Client(); Ec2Client away = clientFor(other)) {
String homeVpc = home.createVpc(CreateVpcRequest.builder().cidrBlock("10.92.0.0/16").build()).vpc().vpcId();
String awayVpc = away.createVpc(CreateVpcRequest.builder().cidrBlock("10.93.0.0/16").build()).vpc().vpcId();
try {
home.createTags(CreateTagsRequest.builder().resources(homeVpc)
.tags(Tag.builder().key("SdkRegionProbe").value("home").build()).build());
away.createTags(CreateTagsRequest.builder().resources(awayVpc)
.tags(Tag.builder().key("SdkRegionProbe").value("away").build()).build());
assertThat(probedIds(home)).containsExactly(homeVpc);
assertThat(probedIds(away)).containsExactly(awayVpc);
} finally {
home.deleteVpc(DeleteVpcRequest.builder().vpcId(homeVpc).build());
away.deleteVpc(DeleteVpcRequest.builder().vpcId(awayVpc).build());
}
}
}
private static List<String> probedIds(Ec2Client ec2) {
return ec2.describeTags(DescribeTagsRequest.builder()
.filters(Filter.builder().name("key").values("SdkRegionProbe").build()).build())
.tags().stream().map(TagDescription::resourceId).toList();
}
private static Region otherRegionInThePartition() {
Region home = TestFixtures.region();
String partition = TestFixtures.partition();
return Region.regions().stream()
.filter(region -> !region.isGlobalRegion() && !region.equals(home))
.filter(region -> {
RegionMetadata metadata = region.metadata();
return metadata != null && partition.equals(metadata.partition().id());
})
.sorted((a, b) -> a.id().compareTo(b.id()))
.findFirst()
.orElseThrow(() -> new AssertionError("no second region in partition " + partition));
}
private static Ec2Client clientFor(Region region) {
return Ec2Client.builder()
.endpointOverride(TestFixtures.endpoint())
.region(region)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
.build();
}
}