diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index 14f7800d2e..208f4d4bdd 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -312,6 +312,9 @@ and [response contract](https://docs.aws.amazon.com/apigateway/latest/developerg ### Custom Domain Names +`GetDomainNames` accepts the `resourceOwner` values `SELF` and `OTHER_ACCOUNTS`. +Other supplied values, including an empty value, return `ValidationException`. + A domain created with `CreateDomainName` is `AVAILABLE` at once. Its `regionalDomainName` is `.regional.local`, and a request whose `Host` header is either that name or the domain itself is routed through the domain's base path mappings to the mapped REST API stage. An diff --git a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java index af0d651ff3..09eed63c68 100644 --- a/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java +++ b/src/main/java/io/floci/aws/services/apigateway/ApiGatewayController.java @@ -58,6 +58,7 @@ import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.core.UriInfo; import java.io.IOException; import java.time.Instant; @@ -1315,7 +1316,14 @@ public class ApiGatewayController { @GET @Path("/domainnames") - public Response getDomainNames(@Context HttpHeaders headers) { + public Response getDomainNames(@Context HttpHeaders headers, + @Context UriInfo uriInfo) { + String resourceOwner = uriInfo.getQueryParameters().getFirst("resourceOwner"); + List owners = List.of("SELF", "OTHER_ACCOUNTS"); + if (resourceOwner != null && !owners.contains(resourceOwner)) { + throw SmithyValidationErrors.validationException( + SmithyValidationErrors.notInEnum(resourceOwner, "resourceOwner", owners)); + } String region = regionResolver.resolveRegion(headers); List domains = service.getDomainNames(region); ObjectNode root = objectMapper.createObjectNode(); diff --git a/src/test/java/io/floci/aws/services/apigateway/ApiGatewayDomainResourceOwnerValidationIntegrationTest.java b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayDomainResourceOwnerValidationIntegrationTest.java new file mode 100644 index 0000000000..3915d18fe8 --- /dev/null +++ b/src/test/java/io/floci/aws/services/apigateway/ApiGatewayDomainResourceOwnerValidationIntegrationTest.java @@ -0,0 +1,79 @@ +package io.floci.aws.services.apigateway; + +import io.floci.aws.testing.AwsTestClients; +import io.floci.aws.testing.PartitionCleanup; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.apigateway.ApiGatewayClient; +import software.amazon.awssdk.services.apigateway.model.ApiGatewayException; +import software.amazon.awssdk.services.apigateway.model.EndpointType; + +import java.util.UUID; +import java.util.concurrent.ThreadLocalRandom; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class ApiGatewayDomainResourceOwnerValidationIntegrationTest { + + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + + @Test + void unknownResourceOwnerIsRejected() { + assertInvalidOwner("unknown"); + } + + @Test + void emptyResourceOwnerIsRejected() { + assertInvalidOwner(""); + } + + @Test + void omittedResourceOwnerKeepsTheExistingDomainListing() { + Fixture fixture = fixture(); + assertTrue(fixture.client().getDomainNames().items().stream() + .anyMatch(domain -> fixture.domain().equals(domain.domainName()))); + } + + @Test + void selfRemainsAnAcceptedSelector() { + Fixture fixture = fixture(); + assertTrue(fixture.client().getDomainNames(r -> r.resourceOwner("SELF")).sdkHttpResponse().isSuccessful()); + } + + @Test + void otherAccountsRemainsAnAcceptedSelector() { + Fixture fixture = fixture(); + assertTrue(fixture.client().getDomainNames(r -> r.resourceOwner("OTHER_ACCOUNTS")) + .sdkHttpResponse().isSuccessful()); + } + + private void assertInvalidOwner(String owner) { + Fixture fixture = fixture(); + ApiGatewayException error = assertThrows(ApiGatewayException.class, + () -> fixture.client().getDomainNames(r -> r.resourceOwner(owner))); + assertEquals(400, error.statusCode()); + assertEquals("ValidationException", error.awsErrorDetails().errorCode()); + assertEquals("1 validation error detected: Value '" + owner + "' at 'resourceOwner' failed to satisfy " + + "constraint: Member must satisfy enum value set: [SELF, OTHER_ACCOUNTS]", + error.awsErrorDetails().errorMessage()); + } + + private Fixture fixture() { + String account = Long.toString(ThreadLocalRandom.current().nextLong(100000000000L, 999999999999L)); + ApiGatewayClient client = AwsTestClients.create(ApiGatewayClient.builder(), account, Region.US_EAST_1); + cleanup.register(client::close); + String domain = "owner-validation-" + UUID.randomUUID() + ".example.com"; + client.createDomainName(r -> r.domainName(domain) + .endpointConfiguration(c -> c.types(EndpointType.REGIONAL))); + cleanup.register(() -> client.deleteDomainName(r -> r.domainName(domain))); + return new Fixture(client, domain); + } + + private record Fixture(ApiGatewayClient client, String domain) {} +}