diff --git a/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java b/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java index 7ed7c424ae..8400fe5fd4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java @@ -121,9 +121,27 @@ public class DocDbService { } public DocDbCluster getDbCluster(String id) { - return clusters.get(id).orElseThrow(() -> + DocDbCluster cluster = clusters.get(id).orElseThrow(() -> new AwsException("DBClusterNotFoundFault", "DocDB cluster " + id + " not found.", 404)); + if (cluster.getDbClusterArn() == null || cluster.getDbClusterArn().isBlank()) { + cluster.setDbClusterArn(legacyArn("cluster:" + id)); + clusters.put(id, cluster); + } + return cluster; + } + + /** + * The ARN a record written before ARNs were stored should have had. + * + *

The default region rather than the caller's: such a record was created when every ARN was + * built from the default, so that is the region it is in — and giving it the region of whoever + * happens to read it first would let a caller elsewhere claim it. Without an ARN a record + * cannot be told apart from another region's of the same identifier, and cannot be tagged + * through an ARN at all. + */ + private String legacyArn(String resource) { + return regionResolver.buildArn("rds", regionResolver.getDefaultRegion(), resource); } public boolean hasCluster(String id) { @@ -148,7 +166,9 @@ public class DocDbService { /** Refuses a record whose own ARN is not the one that was asked for. */ private static void requireArnNamesRecord(String requested, String stored, String errorCode, String message) { - if (stored != null && !stored.equalsIgnoreCase(requested)) { + // No null allowance: a record read through getDbCluster or getDbInstance has been given + // its ARN if it lacked one, so anything reaching here can be compared. + if (!requested.equalsIgnoreCase(stored)) { throw new AwsException(errorCode, message, 404); } } @@ -239,6 +259,7 @@ public class DocDbService { } clusters.delete(id); + writeLocks.remove("cluster:" + id); LOG.infov("DocDB cluster {0} deleted", id); } } @@ -284,9 +305,14 @@ public class DocDbService { } public DocDbInstance getDbInstance(String id) { - return instances.get(id).orElseThrow(() -> + DocDbInstance instance = instances.get(id).orElseThrow(() -> new AwsException("DBInstanceNotFound", "DocDB instance " + id + " not found.", 404)); + if (instance.getDbInstanceArn() == null || instance.getDbInstanceArn().isBlank()) { + instance.setDbInstanceArn(legacyArn("db:" + id)); + instances.put(id, instance); + } + return instance; } public Collection listDbInstances(String filterId) { @@ -334,6 +360,7 @@ public class DocDbService { } instances.delete(id); + writeLocks.remove("instance:" + id); LOG.infov("DocDB instance {0} deleted", id); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbLegacyRecordArnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbLegacyRecordArnIntegrationTest.java new file mode 100644 index 0000000000..2f7d26d55f --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbLegacyRecordArnIntegrationTest.java @@ -0,0 +1,112 @@ +package io.github.hectorvent.floci.services.docdb; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.QuarkusTestProfile; +import io.quarkus.test.junit.TestProfile; +import jakarta.inject.Inject; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static io.restassured.RestAssured.given; +import static io.restassured.http.ContentType.URLENC; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Records written before ARNs were stored still have to resolve to themselves. + * + *

A tag call is answered for the record its ARN names, which needs the record to have one. A + * cluster persisted by an earlier Floci has none, so the comparison had nothing to compare and the + * call resolved by identifier alone — the hole that check closes, left open for exactly the records + * that predate it. The region such a record belongs to is the configured default, which is the + * region it was in fact created under. + */ +@QuarkusTest +@TestProfile(DocDbLegacyRecordArnIntegrationTest.NoContainersProfile.class) +class DocDbLegacyRecordArnIntegrationTest { + + public static class NoContainersProfile implements QuarkusTestProfile { + @Override + public Map getConfigOverrides() { + return Map.of("floci.services.docdb.mock", "true"); + } + } + + @Inject + DocDbService service; + + private static final String ID = "legacy-arnless-cluster"; + private static final String HOME_ARN = "arn:aws:rds:us-east-1:000000000000:cluster:" + ID; + private static final String FOREIGN_ARN = "arn:aws:rds:eu-west-1:000000000000:cluster:" + ID; + + private static io.restassured.specification.RequestSpecification docdb(String region, String action) { + return given().header("Authorization", + "AWS4-HMAC-SHA256 Credential=test/20260615/" + region + "/docdb/aws4_request, " + + "SignedHeaders=content-type;host, Signature=test") + .contentType(URLENC) + .formParam("Action", action) + .formParam("Version", "2014-10-31"); + } + + /** A record as an earlier Floci wrote it: identifier and status, no ARN. */ + private void seedLegacyCluster() { + service.createDbCluster(ID, "5.0.0", "admin", "secret99password", false); + service.getDbCluster(ID).setDbClusterArn(null); + } + + @Test + void aLegacyRecordIsNotTaggedThroughAnotherRegionsArn() { + seedLegacyCluster(); + + docdb("eu-west-1", "AddTagsToResource") + .formParam("ResourceName", FOREIGN_ARN) + .formParam("Tags.Tag.1.Key", "reached") + .formParam("Tags.Tag.1.Value", "wrong-region") + .when().post("/") + .then() + .statusCode(404) + .body(containsString("DBClusterNotFoundFault")); + + docdb("eu-west-1", "ListTagsForResource") + .formParam("ResourceName", FOREIGN_ARN) + .when().post("/") + .then().statusCode(404); + + // The record kept its own tags, and gained the ARN it should always have had — the + // default region's, not the region of whoever happened to read it first. + docdb("us-east-1", "ListTagsForResource") + .formParam("ResourceName", HOME_ARN) + .when().post("/") + .then() + .statusCode(200) + .body(not(containsString("wrong-region"))); + + assertEquals(HOME_ARN, service.getDbCluster(ID).getDbClusterArn()); + + service.deleteDbCluster(ID); + } + + @Test + void aLegacyRecordCanBeTaggedThroughItsOwnArn() { + // The backfill has to make the record usable, not merely refuse the wrong ARN. + seedLegacyCluster(); + + docdb("us-east-1", "AddTagsToResource") + .formParam("ResourceName", HOME_ARN) + .formParam("Tags.Tag.1.Key", "env") + .formParam("Tags.Tag.1.Value", "upgraded") + .when().post("/") + .then().statusCode(200); + + docdb("us-east-1", "ListTagsForResource") + .formParam("ResourceName", HOME_ARN) + .when().post("/") + .then() + .statusCode(200) + .body(containsString("upgraded")); + + service.deleteDbCluster(ID); + } +}