fix(ssm): reject SendCommand with more than 50 InstanceIds (#4754)

This commit is contained in:
Aymen JAMAAOUI authored and GitHub committed 2026-09-30 14:36:26 -04:00
1 parent efbd990ef7
commit b0f30ae94d
3 files changed
+79 -14

No files matched your search

@@ -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("");
@@ -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();
@@ -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 = """
{