mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(apigatewayv2): support custom domain names and API mappings (#2390)
* feat(apigatewayv2): support custom domain names and API mappings POST /v2/domainnames was not routed, so it matched the S3 controller's catch-all and answered with an S3 XML error about multipart uploads, which an SDK expecting JSON cannot deserialize. The same was true of the API mappings a domain is used through. Adds the eight actions over the records the REST endpoints already keep, since a custom domain is one resource in AWS reachable through both APIs. A domain created here is therefore visible to the v1 endpoints and resolves through the Host-header routing that already exists: - CreateDomainName, GetDomainName, GetDomainNames, DeleteDomainName - CreateApiMapping, GetApiMapping, GetApiMappings, DeleteApiMapping - the mapping id is derived from what identifies the mapping, so it survives a restart and is the same id whichever API created it Taken from a live AWS account rather than the service model alone, which the model does not tell you: - every domain reports RoutingMode API_MAPPING_ONLY and an ipv4 IpAddressType - a create without exactly one configuration is "Invalid input. Expected one domain name configuration" - a duplicate domain is BadRequestException, not a conflict — for the REST API too, so the shared code and the test that asserted 409 were both wrong - deleting an API that a domain still maps to is refused: "Please remove all API mappings for the API from your custom domain names" - an unknown domain is "Invalid domain name identifier specified" Tags are stored and returned rather than dropped, which would have left terraform with a diff it could never settle. Mutual TLS, ownership verification certificates, routing rules and non-ipv4 address types are refused rather than accepted and ignored, so nothing is answered with a domain that behaves differently from the one that was asked for. * fix(apigatewayv2): make api mapping ids unique and validate what a mapping points at Five problems from review of the previous commit. Mapping ids were derived by hashing the base path, so two keys sharing a Java hash shared an id and a read or delete by that id picked between them. The base path is encoded rather than hashed, which cannot collide. An omitted mapping key and an explicit empty one produced two different records, where the shared store keeps the root mapping under "(none)" and could no longer find either from v1. Both spellings now canonicalise to the one record, and a second attempt is the ConflictException AWS answers: "ApiMapping key already exists for this domain name". A mapping was accepted for an API or stage that does not exist, returning 201 for something that routes nowhere. Both are looked up first, and answer as AWS does: "Invalid API identifier specified: <id>" and "Invalid stage identifier specified". The delete guard scanned only the caller's region, so an API could still be deleted out from under a mapping made in another region. It scans every region, since a mapping is keyed under the region of its domain rather than of its API. A domainNameConfigurations entry that was not an object passed the size check and then failed an unchecked cast, answering 500 instead of the AWS-shaped 400. * fix(apigateway): canonicalise base paths on write, key mapping ids off what is stored Canonicalising only where the v2 mapping id was derived left the collision it was meant to remove: the store could hold "(none)", "/" and "" as separate records that all mean the root, and folding them together at id time gave them one id for a read or a delete to choose between. Base paths are canonicalised where they are written instead, matching what reads have always done. The store can no longer gain a second record meaning the root, and a v1 mapping created as "" can now be read back as "", which it could not before: the write kept the raw value while the read looked for the canonical one. The id is derived from the path a record is stored under rather than its canonical form, so state persisted before this change keeps one id per record rather than several records sharing one. No endpoint can create such a record now, so that property is asserted directly against the records deserialization can restore. * fix(apigateway): delete the mapping record that was selected, not its canonical twin The v2 endpoints select a mapping by an id derived from the path its record is stored under, then deleted it through the lookup that normalises the root. For a record left under "/" or "" by state written before writes were canonicalised, that deleted the canonical root instead and left the selected record in place. Deletion by record now addresses the stored path exactly, so the record that answered the lookup is the record that goes. The normalising delete stays for callers that pass a key to look one up by, which is what the REST endpoint does. * fix(apigatewayv2): identify a mapping by its stored key, not by the field it reports BasePathMapping normalises an empty base path to "(none)" in its constructor, so a record the old write path left under the key "" reports "(none)" from its own field. Deriving the mapping id from that field gave it the canonical root's id, and deleting by it removed the canonical root while leaving the record behind — the collision the previous commit was meant to close, through the one door it left open. The service exposes mappings keyed by the base path each record is stored under, and the v2 endpoints derive ids, resolve lookups and delete through that key. The key is the only thing that identifies a record; the field can disagree with it. Seeded through the storage backend the service uses, since no endpoint creates such a record any more: the test asserts the two records keep separate ids, each id resolves to its own record, and deleting one leaves the other.
This commit is contained in:
1 parent
48880aa79a
commit
74da157681
9 files changed
+1024
-8
No files matched your search
+3
-2
@@ -210,13 +210,14 @@ class ApiGatewayCustomDomainIntegrationTest {
|
||||
@Test @Order(14)
|
||||
void createDuplicateDomainName_rejected() {
|
||||
// Domain names are globally unique across regions — creating the same
|
||||
// domain name again (even implicitly in the same region) must fail
|
||||
// domain name again (even implicitly in the same region) must fail.
|
||||
// Verified against AWS: both the REST and HTTP APIs answer BadRequestException, not a conflict
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"domainName\":\"api.example.com\",\"certificateArn\":\"arn:aws:acm:us-east-1:123456789012:certificate/dup\"}")
|
||||
.when().post("/domainnames")
|
||||
.then()
|
||||
.statusCode(409);
|
||||
.statusCode(400);
|
||||
}
|
||||
|
||||
@Test @Order(15)
|
||||
|
||||
+439
@@ -0,0 +1,439 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
/**
|
||||
* Custom domain names and API mappings over the HTTP API (v2) endpoints.
|
||||
*
|
||||
* <p>Before these routes existed, {@code POST /v2/domainnames} matched the S3 controller's
|
||||
* catch-all instead and came back as an S3 XML error about multipart uploads, which an SDK
|
||||
* expecting JSON cannot even deserialize.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class ApiGatewayV2DomainNameIntegrationTest {
|
||||
|
||||
private static final String DOMAIN = "v2-domain-test.example.com";
|
||||
private static String apiId;
|
||||
private static String apiMappingId;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createDomainNameIsRoutedToApiGateway() {
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"domainName":"%s","domainNameConfigurations":[
|
||||
{"certificateArn":"arn:aws:acm:us-east-1:000000000000:certificate/abc",
|
||||
"endpointType":"REGIONAL","securityPolicy":"TLS_1_2"}]}
|
||||
""".formatted(DOMAIN))
|
||||
.when()
|
||||
.post("/v2/domainnames")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("domainName", is(DOMAIN))
|
||||
.body("domainNameArn", containsString("/domainnames/" + DOMAIN))
|
||||
.body("apiMappingSelectionExpression", is("$request.basepath"))
|
||||
.body("domainNameConfigurations[0].endpointType", is("REGIONAL"))
|
||||
.body("domainNameConfigurations[0].securityPolicy", is("TLS_1_2"))
|
||||
.body("domainNameConfigurations[0].domainNameStatus", is("AVAILABLE"))
|
||||
.body("domainNameConfigurations[0].apiGatewayDomainName", notNullValue())
|
||||
.body("domainNameConfigurations[0].hostedZoneId", notNullValue())
|
||||
.body("domainNameConfigurations[0].certificateArn", containsString("certificate/abc"))
|
||||
.body("domainNameConfigurations[0].ipAddressType", is("ipv4"))
|
||||
.body("routingMode", is("API_MAPPING_ONLY"))
|
||||
// The symptom this replaces: an S3 error for a call S3 was never asked to serve.
|
||||
.body(not(containsString("uploads")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void duplicateDomainNameIsABadRequest() {
|
||||
// Verified against AWS, which answers BadRequestException here rather than a conflict.
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"domainName":"%s","domainNameConfigurations":[{"endpointType":"REGIONAL"}]}
|
||||
""".formatted(DOMAIN))
|
||||
.when()
|
||||
.post("/v2/domainnames")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("already exists"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void aDomainNameConfigurationIsRequired() {
|
||||
// AWS: "Invalid input. Expected one domain name configuration" — the REST API takes none,
|
||||
// so the two endpoints do not share this rule.
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"domainName\":\"no-configuration.example.com\"}")
|
||||
.when()
|
||||
.post("/v2/domainnames")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("Expected one domain name configuration"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void tagsAreKeptRatherThanDropped() {
|
||||
// Dropping them would leave terraform with a diff it can never settle.
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"domainName":"tagged.example.com",
|
||||
"domainNameConfigurations":[{"endpointType":"REGIONAL"}],
|
||||
"tags":{"env":"prod"}}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/domainnames")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("tags.env", is("prod"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/tagged.example.com")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("tags.env", is("prod"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void anUnsupportedRoutingModeIsRefused() {
|
||||
// floci does not emulate routing rules, so it says so rather than quietly creating a
|
||||
// domain that routes by API mapping regardless.
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"domainName":"routing.example.com",
|
||||
"domainNameConfigurations":[{"endpointType":"REGIONAL"}],
|
||||
"routingMode":"ROUTING_RULE_ONLY"}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/domainnames")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("API_MAPPING_ONLY"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void getAndListDomainNames() {
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("domainName", is(DOMAIN));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("items.domainName", hasItemEqualTo(DOMAIN));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void domainIsTheSameResourceThroughBothApis() {
|
||||
// AWS exposes one custom domain through both APIs, so what v2 created v1 must also see.
|
||||
given()
|
||||
.when()
|
||||
.get("/domainnames/" + DOMAIN)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("domainName", is(DOMAIN));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void createApiMapping() {
|
||||
apiId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"name\":\"v2-domain-api\",\"protocolType\":\"HTTP\"}")
|
||||
.when()
|
||||
.post("/v2/apis")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("apiId");
|
||||
|
||||
// A mapping needs a stage that exists, so the API gets one before it is mapped.
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"stageName\":\"$default\"}")
|
||||
.when()
|
||||
.post("/v2/apis/" + apiId + "/stages")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
apiMappingId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"apiId\":\"%s\",\"stage\":\"$default\",\"apiMappingKey\":\"orders\"}".formatted(apiId))
|
||||
.when()
|
||||
.post("/v2/domainnames/" + DOMAIN + "/apimappings")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("apiId", is(apiId))
|
||||
.body("stage", is("$default"))
|
||||
.body("apiMappingKey", is("orders"))
|
||||
.body("apiMappingId", notNullValue())
|
||||
.extract().path("apiMappingId");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void aMappingNeedsAnApiAndStageThatExist() {
|
||||
// Verified against AWS: both are BadRequestException rather than a 201 for a mapping that
|
||||
// routes nowhere.
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"apiId\":\"zzzzzzzzzz\",\"stage\":\"$default\"}")
|
||||
.when()
|
||||
.post("/v2/domainnames/" + DOMAIN + "/apimappings")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("Invalid API identifier specified"));
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"apiId\":\"%s\",\"stage\":\"nosuchstage\"}".formatted(apiId))
|
||||
.when()
|
||||
.post("/v2/domainnames/" + DOMAIN + "/apimappings")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("Invalid stage identifier specified"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void anOmittedAndAnEmptyMappingKeyAreTheSameMapping() {
|
||||
// AWS answers the second with ConflictException, so the two spellings must land on one
|
||||
// record rather than creating two apparent root mappings.
|
||||
String rootMapping = "{\"apiId\":\"%s\",\"stage\":\"$default\"}".formatted(apiId);
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body(rootMapping)
|
||||
.when()
|
||||
.post("/v2/domainnames/" + DOMAIN + "/apimappings")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("apiMappingKey", is(""));
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"apiId\":\"%s\",\"stage\":\"$default\",\"apiMappingKey\":\"\"}".formatted(apiId))
|
||||
.when()
|
||||
.post("/v2/domainnames/" + DOMAIN + "/apimappings")
|
||||
.then()
|
||||
.statusCode(409)
|
||||
.body(containsString("already exists"));
|
||||
|
||||
// And the root mapping is reachable through the v1 endpoint, which spells it "(none)".
|
||||
given()
|
||||
.when()
|
||||
.get("/domainnames/" + DOMAIN + "/basepathmappings/(none)")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void rootSpellingsCollapseToOneRecordOnWrite() {
|
||||
// The read path has always normalised the root to "(none)"; the write path now does too, so
|
||||
// "/" cannot end up stored beside it as a second record that means the same thing — and a
|
||||
// mapping created as "" can be read back as "", which it could not before.
|
||||
String otherDomain = "root-spelling.example.com";
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"domainName":"%s","domainNameConfigurations":[{"endpointType":"REGIONAL"}]}
|
||||
""".formatted(otherDomain))
|
||||
.when()
|
||||
.post("/v2/domainnames")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"restApiId\":\"%s\",\"stage\":\"$default\",\"basePath\":\"\"}".formatted(apiId))
|
||||
.when()
|
||||
.post("/domainnames/" + otherDomain + "/basepathmappings")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/domainnames/" + otherDomain + "/basepathmappings/(none)")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// One record, whichever spelling created it.
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + otherDomain + "/apimappings")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("items.size()", is(1))
|
||||
.body("items[0].apiMappingKey", is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void mappingIdsDoNotCollide() {
|
||||
// Ids derived by hashing would collide: "Aa" and "BB" share a Java String hashCode, so a
|
||||
// read or delete by that id would pick between the two mappings arbitrarily.
|
||||
String first = createMapping("Aa");
|
||||
String second = createMapping("BB");
|
||||
org.junit.jupiter.api.Assertions.assertNotEquals(first, second);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN + "/apimappings/" + first)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("apiMappingKey", is("Aa"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN + "/apimappings/" + second)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("apiMappingKey", is("BB"));
|
||||
}
|
||||
|
||||
private String createMapping(String key) {
|
||||
return given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{\"apiId\":\"%s\",\"stage\":\"$default\",\"apiMappingKey\":\"%s\"}".formatted(apiId, key))
|
||||
.when()
|
||||
.post("/v2/domainnames/" + DOMAIN + "/apimappings")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("apiMappingId");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void anApiStillMappedToADomainCannotBeDeleted() {
|
||||
// Verified against AWS, which refuses rather than leaving the mapping pointing at an API
|
||||
// that no longer exists.
|
||||
given()
|
||||
.when()
|
||||
.delete("/v2/apis/" + apiId)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("remove all API mappings"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/apis/" + apiId)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void inputsFlociCannotHonourAreRefused() {
|
||||
// Accepting these would answer with a domain that behaves differently from the request.
|
||||
String[] bodies = {
|
||||
"""
|
||||
{"domainName":"mtls.example.com","domainNameConfigurations":[{"endpointType":"REGIONAL"}],
|
||||
"mutualTlsAuthentication":{"truststoreUri":"s3://bucket/truststore.pem"}}""",
|
||||
"""
|
||||
{"domainName":"dualstack.example.com",
|
||||
"domainNameConfigurations":[{"endpointType":"REGIONAL","ipAddressType":"dualstack"}]}""",
|
||||
"""
|
||||
{"domainName":"ownership.example.com","domainNameConfigurations":[
|
||||
{"endpointType":"REGIONAL","ownershipVerificationCertificateArn":"arn:aws:acm:us-east-1:0:certificate/o"}]}"""
|
||||
};
|
||||
for (String body : bodies) {
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/v2/domainnames")
|
||||
.then()
|
||||
.statusCode(400);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void getApiMappingByItsId() {
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN + "/apimappings/" + apiMappingId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("apiMappingId", is(apiMappingId))
|
||||
.body("apiId", is(apiId));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN + "/apimappings")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("items.apiMappingId", hasItemEqualTo(apiMappingId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void unknownApiMappingIsNotFound() {
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN + "/apimappings/does-not-exist")
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void deleteApiMappingThenDomainName() {
|
||||
given()
|
||||
.when()
|
||||
.delete("/v2/domainnames/" + DOMAIN + "/apimappings/" + apiMappingId)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN + "/apimappings/" + apiMappingId)
|
||||
.then()
|
||||
.statusCode(404);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.delete("/v2/domainnames/" + DOMAIN)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN)
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
private static org.hamcrest.Matcher<Iterable<? super String>> hasItemEqualTo(String value) {
|
||||
return org.hamcrest.Matchers.hasItem(equalTo(value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
/**
|
||||
* The v2 mapping id has to name exactly one stored record.
|
||||
*
|
||||
* <p>It is derived from the base path a record is stored under, which is the only thing that
|
||||
* identifies it. Not its canonical form, and not the record's own field: writes canonicalise now,
|
||||
* so no endpoint creates a record under "/" or "", but state written before that can hold one — and
|
||||
* `BasePathMapping` normalises an empty base path to "(none)" in its constructor, so such a record
|
||||
* reports "(none)" while living under the key "". Deriving identity from either would give several
|
||||
* records one id, and a read or a delete would pick between them.
|
||||
*/
|
||||
class ApiMappingIdTest {
|
||||
|
||||
@Test
|
||||
void rootLikeKeysThatWerePersistedSeparatelyKeepSeparateIds() {
|
||||
String canonical = ApiGatewayController.apiMappingId("(none)");
|
||||
String slash = ApiGatewayController.apiMappingId("/");
|
||||
String empty = ApiGatewayController.apiMappingId("");
|
||||
|
||||
assertNotEquals(canonical, slash);
|
||||
assertNotEquals(canonical, empty);
|
||||
assertNotEquals(slash, empty);
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyIdIsNonEmptyAndStable() {
|
||||
// A record stored under an empty key must still produce an id a caller can put in a URL.
|
||||
assertFalse(ApiGatewayController.apiMappingId("").isEmpty());
|
||||
assertFalse(ApiGatewayController.apiMappingId(null).isEmpty());
|
||||
|
||||
assertEquals(ApiGatewayController.apiMappingId("orders"),
|
||||
ApiGatewayController.apiMappingId("orders"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void distinctKeysSharingAJavaHashKeepDistinctIds() {
|
||||
assertEquals("Aa".hashCode(), "BB".hashCode());
|
||||
assertNotEquals(ApiGatewayController.apiMappingId("Aa"),
|
||||
ApiGatewayController.apiMappingId("BB"));
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* Deleting a mapping by the record a caller already holds must remove that record and no other.
|
||||
*
|
||||
* <p>The v2 endpoints select a mapping by an id derived from the path it is stored under, then
|
||||
* delete it. Going back through the normalising lookup would turn a record stored as "/" or "" —
|
||||
* which state written before writes were canonicalised can still hold — into the canonical root,
|
||||
* deleting the wrong record and leaving the selected one behind.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class BasePathMappingRecordDeletionTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@Inject
|
||||
ApiGatewayService service;
|
||||
|
||||
private String domainWithRootMapping(String domainName) {
|
||||
service.createDomainName(REGION, Map.of("domainName", domainName));
|
||||
service.createBasePathMapping(REGION, domainName,
|
||||
Map.of("restApiId", "api123", "stage", "prod"));
|
||||
return domainName;
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletingByARootLikePathDoesNotRemoveTheCanonicalRecord() {
|
||||
String domain = domainWithRootMapping("record-deletion.example.com");
|
||||
|
||||
// No record is stored under "/", so this must find nothing rather than normalise its way
|
||||
// onto the canonical root.
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> service.deleteBasePathMappingRecord(REGION, domain, "/"));
|
||||
assertEquals("NotFoundException", e.getErrorCode());
|
||||
|
||||
assertNotNull(service.getBasePathMapping(REGION, domain, "(none)"),
|
||||
"the canonical root mapping was deleted by a request for another record");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletingByTheStoredPathRemovesThatRecord() {
|
||||
String domain = domainWithRootMapping("record-deletion-2.example.com");
|
||||
|
||||
service.deleteBasePathMappingRecord(REGION, domain, "(none)");
|
||||
|
||||
assertThrows(AwsException.class, () -> service.getBasePathMapping(REGION, domain, "(none)"));
|
||||
}
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.BasePathMapping;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
/**
|
||||
* Mappings written before base paths were canonicalised on write.
|
||||
*
|
||||
* <p>Such a record sits under the key {@code ""} while reporting {@code (none)} from its own field,
|
||||
* because {@link BasePathMapping} normalises in its constructor. No endpoint can create one now, so
|
||||
* the state is seeded through the same storage backend the service uses — the factory hands out one
|
||||
* backend per file.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class LegacyBasePathMappingIntegrationTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String DOMAIN = "legacy-mapping.example.com";
|
||||
|
||||
@Inject
|
||||
StorageFactory storageFactory;
|
||||
|
||||
@Inject
|
||||
ApiGatewayService service;
|
||||
|
||||
private StorageBackend<String, BasePathMapping> mappings() {
|
||||
return storageFactory.create("apigateway", "apigateway-mappings.json",
|
||||
new TypeReference<Map<String, BasePathMapping>>() {});
|
||||
}
|
||||
|
||||
private void seedLegacyState() {
|
||||
service.createDomainName(REGION, Map.of("domainName", DOMAIN));
|
||||
// The canonical root, as writes produce it today.
|
||||
service.createBasePathMapping(REGION, DOMAIN, Map.of("restApiId", "api-canonical", "stage", "prod"));
|
||||
// And one the old write path left under the empty key; its field still reads "(none)".
|
||||
mappings().put(REGION + "::" + DOMAIN + "::", new BasePathMapping("", "api-legacy", "prod"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void eachRecordKeepsItsOwnIdAndDeletesItself() {
|
||||
seedLegacyState();
|
||||
|
||||
String canonicalId = ApiGatewayController.apiMappingId("(none)");
|
||||
String legacyId = ApiGatewayController.apiMappingId("");
|
||||
assertNotEquals(canonicalId, legacyId);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN + "/apimappings")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("items.apiMappingId", hasItem(canonicalId))
|
||||
.body("items.apiMappingId", hasItem(legacyId));
|
||||
|
||||
// Each id resolves to its own record rather than to whichever matched first.
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN + "/apimappings/" + legacyId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("apiId", is("api-legacy"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN + "/apimappings/" + canonicalId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("apiId", is("api-canonical"));
|
||||
|
||||
// Deleting the legacy record removes that record, not the canonical root beside it.
|
||||
given()
|
||||
.when()
|
||||
.delete("/v2/domainnames/" + DOMAIN + "/apimappings/" + legacyId)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN + "/apimappings/" + legacyId)
|
||||
.then()
|
||||
.statusCode(404);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/v2/domainnames/" + DOMAIN + "/apimappings/" + canonicalId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("apiId", is("api-canonical"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user