diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java index 1ab5cb2445..7c8e0689fa 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java @@ -2902,8 +2902,8 @@ public class Ec2QueryHandler { // ─── Security Group handlers ─────────────────────────────────────────────── private Response handleCreateSecurityGroup(MultivaluedMap p, String region) { - String groupName = p.getFirst("GroupName"); - String description = p.getFirst("GroupDescription"); + String groupName = requireParameter(p, "GroupName"); + String description = requireParameter(p, "GroupDescription"); String vpcId = p.getFirst("VpcId"); SecurityGroup sg = service.createSecurityGroup(region, groupName, description, vpcId); applyResourceTags(p, region, "security-group", sg.getGroupId()); @@ -2916,6 +2916,17 @@ public class Ec2QueryHandler { return xmlResponse(xml.build()); } + private static String requireParameter(MultivaluedMap params, String parameterName) { + String value = params.getFirst(parameterName); + if (value == null || value.isBlank()) { + throw new AwsException( + "MissingParameter", + "The request must contain the parameter " + parameterName, + 400); + } + return value; + } + private Response handleDescribeSecurityGroups(MultivaluedMap p, String region) { List groupIds = getList(p, "GroupId"); List groupNames = getList(p, "GroupName"); 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 b0c92f424b..030798a7d7 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 @@ -4395,7 +4395,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { // Check duplicate String finalVpcId = vpcId; boolean exists = securityGroups.scan(k -> true).stream() - .anyMatch(sg -> sg.getRegion().equals(region) && sg.getGroupName().equals(groupName) + .anyMatch(sg -> sg.getRegion().equals(region) && Objects.equals(sg.getGroupName(), groupName) && finalVpcId.equals(sg.getVpcId())); if (exists) { throw new AwsException("InvalidGroup.Duplicate", "The security group '" + groupName + "' already exists", 400); diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java index 11b2d70a84..4693a3f3fe 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java @@ -1489,6 +1489,36 @@ class Ec2IntegrationTest { .extract().path("CreateSecurityGroupResponse.groupId"); } + @Test + void createSecurityGroupMissingGroupName() { + given() + .formParam("Action", "CreateSecurityGroup") + .formParam("GroupDescription", "Test security group") + .formParam("VpcId", vpcId) + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(400) + .body(containsString("MissingParameter")) + .body(containsString("GroupName")); + } + + @Test + void createSecurityGroupMissingDescription() { + given() + .formParam("Action", "CreateSecurityGroup") + .formParam("GroupName", "test-security-group") + .formParam("VpcId", vpcId) + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(400) + .body(containsString("MissingParameter")) + .body(containsString("GroupDescription")); + } + @Test @Order(31) void authorizeSecurityGroupIngress() {