From a7df271bb317e5cd8b9211ff43458b99c508f748 Mon Sep 17 00:00:00 2001 From: Allen Sanborn Date: Fri, 16 Oct 2026 10:10:32 -0500 Subject: [PATCH] fix(backup): return HTTP 400 for ResourceNotFoundException (#5794) * fix(backup): return HTTP 400 for ResourceNotFoundException AWS Backup documents ResourceNotFoundException as HTTP 400 across the API. BackupService threw it with 404 for missing vaults, plans, selections, jobs, recovery points and resource ARNs, while the sub-resource throws already used 400. Use 400 everywhere. * test(backup): expect 400 for a vault deleted with its CloudFormation stack BackupVaultCfnIntegrationTest still asserted the old 404 after ResourceNotFoundException moved to HTTP 400. * test(backup): cover 400 for missing selection, recovery point and tagged resource The ResourceNotFoundException status change reached selection, recovery point and resource-ARN lookups that no test exercised. Assert 400 for GetBackupSelection/DeleteBackupSelection (missing, and under the wrong plan), DescribeRecoveryPoint/DeleteRecoveryPoint, and ListTags/ TagResource/UntagResource on a missing ARN. Align the CFN provisioner fixture to 400. --- .../floci/services/backup/BackupService.java | 18 ++-- .../backup/BackupIntegrationTest.java | 97 ++++++++++++++++++- .../BackupVaultCfnIntegrationTest.java | 4 +- .../BackupVaultCfnProvisionerTest.java | 2 +- 4 files changed, 106 insertions(+), 15 deletions(-) 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 8729ab2fc1..74eb600808 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 @@ -125,7 +125,7 @@ public class BackupService { public BackupVault describeBackupVault(String vaultName, String region) { return vaultStore.get(vaultKey(region, vaultName)) - .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup vault not found: " + vaultName, 404)); + .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup vault not found: " + vaultName, 400)); } public void deleteBackupVault(String vaultName, String region) { @@ -511,7 +511,7 @@ public class BackupService { public BackupPlan getBackupPlan(String planId) { return planStore.get(planId) - .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup plan not found: " + planId, 404)); + .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup plan not found: " + planId, 400)); } public BackupPlan updateBackupPlan(String planId, String planName, List rules) { @@ -564,9 +564,9 @@ public class BackupService { public BackupSelection getBackupSelection(String planId, String selectionId) { BackupSelection sel = selectionStore.get(selectionId) - .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup selection not found: " + selectionId, 404)); + .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup selection not found: " + selectionId, 400)); if (!planId.equals(sel.getBackupPlanId())) { - throw new AwsException("ResourceNotFoundException", "Backup selection not found in plan: " + planId, 404); + throw new AwsException("ResourceNotFoundException", "Backup selection not found in plan: " + planId, 400); } return sel; } @@ -623,7 +623,7 @@ public class BackupService { public BackupJob describeBackupJob(String jobId) { return jobStore.get(jobId) - .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup job not found: " + jobId, 404)); + .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Backup job not found: " + jobId, 400)); } public void stopBackupJob(String jobId) { @@ -657,7 +657,7 @@ public class BackupService { return recoveryStore.get(recoveryPointArn) .filter(rp -> vaultName.equals(rp.getBackupVaultName())) .orElseThrow(() -> new AwsException("ResourceNotFoundException", - "Recovery point not found: " + recoveryPointArn, 404)); + "Recovery point not found: " + recoveryPointArn, 400)); } public List listRecoveryPointsByBackupVault(String vaultName, String region) { @@ -842,7 +842,7 @@ public class BackupService { if (plan.isPresent()) { return new HashMap<>(); } - throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 404); + throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 400); } private void applyTags(String arn, Map newTags) { @@ -855,7 +855,7 @@ public class BackupService { vaultStore.put(vaultKey(vault), vault); return; } - throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 404); + throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 400); } private void removeTags(String arn, List tagKeys) { @@ -868,7 +868,7 @@ public class BackupService { vaultStore.put(vaultKey(vault), vault); return; } - throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 404); + throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 400); } private static void assignRuleIds(List rules) { 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 caa764f3b0..93499689ee 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 @@ -534,6 +534,83 @@ class BackupIntegrationTest { .body("Tags.env", equalTo("test")); } + // ── ResourceNotFoundException is HTTP 400 on every lookup ────────────────── + + @Test + @Order(63) + void missingSelectionReturns400() { + given().header("Authorization", AUTH) + .when().get("/backup/plans/" + planId + "/selections/no-such-selection") + .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException")); + + given().header("Authorization", AUTH) + .when().delete("/backup/plans/" + planId + "/selections/no-such-selection") + .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException")); + } + + @Test + @Order(64) + void selectionLookedUpUnderAnotherPlanReturns400() { + String otherPlanId = given() + .header("Authorization", AUTH) + .contentType("application/json") + .body(""" + { + "BackupPlan": { + "BackupPlanName": "other-plan", + "Rules": [{ + "RuleName": "daily", + "TargetBackupVaultName": "%s", + "ScheduleExpression": "cron(0 12 * * ? *)" + }] + } + } + """.formatted(VAULT_NAME)) + .when().put("/backup/plans/") + .then().statusCode(200).extract().path("BackupPlanId"); + + given().header("Authorization", AUTH) + .when().get("/backup/plans/" + otherPlanId + "/selections/" + selectionId) + .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException")); + + given().header("Authorization", AUTH) + .when().delete("/backup/plans/" + otherPlanId) + .then().statusCode(204); + } + + @Test + @Order(65) + void missingRecoveryPointReturns400() { + // The vault exists; the recovery point was deleted at Order(54). + given().header("Authorization", AUTH) + .when().get("/backup-vaults/" + VAULT_NAME + "/recovery-points/" + recoveryPointArn) + .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException")); + + given().header("Authorization", AUTH) + .when().delete("/backup-vaults/" + VAULT_NAME + "/recovery-points/" + recoveryPointArn) + .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException")); + } + + @Test + @Order(66) + void tagOperationsOnAMissingResourceReturn400() { + String missingArn = "arn:aws:backup:us-east-1:000000000000:backup-vault:no-such-vault"; + + given().header("Authorization", AUTH) + .when().get("/tags/" + missingArn) + .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException")); + + given().header("Authorization", AUTH).contentType("application/json") + .body("{\"Tags\":{\"team\":\"platform\"}}") + .when().post("/tags/" + missingArn) + .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException")); + + given().header("Authorization", AUTH).contentType("application/json") + .body("{\"TagKeyList\":[\"team\"]}") + .when().post("/untag/" + missingArn) + .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException")); + } + // ── Supported Resource Types ─────────────────────────────────────────────── @Test @@ -587,13 +664,27 @@ class BackupIntegrationTest { @Test @Order(83) - void describeDeletedVaultReturns404() { + void describeDeletedVaultReturns400() { + // AWS Backup documents ResourceNotFoundException as HTTP 400, not 404. given() .header("Authorization", AUTH) .when() .get("/backup-vaults/" + VAULT_NAME) .then() - .statusCode(404); + .statusCode(400) + .body("__type", equalTo("ResourceNotFoundException")); + } + + @Test + @Order(84) + void missingPlanAndJobReturn400() { + given().header("Authorization", AUTH) + .when().get("/backup/plans/no-such-plan/") + .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException")); + + given().header("Authorization", AUTH) + .when().get("/backup-jobs/no-such-job") + .then().statusCode(400).body("__type", equalTo("ResourceNotFoundException")); } // ── Vault sub-resources: access policy, notifications, lock ──────────────── @@ -670,7 +761,7 @@ class BackupIntegrationTest { given().header("Authorization", AUTH).contentType("application/json") .body("{\"Policy\":\"{}\"}") .when().put("/backup-vaults/no-such-vault/access-policy") - .then().statusCode(404).body("message", containsString("no-such-vault")); + .then().statusCode(400).body("message", containsString("no-such-vault")); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/BackupVaultCfnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/BackupVaultCfnIntegrationTest.java index 42dd4e2589..13ae0caeb4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/BackupVaultCfnIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/BackupVaultCfnIntegrationTest.java @@ -93,8 +93,8 @@ class BackupVaultCfnIntegrationTest { cloudFormation("DeleteStack", Map.of()); CfnStackWaits.awaitStackDeleted(STACK); - describeVault(NAMED).statusCode(404); - describeVault(unnamed).statusCode(404); + describeVault(NAMED).statusCode(400).body("__type", equalTo("ResourceNotFoundException")); + describeVault(unnamed).statusCode(400).body("__type", equalTo("ResourceNotFoundException")); } private static void cloudFormation(String action, Map parameters) { diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BackupVaultCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BackupVaultCfnProvisionerTest.java index ac14af5991..79048f854c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BackupVaultCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BackupVaultCfnProvisionerTest.java @@ -39,7 +39,7 @@ class BackupVaultCfnProvisionerTest { private static final String ARN = "arn:aws:backup:us-east-1:000000000000:backup-vault:my-vault"; private static final String KMS = "arn:aws:kms:us-east-1:000000000000:key/abc"; private static final AwsException NOT_FOUND = - new AwsException("ResourceNotFoundException", "Backup vault not found: my-vault", 404); + new AwsException("ResourceNotFoundException", "Backup vault not found: my-vault", 400); private final BackupService backup = mock(BackupService.class); private final BackupVaultCfnProvisioner provisioner = new BackupVaultCfnProvisioner(backup);