diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java index 56b2e57ebe..ebfc0dd4cd 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java @@ -2728,18 +2728,45 @@ public class Ec2Service implements ContainerTeardown { } private String inferResourceType(String resourceId) { - if (resourceId.startsWith("i-")) return "instance"; - if (resourceId.startsWith("vpc-")) return "vpc"; - if (resourceId.startsWith("subnet-")) return "subnet"; - if (resourceId.startsWith("sg-")) return "security-group"; - if (resourceId.startsWith("igw-")) return "internet-gateway"; - if (resourceId.startsWith("rtb-")) return "route-table"; - if (resourceId.startsWith("key-")) return "key-pair"; - if (resourceId.startsWith("eipalloc-")) return "elastic-ip"; - if (resourceId.startsWith("lt-")) return "launch-template"; - if (resourceId.startsWith("vpce-")) return "vpc-endpoint"; - if (resourceId.startsWith("nat-")) return "natgateway"; - if (resourceId.startsWith("pl-")) return "prefix-list"; + if (resourceId.startsWith("i-")) { + return "instance"; + } + if (resourceId.startsWith("vpc-")) { + return "vpc"; + } + if (resourceId.startsWith("subnet-")) { + return "subnet"; + } + if (resourceId.startsWith("sgr-")) { + return "security-group-rule"; + } + if (resourceId.startsWith("sg-")) { + return "security-group"; + } + if (resourceId.startsWith("igw-")) { + return "internet-gateway"; + } + if (resourceId.startsWith("rtb-")) { + return "route-table"; + } + if (resourceId.startsWith("key-")) { + return "key-pair"; + } + if (resourceId.startsWith("eipalloc-")) { + return "elastic-ip"; + } + if (resourceId.startsWith("lt-")) { + return "launch-template"; + } + if (resourceId.startsWith("vpce-")) { + return "vpc-endpoint"; + } + if (resourceId.startsWith("nat-")) { + return "natgateway"; + } + if (resourceId.startsWith("pl-")) { + return "prefix-list"; + } return "unknown"; } diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java index c76e1ced1b..1234516d5d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java @@ -13,6 +13,8 @@ import io.github.hectorvent.floci.services.ec2.model.Image; import io.github.hectorvent.floci.services.ec2.model.Instance; import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate; import io.github.hectorvent.floci.services.ec2.model.ManagedPrefixList; +import io.github.hectorvent.floci.services.ec2.model.IpPermission; +import io.github.hectorvent.floci.services.ec2.model.IpRange; import io.github.hectorvent.floci.services.ec2.model.PrefixListEntry; import io.github.hectorvent.floci.services.ec2.model.NetworkInterface; import io.github.hectorvent.floci.services.ec2.model.Reservation; @@ -1067,6 +1069,38 @@ class Ec2ServiceTest { .getFirst().getTags().isEmpty()); } + /** + * A rule's tags already reach the store and the rule itself; only DescribeTags mistyped them, + * so a resource-type filter never matched. + */ + @Test + void tagsOnASecurityGroupRuleAreTypedAsSecurityGroupRule() { + Ec2Service service = prefixListService(); + String groupId = service.createSecurityGroup("us-east-1", "db", "db", null).getGroupId(); + IpPermission perm = new IpPermission(); + perm.setIpProtocol("tcp"); + perm.setFromPort(443); + perm.setToPort(443); + perm.getIpRanges().add(new IpRange("10.0.0.0/8", null)); + String ruleId = service.authorizeSecurityGroupIngress("us-east-1", groupId, List.of(perm)) + .getFirst().getSecurityGroupRuleId(); + + service.createTags("us-east-1", List.of(ruleId), List.of(new Tag("env", "prod"))); + + assertEquals("security-group-rule", service.describeTags("us-east-1", + Map.of("resource-id", List.of(ruleId))).getFirst().get("resourceType")); + assertEquals(1, service.describeTags("us-east-1", + Map.of("resource-type", List.of("security-group-rule"))).size()); + + // Tag the group as well, so the sg- classification is genuinely exercised rather than + // read off an empty result. + service.createTags("us-east-1", List.of(groupId), List.of(new Tag("env", "prod"))); + assertEquals("security-group", service.describeTags("us-east-1", + Map.of("resource-id", List.of(groupId))).getFirst().get("resourceType")); + assertEquals(1, service.describeTags("us-east-1", + Map.of("resource-type", List.of("security-group"))).size()); + } + private static EmulatorConfig mockConfig(boolean ec2Mock) { EmulatorConfig config = mock(EmulatorConfig.class); EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);