mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(cognito): provision AWS::Cognito::UserPoolDomain (#3005)
* feat(cognito): provision AWS::Cognito::UserPoolDomain The type was a stub: the stack turned green, but Fn::GetAtt CloudFrontDistribution resolved to the literal LogicalId.CloudFrontDistribution, so a Route 53 alias record for a custom domain pointed at that text. CognitoCfnProvisioner creates the domain through the existing CreateUserPoolDomain. Ref is the domain name and CloudFrontDistribution is the CloudFront name DescribeUserPoolDomain reports. A changed Domain or UserPoolId replaces the domain; any other change is applied in place through the new UpdateUserPoolDomain operation, so the distribution a DNS alias points at survives a certificate change, as on AWS. Delete uses the pool id recorded at create time and tolerates an already deleted domain. UpdateUserPoolDomain is also exposed on the Cognito API, with the same response shape as CreateUserPoolDomain. * feat(cognito): treat null domain settings as absent and pin the same-name replacement failure Review follow-ups for AWS::Cognito::UserPoolDomain: - A JSON null inside CustomDomainConfig was copied as the text "null", which would have passed for a certificate ARN. Null values are now skipped, so the service rejects the missing ARN as it should. - ManagedLoginVersion on CreateUserPoolDomain and UpdateUserPoolDomain treated an explicit null as 0, overwriting the stored version. Null now counts as omitted, and a value of another JSON type is a SerializationException, as on AWS. - Moving an unchanged domain to another pool fails on the create, because domain names are unique across pools and the replacement is created before the original is deleted, exactly as on AWS. The unit test that mocked this path as a success is replaced by one that pins the failure and checks the original domain is left in place. * test(cognito): cover domain replacement, prefix domains and SecurityPolicy updates The stack test now also renames the domain through UpdateStack, asserting the old domain is gone and the new one has its own distribution, and a second stack proves a prefix domain resolves Fn::GetAtt CloudFrontDistribution to an empty string rather than the literal attribute name. The service test covers a SecurityPolicy change and that an omitted policy is kept. The in-place check reads ctx.reusesPriorEntity(domain), as the sibling provisioners do. * test(cognito): provision the stack test's certificate through AWS::CertificateManager::Certificate With the ACM provisioner merged, the stack test now declares the certificate as a resource and references it with Fn::GetAtt, the way CDK does. Rotating to a new certificate resource updates the domain in place and removes the old certificate once nothing references it. A wildcard name keeps a domain rename from replacing the certificate.
This commit is contained in:
1 parent
71c7836da1
commit
f3c251ef20
12 files changed
+1020
-12
No files matched your search
+141
@@ -0,0 +1,141 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.cognito.CognitoService;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPoolDomain;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Provisions {@code AWS::Cognito::UserPoolDomain}. The physical id is the domain name, as in AWS,
|
||||
* and {@code Fn::GetAtt CloudFrontDistribution} is the CloudFront name a custom domain's DNS alias
|
||||
* points at. A prefix domain has no distribution of its own in Floci, so for one the attribute is
|
||||
* empty rather than the literal {@code LogicalId.CloudFrontDistribution} an unset attribute yields.
|
||||
*
|
||||
* <p>{@code Routing} (regional failover) is accepted and ignored; nothing in the emulator fails
|
||||
* over. {@code AWS::Cognito::UserPool} and {@code UserPoolClient} still live in the legacy switch.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class CognitoCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CognitoCfnProvisioner.class);
|
||||
private static final String NOT_FOUND = "ResourceNotFoundException";
|
||||
|
||||
private final CognitoService cognitoService;
|
||||
|
||||
public CognitoCfnProvisioner(CognitoService cognitoService) {
|
||||
this.cognitoService = cognitoService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> resourceTypes() {
|
||||
return Set.of("AWS::Cognito::UserPoolDomain");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
String domain = ctx.resolveOptional(props, "Domain");
|
||||
String userPoolId = ctx.resolveOptional(props, "UserPoolId");
|
||||
if (domain == null || domain.isBlank() || userPoolId == null || userPoolId.isBlank()) {
|
||||
throw new IllegalArgumentException("AWS::Cognito::UserPoolDomain requires Domain and UserPoolId");
|
||||
}
|
||||
Map<String, Object> customDomainConfig = resolveCustomDomainConfig(props, ctx);
|
||||
Integer managedLoginVersion = resolveManagedLoginVersion(props, ctx);
|
||||
|
||||
// Domain and UserPoolId are createOnly in the schema: a change to either replaces the
|
||||
// domain, and the prior one is removed once the new one exists. Domain names are unique
|
||||
// across pools, so a change to UserPoolId alone fails on the create, as it does on AWS,
|
||||
// where CloudFormation also creates the replacement before deleting the original. Anything
|
||||
// else, a renewed certificate or another managed login version, is applied in place so the
|
||||
// CloudFront distribution a DNS alias points at survives, as on AWS.
|
||||
UserPoolDomain existing = ctx.isUpdate() ? findExisting(ctx.priorPhysicalId()) : null;
|
||||
UserPoolDomain provisioned;
|
||||
if (existing != null && ctx.reusesPriorEntity(domain) && userPoolId.equals(existing.getUserPoolId())) {
|
||||
provisioned = cognitoService.updateUserPoolDomain(domain, userPoolId, customDomainConfig, managedLoginVersion);
|
||||
} else {
|
||||
provisioned = cognitoService.createUserPoolDomain(domain, userPoolId, customDomainConfig, managedLoginVersion);
|
||||
if (existing != null) {
|
||||
deleteDomain(existing.getDomain(), existing.getUserPoolId());
|
||||
}
|
||||
}
|
||||
r.setPhysicalId(domain);
|
||||
// Recorded so delete can name the owning pool: DeleteUserPoolDomain needs both.
|
||||
r.getAttributes().put("UserPoolId", userPoolId);
|
||||
r.getAttributes().put("CloudFrontDistribution",
|
||||
provisioned.getCloudFrontDistribution() == null ? "" : provisioned.getCloudFrontDistribution());
|
||||
}
|
||||
|
||||
/** Deletes the domain from the pool recorded at create time; without that record, looks the pool up. */
|
||||
@Override
|
||||
public void delete(StackResource resource, String region) {
|
||||
String userPoolId = resource.getAttributes() == null ? null : resource.getAttributes().get("UserPoolId");
|
||||
if (userPoolId == null || userPoolId.isBlank()) {
|
||||
delete(resource.getResourceType(), resource.getPhysicalId(), region);
|
||||
return;
|
||||
}
|
||||
deleteDomain(resource.getPhysicalId(), userPoolId);
|
||||
}
|
||||
|
||||
/** Only the domain itself can say which pool owns it here; one that is already gone needs nothing. */
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
UserPoolDomain existing = findExisting(physicalId);
|
||||
if (existing != null) {
|
||||
deleteDomain(physicalId, existing.getUserPoolId());
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteDomain(String domain, String userPoolId) {
|
||||
CfnDeletes.safeDelete("user pool domain", domain,
|
||||
() -> cognitoService.deleteUserPoolDomain(domain, userPoolId), NOT_FOUND);
|
||||
}
|
||||
|
||||
private UserPoolDomain findExisting(String domain) {
|
||||
try {
|
||||
return cognitoService.describeUserPoolDomain(domain);
|
||||
} catch (AwsException e) {
|
||||
if (!NOT_FOUND.equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
LOG.debugv("User pool domain {0} is gone", domain);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, Object> resolveCustomDomainConfig(JsonNode props, ProvisionContext ctx) {
|
||||
if (props == null || !props.hasNonNull("CustomDomainConfig")) {
|
||||
return null;
|
||||
}
|
||||
JsonNode resolved = ctx.engine().resolveNode(props.get("CustomDomainConfig"));
|
||||
if (resolved == null || !resolved.isObject()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Object> config = new LinkedHashMap<>();
|
||||
resolved.fields().forEachRemaining(field -> {
|
||||
// A JSON null is an absent value, not the text "null" that asText() would make of it.
|
||||
if (!field.getValue().isNull()) {
|
||||
config.put(field.getKey(), field.getValue().asText());
|
||||
}
|
||||
});
|
||||
return config;
|
||||
}
|
||||
|
||||
private static Integer resolveManagedLoginVersion(JsonNode props, ProvisionContext ctx) {
|
||||
String value = ctx.resolveOptional(props, "ManagedLoginVersion");
|
||||
if (value == null || value.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return Integer.valueOf(value.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"AWS::Cognito::UserPoolDomain ManagedLoginVersion must be an integer, got: " + value, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,7 @@ public class CognitoJsonHandler {
|
||||
case "DeleteIdentityProvider" -> handleDeleteIdentityProvider(request);
|
||||
case "CreateUserPoolDomain" -> handleCreateUserPoolDomain(request);
|
||||
case "DescribeUserPoolDomain" -> handleDescribeUserPoolDomain(request);
|
||||
case "UpdateUserPoolDomain" -> handleUpdateUserPoolDomain(request);
|
||||
case "DeleteUserPoolDomain" -> handleDeleteUserPoolDomain(request);
|
||||
case "SetLogDeliveryConfiguration" -> handleSetLogDeliveryConfiguration(request);
|
||||
case "GetLogDeliveryConfiguration" -> handleGetLogDeliveryConfiguration(request);
|
||||
@@ -443,16 +444,46 @@ public class CognitoJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleCreateUserPoolDomain(JsonNode request) {
|
||||
JsonNode customDomainConfigNode = request.path("CustomDomainConfig");
|
||||
Map<String, Object> customDomainConfig = customDomainConfigNode.isObject()
|
||||
? objectMapper.convertValue(customDomainConfigNode, new TypeReference<Map<String, Object>>() {})
|
||||
: null;
|
||||
UserPoolDomain domain = service.createUserPoolDomain(
|
||||
request.path("Domain").asText(),
|
||||
request.path("UserPoolId").asText(),
|
||||
customDomainConfig,
|
||||
request.has("ManagedLoginVersion") ? request.path("ManagedLoginVersion").asInt() : null
|
||||
customDomainConfig(request),
|
||||
managedLoginVersion(request)
|
||||
);
|
||||
return Response.ok(userPoolDomainResultToNode(domain)).build();
|
||||
}
|
||||
|
||||
private Response handleUpdateUserPoolDomain(JsonNode request) {
|
||||
UserPoolDomain domain = service.updateUserPoolDomain(
|
||||
request.path("Domain").asText(),
|
||||
request.path("UserPoolId").asText(),
|
||||
customDomainConfig(request),
|
||||
managedLoginVersion(request)
|
||||
);
|
||||
return Response.ok(userPoolDomainResultToNode(domain)).build();
|
||||
}
|
||||
|
||||
private Map<String, Object> customDomainConfig(JsonNode request) {
|
||||
JsonNode node = request.path("CustomDomainConfig");
|
||||
return node.isObject()
|
||||
? objectMapper.convertValue(node, new TypeReference<Map<String, Object>>() {})
|
||||
: null;
|
||||
}
|
||||
|
||||
/** Absent or null yields null; a value of another JSON type is a SerializationException, as on AWS. */
|
||||
private static Integer managedLoginVersion(JsonNode request) {
|
||||
JsonNode node = request.get("ManagedLoginVersion");
|
||||
if (node == null || node.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!node.isIntegralNumber()) {
|
||||
throw new AwsException("SerializationException", "Expected integer or null", 400);
|
||||
}
|
||||
return node.intValue();
|
||||
}
|
||||
|
||||
/** CreateUserPoolDomain and UpdateUserPoolDomain share this result shape. */
|
||||
private ObjectNode userPoolDomainResultToNode(UserPoolDomain domain) {
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
if (domain.isCustomDomain()) {
|
||||
response.put("CloudFrontDomain", domain.getCloudFrontDistribution());
|
||||
@@ -460,7 +491,7 @@ public class CognitoJsonHandler {
|
||||
if (domain.getManagedLoginVersion() != null) {
|
||||
response.put("ManagedLoginVersion", domain.getManagedLoginVersion());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
return response;
|
||||
}
|
||||
|
||||
private Response handleDescribeUserPoolDomain(JsonNode request) {
|
||||
|
||||
@@ -1123,6 +1123,43 @@ public class CognitoService implements ResourceProvider {
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Domain does not exist", 404));
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes a domain in place: the certificate behind a custom domain ({@code CustomDomainConfig})
|
||||
* or the managed login version. As on AWS the domain keeps its CloudFront distribution, so a
|
||||
* DNS alias pointing at it stays valid. A setting the request omits is left unchanged.
|
||||
*/
|
||||
public UserPoolDomain updateUserPoolDomain(String domain, String userPoolId,
|
||||
Map<String, Object> customDomainConfig, Integer managedLoginVersion) {
|
||||
describeUserPool(userPoolId);
|
||||
UserPoolDomain userPoolDomain = describeUserPoolDomain(domain);
|
||||
if (!userPoolDomain.getUserPoolId().equals(userPoolId)) {
|
||||
throw new AwsException("ResourceNotFoundException", "Domain does not exist", 404);
|
||||
}
|
||||
if (customDomainConfig != null) {
|
||||
if (!userPoolDomain.isCustomDomain()) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"CustomDomainConfig cannot be set on an Amazon Cognito prefix domain", 400);
|
||||
}
|
||||
String certificateArn = (String) customDomainConfig.get("CertificateArn");
|
||||
if (certificateArn == null || certificateArn.isBlank()) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"CertificateArn is required in CustomDomainConfig", 400);
|
||||
}
|
||||
userPoolDomain.setCertificateArn(certificateArn);
|
||||
Object securityPolicy = customDomainConfig.get("SecurityPolicy");
|
||||
if (securityPolicy != null) {
|
||||
userPoolDomain.setSecurityPolicy(securityPolicy.toString());
|
||||
}
|
||||
}
|
||||
if (managedLoginVersion != null) {
|
||||
userPoolDomain.setManagedLoginVersion(managedLoginVersion);
|
||||
}
|
||||
userPoolDomain.setLastModifiedDate(System.currentTimeMillis() / 1000L);
|
||||
domainStore.put(domain, userPoolDomain);
|
||||
LOG.infov("Updated User Pool Domain: {0} for pool {1}", domain, userPoolId);
|
||||
return userPoolDomain;
|
||||
}
|
||||
|
||||
public void deleteUserPoolDomain(String domain, String userPoolId) {
|
||||
UserPoolDomain userPoolDomain = describeUserPoolDomain(domain);
|
||||
if (!userPoolDomain.getUserPoolId().equals(userPoolId)) {
|
||||
|
||||
Reference in new issue
Block a user