diff --git a/src/main/java/io/github/hectorvent/floci/services/backup/BackupController.java b/src/main/java/io/github/hectorvent/floci/services/backup/BackupController.java index c4c2be3f82..17bbb4794e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/backup/BackupController.java +++ b/src/main/java/io/github/hectorvent/floci/services/backup/BackupController.java @@ -147,8 +147,13 @@ public class BackupController { String body) throws IOException { String region = regionResolver.resolveRegion(headers); JsonNode req = objectMapper.readTree(body == null || body.isBlank() ? "{}" : body); - service.putBackupVaultNotifications(vaultName, region, - textOrNull(req, "SNSTopicArn"), readStringList(req.path("BackupVaultEvents"))); + // null, not an empty list, when the member is absent: the service answers a missing + // parameter with MissingParameterValueException and an empty one with + // InvalidParameterValueException, and readStringList cannot tell them apart. + JsonNode eventsNode = req.path("BackupVaultEvents"); + List events = eventsNode.isMissingNode() || eventsNode.isNull() ? null + : readStringList(eventsNode); + service.putBackupVaultNotifications(vaultName, region, textOrNull(req, "SNSTopicArn"), events); return Response.noContent().build(); } diff --git a/src/main/java/io/github/hectorvent/floci/services/backup/BackupService.java b/src/main/java/io/github/hectorvent/floci/services/backup/BackupService.java index c3e6af7a6d..cca39562e6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/backup/BackupService.java +++ b/src/main/java/io/github/hectorvent/floci/services/backup/BackupService.java @@ -199,11 +199,23 @@ public class BackupService { public void putBackupVaultNotifications(String vaultName, String region, String snsTopicArn, List events) { describeBackupVault(vaultName, region); - if (snsTopicArn == null || snsTopicArn.isBlank()) { - throw new AwsException("InvalidParameterValueException", + // Absent and present-but-empty are different faults and AWS answers them differently. + // PutBackupVaultNotifications lists MissingParameterValueException, "Indicates that a + // required parameter is missing", in its Errors section; a parameter that is present and + // unusable stays InvalidParameterValueException. + if (snsTopicArn == null) { + throw new AwsException("MissingParameterValueException", "SNSTopicArn is required", 400); } - if (events == null || events.isEmpty()) { + if (snsTopicArn.isBlank()) { + throw new AwsException("InvalidParameterValueException", + "SNSTopicArn must not be empty", 400); + } + if (events == null) { + throw new AwsException("MissingParameterValueException", + "BackupVaultEvents is required", 400); + } + if (events.isEmpty()) { throw new AwsException("InvalidParameterValueException", "BackupVaultEvents must name at least one event", 400); } diff --git a/src/test/java/io/github/hectorvent/floci/services/backup/BackupIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/backup/BackupIntegrationTest.java index 863e74cf7e..c04d9c15d0 100644 --- a/src/test/java/io/github/hectorvent/floci/services/backup/BackupIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/backup/BackupIntegrationTest.java @@ -696,15 +696,30 @@ class BackupIntegrationTest { @Test @Order(122) void notificationsRequireATopicAndAtLeastOneEvent() { + // A missing required parameter is MissingParameterValueException, which the operation + // lists in its Errors section; present-but-empty is InvalidParameterValueException. An + // SDK maps __type to a typed exception, so collapsing the two hands the caller the wrong + // one for a request real AWS names precisely. given().header("Authorization", AUTH).contentType("application/json") .body("{\"BackupVaultEvents\":[\"BACKUP_JOB_COMPLETED\"]}") .when().put("/backup-vaults/" + SUB_VAULT + "/notification-configuration") - .then().statusCode(400).body("__type", equalTo("InvalidParameterValueException")); + .then().statusCode(400) + .body("__type", equalTo("MissingParameterValueException")) + .body("message", containsString("SNSTopicArn")); + + given().header("Authorization", AUTH).contentType("application/json") + .body("{\"SNSTopicArn\":\"" + TOPIC + "\"}") + .when().put("/backup-vaults/" + SUB_VAULT + "/notification-configuration") + .then().statusCode(400) + .body("__type", equalTo("MissingParameterValueException")) + .body("message", containsString("BackupVaultEvents")); given().header("Authorization", AUTH).contentType("application/json") .body("{\"SNSTopicArn\":\"" + TOPIC + "\",\"BackupVaultEvents\":[]}") .when().put("/backup-vaults/" + SUB_VAULT + "/notification-configuration") - .then().statusCode(400).body("__type", equalTo("InvalidParameterValueException")); + .then().statusCode(400) + .body("__type", equalTo("InvalidParameterValueException")) + .body("message", containsString("BackupVaultEvents")); } @Test