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

@@ -209,7 +209,7 @@ class IamConditionContextResolverTest {
query.add("Tag.1.Key", "Team");
query.add("Tag.1.Value", "A+B%2F");
query.add("Tag.1.Value", "ignored");
when(ec2Service.resourceTags("i-target")).thenReturn(List.of(new Tag("Owner", "alice")));
when(ec2Service.resourceTags("us-east-1", "i-target")).thenReturn(List.of(new Tag("Owner", "alice")));
Map<String, List<String>> conditions = resolver.resolve(
"ec2", "ec2:CreateTags", queryRequest("ec2", query));
assertEquals(List.of("A+B%2F"), conditions.get("aws:RequestTag/Team"));
@@ -672,7 +672,7 @@ class IamConditionContextResolverTest {
@Test
void createTagsResolvesBothTheRequestedTagsAndTheResourcesCurrentTags() {
when(ec2Service.resourceTags("i-0123456789")).thenReturn(List.of(new Tag("Owner", "alice")));
when(ec2Service.resourceTags("us-east-1", "i-0123456789")).thenReturn(List.of(new Tag("Owner", "alice")));
ContainerRequestContext containerRequest = formRequest(
"Action=CreateTags&ResourceId.1=i-0123456789&Tag.1.Key=Env&Tag.1.Value=prod");
@@ -685,7 +685,7 @@ class IamConditionContextResolverTest {
@Test
void instanceActionsResolveResourceTagsFromTheFirstNamedInstance() {
when(ec2Service.resourceTags("i-0123456789")).thenReturn(List.of(new Tag("Team", "payments")));
when(ec2Service.resourceTags("us-east-1", "i-0123456789")).thenReturn(List.of(new Tag("Team", "payments")));
for (String action : List.of("ec2:TerminateInstances", "ec2:DescribeInstances")) {
Map<String, List<String>> conditions = resolver.resolve("ec2", action,
@@ -698,6 +698,16 @@ class IamConditionContextResolverTest {
.get("aws:ResourceTag/Team"));
}
@Test
void resourceTagsAreReadInTheRequestRegion() {
requestContext.setRegion("eu-west-1");
when(ec2Service.resourceTags("eu-west-1", "i-0123456789")).thenReturn(List.of(new Tag("Team", "payments")));
when(ec2Service.resourceTags("us-east-1", "i-0123456789")).thenReturn(List.of(new Tag("Team", "other")));
assertEquals(List.of("payments"), resolver.resolve("ec2", "ec2:TerminateInstances",
formRequest("Action=TerminateInstances&InstanceId.1=i-0123456789")).get("aws:ResourceTag/Team"));
}
@Test
void ec2ContextIsAbsentWhenNoResourceIsNamedOrTheActionIsNotCovered() {
assertNull(resolver.resolve("ec2", "ec2:DescribeInstances", formRequest("Action=DescribeInstances")));
@@ -709,7 +719,7 @@ class IamConditionContextResolverTest {
@Test
void formParametersAreBufferedOncePerRequest() {
ContainerRequestContext containerRequest = formRequest("Action=TerminateInstances&InstanceId.1=i-1");
when(ec2Service.resourceTags("i-1")).thenReturn(List.of(new Tag("Team", "payments")));
when(ec2Service.resourceTags("us-east-1", "i-1")).thenReturn(List.of(new Tag("Team", "payments")));
resolver.resolve("ec2", "ec2:TerminateInstances", containerRequest);
@@ -730,12 +740,12 @@ class IamConditionContextResolverTest {
]}""";
String body = "Action=TerminateInstances&InstanceId.1=i-0123456789";
when(ec2Service.resourceTags("i-0123456789")).thenReturn(List.of(new Tag("Team", "payments")));
when(ec2Service.resourceTags("us-east-1", "i-0123456789")).thenReturn(List.of(new Tag("Team", "payments")));
assertEquals(Decision.ALLOW, evaluator.simulateCustomPolicy(List.of(policy),
"ec2:TerminateInstances", "*",
resolver.resolve("ec2", "ec2:TerminateInstances", formRequest(body))));
when(ec2Service.resourceTags("i-0123456789")).thenReturn(List.of(new Tag("Team", "engineering")));
when(ec2Service.resourceTags("us-east-1", "i-0123456789")).thenReturn(List.of(new Tag("Team", "engineering")));
assertEquals(Decision.DENY, evaluator.simulateCustomPolicy(List.of(policy),
"ec2:TerminateInstances", "*",
resolver.resolve("ec2", "ec2:TerminateInstances", formRequest(body))));
@@ -778,9 +788,9 @@ class IamConditionContextResolverTest {
@Test
void remainingTargetsCarryEachLaterResourcesOwnTags() {
when(ec2Service.resourceTags("i-1")).thenReturn(List.of(new Tag("Team", "payments")));
when(ec2Service.resourceTags("i-2")).thenReturn(List.of(new Tag("Team", "engineering")));
when(ec2Service.resourceTags("i-3")).thenReturn(List.of());
when(ec2Service.resourceTags("us-east-1", "i-1")).thenReturn(List.of(new Tag("Team", "payments")));
when(ec2Service.resourceTags("us-east-1", "i-2")).thenReturn(List.of(new Tag("Team", "engineering")));
when(ec2Service.resourceTags("us-east-1", "i-3")).thenReturn(List.of());
ContainerRequestContext containerRequest = formRequest(
"Action=TerminateInstances&InstanceId.1=i-1&InstanceId.2=i-2&InstanceId.3=i-3");
@@ -795,7 +805,7 @@ class IamConditionContextResolverTest {
@Test
void remainingTargetsOfCreateTagsRepeatTheRequestedTagsPerResource() {
when(ec2Service.resourceTags("vpc-2")).thenReturn(List.of(new Tag("Owner", "bob")));
when(ec2Service.resourceTags("us-east-1", "vpc-2")).thenReturn(List.of(new Tag("Owner", "bob")));
ContainerRequestContext containerRequest = formRequest(
"Action=CreateTags&ResourceId.1=vpc-1&ResourceId.2=vpc-2&Tag.1.Key=Env&Tag.1.Value=prod");
@@ -825,19 +835,19 @@ class IamConditionContextResolverTest {
"Condition":{"StringEquals":{"aws:ResourceTag/Team":"payments"}}}
]}""";
String body = "Action=TerminateInstances&InstanceId.1=i-1&InstanceId.2=i-2";
when(ec2Service.resourceTags("i-1")).thenReturn(List.of(new Tag("Team", "payments")));
when(ec2Service.resourceTags("us-east-1", "i-1")).thenReturn(List.of(new Tag("Team", "payments")));
when(ec2Service.resourceTags("i-2")).thenReturn(List.of(new Tag("Team", "engineering")));
when(ec2Service.resourceTags("us-east-1", "i-2")).thenReturn(List.of(new Tag("Team", "engineering")));
assertEquals(Decision.DENY, decisionForEveryTarget(policy, "ec2:TerminateInstances", formRequest(body)));
when(ec2Service.resourceTags("i-2")).thenReturn(List.of(new Tag("Team", "payments")));
when(ec2Service.resourceTags("us-east-1", "i-2")).thenReturn(List.of(new Tag("Team", "payments")));
assertEquals(Decision.ALLOW, decisionForEveryTarget(policy, "ec2:TerminateInstances", formRequest(body)));
}
@Test
void aRepeatedNumberedParameterKeepsItsFirstValueLikeTheHandlers() {
when(ec2Service.resourceTags("i-first")).thenReturn(List.of(new Tag("Team", "engineering")));
when(ec2Service.resourceTags("i-second")).thenReturn(List.of(new Tag("Team", "payments")));
when(ec2Service.resourceTags("us-east-1", "i-first")).thenReturn(List.of(new Tag("Team", "engineering")));
when(ec2Service.resourceTags("us-east-1", "i-second")).thenReturn(List.of(new Tag("Team", "payments")));
Map<String, List<String>> conditions = resolver.resolve("ec2", "ec2:TerminateInstances",
formRequest("Action=TerminateInstances&InstanceId.1=i-first&InstanceId.1=i-second"));
@@ -847,7 +857,7 @@ class IamConditionContextResolverTest {
@Test
void remainingTargetsStopAtTheFirstNumberingGapLikeTheHandlers() {
when(ec2Service.resourceTags("i-2")).thenReturn(List.of(new Tag("Team", "payments")));
when(ec2Service.resourceTags("us-east-1", "i-2")).thenReturn(List.of(new Tag("Team", "payments")));
assertEquals(List.of(), resolver.resolveRemainingTargets("ec2", "ec2:TerminateInstances",
formRequest("Action=TerminateInstances&InstanceId.2=i-2")));
@@ -862,8 +872,8 @@ class IamConditionContextResolverTest {
{"Effect":"Allow","Action":"ec2:TerminateInstances","Resource":"*",
"Condition":{"StringEquals":{"aws:ResourceTag/Team":"payments"}}}
]}""";
when(ec2Service.resourceTags("i-denied")).thenReturn(List.of(new Tag("Team", "engineering")));
when(ec2Service.resourceTags("i-allowed")).thenReturn(List.of(new Tag("Team", "payments")));
when(ec2Service.resourceTags("us-east-1", "i-denied")).thenReturn(List.of(new Tag("Team", "engineering")));
when(ec2Service.resourceTags("us-east-1", "i-allowed")).thenReturn(List.of(new Tag("Team", "payments")));
// The handler terminates i-denied, the first value, so that is what must be authorized.
assertEquals(Decision.DENY, decisionForEveryTarget(policy, "ec2:TerminateInstances",
@@ -223,6 +223,18 @@ class Ec2DeregisterAndCopyImageIntegrationTest {
() -> service.describeSnapshots(EAST, List.of(deletedSnapshot), null, null));
}
@Test
void aSnapshotDeletedWithItsImageTakesItsTags() {
Image image = service.registerImage(EAST, uniqueName("floci-tagged-snapshot"), null,
"x86_64", "/dev/xvda", List.of(mapping("/dev/xvda", "snap-" + hex())));
String snapshotId = snapshotIdOf(image);
service.createTags(EAST, List.of(snapshotId), List.of(new Tag("Team", "snapshots")));
service.deregisterImage(EAST, image.getImageId(), true);
assertTrue(service.describeTags(EAST, Map.of("resource-id", List.of(snapshotId))).isEmpty());
}
@Test
void aSnapshotSharedWithAnotherAmiIsSkippedRatherThanDeleted() {
// "if a snapshot is associated with multiple AMIs, it won't be deleted even if specified
@@ -666,6 +666,228 @@ class Ec2ServiceTest {
}
}
@Test
void describeTagsListsOnlyTheRequestRegionsTags() {
Ec2Service service = tagTestService(new InMemoryStorageFactory());
String east = service.createVpc("us-east-1", "10.80.0.0/16", false).getVpcId();
String west = service.createVpc("eu-west-1", "10.81.0.0/16", false).getVpcId();
service.createTags("us-east-1", List.of(east), List.of(new Tag("Team", "east")));
service.createTags("eu-west-1", List.of(west), List.of(new Tag("Team", "west")));
assertEquals(List.of(east), tagResourceIds(service.describeTags("us-east-1", Map.of("key", List.of("Team")))));
assertEquals(List.of(west), tagResourceIds(service.describeTags("eu-west-1", Map.of("key", List.of("Team")))));
assertTrue(service.describeTags("eu-west-1", Map.of("resource-id", List.of(east))).isEmpty());
assertTrue(service.resourceTags("eu-west-1", east).isEmpty());
assertEquals("east", service.resourceTags("us-east-1", east).getFirst().getValue());
}
@Test
void tagsCarryTheResourceTypeOfImagesVolumesSnapshotsAclsPeeringsAndSpotRequests() {
Ec2Service service = tagTestService(new InMemoryStorageFactory());
Map<String, String> expected = Map.of(
"ami-0abc12345678", "image", "vol-0abc12345678", "volume", "snap-0abc12345678", "snapshot",
"acl-0abc12345678", "network-acl", "pcx-0abc12345678", "vpc-peering-connection",
"sir-0abc12345678", "spot-instances-request");
service.createSystemTags("us-east-1", List.copyOf(expected.keySet()), List.of(new Tag("Team", "typed")));
Map<String, String> types = new HashMap<>();
for (Map<String, String> item : service.describeTags("us-east-1", Map.of("key", List.of("Team")))) {
types.put(item.get("resourceId"), item.get("resourceType"));
}
assertEquals(expected, types);
}
@Test
void volumeTagsGivenAtCreationAreListedByDescribeTags() {
Ec2Service service = tagTestService(new InMemoryStorageFactory());
Volume volume = service.createVolume("us-east-1", "us-east-1a", "gp3", 8,
false, 0, null, null, List.of(new Tag("Team", "storage")));
List<Map<String, String>> listed = service.describeTags("us-east-1",
Map.of("resource-id", List.of(volume.getVolumeId())));
assertEquals(1, listed.size());
assertEquals("volume", listed.getFirst().get("resourceType"));
assertTrue(service.describeTags("eu-west-1", Map.of("resource-id", List.of(volume.getVolumeId()))).isEmpty());
}
@Test
void tagsStoredByBareIdMoveUnderTheirResourcesRegionAndOrphansAreDropped() {
AccountAwareStorageBackend<List<Tag>> tagStore = AccountAwareStorageBackend.inMemory("000000000000");
Ec2Service service = tagTestService(new InMemoryStorageFactory(Map.of("ec2-tags.json", tagStore)));
String vpcId = service.createVpc("eu-west-1", "10.82.0.0/16", false).getVpcId();
tagStore.put(vpcId, new ArrayList<>(List.of(new Tag("Team", "legacy"))));
tagStore.put("vpc-0000dead0000", new ArrayList<>(List.of(new Tag("Team", "orphan"))));
service.migrateTagsToRegionKeys();
service.migrateTagsToRegionKeys();
assertEquals(List.of(vpcId), tagResourceIds(service.describeTags("eu-west-1", Map.of("key", List.of("Team")))));
assertTrue(service.describeTags("us-east-1", Map.of("key", List.of("Team"))).isEmpty());
assertTrue(tagStore.keys().stream().allMatch(storeKey -> storeKey.contains("::")));
}
@Test
void volumeTagChangesShowInDescribeVolumes() {
Ec2Service service = tagTestService(new InMemoryStorageFactory());
String volumeId = service.createVolume("us-east-1", "us-east-1a", "gp3", 8,
false, 0, null, null, List.of(new Tag("Team", "storage"))).getVolumeId();
service.createTags("us-east-1", List.of(volumeId), List.of(new Tag("Env", "dev")));
service.deleteTags("us-east-1", List.of(volumeId), List.of(new Tag("Team", null)));
List<Tag> described = service.describeVolumes("us-east-1", List.of(volumeId), Map.of()).getFirst().getTags();
assertEquals(List.of("Env"), described.stream().map(Tag::getKey).toList());
}
@Test
void eachSideOfACrossRegionPeeringConnectionSeesItsOwnTags() {
Ec2Service service = tagTestService(new InMemoryStorageFactory());
String eastVpc = service.createVpc("us-east-1", "10.83.0.0/16", false).getVpcId();
String westVpc = service.createVpc("eu-west-1", "10.84.0.0/16", false).getVpcId();
String pcxId = service.createVpcPeeringConnection("us-east-1", eastVpc, westVpc, null, "eu-west-1",
List.of(new Tag("Side", "requester"))).getVpcPeeringConnectionId();
service.createTags("eu-west-1", List.of(pcxId), List.of(new Tag("Side", "accepter")));
assertEquals("requester", peeringTag(service, "us-east-1", pcxId));
assertEquals("accepter", peeringTag(service, "eu-west-1", pcxId));
assertEquals(List.of(pcxId), tagResourceIds(service.describeTags("us-east-1", Map.of("key", List.of("Side")))));
assertEquals("requester", peeringTag(service, "us-east-1", pcxId));
}
private static String peeringTag(Ec2Service service, String region, String pcxId) {
return service.describeVpcPeeringConnections(region, List.of(pcxId), Map.of()).getFirst().getTags().stream()
.filter(tag -> "Side".equals(tag.getKey())).map(Tag::getValue).findFirst().orElse(null);
}
@Test
void aDeletedVolumesTagsAreNoLongerListed() {
Ec2Service service = tagTestService(new InMemoryStorageFactory());
String volumeId = service.createVolume("us-east-1", "us-east-1a", "gp3", 8,
false, 0, null, null, List.of(new Tag("Team", "storage"))).getVolumeId();
service.createTags("us-east-1", List.of(volumeId), List.of(new Tag("Env", "dev")));
service.deleteVolume("us-east-1", volumeId);
assertTrue(service.describeTags("us-east-1", Map.of("resource-id", List.of(volumeId))).isEmpty());
}
@Test
void aRootVolumeDeletedOnTerminationTakesItsTags() {
Ec2Service service = tagTestService(new InMemoryStorageFactory());
Instance inst = service.runInstances("us-east-1", "ami-1234567890abcdef0", "t3.micro",
1, 1, null, List.of(), null, null, List.of(), null, null).getInstances().getFirst();
String rootVolumeId = inst.getRootVolumeId();
service.createTags("us-east-1", List.of(rootVolumeId), List.of(new Tag("Team", "root")));
service.terminateInstances("us-east-1", List.of(inst.getInstanceId()));
assertTrue(service.describeVolumes("us-east-1", List.of(), Map.of()).stream()
.noneMatch(volume -> rootVolumeId.equals(volume.getVolumeId())));
assertTrue(service.describeTags("us-east-1", Map.of("resource-id", List.of(rootVolumeId))).isEmpty());
}
@Test
void deletingAPeeringConnectionRemovesBothSidesTags() {
Ec2Service service = tagTestService(new InMemoryStorageFactory());
String eastVpc = service.createVpc("us-east-1", "10.89.0.0/16", false).getVpcId();
String westVpc = service.createVpc("eu-west-1", "10.90.0.0/16", false).getVpcId();
String pcxId = service.createVpcPeeringConnection("us-east-1", eastVpc, westVpc, null, "eu-west-1",
List.of(new Tag("Side", "requester"))).getVpcPeeringConnectionId();
service.createTags("eu-west-1", List.of(pcxId), List.of(new Tag("Side", "accepter")));
service.deleteVpcPeeringConnection("eu-west-1", pcxId);
assertTrue(service.describeTags("us-east-1", Map.of("resource-id", List.of(pcxId))).isEmpty());
assertTrue(service.describeTags("eu-west-1", Map.of("resource-id", List.of(pcxId))).isEmpty());
}
@Test
void migrationKeepsCatalogImageAndClusterNodeTags() {
AccountAwareStorageBackend<List<Tag>> tagStore = AccountAwareStorageBackend.inMemory("000000000000");
EmulatorConfig config = mockConfig(true);
when(config.defaultRegion()).thenReturn("us-east-1");
Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog();
Ec2Service service = new Ec2Service(config, mock(Ec2ContainerManager.class), mock(Ec2PortForwardManager.class),
new AmiImageResolver(imageCatalog), imageCatalog, new Ec2InstanceTypeCatalog(),
new InMemoryStorageFactory(Map.of("ec2-tags.json", tagStore)));
String catalogImageId = imageCatalog.images().getFirst().imageId;
String nodeId = "i-0de0000000000001a";
tagStore.put(catalogImageId, new ArrayList<>(List.of(new Tag("Team", "image"))));
tagStore.put(nodeId, new ArrayList<>(List.of(new Tag("Team", "node"))));
tagStore.put("vpc-0000dead0000", new ArrayList<>(List.of(new Tag("Team", "orphan"))));
service.migrateTagsToRegionKeys();
assertEquals(List.of(catalogImageId),
tagResourceIds(service.describeTags("us-east-1", Map.of("key", List.of("Team")))));
assertTrue(tagStore.keys().contains(nodeId));
assertFalse(tagStore.keys().contains("vpc-0000dead0000"));
Instance node = new Instance();
node.setInstanceId(nodeId);
node.setRegion("eu-west-1");
node.setPlacement(new Placement("eu-west-1a"));
node.setState(InstanceState.running());
node.setInstanceType("m5.large");
service.setClusterNodeInstanceProvider(new ClusterNodeInstanceProvider() {
@Override
public Optional<Instance> findInstance(String accountId, String region, String instanceId) {
return nodeId.equals(instanceId) && "eu-west-1".equals(region) ? Optional.of(node) : Optional.empty();
}
@Override
public List<Instance> listInstances(String accountId, String region) {
return "eu-west-1".equals(region) ? List.of(node) : List.of();
}
});
assertEquals("node", service.describeInstances("eu-west-1", List.of(nodeId), Map.of()).getFirst()
.getInstances().getFirst().getTags().getFirst().getValue());
assertEquals(List.of(nodeId), tagResourceIds(service.describeTags("eu-west-1", Map.of("key", List.of("Team")))));
assertFalse(tagStore.keys().contains(nodeId));
}
@Test
void regionlessLookupFilesClusterNodeTagsUnderTheNodesRegion() {
AccountAwareStorageBackend<List<Tag>> tagStore = AccountAwareStorageBackend.inMemory("000000000000");
Ec2Service service = tagTestService(new InMemoryStorageFactory(Map.of("ec2-tags.json", tagStore)));
String nodeId = "i-0de0000000000002b";
tagStore.put(nodeId, new ArrayList<>(List.of(new Tag("Team", "node"))));
service.migrateTagsToRegionKeys();
Instance node = new Instance();
node.setInstanceId(nodeId);
node.setRegion("eu-west-1");
node.setPlacement(new Placement("eu-west-1a"));
node.setState(InstanceState.running());
node.setInstanceType("m5.large");
service.setClusterNodeInstanceProvider(new ClusterNodeInstanceProvider() {
@Override
public Optional<Instance> findInstance(String accountId, String region, String instanceId) {
return nodeId.equals(instanceId) && (region == null || "eu-west-1".equals(region))
? Optional.of(node) : Optional.empty();
}
@Override
public List<Instance> listInstances(String accountId, String region) {
return region == null || "eu-west-1".equals(region) ? List.of(node) : List.of();
}
});
assertEquals("node", service.findInstanceById(nodeId).getTags().getFirst().getValue());
assertEquals(List.of(nodeId), tagResourceIds(service.describeTags("eu-west-1", Map.of("key", List.of("Team")))));
assertFalse(tagStore.keys().contains("null::" + nodeId));
}
private static Ec2Service tagTestService(StorageFactory storageFactory) {
return new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), mock(Ec2PortForwardManager.class),
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(), storageFactory);
}
private static List<String> tagResourceIds(List<Map<String, String>> items) {
return items.stream().map(item -> item.get("resourceId")).distinct().toList();
}
@Test
void deleteVpcRemovesItsOwnTagsAndThoseOnItsDefaultResources() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
@@ -0,0 +1,54 @@
package io.github.hectorvent.floci.services.ec2;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.xml.XmlPath;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** EC2 tags are regional: a tag set in one region is listed only there, over the Query API. */
@QuarkusTest
class Ec2TagRegionIntegrationTest {
private static final String EAST_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
private static final String WEST_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20260205/eu-west-1/ec2/aws4_request";
private XmlPath ec2(String auth, String action, String... formParams) {
RequestSpecification req = given().formParam("Action", action).header("Authorization", auth);
for (int i = 0; i < formParams.length; i += 2) {
req = req.formParam(formParams[i], formParams[i + 1]);
}
return req.when().post("/").then().statusCode(200).extract().xmlPath();
}
@Test
void describeTagsListsOnlyTheRequestRegionsTags() {
String east = ec2(EAST_AUTH, "CreateVpc", "CidrBlock", "10.90.0.0/16").getString("CreateVpcResponse.vpc.vpcId");
String west = ec2(WEST_AUTH, "CreateVpc", "CidrBlock", "10.91.0.0/16").getString("CreateVpcResponse.vpc.vpcId");
try {
ec2(EAST_AUTH, "CreateTags", "ResourceId.1", east, "Tag.1.Key", "RegionProbe", "Tag.1.Value", "east");
ec2(WEST_AUTH, "CreateTags", "ResourceId.1", west, "Tag.1.Key", "RegionProbe", "Tag.1.Value", "west");
assertEquals(List.of(east), probedIds(EAST_AUTH));
assertEquals(List.of(west), probedIds(WEST_AUTH));
XmlPath otherRegion = ec2(WEST_AUTH, "DescribeTags",
"Filter.1.Name", "resource-id", "Filter.1.Value.1", east);
assertTrue(otherRegion.getList("DescribeTagsResponse.tagSet.item.resourceId").isEmpty());
} finally {
ec2(EAST_AUTH, "DeleteVpc", "VpcId", east);
ec2(WEST_AUTH, "DeleteVpc", "VpcId", west);
}
}
private List<String> probedIds(String auth) {
return ec2(auth, "DescribeTags", "Filter.1.Name", "key", "Filter.1.Value.1", "RegionProbe")
.getList("DescribeTagsResponse.tagSet.item.resourceId", String.class);
}
}
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.iam;
import io.github.hectorvent.floci.core.common.IamConditionContextResolver;
import io.github.hectorvent.floci.core.common.RequestContext;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.ec2.model.Vpc;
@@ -47,6 +48,9 @@ class IamEnforcementIntegrationTest {
@Inject
S3Service s3Service;
@Inject
RequestContext requestContext;
// =========================================================================
// IamPolicyEvaluator — basic allow / deny / implicit-deny
// =========================================================================
@@ -290,6 +294,28 @@ class IamEnforcementIntegrationTest {
ec2Request("Action=DeleteTags&ResourceId.1=" + other.getVpcId() + "&Tag.1.Key=Team"))));
}
@Test
void resourceTagConditionReadsTheTagsOfTheRequestRegion() {
Vpc west = ec2Service.createVpc("eu-west-1", "10.46.0.0/16", false);
ec2Service.createTags("eu-west-1", List.of(west.getVpcId()), List.of(new Tag("Team", "payments")));
String deleteTeamTag = "Action=DeleteTags&ResourceId.1=" + west.getVpcId() + "&Tag.1.Key=Team";
String previousRegion = requestContext.getRegion();
try {
requestContext.setRegion("eu-west-1");
assertEquals(Decision.ALLOW, evaluator.simulateCustomPolicy(
List.of(TEAM_SCOPED_EC2_POLICY), "ec2:DeleteTags", "*",
conditionContextResolver.resolve("ec2", "ec2:DeleteTags", ec2Request(deleteTeamTag))));
requestContext.setRegion("us-east-1");
assertEquals(Decision.DENY, evaluator.simulateCustomPolicy(
List.of(TEAM_SCOPED_EC2_POLICY), "ec2:DeleteTags", "*",
conditionContextResolver.resolve("ec2", "ec2:DeleteTags", ec2Request(deleteTeamTag))));
} finally {
requestContext.setRegion(previousRegion);
ec2Service.deleteVpc("eu-west-1", west.getVpcId());
}
}
@Test
void aMultiResourceRequestIsAuthorizedOncePerResource() {
Vpc payments = ec2Service.createVpc("us-east-1", "10.44.0.0/16", false);