fix(backup): persist and manage backup plan tags (#6796)

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-28 07:08:52 -04:00
1 parent 7332c69c7d
commit a636de20dc
6 files changed
+326 -29

No files matched your search

+7
View File
@@ -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`:
@@ -264,7 +264,8 @@ public class BackupController {
List<BackupRule> 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<String, String> 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<String, String> result = new HashMap<>();
mapNode.properties().forEach(entry -> result.put(entry.getKey(), entry.getValue().textValue()));
return result;
}
private static List<String> readStringList(JsonNode node) {
@@ -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<String, Object> 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<BackupRule> rules,
String creatorRequestId, String region) {
return createBackupPlan(planName, rules, creatorRequestId, Map.of(), region);
}
public BackupPlan createBackupPlan(String planName, List<BackupRule> rules,
String creatorRequestId, Map<String, String> 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<BackupRule> 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<BackupPlan> listBackupPlans() {
@@ -1011,13 +1025,17 @@ public class BackupService {
if (vault.isPresent()) {
return vault.get().getTags();
}
Optional<BackupPlan> 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<String, String> 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<String> 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<BackupRule> rules) {
@@ -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<BackupRule> rules = new ArrayList<>();
@JsonProperty("Tags")
private Map<String, String> 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<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags != null ? new HashMap<>(tags) : new HashMap<>(); }
public List<BackupRule> getRules() { return rules; }
public void setRules(List<BackupRule> rules) { this.rules = rules != null ? rules : new ArrayList<>(); }
}
@@ -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<BackupPlan> 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<Void> 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<Void> 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();
}
}
}
@@ -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<String> 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<String, String> 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();
}
}