Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ssm): reject SendCommand with more than 50 InstanceIds (#4754)
Aymen JAMAAOUIb0f30ae94d8d
diff --git a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmCommandService.java b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmCommandService.javaindex 7cc801bfd2..e8d6984363 100644--- a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmCommandService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmCommandService.java@@ -45,6 +45,7 @@ public class SsmCommandService implements Resettable {private static final Logger LOG = Logger.getLogger(SsmCommandService.class);private static final int MIN_TIMEOUT_SECONDS = 30;+ private static final int MAX_INSTANCE_IDS = 50;static final int MAX_STDOUT_CHARS = 24000;static final int MAX_STDERR_CHARS = 8000;@@ -150,6 +151,12 @@ public class SsmCommandService implements Resettable {if (instanceIds.isEmpty()) {throw new AwsException("InvalidInstanceId", "At least one InstanceId is required.", 400);}+ if (instanceIds.size() > MAX_INSTANCE_IDS) {+ throw new AwsException(+ "ValidationException",+ "1 validation error detected: Value at 'instanceIds' failed to satisfy constraint: Member must have length less than or equal to " + MAX_INSTANCE_IDS,+ 400);+ }Map<String, List<String>> parameters = parseParameters(request.path("Parameters"));String comment = request.path("Comment").asText("");diff --git a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmCommandServiceDirectExecutionTest.java b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmCommandServiceDirectExecutionTest.javaindex 5afc22e478..46ff862177 100644--- a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmCommandServiceDirectExecutionTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmCommandServiceDirectExecutionTest.java@@ -632,16 +632,19 @@ class SsmCommandServiceDirectExecutionTest {SsmCommandService service = new SsmCommandService(new InMemoryStorageFactory(), objectMapper, regionResolver, executor);- ObjectNode request = objectMapper.createObjectNode();- ArrayNode instanceIdsNode = request.putArray("InstanceIds");- for (int i = 0; i < instanceCount; i++) {- instanceIdsNode.add("i-burst-" + i);+ // SendCommand takes at most 50 InstanceIds, so the burst is split across several commands.+ List<Command> commands = new ArrayList<>();+ for (int first = 0; first < instanceCount; first += 50) {+ ObjectNode request = objectMapper.createObjectNode();+ ArrayNode instanceIdsNode = request.putArray("InstanceIds");+ for (int i = first; i < Math.min(first + 50, instanceCount); i++) {+ instanceIdsNode.add("i-burst-" + i);+ }+ request.put("DocumentName", "AWS-RunShellScript");+ request.putObject("Parameters").putArray("commands").add("sleep 60");+ request.put("TimeoutSeconds", 60);+ commands.add(service.sendCommand(request, "us-west-2"));}- request.put("DocumentName", "AWS-RunShellScript");- request.putObject("Parameters").putArray("commands").add("sleep 60");- request.put("TimeoutSeconds", 60);-- Command command = service.sendCommand(request, "us-west-2");try {assertTrue(allStarted.await(10, TimeUnit.SECONDS), (instanceCount - allStarted.getCount())@@ -650,11 +653,9 @@ class SsmCommandServiceDirectExecutionTest {} finally {release.countDown();}- for (int i = 0; i < 250 && "InProgress".equals(- service.listCommands(command.getCommandId(), null, "us-west-2").getFirst().getStatus()); i++) {- TimeUnit.MILLISECONDS.sleep(20);+ for (Command command : commands) {+ assertEquals("Success", waitForCommandStatus(service, command.getCommandId(), "us-west-2"));}- assertEquals("Success", service.listCommands(command.getCommandId(), null, "us-west-2").getFirst().getStatus());}@Test@@ -738,7 +739,7 @@ class SsmCommandServiceDirectExecutionTest {}private static String waitForCommandStatus(SsmCommandService service, String commandId, String region) throws InterruptedException {- for (int i = 0; i < 50; i++) {+ for (int i = 0; i < 250; i++) {Command command = service.listCommands(commandId, null, region).getFirst();if (!"InProgress".equals(command.getStatus())) {return command.getStatus();diff --git a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmSendCommandIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmSendCommandIntegrationTest.javaindex 98c5a3c6b8..ee1dc14383 100644--- a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmSendCommandIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmSendCommandIntegrationTest.java@@ -14,6 +14,8 @@ import java.time.Instant;import java.util.Base64;import java.util.Optional;import java.util.UUID;+import java.util.stream.Collectors;+import java.util.stream.IntStream;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.*;@@ -479,6 +481,61 @@ class SsmSendCommandIntegrationTest {.body("Messages", empty());}+ // ── InstanceIds limit ──────────────────────────────────────────────────++ @Test+ @Order(13)+ void sendCommandRejectsMoreThanFiftyInstanceIds() {+ given()+ .header("X-Amz-Target", "AmazonSSM.SendCommand")+ .contentType(SSM_CT)+ .body("""+ {+ "InstanceIds": %s,+ "DocumentName": "AWS-RunShellScript",+ "Parameters": {+ "commands": ["echo hello"]+ }+ }+ """.formatted(instanceIdsJson(51)))+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("ValidationException"))+ .body("message", containsString("at 'instanceIds' failed to satisfy constraint"))+ .body("message", containsString("less than or equal to 50"));+ }++ @Test+ @Order(14)+ void sendCommandAcceptsFiftyInstanceIds() {+ given()+ .header("X-Amz-Target", "AmazonSSM.SendCommand")+ .contentType(SSM_CT)+ .body("""+ {+ "InstanceIds": %s,+ "DocumentName": "AWS-RunShellScript",+ "Parameters": {+ "commands": ["echo hello"]+ }+ }+ """.formatted(instanceIdsJson(50)))+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("Command.TargetCount", equalTo(50))+ .body("Command.InstanceIds", hasSize(50));+ }++ private static String instanceIdsJson(int count) {+ return IntStream.range(0, count)+ .mapToObj(i -> "\"i-limit-" + i + "\"")+ .collect(Collectors.joining(",", "[", "]"));+ }+private static String buildReplyPayload(String stdout, String status, int returnCode) {String json = """{