feat(cloudformation): provision Cognito user pool identity providers (#7350)

This commit is contained in:
Ronald Li 🛠️ authored and GitHub committed 2026-11-03 08:21:09 -04:00
1 parent 121dc22563
commit d0747f26d1
15 files changed
+1617 -345

No files matched your search

@@ -45,6 +45,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudWatc
import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudWatchCompositeAlarmCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudWatchDashboardCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.CognitoCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.CognitoIdentityProviderCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.CognitoResourceServerCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.CognitoUserPoolGroupCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.CognitoUserPoolUserCfnProvisioner;
@@ -355,6 +356,7 @@ final class CfnProvisionerFixture {
}
if (cognitoService != null) {
discovered.add(new CognitoCfnProvisioner(cognitoService));
discovered.add(new CognitoIdentityProviderCfnProvisioner(cognitoService));
discovered.add(new CognitoResourceServerCfnProvisioner(cognitoService));
discovered.add(new CognitoUserPoolGroupCfnProvisioner(cognitoService));
discovered.add(new CognitoUserPoolUserCfnProvisioner(cognitoService));
@@ -0,0 +1,287 @@
package io.github.hectorvent.floci.services.cloudformation;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.testing.CfnStackWaits;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
import static io.github.hectorvent.floci.testing.CfnRequests.cfn;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@QuarkusTest
class CognitoIdentityProviderCfnIntegrationTest {
private static final String CFN_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20261101/us-east-1/cloudformation/aws4_request";
private static final String SM_CONTENT_TYPE = "application/x-amz-json-1.1";
private static final String NAME = "ExampleOIDC";
private final ObjectMapper mapper = new ObjectMapper();
private final List<String> poolIds = new ArrayList<>();
private final List<String> secretIds = new ArrayList<>();
private final String stack = "cognito-idp-" + Long.toString(System.nanoTime(), 36);
private boolean createdStack;
@BeforeAll
static void configureAwsContentTypes() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@AfterEach
void cleanup() {
if (createdStack) {
cloudFormation("DeleteStack", null);
CfnStackWaits.awaitStackDeleted(stack);
}
for (String pool : poolIds) {
cognitoAction("DeleteUserPool", "{\"UserPoolId\":\"" + pool + "\"}").then().statusCode(200);
}
for (String secret : secretIds) {
given().header("X-Amz-Target", "secretsmanager.DeleteSecret").contentType(SM_CONTENT_TYPE)
.body(mapper.createObjectNode().put("SecretId", secret)
.put("ForceDeleteWithoutRecovery", true).toString())
.when().post("/").then().statusCode(200);
}
}
@Test
void stackCreatesUpdatesReplacesAndDeletesTheBackingProvider() throws Exception {
String poolA = createPool();
String poolB = createPool();
createStack(template(poolA, NAME, "OIDC", "original-client"));
assertEquals(NAME, output("ProviderRef"));
JsonNode created = provider(poolA, NAME);
assertEquals("OIDC", created.path("ProviderType").asText());
assertEquals("original-client", created.path("ProviderDetails").path("client_id").asText());
assertEquals("email", created.path("AttributeMapping").path("email").asText());
updateStack(template(poolA, NAME, "OIDC", "updated-client"), "UPDATE_COMPLETE");
assertEquals(NAME, output("ProviderRef"));
assertClientId(poolA, NAME, "updated-client");
updateStack(template(poolA, "RenamedOIDC", "OIDC", "updated-client"), "UPDATE_COMPLETE");
assertEquals("RenamedOIDC", output("ProviderRef"));
assertProviderGone(poolA, NAME);
assertClientId(poolA, "RenamedOIDC", "updated-client");
updateStack(template(poolB, "RenamedOIDC", "OIDC", "moved-client"), "UPDATE_COMPLETE");
assertEquals("RenamedOIDC", output("ProviderRef"));
assertProviderGone(poolA, "RenamedOIDC");
assertClientId(poolB, "RenamedOIDC", "moved-client");
cloudFormation("DeleteStack", null);
CfnStackWaits.awaitStackDeleted(stack);
createdStack = false;
assertProviderGone(poolB, "RenamedOIDC");
}
@Test
void aUserPoolClientListsTheProviderThroughItsRef() throws Exception {
ObjectNode template = mapper.createObjectNode();
ObjectNode resources = template.putObject("Resources");
resources.putObject("Pool").put("Type", "AWS::Cognito::UserPool")
.putObject("Properties").put("UserPoolName", stack);
ObjectNode idp = resources.putObject("Idp");
idp.put("Type", "AWS::Cognito::UserPoolIdentityProvider");
ObjectNode idpProperties = providerProperties(NAME, "OIDC", "example-client");
idpProperties.putObject("UserPoolId").put("Ref", "Pool");
idp.set("Properties", idpProperties);
ObjectNode client = resources.putObject("Client");
client.put("Type", "AWS::Cognito::UserPoolClient");
ObjectNode clientProperties = client.putObject("Properties");
clientProperties.putObject("UserPoolId").put("Ref", "Pool");
clientProperties.put("ClientName", "federated");
clientProperties.putArray("SupportedIdentityProviders").addObject().put("Ref", "Idp");
ObjectNode outputs = template.putObject("Outputs");
outputs.putObject("PoolId").putObject("Value").put("Ref", "Pool");
outputs.putObject("ClientId").putObject("Value").put("Ref", "Client");
createStack(template);
String pool = output("PoolId");
assertClientId(pool, NAME, "example-client");
JsonNode described = cognitoJson("DescribeUserPoolClient", mapper.createObjectNode()
.put("UserPoolId", pool).put("ClientId", output("ClientId")).toString()).path("UserPoolClient");
assertEquals(NAME, described.path("SupportedIdentityProviders").get(0).asText());
}
@Test
void clientSecretResolvesFromASecretsManagerDynamicReference() throws Exception {
String pool = createPool();
String secret = stack + "-oidc";
given().header("X-Amz-Target", "secretsmanager.CreateSecret").contentType(SM_CONTENT_TYPE)
.body(mapper.createObjectNode().put("Name", secret)
.put("SecretString", "{\"clientSecret\":\"resolved-secret\"}").toString())
.when().post("/").then().statusCode(200);
secretIds.add(secret);
ObjectNode template = template(pool, NAME, "OIDC", "example-client");
((ObjectNode) template.path("Resources").path("Idp").path("Properties").path("ProviderDetails"))
.put("client_secret", "{{resolve:secretsmanager:" + secret + ":SecretString:clientSecret}}");
createStack(template);
assertEquals("resolved-secret",
provider(pool, NAME).path("ProviderDetails").path("client_secret").asText());
}
@Test
void changingOnlyTheProviderTypeRollsTheUpdateBack() throws Exception {
String pool = createPool();
createStack(template(pool, NAME, "OIDC", "original-client"));
ObjectNode saml = template(pool, NAME, "SAML", "original-client");
((ObjectNode) saml.path("Resources").path("Idp").path("Properties").path("ProviderDetails"))
.put("MetadataURL", "https://issuer.example.com/saml/metadata");
updateStack(saml, "UPDATE_ROLLBACK_COMPLETE");
JsonNode kept = provider(pool, NAME);
assertEquals("OIDC", kept.path("ProviderType").asText());
assertEquals("original-client", kept.path("ProviderDetails").path("client_id").asText());
assertFalse(kept.path("ProviderDetails").has("MetadataURL"));
assertEquals(NAME, output("ProviderRef"));
}
@Test
void theOriginalProviderUpdatesInPlaceAfterARolledBackTypeAndNameReplacement() throws Exception {
String pool = createPool();
createStack(template(pool, NAME, "OIDC", "original-client"));
ObjectNode saml = template(pool, "ExampleSAML", "SAML", "original-client");
((ObjectNode) saml.path("Resources").path("Idp").path("Properties").path("ProviderDetails"))
.put("MetadataURL", "https://issuer.example.com/saml/metadata");
addFailingDependentResource(saml);
updateStack(saml, "UPDATE_ROLLBACK_COMPLETE");
assertProviderGone(pool, "ExampleSAML");
updateStack(template(pool, NAME, "OIDC", "updated-client"), "UPDATE_COMPLETE");
assertEquals(NAME, output("ProviderRef"));
assertClientId(pool, NAME, "updated-client");
}
@Test
void aFailedDependentResourceRestoresThePriorInPlaceConfiguration() throws Exception {
String pool = createPool();
createStack(template(pool, NAME, "OIDC", "original-client"));
ObjectNode attempted = template(pool, NAME, "OIDC", "attempted-client");
ObjectNode attemptedProperties = (ObjectNode) attempted.path("Resources").path("Idp").path("Properties");
attemptedProperties.putObject("AttributeMapping").put("email", "mail");
attemptedProperties.putArray("IdpIdentifiers").add("example.com");
addFailingDependentResource(attempted);
updateStack(attempted, "UPDATE_ROLLBACK_COMPLETE");
JsonNode restored = provider(pool, NAME);
assertEquals("original-client", restored.path("ProviderDetails").path("client_id").asText());
assertEquals("email", restored.path("AttributeMapping").path("email").asText());
assertEquals(0, restored.path("IdpIdentifiers").size());
}
@Test
void aFailedDependentResourceDeletesTheReplacementAndRestoresThePriorPool() throws Exception {
String poolA = createPool();
String poolB = createPool();
createStack(template(poolA, NAME, "OIDC", "original-client"));
ObjectNode attempted = template(poolB, NAME, "OIDC", "replacement-client");
addFailingDependentResource(attempted);
updateStack(attempted, "UPDATE_ROLLBACK_COMPLETE");
assertClientId(poolA, NAME, "original-client");
assertProviderGone(poolB, NAME);
assertEquals(NAME, output("ProviderRef"));
}
private String createPool() throws Exception {
String id = cognitoJson("CreateUserPool", "{\"PoolName\":\"" + stack + "\"}")
.path("UserPool").path("Id").asText();
poolIds.add(id);
return id;
}
private void createStack(ObjectNode template) {
cloudFormation("CreateStack", template);
createdStack = true;
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack).status());
}
private void updateStack(ObjectNode template, String expectedStatus) {
cloudFormation("UpdateStack", template);
assertEquals(expectedStatus, CfnStackWaits.awaitTerminal(stack).status());
}
private void cloudFormation(String action, ObjectNode template) {
RequestSpecification request = cfn(action, stack).header("Authorization", CFN_AUTH);
if (template != null) {
request.formParam("TemplateBody", template.toString());
}
request.when().post("/").then().statusCode(200);
}
private String output(String key) {
String xml = cfn("DescribeStacks", stack).header("Authorization", CFN_AUTH)
.when().post("/").then().statusCode(200).extract().asString();
return XmlParser.extractPairs(xml, "Outputs", "OutputKey", "OutputValue").get(key);
}
private ObjectNode providerProperties(String name, String type, String clientId) {
ObjectNode properties = mapper.createObjectNode();
properties.put("ProviderName", name);
properties.put("ProviderType", type);
properties.putObject("ProviderDetails")
.put("client_id", clientId)
.put("oidc_issuer", "https://issuer.example.com")
.put("authorize_scopes", "openid email")
.put("attributes_request_method", "GET");
properties.putObject("AttributeMapping").put("email", "email");
return properties;
}
private ObjectNode template(String pool, String name, String type, String clientId) {
ObjectNode template = mapper.createObjectNode();
ObjectNode resource = template.putObject("Resources").putObject("Idp");
resource.put("Type", "AWS::Cognito::UserPoolIdentityProvider");
ObjectNode properties = providerProperties(name, type, clientId);
properties.put("UserPoolId", pool);
resource.set("Properties", properties);
template.putObject("Outputs").putObject("ProviderRef").putObject("Value").put("Ref", "Idp");
return template;
}
private static void addFailingDependentResource(ObjectNode template) {
ObjectNode broken = ((ObjectNode) template.path("Resources")).putObject("Broken");
broken.put("Type", "AWS::Cognito::UserPoolClient");
broken.put("DependsOn", "Idp");
broken.putObject("Properties").put("UserPoolId", "us-east-1_nonexistent").put("ClientName", "broken");
}
private static JsonNode provider(String pool, String name) throws Exception {
return cognitoJson("DescribeIdentityProvider", "{\"UserPoolId\":\"" + pool
+ "\",\"ProviderName\":\"" + name + "\"}").path("IdentityProvider");
}
private static void assertClientId(String pool, String name, String clientId) throws Exception {
JsonNode provider = provider(pool, name);
assertEquals(name, provider.path("ProviderName").asText());
assertEquals(clientId, provider.path("ProviderDetails").path("client_id").asText());
}
private static void assertProviderGone(String pool, String name) {
cognitoAction("DescribeIdentityProvider", "{\"UserPoolId\":\"" + pool
+ "\",\"ProviderName\":\"" + name + "\"}")
.then().statusCode(400).body("__type", equalTo("ResourceNotFoundException"));
}
}
@@ -0,0 +1,373 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
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.IdentityProvider;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
class CognitoIdentityProviderCfnProvisionerTest {
private static final String POOL = "us-east-1_original";
private static final String NEW_POOL = "us-east-1_replacement";
private static final String NAME = "ExampleOIDC";
private static final String POOL_ATTR = "__FlociIdentityProviderPoolId";
private final CognitoService cognito = mock(CognitoService.class);
private final CognitoIdentityProviderCfnProvisioner provisioner = new CognitoIdentityProviderCfnProvisioner(cognito);
private final ObjectMapper mapper = new ObjectMapper();
private ProvisionContext ctx(String priorId) {
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
when(engine.resolve(any())).thenAnswer(invocation -> {
JsonNode node = invocation.getArgument(0);
return node == null || node.isNull() ? null : node.asText();
});
when(engine.resolveNode(any())).thenAnswer(invocation -> invocation.getArgument(0));
when(engine.resolveNodeOmittingNoValue(any())).thenAnswer(invocation -> invocation.getArgument(0));
when(engine.resolveStringList(any())).thenAnswer(invocation -> {
List<String> values = new ArrayList<>();
JsonNode node = invocation.getArgument(0);
node.forEach(value -> values.add(value.asText()));
return values;
});
return new ProvisionContext(engine, "us-east-1", "000000000000", "test-stack", priorId);
}
private ProvisionContext intrinsicContext() {
CloudFormationTemplateEngine engine = CloudFormationTemplateEngine.standalone(
"000000000000", "us-east-1", "test-stack", mapper, null);
return new ProvisionContext(engine, "us-east-1", "000000000000", "test-stack");
}
private ObjectNode properties(String poolId, String providerName, String providerType, String clientId) {
ObjectNode properties = mapper.createObjectNode();
if (poolId != null) {
properties.put("UserPoolId", poolId);
}
if (providerName != null) {
properties.put("ProviderName", providerName);
}
if (providerType != null) {
properties.put("ProviderType", providerType);
}
if (clientId != null) {
properties.putObject("ProviderDetails")
.put("client_id", clientId)
.put("oidc_issuer", "https://issuer.example.com");
}
return properties;
}
private ObjectNode oidc(String poolId, String providerName, String clientId) {
return properties(poolId, providerName, "OIDC", clientId);
}
private static Map<String, String> details(String clientId) {
Map<String, String> details = new LinkedHashMap<>();
details.put("client_id", clientId);
details.put("oidc_issuer", "https://issuer.example.com");
return details;
}
private StackResource resource(String providerName, String poolId) {
StackResource resource = new StackResource();
resource.setLogicalId("Idp");
resource.setResourceType("AWS::Cognito::UserPoolIdentityProvider");
resource.setPhysicalId(providerName);
resource.setAttributes(new HashMap<>());
if (poolId != null) {
resource.getAttributes().put(POOL_ATTR, poolId);
}
return resource;
}
private static IdentityProvider provider(String poolId, String providerName, String clientId) {
IdentityProvider provider = new IdentityProvider();
provider.setUserPoolId(poolId);
provider.setProviderName(providerName);
provider.setProviderType("OIDC");
provider.setProviderDetails(details(clientId));
provider.setAttributeMapping(new LinkedHashMap<>(Map.of("email", "email")));
provider.setIdpIdentifiers(new ArrayList<>(List.of("old-idp")));
return provider;
}
@Test
void servesIdentityProviderType() {
assertEquals(Set.of("AWS::Cognito::UserPoolIdentityProvider"), provisioner.resourceTypes());
}
@Test
void createsProviderWithResolvedMembersAndSetsRefAndPoolTracking() {
ObjectNode properties = oidc(POOL, NAME, "client");
properties.putObject("AttributeMapping").put("email", "email");
properties.putArray("IdpIdentifiers").add("example.com");
StackResource resource = resource(null, null);
provisioner.provision(resource, properties, ctx(null));
verify(cognito).createIdentityProvider(POOL, NAME, "OIDC", details("client"),
Map.of("email", "email"), List.of("example.com"));
assertEquals(NAME, resource.getPhysicalId());
assertEquals(POOL, resource.getAttributes().get(POOL_ATTR));
assertFalse(resource.getAttributes().containsKey("ProviderName"));
}
@Test
void absentOptionalMembersReachCognitoAsNullSoItsDefaultsApply() {
provisioner.provision(resource(null, null), oidc(POOL, NAME, "client"), ctx(null));
verify(cognito).createIdentityProvider(POOL, NAME, "OIDC", details("client"), null, null);
}
@Test
void anAttributeMappingOfNoValueIsOmitted() {
ObjectNode properties = oidc(POOL, NAME, "client");
properties.putObject("AttributeMapping").put("Ref", "AWS::NoValue");
provisioner.provision(resource(null, null), properties, intrinsicContext());
verify(cognito).createIdentityProvider(POOL, NAME, "OIDC", details("client"), null, null);
}
@Test
void rejectsMissingRequiredPropertiesBeforeCallingCognito() {
assertThrows(AwsException.class, () -> provisioner.provision(resource(null, null),
oidc(null, NAME, "client"), ctx(null)));
assertThrows(AwsException.class, () -> provisioner.provision(resource(null, null),
oidc(POOL, null, "client"), ctx(null)));
assertThrows(AwsException.class, () -> provisioner.provision(resource(null, null),
properties(POOL, NAME, null, "client"), ctx(null)));
assertThrows(AwsException.class, () -> provisioner.provision(resource(null, null),
oidc(POOL, NAME, null), ctx(null)));
verifyNoInteractions(cognito);
}
@Test
void validatesSchemaBoundsBeforeCallingCognito() {
assertThrows(AwsException.class, () -> provisioner.provision(resource(null, null),
properties(POOL, NAME, "Okta", "client"), ctx(null)));
assertThrows(AwsException.class, () -> provisioner.provision(resource(null, null),
oidc(POOL, "n".repeat(33), "client"), ctx(null)));
assertThrows(AwsException.class, () -> provisioner.provision(resource(null, null),
oidc("not a pool", NAME, "client"), ctx(null)));
ObjectNode nestedDetails = oidc(POOL, NAME, null);
nestedDetails.putObject("ProviderDetails").putObject("client_id");
assertThrows(AwsException.class, () -> provisioner.provision(resource(null, null), nestedDetails, ctx(null)));
ObjectNode tooMany = oidc(POOL, NAME, "client");
for (int index = 0; index < 51; index++) {
tooMany.withArray("IdpIdentifiers").add("idp" + index);
}
assertThrows(AwsException.class, () -> provisioner.provision(resource(null, null), tooMany, ctx(null)));
verifyNoInteractions(cognito);
}
@Test
void unchangedIdentityUpdatesInPlaceAndRollbackRestoresTheSnapshot() {
when(cognito.describeIdentityProvider(POOL, NAME)).thenReturn(provider(POOL, NAME, "old-client"));
StackResource resource = resource(NAME, POOL);
provisioner.provision(resource, oidc(POOL, NAME, "new-client"), ctx(NAME));
verify(cognito).updateIdentityProvider(POOL, NAME, details("new-client"), null, null);
verify(cognito, never()).createIdentityProvider(any(), any(), any(), any(), any(), any());
assertFalse(provisioner.hasReplacementUpdate(resource));
assertTrue(provisioner.retainsFailedUpdateState(resource));
assertTrue(provisioner.rollbackUpdate(resource));
verify(cognito).updateIdentityProvider(POOL, NAME, details("old-client"),
Map.of("email", "email"), List.of("old-idp"));
assertEquals(NAME, resource.getPhysicalId());
assertEquals(POOL, resource.getAttributes().get(POOL_ATTR));
assertFalse(provisioner.retainsFailedUpdateState(resource));
}
@Test
void committingAnInPlaceUpdateDropsItsSnapshot() {
when(cognito.describeIdentityProvider(POOL, NAME)).thenReturn(provider(POOL, NAME, "old-client"));
StackResource resource = resource(NAME, POOL);
provisioner.provision(resource, oidc(POOL, NAME, "new-client"), ctx(NAME));
assertTrue(provisioner.completeUpdate(resource).complete());
assertFalse(provisioner.retainsFailedUpdateState(resource));
assertFalse(provisioner.rollbackUpdate(resource));
verify(cognito, never()).deleteIdentityProvider(any(), any());
}
@Test
void changingOnlyTheProviderTypeIsRefusedBeforeChangingAnything() {
when(cognito.describeIdentityProvider(POOL, NAME)).thenReturn(provider(POOL, NAME, "client"));
StackResource resource = resource(NAME, POOL);
HashMap<String, String> priorAttributes = new HashMap<>(resource.getAttributes());
AwsException failure = assertThrows(AwsException.class, () -> provisioner.provision(resource,
properties(POOL, NAME, "SAML", "client"), ctx(NAME)));
assertEquals("ValidationError", failure.getErrorCode());
assertTrue(failure.getMessage().contains("custom-named resource requires replacing"));
assertEquals(NAME, resource.getPhysicalId());
assertEquals(priorAttributes, resource.getAttributes());
verify(cognito, never()).createIdentityProvider(any(), any(), any(), any(), any(), any());
verify(cognito, never()).updateIdentityProvider(any(), any(), any(), any(), any());
verify(cognito, never()).deleteIdentityProvider(any(), any());
}
@Test
void changingTheTypeWithANewNameReplacesTheProvider() {
StackResource resource = resource(NAME, POOL);
provisioner.provision(resource, properties(POOL, "ExampleSAML", "SAML", "client"), ctx(NAME));
verify(cognito).createIdentityProvider(POOL, "ExampleSAML", "SAML", details("client"), null, null);
assertTrue(provisioner.completeUpdate(resource).complete());
verify(cognito).deleteIdentityProvider(POOL, NAME);
}
@Test
void theOriginalTypeUpdatesInPlaceAfterARolledBackTypeAndNameReplacement() {
when(cognito.describeIdentityProvider(POOL, NAME)).thenReturn(provider(POOL, NAME, "client"));
StackResource resource = resource(NAME, POOL);
resource.getAttributes().put(CfnRollback.ROLLBACK_OWNED_ATTR, "true");
provisioner.provision(resource, properties(POOL, "ExampleSAML", "SAML", "client"), ctx(NAME));
assertTrue(provisioner.rollbackUpdate(resource));
assertEquals(NAME, resource.getPhysicalId());
provisioner.provision(resource, oidc(POOL, NAME, "updated-client"), ctx(NAME));
verify(cognito).deleteIdentityProvider(POOL, "ExampleSAML");
verify(cognito).updateIdentityProvider(POOL, NAME, details("updated-client"), null, null);
verify(cognito, never()).createIdentityProvider(eq(POOL), eq(NAME), any(), any(), any(), any());
}
@Test
void renamingReplacesAndCommitDeletesTheOldProvider() {
StackResource resource = resource(NAME, POOL);
provisioner.provision(resource, oidc(POOL, "RenamedOIDC", "client"), ctx(NAME));
verify(cognito).createIdentityProvider(POOL, "RenamedOIDC", "OIDC", details("client"), null, null);
verify(cognito, never()).describeIdentityProvider(any(), any());
assertEquals("RenamedOIDC", resource.getPhysicalId());
assertTrue(provisioner.hasReplacementUpdate(resource));
assertEquals(NAME, provisioner.updateCleanupPhysicalId(resource));
assertTrue(provisioner.completeUpdate(resource).complete());
verify(cognito).deleteIdentityProvider(POOL, NAME);
verify(cognito, never()).deleteIdentityProvider(POOL, "RenamedOIDC");
}
@Test
void changingPoolReplacesEvenWhenRefIsUnchanged() {
StackResource resource = resource(NAME, POOL);
provisioner.provision(resource, oidc(NEW_POOL, NAME, "client"), ctx(NAME));
verify(cognito).createIdentityProvider(NEW_POOL, NAME, "OIDC", details("client"), null, null);
assertEquals(NAME, resource.getPhysicalId());
assertEquals(NEW_POOL, resource.getAttributes().get(POOL_ATTR));
assertTrue(provisioner.hasReplacementUpdate(resource));
assertTrue(provisioner.completeUpdate(resource).complete());
verify(cognito).deleteIdentityProvider(POOL, NAME);
verify(cognito, never()).deleteIdentityProvider(NEW_POOL, NAME);
}
@Test
void replacementRollbackRestoresPriorIdentityAndDeletesOnlyTheNewProvider() {
StackResource resource = resource(NAME, POOL);
resource.getAttributes().put(CfnRollback.ROLLBACK_OWNED_ATTR, "true");
HashMap<String, String> priorAttributes = new HashMap<>(resource.getAttributes());
provisioner.provision(resource, oidc(NEW_POOL, NAME, "client"), ctx(NAME));
assertTrue(provisioner.rollbackUpdate(resource));
assertEquals(NAME, resource.getPhysicalId());
assertEquals(priorAttributes, resource.getAttributes());
verify(cognito).deleteIdentityProvider(NEW_POOL, NAME);
verify(cognito, never()).deleteIdentityProvider(POOL, NAME);
}
@Test
void retainingAReplacementDoesNotDeleteThePriorProvider() {
StackResource resource = resource(NAME, POOL);
resource.setUpdateReplacePolicy("Retain");
provisioner.provision(resource, oidc(POOL, "RenamedOIDC", "client"), ctx(NAME));
assertNull(provisioner.updateCleanupPhysicalId(resource));
assertTrue(provisioner.completeUpdate(resource).complete());
verify(cognito, never()).deleteIdentityProvider(any(), any());
}
@Test
void aFailedReplacementCreateLeavesThePriorAddressAndMetadataUnchanged() {
StackResource resource = resource(NAME, POOL);
HashMap<String, String> priorAttributes = new HashMap<>(resource.getAttributes());
AwsException failure = new AwsException("DuplicateProviderException", "exists", 400);
when(cognito.createIdentityProvider(eq(POOL), eq("RenamedOIDC"), any(), any(), any(), any()))
.thenThrow(failure);
assertEquals(failure, assertThrows(AwsException.class, () -> provisioner.provision(resource,
oidc(POOL, "RenamedOIDC", "client"), ctx(NAME))));
assertEquals(NAME, resource.getPhysicalId());
assertEquals(priorAttributes, resource.getAttributes());
verify(cognito, never()).deleteIdentityProvider(any(), any());
}
@Test
void migratesDispatcherStubByCreatingTheProviderWithoutOwingTheFakeIdADelete() {
StackResource resource = resource("Idp-1a2b3c4d", null);
resource.getAttributes().put("Arn", CfnResourceDispatcher.STUB_ARN_PREFIX + resource.getLogicalId());
provisioner.provision(resource, oidc(POOL, NAME, "client"), ctx(resource.getPhysicalId()));
verify(cognito).createIdentityProvider(POOL, NAME, "OIDC", details("client"), null, null);
assertEquals(NAME, resource.getPhysicalId());
assertFalse(resource.getAttributes().containsKey("Arn"));
assertNull(provisioner.updateCleanupPhysicalId(resource));
provisioner.completeUpdate(resource);
verify(cognito, never()).deleteIdentityProvider(any(), any());
}
@Test
void deleteUsesStoredPoolAndOnlyToleratesAlreadyMissingProviders() {
StackResource resource = resource(NAME, POOL);
doThrow(new AwsException("ResourceNotFoundException", "gone", 400))
.when(cognito).deleteIdentityProvider(POOL, NAME);
provisioner.delete(resource, "us-east-1");
verify(cognito).deleteIdentityProvider(POOL, NAME);
StackResource failing = resource(NAME, POOL);
doThrow(new AwsException("InternalErrorException", "storage unavailable", 500))
.when(cognito).deleteIdentityProvider(POOL, NAME);
assertThrows(AwsException.class, () -> provisioner.delete(failing, "us-east-1"));
}
}