From 4058dfae00b280185588703e70a15a8026f4269a Mon Sep 17 00:00:00 2001 From: Michal Schreiber <73832189+ck-michals@users.noreply.github.com> Date: Sat, 15 Aug 2026 04:50:03 +0200 Subject: [PATCH] fix(ec2): type a security group rule's tags as security-group-rule (#2272) * fix(ec2): type a security group rule's tags as security-group-rule inferResourceType had no sgr- case, so DescribeTags reported a rule's tags as resourceType "unknown" and a resource-type filter never matched them. The tags themselves were already correct: createTags stores them and updateResourceTags mirrors them onto the rule, so only the aggregation surface was wrong. Closes #2270 * fix(ec2): brace inferResourceType and tag the group in its test Bracing only the new arm would have left it the single braced line among thirteen, so the whole method is braced rather than just the addition. The test tagged only the rule, so the security-group assertion read an empty result and fell back to the expected value through orElse, passing without exercising the sg- classification at all. Tag the group too and assert on the result directly; removing the sg- arm now fails the test. --------- Co-authored-by: Michal Schreiber --- .../floci/services/ec2/Ec2Service.java | 51 ++++++++++++++----- .../floci/services/ec2/Ec2ServiceTest.java | 34 +++++++++++++ 2 files changed, 73 insertions(+), 12 deletions(-) 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);