diff --git a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java index 15c81a02aa..3c48d3ca65 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonHandler.java @@ -383,8 +383,15 @@ public class SsmJsonHandler { * be reported back by DescribeDocumentPermission as though the share had happened. */ private List accountIds(JsonNode request, String field) { + JsonNode fieldNode = request.path(field); + if (!fieldNode.isMissingNode() && !fieldNode.isNull() && !fieldNode.isArray()) { + throw new AwsException("ValidationException", + "1 validation error detected: Value at '" + decapitalize(field) + "' failed to satisfy " + + "constraint: Member must be a list", + 400); + } List accountIds = new ArrayList<>(); - request.path(field).forEach(node -> accountIds.add(node.asText())); + fieldNode.forEach(node -> accountIds.add(node.asText())); if (accountIds.size() > MAX_ACCOUNT_IDS) { throw new AwsException("ValidationException", "1 validation error detected: Value at '" + decapitalize(field) + "' failed to satisfy " @@ -474,6 +481,13 @@ public class SsmJsonHandler { requireSharePermissionType(request); List accountIdsToAdd = accountIds(request, "AccountIdsToAdd"); List accountIdsToRemove = accountIds(request, "AccountIdsToRemove"); + if (accountIdsToAdd.isEmpty() && accountIdsToRemove.isEmpty()) { + throw new AwsException("ValidationException", + "1 validation error detected: Value null at 'accountIdsToAdd' failed to satisfy " + + "constraint: Member must not be null, or a value must be specified for " + + "'accountIdsToRemove'", + 400); + } ssmService.modifyDocumentPermission(name, accountIdsToAdd, accountIdsToRemove, region); return Response.ok(objectMapper.createObjectNode()).build(); @@ -601,6 +615,33 @@ public class SsmJsonHandler { return settingId; } + /** + * Reads the required {@code SettingValue} member of UpdateServiceSetting. Botocore models + * {@code ServiceSettingValue} as a string with {@code min: 1, max: 4096}; an absent or blank + * value silently stores an empty customized setting rather than failing, and an oversized one + * would sit in the store forever since GetServiceSetting just echoes it back. + */ + private static final int MAX_SETTING_VALUE_LENGTH = 4096; + + private String requireSettingValue(JsonNode request) { + JsonNode valueNode = request.path("SettingValue"); + String settingValue = valueNode.asText(); + if (!valueNode.isTextual() || settingValue.isEmpty()) { + throw new AwsException("ValidationException", + "1 validation error detected: Value null at 'settingValue' failed to satisfy " + + "constraint: Member must not be null", + 400); + } + if (settingValue.length() > MAX_SETTING_VALUE_LENGTH) { + throw new AwsException("ValidationException", + "1 validation error detected: Value at 'settingValue' failed to satisfy " + + "constraint: Member must have length less than or equal to " + + MAX_SETTING_VALUE_LENGTH, + 400); + } + return settingValue; + } + private Response handleGetServiceSetting(JsonNode request, String region) { String settingId = requireSettingId(request); ServiceSetting setting = ssmService.getServiceSetting(settingId, region); @@ -612,7 +653,7 @@ public class SsmJsonHandler { private Response handleUpdateServiceSetting(JsonNode request, String region) { String settingId = requireSettingId(request); - String settingValue = request.path("SettingValue").asText(); + String settingValue = requireSettingValue(request); ssmService.updateServiceSetting(settingId, settingValue, region); return Response.ok(objectMapper.createObjectNode()).build(); } diff --git a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmIntegrationTest.java index fffb9c4952..4a546b59d2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ssm/SsmIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ssm/SsmIntegrationTest.java @@ -321,6 +321,71 @@ class SsmIntegrationTest { .body("__type", equalTo("ServiceSettingNotFound")); } + // ── SettingValue (botocore: ServiceSettingValue, required, min 1 / max 4096) ── + + @Test + void updateServiceSetting_missingSettingValueReturnsValidationException() { + given() + .header("X-Amz-Target", "AmazonSSM.UpdateServiceSetting") + .contentType(SSM_CONTENT_TYPE) + .body(""" + { "SettingId": "/ssm/documents/console/public-sharing-permission" } + """) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")); + } + + @Test + void updateServiceSetting_whitespaceOnlySettingValueIsStoredVerbatim() { + given() + .header("X-Amz-Target", "AmazonSSM.UpdateServiceSetting") + .contentType(SSM_CONTENT_TYPE) + .body(""" + { + "SettingId": "/ssm/parameter-store/default-parameter-tier", + "SettingValue": " " + } + """) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "AmazonSSM.GetServiceSetting") + .contentType(SSM_CONTENT_TYPE) + .body(""" + { "SettingId": "/ssm/parameter-store/default-parameter-tier" } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("ServiceSetting.SettingValue", equalTo(" ")); + } + + @Test + void updateServiceSetting_settingValueOverMaxLengthReturnsValidationException() { + String tooLong = "x".repeat(4097); + given() + .header("X-Amz-Target", "AmazonSSM.UpdateServiceSetting") + .contentType(SSM_CONTENT_TYPE) + .body(""" + { + "SettingId": "/ssm/documents/console/public-sharing-permission", + "SettingValue": "%s" + } + """.formatted(tooLong)) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")); + } + @Test void serviceSettingOperations_missingSettingIdReturnsValidationException() { for (String target : new String[]{"GetServiceSetting", "UpdateServiceSetting", "ResetServiceSetting"}) { @@ -1055,6 +1120,40 @@ class SsmIntegrationTest { } } + @Test + void modifyDocumentPermission_nonListAccountIdsToAddReturnsValidationException() { + createSharableDocument("Non-List-Account-Ids-Document"); + + given() + .header("X-Amz-Target", "AmazonSSM.ModifyDocumentPermission") + .contentType(SSM_CONTENT_TYPE) + .body(""" + { + "Name": "Non-List-Account-Ids-Document", + "PermissionType": "Share", + "AccountIdsToAdd": "444444444444" + } + """) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")); + + // Nothing may have been shared — the scalar must not be silently treated as empty. + given() + .header("X-Amz-Target", "AmazonSSM.DescribeDocumentPermission") + .contentType(SSM_CONTENT_TYPE) + .body(""" + { "Name": "Non-List-Account-Ids-Document", "PermissionType": "Share" } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("AccountIds", empty()); + } + // ── AccountIdsToAdd/Remove (botocore: list max 20, member (?i)all|[0-9]{12}) ── @Test @@ -1138,6 +1237,43 @@ class SsmIntegrationTest { .body("__type", equalTo("ValidationException")); } + // ── ModifyDocumentPermission: at least one of AccountIdsToAdd/AccountIdsToRemove + // required (botocore: documented on both members, not a JSON `required` or shape + // constraint — "You must specify a value for this parameter or the + // AccountIdsToRemove/AccountIdsToAdd parameter.") ── + + @Test + void modifyDocumentPermission_neitherAccountListSpecifiedReturnsValidationException() { + createSharableDocument("No-Account-Lists-Document"); + + given() + .header("X-Amz-Target", "AmazonSSM.ModifyDocumentPermission") + .contentType(SSM_CONTENT_TYPE) + .body(""" + { + "Name": "No-Account-Lists-Document", + "PermissionType": "Share" + } + """) + .when() + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")); + + given() + .header("X-Amz-Target", "AmazonSSM.DescribeDocumentPermission") + .contentType(SSM_CONTENT_TYPE) + .body(""" + { "Name": "No-Account-Lists-Document", "PermissionType": "Share" } + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("AccountIds", empty()); + } + private static void createSharableDocument(String name) { given() .header("X-Amz-Target", "AmazonSSM.CreateDocument")