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.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-16 11:10:32 -04:00
1 parent 97b49b56f0
commit a7df271bb3
4 files changed
+106 -15

No files matched your search

@@ -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<BackupRule> 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<RecoveryPoint> 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<String, String> 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<String> 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<BackupRule> rules) {
@@ -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
@@ -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<String, String> parameters) {
@@ -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);