diff --git a/src/main/java/io/floci/aws/services/ec2/Ec2StoredResourceArns.java b/src/main/java/io/floci/aws/services/ec2/Ec2StoredResourceArns.java index e7f879ceaf..76af7e658f 100644 --- a/src/main/java/io/floci/aws/services/ec2/Ec2StoredResourceArns.java +++ b/src/main/java/io/floci/aws/services/ec2/Ec2StoredResourceArns.java @@ -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 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; diff --git a/src/test/java/io/floci/aws/core/common/IamConditionContextResolverTest.java b/src/test/java/io/floci/aws/core/common/IamConditionContextResolverTest.java index 8677edcbbd..d677566d20 100644 --- a/src/test/java/io/floci/aws/core/common/IamConditionContextResolverTest.java +++ b/src/test/java/io/floci/aws/core/common/IamConditionContextResolverTest.java @@ -928,6 +928,24 @@ class IamConditionContextResolverTest { } } + @ParameterizedTest + @ValueSource(strings = {"111111111111", "123456789012"}) + void ec2ArnWithoutARegionReadsTheCallersRegionInItsOwnAccount(String targetAccount) { + AccountAwareStorageBackend> 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> 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> tags = AccountAwareStorageBackend.inMemory("123456789012");