fix(apigateway): validate the domain resource owner selector (#9516)

GetDomainNames accepted any resourceOwner. An unknown or empty value now answers HTTP 400 ValidationException with AWS's enum message; an omitted value, SELF and OTHER_ACCOUNTS are still accepted.

Closes #9026
This commit is contained in:
galsuchetzky-act authored and GitHub committed 2026-11-26 08:08:47 -04:00
1 parent 370b49861a
commit 33c843535b
3 files changed
+91 -1

No files matched your search

+3
View File
@@ -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
`<domain>.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
@@ -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<String> 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<CustomDomain> domains = service.getDomainNames(region);
ObjectNode root = objectMapper.createObjectNode();
@@ -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) {}
}