diff --git a/docs/services/backup.md b/docs/services/backup.md index 4537809c0f..1c52951e4b 100644 --- a/docs/services/backup.md +++ b/docs/services/backup.md @@ -89,6 +89,13 @@ |---|---|---|---| | `GetSupportedResourceTypes` | `GET` | `/supported-resource-types` | List resource types supported for backup | +## Plan Tags + +`CreateBackupPlan` stores `BackupPlanTags`. `TagResource`, `ListTags` and `UntagResource` +accept the plan ARN returned at creation. Adding tags merges keys, an existing key's value +is replaced, and removing absent keys is harmless. Tags survive plan updates and are stored +with the plan in persistent modes. Plans stored before tag support start with an empty tag map. + ## Job Lifecycle Backup jobs transition through states automatically after `StartBackupJob`: 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 8e869b3b28..bef9e615c2 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 @@ -264,7 +264,8 @@ public class BackupController { List rules = readRules(planNode.path("Rules")); String creatorRequestId = textOrNull(req, "CreatorRequestId"); - BackupPlan plan = service.createBackupPlan(planName, rules, creatorRequestId, region); + BackupPlan plan = service.createBackupPlan(planName, rules, creatorRequestId, + readStringMap(req, "BackupPlanTags"), region); ObjectNode out = objectMapper.createObjectNode(); out.put("BackupPlanId", plan.getBackupPlanId()); @@ -583,13 +584,19 @@ public class BackupController { return n.asText(); } - @SuppressWarnings("unchecked") private Map readStringMap(JsonNode node, String field) { JsonNode mapNode = node.path(field); if (mapNode.isMissingNode() || mapNode.isNull()) { return new HashMap<>(); } - return objectMapper.convertValue(mapNode, Map.class); + if (!mapNode.isObject() || mapNode.properties().stream() + .anyMatch(entry -> !entry.getValue().isTextual())) { + throw new AwsException("InvalidParameterValueException", + field + " must be an object with string values", 400); + } + Map result = new HashMap<>(); + mapNode.properties().forEach(entry -> result.put(entry.getKey(), entry.getValue().textValue())); + return result; } private static List readStringList(JsonNode node) { 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 ff2c3c55ac..98ad63e322 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 @@ -69,8 +69,8 @@ public class BackupService { private final int jobCompletionDelaySeconds; private final ObjectMapper objectMapper; - // One monitor per vault key, so the check-then-write in createBackupVault and the - // multi-store cleanup in deleteBackupVault are atomic for a given name. Same shape as + // One monitor per account/resource key, so lookups, mutations, and deletion cannot + // interleave for a given resource. Same shape as // SecretsManagerService#lockFor and KinesisService's append locks. private final ConcurrentHashMap vaultLocks = new ConcurrentHashMap<>(); @@ -152,6 +152,10 @@ public class BackupService { return "job:" + regionResolver.getAccountId() + ":" + jobId; } + private String planLockKey(String planId) { + return "plan:" + regionResolver.getAccountId() + ":" + planId; + } + // ── Vault ────────────────────────────────────────────────────────────────── public BackupVault createBackupVault(String vaultName, String encryptionKeyArn, @@ -636,11 +640,17 @@ public class BackupService { public BackupPlan createBackupPlan(String planName, List rules, String creatorRequestId, String region) { + return createBackupPlan(planName, rules, creatorRequestId, Map.of(), region); + } + + public BackupPlan createBackupPlan(String planName, List rules, + String creatorRequestId, Map tags, String region) { String planId = UUID.randomUUID().toString(); BackupPlan plan = new BackupPlan(); plan.setBackupPlanId(planId); plan.setBackupPlanArn(regionResolver.buildArn("backup", region, "backup-plan:" + planId)); plan.setBackupPlanName(planName); + plan.setTags(tags); plan.setCreationDate(Instant.now().getEpochSecond()); plan.setVersionId(shortId()); assignRuleIds(rules); @@ -655,27 +665,31 @@ public class BackupService { } public BackupPlan updateBackupPlan(String planId, String planName, List rules) { - BackupPlan plan = getBackupPlan(planId); - if (planName != null) { - plan.setBackupPlanName(planName); + synchronized (lockFor(planLockKey(planId))) { + BackupPlan plan = getBackupPlan(planId); + if (planName != null) { + plan.setBackupPlanName(planName); + } + assignRuleIds(rules); + plan.setRules(rules); + plan.setVersionId(shortId()); + planStore.put(planId, plan); + return plan; } - assignRuleIds(rules); - plan.setRules(rules); - plan.setVersionId(shortId()); - planStore.put(planId, plan); - return plan; } public void deleteBackupPlan(String planId) { - getBackupPlan(planId); - long selectionCount = selectionStore.scan(k -> true).stream() - .filter(s -> planId.equals(s.getBackupPlanId())) - .count(); - if (selectionCount > 0) { - throw new AwsException("InvalidRequestException", - "Backup plan has active selections and cannot be deleted", 400); + synchronized (lockFor(planLockKey(planId))) { + getBackupPlan(planId); + long selectionCount = selectionStore.scan(k -> true).stream() + .filter(s -> planId.equals(s.getBackupPlanId())) + .count(); + if (selectionCount > 0) { + throw new AwsException("InvalidRequestException", + "Backup plan has active selections and cannot be deleted", 400); + } + planStore.delete(planId); } - planStore.delete(planId); } public List listBackupPlans() { @@ -1011,13 +1025,17 @@ public class BackupService { if (vault.isPresent()) { return vault.get().getTags(); } - Optional plan = planStore.scan(k -> true).stream() - .filter(p -> arn.equals(p.getBackupPlanArn())) - .findFirst(); - if (plan.isPresent()) { - return new HashMap<>(); + String planId = findPlanByArn(arn).getBackupPlanId(); + synchronized (lockFor(planLockKey(planId))) { + return new HashMap<>(getBackupPlan(planId).getTags()); } - throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 400); + } + + private BackupPlan findPlanByArn(String arn) { + return planStore.scan(k -> true).stream() + .filter(plan -> arn.equals(plan.getBackupPlanArn())) + .findFirst() + .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 400)); } private void applyTags(String arn, Map newTags) { @@ -1030,7 +1048,12 @@ public class BackupService { vaultStore.put(vaultKey(vault), vault); return; } - throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 400); + String planId = findPlanByArn(arn).getBackupPlanId(); + synchronized (lockFor(planLockKey(planId))) { + BackupPlan plan = getBackupPlan(planId); + plan.getTags().putAll(newTags); + planStore.put(planId, plan); + } } private void removeTags(String arn, List tagKeys) { @@ -1043,7 +1066,12 @@ public class BackupService { vaultStore.put(vaultKey(vault), vault); return; } - throw new AwsException("ResourceNotFoundException", "Resource not found: " + arn, 400); + String planId = findPlanByArn(arn).getBackupPlanId(); + synchronized (lockFor(planLockKey(planId))) { + BackupPlan plan = getBackupPlan(planId); + tagKeys.forEach(plan.getTags()::remove); + planStore.put(planId, plan); + } } private static void assignRuleIds(List rules) { diff --git a/src/main/java/io/github/hectorvent/floci/services/backup/model/BackupPlan.java b/src/main/java/io/github/hectorvent/floci/services/backup/model/BackupPlan.java index dccfd4921e..5d64619680 100644 --- a/src/main/java/io/github/hectorvent/floci/services/backup/model/BackupPlan.java +++ b/src/main/java/io/github/hectorvent/floci/services/backup/model/BackupPlan.java @@ -6,7 +6,9 @@ import com.fasterxml.jackson.annotation.JsonProperty; import io.quarkus.runtime.annotations.RegisterForReflection; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; @RegisterForReflection @JsonIgnoreProperties(ignoreUnknown = true) @@ -37,6 +39,9 @@ public class BackupPlan { @JsonProperty("Rules") private List rules = new ArrayList<>(); + @JsonProperty("Tags") + private Map tags = new HashMap<>(); + public BackupPlan() {} public String getBackupPlanId() { return backupPlanId; } @@ -60,6 +65,9 @@ public class BackupPlan { public String getVersionId() { return versionId; } public void setVersionId(String versionId) { this.versionId = versionId; } + public Map getTags() { return tags; } + public void setTags(Map tags) { this.tags = tags != null ? new HashMap<>(tags) : new HashMap<>(); } + public List getRules() { return rules; } public void setRules(List rules) { this.rules = rules != null ? rules : new ArrayList<>(); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/backup/BackupPlanConcurrencyTest.java b/src/test/java/io/github/hectorvent/floci/services/backup/BackupPlanConcurrencyTest.java new file mode 100644 index 0000000000..ed8c569a60 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/backup/BackupPlanConcurrencyTest.java @@ -0,0 +1,84 @@ +package io.github.hectorvent.floci.services.backup; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.backup.model.BackupPlan; +import io.github.hectorvent.floci.services.sns.SnsService; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.time.Duration; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeUnit; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +class BackupPlanConcurrencyTest { + @ParameterizedTest + @ValueSource(strings = {"tag", "untag", "update"}) + void overlappingWritesCannotRecreateADeletedPlan(String operation) throws Exception { + String account = "741677900003"; + AccountAwareStorageBackend plans = spy(AccountAwareStorageBackend.inMemory(account)); + StorageFactory storage = mock(StorageFactory.class); + when(storage.create(eq("backup"), anyString(), any())).thenAnswer(call -> + "backup-plans.json".equals(call.getArgument(1)) + ? plans : AccountAwareStorageBackend.inMemory(account)); + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.services().backup().jobCompletionDelaySeconds()).thenReturn(3600); + BackupService service = new BackupService(storage, config, new RegionResolver("eu-west-2", account), + new ObjectMapper(), mock(SnsService.class)); + CountDownLatch writing = new CountDownLatch(1); + CountDownLatch release = new CountDownLatch(1); + try { + BackupPlan plan = service.createBackupPlan("overlap", List.of(), null, Map.of("env", "old"), "eu-west-2"); + String id = plan.getBackupPlanId(); + doAnswer(call -> { + writing.countDown(); + assertTrue(release.await(5, TimeUnit.SECONDS)); + return call.callRealMethod(); + }).when(plans).put(eq(id), any()); + FutureTask write = new FutureTask<>(() -> { + Runnable mutation = switch (operation) { + case "tag" -> () -> service.tagResource(plan.getBackupPlanArn(), Map.of("env", "new")); + case "untag" -> () -> service.untagResource(plan.getBackupPlanArn(), List.of("env")); + case "update" -> () -> service.updateBackupPlan(id, "updated", List.of()); + default -> throw new AssertionError(operation); + }; + mutation.run(); + return null; + }); + Thread writer = new Thread(write, "backup-plan-write"); + writer.setDaemon(true); + writer.start(); + assertTrue(writing.await(5, TimeUnit.SECONDS)); + FutureTask delete = new FutureTask<>(() -> { + service.deleteBackupPlan(id); + return null; + }); + Thread deleter = new Thread(delete, "backup-plan-delete"); + deleter.setDaemon(true); + deleter.start(); + await().atMost(Duration.ofSeconds(5)).until(() -> + deleter.getState() == Thread.State.BLOCKED || delete.isDone()); + release.countDown(); + write.get(5, TimeUnit.SECONDS); + delete.get(5, TimeUnit.SECONDS); + assertEquals("ResourceNotFoundException", assertThrows(AwsException.class, + () -> service.getBackupPlan(id)).getErrorCode()); + assertTrue(service.listBackupPlans().isEmpty()); + } finally { + release.countDown(); + service.shutdown(); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/backup/BackupPlanTagsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/backup/BackupPlanTagsIntegrationTest.java new file mode 100644 index 0000000000..ead5ff258e --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/backup/BackupPlanTagsIntegrationTest.java @@ -0,0 +1,163 @@ +package io.github.hectorvent.floci.services.backup; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.core.common.RequestScopes; +import io.github.hectorvent.floci.services.backup.model.BackupPlan; +import io.github.hectorvent.floci.testing.AwsTestClients; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.Response; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.backup.BackupClient; +import software.amazon.awssdk.services.backup.model.BackupRuleInput; +import software.amazon.awssdk.services.backup.model.CreateBackupPlanResponse; +import software.amazon.awssdk.services.backup.model.ResourceNotFoundException; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.*; + +@QuarkusTest +class BackupPlanTagsIntegrationTest { + private static final String ACCOUNT = "741677900001"; + private static final String REGION = "eu-west-2"; + private final String name = "tags-" + UUID.randomUUID(); + private final List plans = new ArrayList<>(); + private BackupClient client; + + @Inject BackupService service; + @Inject ObjectMapper mapper; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @BeforeEach + void openClient() { + client = AwsTestClients.create(BackupClient.builder(), ACCOUNT, Region.of(REGION)); + client.createBackupVault(request -> request.backupVaultName(name).backupVaultTags(Map.of("vault", "kept"))); + } + + @AfterEach + void cleanUp() { + try { + plans.forEach(id -> client.deleteBackupPlan(request -> request.backupPlanId(id))); + client.deleteBackupVault(request -> request.backupVaultName(name)); + } finally { + client.close(); + } + } + + @Test + void sdkCanTagReadReplaceAndRemoveTagsFromAnExistingPlan() { + CreateBackupPlanResponse plan = create(Map.of()); + String arn = plan.backupPlanArn(); + assertTrue(client.listTags(request -> request.resourceArn(arn)).tags().isEmpty()); + assertDoesNotThrow(() -> client.tagResource(request -> request.resourceArn(arn).tags(Map.of("env", "dev", "keep", "yes")))); + assertEquals(Map.of("env", "dev", "keep", "yes"), client.listTags(request -> request.resourceArn(arn)).tags()); + client.tagResource(request -> request.resourceArn(arn).tags(Map.of("env", "prod", "added", "yes"))); + assertEquals(Map.of("env", "prod", "keep", "yes", "added", "yes"), + client.listTags(request -> request.resourceArn(arn)).tags()); + client.untagResource(request -> request.resourceArn(arn).tagKeyList("keep", "absent")); + assertEquals(Map.of("env", "prod", "added", "yes"), client.listTags(request -> request.resourceArn(arn)).tags()); + client.untagResource(request -> request.resourceArn(arn).tagKeyList("env", "added")); + assertTrue(client.listTags(request -> request.resourceArn(arn)).tags().isEmpty()); + client.deleteBackupPlan(request -> request.backupPlanId(plan.backupPlanId())); + plans.remove(plan.backupPlanId()); + assertThrows(ResourceNotFoundException.class, () -> client.listTags(request -> request.resourceArn(arn))); + assertThrows(ResourceNotFoundException.class, () -> client.tagResource(request -> request.resourceArn(arn).tags(Map.of("env", "new")))); + } + + @Test + void creationTagsSurvivePlanUpdatesAndStoredJsonRoundTrips() throws Exception { + CreateBackupPlanResponse plan = create(Map.of("initial", "kept")); + String arn = plan.backupPlanArn(); + assertEquals(Map.of("initial", "kept"), client.listTags(request -> request.resourceArn(arn)).tags()); + client.updateBackupPlan(request -> request.backupPlanId(plan.backupPlanId()).backupPlan(body -> body + .backupPlanName(name + "-renamed").rules(rule()))); + assertEquals(Map.of("initial", "kept"), client.listTags(request -> request.resourceArn(arn)).tags()); + BackupPlan stored = RequestScopes.callAs(ACCOUNT, REGION, () -> service.getBackupPlan(plan.backupPlanId())); + String json = mapper.writeValueAsString(stored); + assertEquals("kept", mapper.readTree(mapper.writeValueAsString(mapper.readValue(json, BackupPlan.class))) + .path("Tags").path("initial").asText()); + } + + @Test + void anotherAccountOrNonMatchingArnCannotTagThePlan() { + CreateBackupPlanResponse plan = create(Map.of()); + String arn = plan.backupPlanArn(); + try (BackupClient other = AwsTestClients.create(BackupClient.builder(), "741677900002", Region.of(REGION))) { + assertThrows(ResourceNotFoundException.class, () -> other.listTags(request -> request.resourceArn(arn))); + assertThrows(ResourceNotFoundException.class, () -> other.tagResource(request -> request.resourceArn(arn).tags(Map.of("env", "wrong")))); + assertThrows(ResourceNotFoundException.class, () -> other.untagResource(request -> request.resourceArn(arn).tagKeyList("env"))); + } + String wrongRegion = arn.replace(":" + REGION + ":", ":us-east-1:"); + assertThrows(ResourceNotFoundException.class, () -> client.tagResource(request -> request.resourceArn(wrongRegion).tags(Map.of("env", "wrong")))); + assertTrue(client.listTags(request -> request.resourceArn(arn)).tags().isEmpty()); + } + + @Test + void vaultTaggingKeepsItsExistingBehavior() { + String arn = client.describeBackupVault(request -> request.backupVaultName(name)).backupVaultArn(); + client.tagResource(request -> request.resourceArn(arn).tags(Map.of("env", "dev"))); + assertEquals(Map.of("vault", "kept", "env", "dev"), client.listTags(request -> request.resourceArn(arn)).tags()); + client.untagResource(request -> request.resourceArn(arn).tagKeyList("env")); + assertEquals(Map.of("vault", "kept"), client.listTags(request -> request.resourceArn(arn)).tags()); + } + + @ParameterizedTest + @ValueSource(strings = {"{\"env\":123}", "{\"env\":true}", "{\"env\":null}", + "{\"env\":{}}", "{\"env\":[]}", "[]", "123", "\"invalid\""}) + void malformedCreationTagsAreRejectedWithoutCreatingAPlan(String tags) { + Response response = given() + .header("Authorization", "AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20261028/" + + REGION + "/backup/aws4_request") + .contentType("application/json") + .body(""" + {"BackupPlan":{"BackupPlanName":"%s","Rules":[ + {"RuleName":"daily","TargetBackupVaultName":"%s"}]},"BackupPlanTags":%s} + """.formatted(name, name, tags)) + .put("/backup/plans/"); + if (response.statusCode() == 200) { + plans.add(response.jsonPath().getString("BackupPlanId")); + } + assertEquals(400, response.statusCode()); + assertEquals("InvalidParameterValueException", response.jsonPath().getString("__type")); + assertTrue(client.listBackupPlans().backupPlansList().stream() + .noneMatch(plan -> name.equals(plan.backupPlanName()))); + } + + @ParameterizedTest + @ValueSource(strings = {"{}", "{\"Tags\":null}"}) + void legacyStoredPlansHaveMutableEmptyTags(String json) throws Exception { + BackupPlan restored = mapper.readValue(json, BackupPlan.class); + assertTrue(restored.getTags().isEmpty()); + restored.getTags().put("after-restart", "kept"); + assertEquals("kept", mapper.readTree(mapper.writeValueAsString(restored)) + .path("Tags").path("after-restart").asText()); + } + + private CreateBackupPlanResponse create(Map tags) { + CreateBackupPlanResponse plan = client.createBackupPlan(request -> request.backupPlan(body -> body + .backupPlanName(name).rules(rule())).backupPlanTags(tags)); + plans.add(plan.backupPlanId()); + return plan; + } + + private BackupRuleInput rule() { + return BackupRuleInput.builder().ruleName("daily").targetBackupVaultName(name) + .scheduleExpression("cron(0 12 * * ? *)").build(); + } +}