fix(ec2): retain caller Region for stored tag lookup (#9540)

This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-26 14:49:10 -04:00
1 parent 84258c11fb
commit 85414ddb86
2 files changed
+20 -1

No files matched your search

@@ -42,8 +42,9 @@ public class Ec2StoredResourceArns implements StoredResourceArns {
return Map.of();
}
String account = arn.accountId().isEmpty() ? regionResolver.getAccountId() : arn.accountId();
String region = arn.region().isEmpty() ? regionResolver.getRegion() : arn.region();
Map<String, String> tags = new LinkedHashMap<>();
for (Tag tag : ec2.resourceTags(account, arn.region(), parts[1])) {
for (Tag tag : ec2.resourceTags(account, region, parts[1])) {
tags.put(tag.getKey(), tag.getValue() == null ? "" : tag.getValue());
}
return tags;
@@ -928,6 +928,24 @@ class IamConditionContextResolverTest {
}
}
@ParameterizedTest
@ValueSource(strings = {"111111111111", "123456789012"})
void ec2ArnWithoutARegionReadsTheCallersRegionInItsOwnAccount(String targetAccount) {
AccountAwareStorageBackend<List<Tag>> tags = AccountAwareStorageBackend.inMemory("123456789012");
tags.putForAccount("123456789012", "us-east-1::vpce-0123456789", List.of(new Tag("Team", "caller-east")));
tags.putForAccount("111111111111", "us-east-1::vpce-0123456789", List.of(new Tag("Team", "foreign-east")));
tags.putForAccount(targetAccount, "eu-west-1::vpce-0123456789", List.of(new Tag("Team", "other-region")));
tags.putForAccount(targetAccount, "::vpce-0123456789", List.of(new Tag("Team", "empty-region")));
IamConditionContextResolver reading = resolverWithStoredEc2Tags(tags);
String value = targetAccount.equals("123456789012") ? "caller-east" : "foreign-east";
Map<String, List<String>> context = ec2ResourceContext(reading, "ec2", "ec2:ModifyVpcEndpoint",
"arn:aws:ec2::" + targetAccount + ":vpc-endpoint/vpce-0123456789", "us-east-1", "123456789012",
Map.of("aws:ResourceTag/Team", List.of("stale")));
assertEquals(List.of(value), context.get("aws:ResourceTag/Team"));
assertEquals(List.of("other-region"), reading.resourceTags(
"arn:aws:ec2:eu-west-1:" + targetAccount + ":vpc-endpoint/vpce-0123456789").get("aws:ResourceTag/Team"));
}
@Test
void ec2UntaggedForeignArnDoesNotBorrowRealCallerTags() {
AccountAwareStorageBackend<List<Tag>> tags = AccountAwareStorageBackend.inMemory("123456789012");