mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 10:38:00 -05:00
feat(cognito): provision user pool users (#4805)
* feat(cognito): provision user pool users Add a CloudFormation provisioner for AWS::Cognito::UserPoolUser. Ref is the username Cognito reports (the sub in an email-as-username pool). Every property is createOnly: a changed custom-named user is refused as on AWS, any other change replaces the user, and each delete targets the pool the user was created in. * fix(cognito): refuse a RESEND user pool user that already exists * fix(cognito): keep the user's pool out of Fn::GetAtt * fix(cognito): keep only owed users in the pool record
This commit is contained in:
1 parent
bdab4448d6
commit
ae0a591bb1
8 files changed
+1137
-1
No files matched your search
+2
@@ -52,6 +52,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudWatc
|
||||
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.CognitoUserPoolGroupCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.CognitoUserPoolUserCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2LaunchTemplateCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2NetworkCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.Ec2NetworkAclCfnProvisioner;
|
||||
@@ -293,6 +294,7 @@ final class CfnProvisionerFixture {
|
||||
if (cognitoService != null) {
|
||||
discovered.add(new CognitoCfnProvisioner(cognitoService));
|
||||
discovered.add(new CognitoUserPoolGroupCfnProvisioner(cognitoService));
|
||||
discovered.add(new CognitoUserPoolUserCfnProvisioner(cognitoService));
|
||||
}
|
||||
if (cloudFrontService != null) {
|
||||
discovered.add(new CloudFrontCfnProvisioner(cloudFrontService));
|
||||
|
||||
+280
@@ -0,0 +1,280 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Provisions {@code AWS::Cognito::UserPoolUser} through CloudFormation stacks and reads the users
|
||||
* back through the Cognito JSON API: Ref is the template username or a generated one, an update that
|
||||
* changes a custom-named user is refused and rolled back with the user untouched, a renamed user is
|
||||
* replaced and the old one deleted once the update commits, DeleteStack removes the users, and in an
|
||||
* email-as-username pool Ref is the user's sub.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationCognitoUserPoolUserIntegrationTest {
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20261001/us-east-1/cloudformation/aws4_request";
|
||||
private static final String COGNITO = "AWSCognitoIdentityProviderService";
|
||||
private static final String UUID_PATTERN = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createStackProvisionsUsersWhoseRefIsTheirUsername() {
|
||||
String stack = "cfn-cognito-users-" + Long.toString(System.nanoTime(), 36);
|
||||
|
||||
stackAction(stack, "CreateStack", poolAndUsersTemplate(), Map.of());
|
||||
String outputs = describeStacks(stack, "CREATE_COMPLETE");
|
||||
String poolId = outputValue(outputs, "PoolId");
|
||||
String generated = outputValue(outputs, "GeneratedRef");
|
||||
assertEquals("user-one", outputValue(outputs, "NamedRef"));
|
||||
assertThat(generated, matchesPattern(Pattern.quote(stack) + "-GeneratedUser-[a-z0-9]{12}"));
|
||||
adminGetUser(poolId, "user-one")
|
||||
.statusCode(200)
|
||||
.body("Username", equalTo("user-one"))
|
||||
.body("UserAttributes.find { it.Name == 'email' }.Value", equalTo("one@example.com"))
|
||||
.body("UserAttributes.find { it.Name == 'name' }.Value", equalTo("One"));
|
||||
adminGetUser(poolId, generated).statusCode(200).body("Username", equalTo(generated));
|
||||
|
||||
stackAction(stack, "DeleteStack", null, Map.of());
|
||||
CfnStackWaits.awaitStackDeleted(stack);
|
||||
cognito("DescribeUserPool", "{\"UserPoolId\": \"" + poolId + "\"}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The pool lives outside the stack, so the update exercises only the users and DeleteStack can
|
||||
* show each user is gone rather than only its pool.
|
||||
*/
|
||||
@Test
|
||||
void aChangedCustomNamedUserIsRefusedARenameReplacesItAndDeleteStackRemovesIt() {
|
||||
String stack = "cfn-cognito-users-update-" + Long.toString(System.nanoTime(), 36);
|
||||
String poolId = cognito("CreateUserPool", "{\"PoolName\": \"" + stack + "\"}")
|
||||
.statusCode(200)
|
||||
.extract().path("UserPool.Id");
|
||||
|
||||
stackAction(stack, "CreateStack", usersInExistingPoolTemplate(), userParameters(poolId, "user-one", "one@example.com"));
|
||||
String outputs = describeStacks(stack, "CREATE_COMPLETE");
|
||||
String generated = outputValue(outputs, "GeneratedRef");
|
||||
assertEquals("user-one", outputValue(outputs, "NamedRef"));
|
||||
String sub = adminGetUser(poolId, "user-one")
|
||||
.statusCode(200)
|
||||
.extract().path("UserAttributes.find { it.Name == 'sub' }.Value");
|
||||
|
||||
// Every property is createOnly and the username is explicit, so CloudFormation refuses it.
|
||||
stackAction(stack, "UpdateStack", usersInExistingPoolTemplate(), userParameters(poolId, "user-one", "two@example.com"));
|
||||
describeStacks(stack, "UPDATE_ROLLBACK_COMPLETE");
|
||||
assertTrue(XmlParser.extractGroups(describeEvents(stack), "member").stream()
|
||||
.anyMatch(event -> "NamedUser".equals(event.get("LogicalResourceId"))
|
||||
&& "UPDATE_FAILED".equals(event.get("ResourceStatus"))
|
||||
&& event.getOrDefault("ResourceStatusReason", "")
|
||||
.contains("custom-named resource requires replacing. Rename user-one")),
|
||||
"no refused update event for NamedUser");
|
||||
adminGetUser(poolId, "user-one")
|
||||
.statusCode(200)
|
||||
.body("UserAttributes.find { it.Name == 'email' }.Value", equalTo("one@example.com"))
|
||||
.body("UserAttributes.find { it.Name == 'sub' }.Value", equalTo(sub));
|
||||
|
||||
stackAction(stack, "UpdateStack", usersInExistingPoolTemplate(), userParameters(poolId, "user-one-b", "one@example.com"));
|
||||
outputs = describeStacks(stack, "UPDATE_COMPLETE");
|
||||
assertEquals("user-one-b", outputValue(outputs, "NamedRef"));
|
||||
assertEquals(generated, outputValue(outputs, "GeneratedRef"));
|
||||
adminGetUser(poolId, "user-one-b")
|
||||
.statusCode(200)
|
||||
.body("UserAttributes.find { it.Name == 'email' }.Value", equalTo("one@example.com"));
|
||||
adminGetUser(poolId, "user-one").statusCode(400).body("__type", equalTo("UserNotFoundException"));
|
||||
adminGetUser(poolId, generated).statusCode(200);
|
||||
|
||||
stackAction(stack, "DeleteStack", null, Map.of());
|
||||
CfnStackWaits.awaitStackDeleted(stack);
|
||||
adminGetUser(poolId, "user-one-b").statusCode(400).body("__type", equalTo("UserNotFoundException"));
|
||||
adminGetUser(poolId, generated).statusCode(400).body("__type", equalTo("UserNotFoundException"));
|
||||
cognito("DeleteUserPool", "{\"UserPoolId\": \"" + poolId + "\"}").statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEmailAsUsernamePoolRefsTheUsersSub() {
|
||||
String stack = "cfn-cognito-users-alias-" + Long.toString(System.nanoTime(), 36);
|
||||
|
||||
stackAction(stack, "CreateStack", emailPoolTemplate(), Map.of());
|
||||
String outputs = describeStacks(stack, "CREATE_COMPLETE");
|
||||
String ref = outputValue(outputs, "UserRef");
|
||||
assertThat(ref, matchesPattern(UUID_PATTERN));
|
||||
adminGetUser(outputValue(outputs, "PoolId"), "alias.user@example.com")
|
||||
.statusCode(200)
|
||||
.body("Username", equalTo(ref))
|
||||
.body("UserAttributes.find { it.Name == 'sub' }.Value", equalTo(ref))
|
||||
.body("UserAttributes.find { it.Name == 'email' }.Value", equalTo("alias.user@example.com"));
|
||||
|
||||
stackAction(stack, "DeleteStack", null, Map.of());
|
||||
CfnStackWaits.awaitStackDeleted(stack);
|
||||
}
|
||||
|
||||
private static void stackAction(String stack, String action, String templateBody, Map<String, String> parameters) {
|
||||
RequestSpecification request = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", action)
|
||||
.formParam("StackName", stack);
|
||||
if (templateBody != null) {
|
||||
request.formParam("TemplateBody", templateBody);
|
||||
}
|
||||
int index = 1;
|
||||
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
|
||||
request.formParam("Parameters.member." + index + ".ParameterKey", parameter.getKey());
|
||||
request.formParam("Parameters.member." + index + ".ParameterValue", parameter.getValue());
|
||||
index++;
|
||||
}
|
||||
request.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static String describeStacks(String stack, String expectedStatus) {
|
||||
CfnStackWaits.StackState state = CfnStackWaits.awaitTerminal(stack);
|
||||
assertEquals(expectedStatus, state.status(), state.reason());
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stack)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<StackStatus>" + expectedStatus + "</StackStatus>"))
|
||||
.extract().asString();
|
||||
}
|
||||
|
||||
private static String describeEvents(String stack) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackEvents")
|
||||
.formParam("StackName", stack)
|
||||
.when().post("/").then().statusCode(200).extract().asString();
|
||||
}
|
||||
|
||||
private static String outputValue(String xml, String key) {
|
||||
return XmlParser.extractPairs(xml, "Outputs", "OutputKey", "OutputValue").get(key);
|
||||
}
|
||||
|
||||
private static ValidatableResponse adminGetUser(String poolId, String username) {
|
||||
return cognito("AdminGetUser", "{\"UserPoolId\": \"" + poolId + "\", \"Username\": \"" + username + "\"}");
|
||||
}
|
||||
|
||||
private static ValidatableResponse cognito(String action, String body) {
|
||||
return RestAssuredJsonUtils.awsAction(COGNITO, action, body).then();
|
||||
}
|
||||
|
||||
private static Map<String, String> userParameters(String poolId, String username, String email) {
|
||||
return Map.of("PoolId", poolId, "Username", username, "Email", email);
|
||||
}
|
||||
|
||||
private static String poolAndUsersTemplate() {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"Pool": {
|
||||
"Type": "AWS::Cognito::UserPool",
|
||||
"Properties": { "UserPoolName": "cfn-user-pool" }
|
||||
},
|
||||
"NamedUser": {
|
||||
"Type": "AWS::Cognito::UserPoolUser",
|
||||
"Properties": {
|
||||
"UserPoolId": { "Ref": "Pool" },
|
||||
"Username": "user-one",
|
||||
"MessageAction": "SUPPRESS",
|
||||
"UserAttributes": [
|
||||
{ "Name": "email", "Value": "one@example.com" },
|
||||
{ "Name": "name", "Value": "One" }
|
||||
]
|
||||
}
|
||||
},
|
||||
"GeneratedUser": {
|
||||
"Type": "AWS::Cognito::UserPoolUser",
|
||||
"Properties": { "UserPoolId": { "Ref": "Pool" }, "MessageAction": "SUPPRESS" }
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"PoolId": { "Value": { "Ref": "Pool" } },
|
||||
"NamedRef": { "Value": { "Ref": "NamedUser" } },
|
||||
"GeneratedRef": { "Value": { "Ref": "GeneratedUser" } }
|
||||
}
|
||||
}
|
||||
""";
|
||||
}
|
||||
|
||||
private static String usersInExistingPoolTemplate() {
|
||||
return """
|
||||
{
|
||||
"Parameters": {
|
||||
"PoolId": { "Type": "String" },
|
||||
"Username": { "Type": "String" },
|
||||
"Email": { "Type": "String" }
|
||||
},
|
||||
"Resources": {
|
||||
"NamedUser": {
|
||||
"Type": "AWS::Cognito::UserPoolUser",
|
||||
"Properties": {
|
||||
"UserPoolId": { "Ref": "PoolId" },
|
||||
"Username": { "Ref": "Username" },
|
||||
"MessageAction": "SUPPRESS",
|
||||
"UserAttributes": [ { "Name": "email", "Value": { "Ref": "Email" } } ]
|
||||
}
|
||||
},
|
||||
"GeneratedUser": {
|
||||
"Type": "AWS::Cognito::UserPoolUser",
|
||||
"Properties": { "UserPoolId": { "Ref": "PoolId" } }
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"NamedRef": { "Value": { "Ref": "NamedUser" } },
|
||||
"GeneratedRef": { "Value": { "Ref": "GeneratedUser" } }
|
||||
}
|
||||
}
|
||||
""";
|
||||
}
|
||||
|
||||
private static String emailPoolTemplate() {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"EmailPool": {
|
||||
"Type": "AWS::Cognito::UserPool",
|
||||
"Properties": { "UserPoolName": "cfn-email-pool", "UsernameAttributes": ["email"] }
|
||||
},
|
||||
"EmailUser": {
|
||||
"Type": "AWS::Cognito::UserPoolUser",
|
||||
"Properties": {
|
||||
"UserPoolId": { "Ref": "EmailPool" },
|
||||
"Username": "alias.user@example.com",
|
||||
"MessageAction": "SUPPRESS",
|
||||
"UserAttributes": [ { "Name": "email", "Value": "alias.user@example.com" } ]
|
||||
}
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"PoolId": { "Value": { "Ref": "EmailPool" } },
|
||||
"UserRef": { "Value": { "Ref": "EmailUser" } }
|
||||
}
|
||||
}
|
||||
""";
|
||||
}
|
||||
}
|
||||
+442
@@ -0,0 +1,442 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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.CognitoUser;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
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.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.clearInvocations;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* The Cognito user pool user CFN provisioner in isolation, with only CognitoService mocked: Ref is
|
||||
* the username Cognito reports, an unchanged template is a no-op, a changed custom-named user is
|
||||
* refused, any other change replaces the user, and every delete goes to the user's own pool.
|
||||
*/
|
||||
class CognitoUserPoolUserCfnProvisionerTest {
|
||||
|
||||
private static final String TYPE = "AWS::Cognito::UserPoolUser";
|
||||
private static final String CREATE_ONLY_ATTR = "__FlociCreateOnly";
|
||||
private static final String POOLS_ATTR = "__FlociCognitoUserPools";
|
||||
|
||||
private final CognitoService cognito = mock(CognitoService.class);
|
||||
private final CognitoUserPoolUserCfnProvisioner provisioner = new CognitoUserPoolUserCfnProvisioner(cognito);
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void servesTheUserPoolUserType() {
|
||||
assertEquals(Set.of(TYPE), provisioner.resourceTypes());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createPassesUsernameAttributesAndMessageActionThrough() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
StackResource r = resource(null);
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "user-one", "MessageAction": "SUPPRESS",
|
||||
"ForceAliasCreation": true, "DesiredDeliveryMediums": ["EMAIL"],
|
||||
"UserAttributes": [{"Name": "email", "Value": "one@example.com"},
|
||||
{"Name": "name", "Value": "One"}]}"""), ctx(null));
|
||||
|
||||
Map<String, String> attributes = new LinkedHashMap<>();
|
||||
attributes.put("email", "one@example.com");
|
||||
attributes.put("name", "One");
|
||||
verify(cognito).adminCreateUser("pool-a", "user-one", attributes, null, "SUPPRESS", true);
|
||||
assertEquals("user-one", r.getPhysicalId(), "Ref must resolve to the username");
|
||||
assertTrue(r.getAttributes().keySet().stream().allMatch(key -> key.startsWith("__Floci")),
|
||||
"AWS::Cognito::UserPoolUser exposes no Fn::GetAtt attribute");
|
||||
assertFalse(provisioner.hasReplacementUpdate(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEmailAsUsernamePoolRefsTheCanonicalUsernameCognitoReturns() throws Exception {
|
||||
CognitoUser created = new CognitoUser();
|
||||
created.setUsername("d275a484-c0c1-70b3-46da-d9dafa5205e3");
|
||||
when(cognito.adminCreateUser(anyString(), anyString(), any(), any(), any(), anyBoolean()))
|
||||
.thenReturn(created);
|
||||
StackResource r = resource(null);
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "alias.user@example.com",
|
||||
"UserAttributes": [{"Name": "email", "Value": "alias.user@example.com"}]}"""), ctx(null));
|
||||
|
||||
verify(cognito).adminCreateUser("pool-a", "alias.user@example.com",
|
||||
Map.of("email", "alias.user@example.com"), null, null, false);
|
||||
assertEquals("d275a484-c0c1-70b3-46da-d9dafa5205e3", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aUserWithoutUsernameGetsAGeneratedName() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
StackResource r = resource(null);
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a"}"""), ctx(null));
|
||||
|
||||
assertThat(r.getPhysicalId(), matchesPattern("my-stack-User-[a-z0-9]{12}"));
|
||||
verify(cognito).adminCreateUser("pool-a", r.getPhysicalId(), Map.of(), null, null, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingUserPoolIdIsAValidationErrorAndCreatesNothing() throws Exception {
|
||||
StackResource r = resource(null);
|
||||
|
||||
AwsException thrown = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(r, props("""
|
||||
{"Username": "user-one"}"""), ctx(null)));
|
||||
|
||||
assertEquals("ValidationError", thrown.getErrorCode());
|
||||
assertEquals("AWS::Cognito::UserPoolUser requires UserPoolId", thrown.getMessage());
|
||||
verifyNoInteractions(cognito);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resendForAnExistingUserFailsWithoutAdoptingIt() throws Exception {
|
||||
CognitoUser existing = new CognitoUser();
|
||||
existing.setUsername("alice");
|
||||
when(cognito.adminGetUser("pool-a", "alice")).thenReturn(existing);
|
||||
StackResource r = resource(null);
|
||||
|
||||
AwsException thrown = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "alice", "MessageAction": "RESEND"}"""), ctx(null)));
|
||||
|
||||
assertEquals("AlreadyExists", thrown.getErrorCode());
|
||||
assertEquals("Resource of type 'AWS::Cognito::UserPoolUser' with identifier 'pool-a|alice' already exists.",
|
||||
thrown.getMessage());
|
||||
verify(cognito, never()).adminCreateUser(any(), any(), any(), any(), any(), anyBoolean());
|
||||
assertNull(r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resendForAMissingUserFailsUserNotFound() throws Exception {
|
||||
when(cognito.adminGetUser("pool-a", "alice"))
|
||||
.thenThrow(new AwsException("UserNotFoundException", "User does not exist.", 400));
|
||||
StackResource r = resource(null);
|
||||
|
||||
AwsException thrown = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "alice", "MessageAction": "RESEND"}"""), ctx(null)));
|
||||
|
||||
assertEquals("UserNotFoundException", thrown.getErrorCode());
|
||||
verify(cognito, never()).adminCreateUser(any(), any(), any(), any(), any(), anyBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnchangedTemplateLeavesTheUserAlone() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
String template = """
|
||||
{"UserPoolId": "pool-a", "Username": "user-one",
|
||||
"UserAttributes": [{"Name": "email", "Value": "one@example.com"}]}""";
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props(template), ctx(null));
|
||||
clearInvocations(cognito);
|
||||
|
||||
provisioner.provision(r, props(template), ctx("user-one"));
|
||||
|
||||
verifyNoInteractions(cognito);
|
||||
assertEquals("user-one", r.getPhysicalId());
|
||||
assertFalse(provisioner.hasReplacementUpdate(r));
|
||||
|
||||
provisioner.delete(r, "us-east-1");
|
||||
verify(cognito).adminDeleteUser("pool-a", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changingACustomNamedUserIsRefusedWithoutTouchingIt() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "user-one",
|
||||
"UserAttributes": [{"Name": "email", "Value": "one@example.com"}]}"""), ctx(null));
|
||||
clearInvocations(cognito);
|
||||
|
||||
AwsException thrown = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "user-one",
|
||||
"UserAttributes": [{"Name": "email", "Value": "two@example.com"}]}"""), ctx("user-one")));
|
||||
|
||||
assertEquals("ValidationError", thrown.getErrorCode());
|
||||
assertEquals("CloudFormation cannot update a stack when a custom-named resource requires replacing. "
|
||||
+ "Rename user-one and update the stack again.", thrown.getMessage());
|
||||
verifyNoInteractions(cognito);
|
||||
}
|
||||
|
||||
@Test
|
||||
void renamingTheUserReplacesItAndCleanupDeletesTheOldOne() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "user-one"}"""), ctx(null));
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "user-one-b"}"""), ctx("user-one"));
|
||||
|
||||
verify(cognito).adminCreateUser("pool-a", "user-one-b", Map.of(), null, null, false);
|
||||
assertEquals("user-one-b", r.getPhysicalId());
|
||||
assertTrue(provisioner.hasReplacementUpdate(r));
|
||||
assertEquals("user-one", provisioner.updateCleanupPhysicalId(r));
|
||||
|
||||
UpdateCleanupResult result = provisioner.completeUpdate(r);
|
||||
|
||||
assertTrue(result.complete());
|
||||
verify(cognito).adminDeleteUser("pool-a", "user-one");
|
||||
verify(cognito, never()).adminDeleteUser(any(), eq("user-one-b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void changingAGeneratedNameUserReplacesItUnderANewGeneratedName() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "UserAttributes": [{"Name": "name", "Value": "One"}]}"""), ctx(null));
|
||||
String first = r.getPhysicalId();
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "UserAttributes": [{"Name": "name", "Value": "Two"}]}"""), ctx(first));
|
||||
|
||||
assertNotEquals(first, r.getPhysicalId());
|
||||
assertThat(r.getPhysicalId(), matchesPattern("my-stack-User-[a-z0-9]{12}"));
|
||||
verify(cognito).adminCreateUser("pool-a", r.getPhysicalId(), Map.of("name", "Two"), null, null, false);
|
||||
assertTrue(provisioner.hasReplacementUpdate(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cleanupDeletesTheDisplacedUserInItsOwnPool() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a"}"""), ctx(null));
|
||||
String first = r.getPhysicalId();
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-b"}"""), ctx(first));
|
||||
provisioner.completeUpdate(r);
|
||||
|
||||
verify(cognito).adminDeleteUser("pool-a", first);
|
||||
verify(cognito, never()).adminDeleteUser(eq("pool-b"), any());
|
||||
|
||||
provisioner.delete(r, "us-east-1");
|
||||
verify(cognito).adminDeleteUser("pool-b", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rollingBackAReplacementDeletesItAndRestoresThePriorUser() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
String original = """
|
||||
{"UserPoolId": "pool-a", "Username": "user-one"}""";
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props(original), ctx(null));
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-b", "Username": "user-one-b"}"""), ctx("user-one"));
|
||||
clearInvocations(cognito);
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
|
||||
verify(cognito).adminDeleteUser("pool-b", "user-one-b");
|
||||
verifyNoMoreInteractions(cognito);
|
||||
assertEquals("user-one", r.getPhysicalId());
|
||||
assertFalse(provisioner.hasReplacementUpdate(r));
|
||||
|
||||
// The createOnly record is the prior one again, so re-applying the original template is a no-op.
|
||||
clearInvocations(cognito);
|
||||
provisioner.provision(r, props(original), ctx("user-one"));
|
||||
verifyNoInteractions(cognito);
|
||||
|
||||
provisioner.delete(r, "us-east-1");
|
||||
verify(cognito).adminDeleteUser("pool-a", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anOlderUserStillOwedADeleteIsDeletedInItsOwnPool() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "user-a"}"""), ctx(null));
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-b", "Username": "user-b"}"""), ctx("user-a"));
|
||||
doThrow(new AwsException("InternalFailure", "storage unavailable", 500)).doNothing()
|
||||
.when(cognito).adminDeleteUser("pool-b", "user-b");
|
||||
assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r));
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-c", "Username": "user-c"}"""), ctx("user-a"));
|
||||
provisioner.completeUpdate(r);
|
||||
|
||||
verify(cognito, times(2)).adminDeleteUser("pool-b", "user-b");
|
||||
verify(cognito, never()).adminDeleteUser("pool-c", "user-b");
|
||||
verify(cognito).adminDeleteUser("pool-a", "user-a");
|
||||
}
|
||||
|
||||
@Test
|
||||
void poolRecordKeepsOnlyTheCurrentUserAndUsersStillOwedADelete() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "alice"}"""), ctx(null));
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-b", "Username": "bob"}"""), ctx("alice"));
|
||||
provisioner.completeUpdate(r);
|
||||
provisioner.clearUpdate(r);
|
||||
verify(cognito).adminDeleteUser("pool-a", "alice");
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-c", "Username": "carol"}"""), ctx("bob"));
|
||||
|
||||
JsonNode pools = mapper.readTree(r.getAttributes().get(POOLS_ATTR));
|
||||
Set<String> recorded = new HashSet<>();
|
||||
pools.fieldNames().forEachRemaining(recorded::add);
|
||||
assertEquals(Set.of("bob", "carol"), recorded, "a user deleted by a committed cleanup must leave the record");
|
||||
assertEquals("pool-b", pools.path("bob").asText());
|
||||
assertEquals("pool-c", pools.path("carol").asText());
|
||||
|
||||
provisioner.completeUpdate(r);
|
||||
|
||||
verify(cognito).adminDeleteUser("pool-b", "bob");
|
||||
verify(cognito, never()).adminDeleteUser(any(), eq("carol"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reorderedPropertiesOfACustomNamedUserAreNotAChange() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "user-one",
|
||||
"UserAttributes": [{"Name": "email", "Value": "one@example.com"}]}"""), ctx(null));
|
||||
clearInvocations(cognito);
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"UserAttributes": [{"Value": "one@example.com", "Name": "email"}],
|
||||
"Username": "user-one", "UserPoolId": "pool-a"}"""), ctx("user-one"));
|
||||
|
||||
verifyNoInteractions(cognito);
|
||||
assertEquals("user-one", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRollbackWhoseDeleteFailsStillRestoresThePriorCreateOnlyRecord() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
String original = """
|
||||
{"UserPoolId": "pool-a", "Username": "user-one"}""";
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props(original), ctx(null));
|
||||
String priorRecord = r.getAttributes().get(CREATE_ONLY_ATTR);
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "user-one-b"}"""), ctx("user-one"));
|
||||
doThrow(new AwsException("InternalFailure", "storage unavailable", 500))
|
||||
.when(cognito).adminDeleteUser("pool-a", "user-one-b");
|
||||
|
||||
assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r));
|
||||
|
||||
assertEquals(priorRecord, r.getAttributes().get(CREATE_ONLY_ATTR));
|
||||
clearInvocations(cognito);
|
||||
provisioner.provision(r, props(original), ctx("user-one"));
|
||||
verifyNoInteractions(cognito);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rollingBackWithoutAReplacementHasNothingToUndo() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "user-one"}"""), ctx(null));
|
||||
clearInvocations(cognito);
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
|
||||
verifyNoInteractions(cognito);
|
||||
assertEquals("user-one", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteUsesTheUsersPoolAndToleratesAUserOrPoolAlreadyGone() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "user-one"}"""), ctx(null));
|
||||
doThrow(new AwsException("UserNotFoundException", "User does not exist.", 400))
|
||||
.doThrow(new AwsException("ResourceNotFoundException", "User pool pool-a does not exist.", 400))
|
||||
.when(cognito).adminDeleteUser("pool-a", "user-one");
|
||||
|
||||
provisioner.delete(r, "us-east-1");
|
||||
provisioner.delete(r, "us-east-1");
|
||||
|
||||
verify(cognito, times(2)).adminDeleteUser("pool-a", "user-one");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteSurfacesAFailureThatIsNotAMissingUser() throws Exception {
|
||||
stubCreateEchoingTheUsername();
|
||||
StackResource r = resource(null);
|
||||
provisioner.provision(r, props("""
|
||||
{"UserPoolId": "pool-a", "Username": "user-one"}"""), ctx(null));
|
||||
doThrow(new AwsException("InternalFailure", "storage unavailable", 500))
|
||||
.when(cognito).adminDeleteUser("pool-a", "user-one");
|
||||
|
||||
AwsException thrown = assertThrows(AwsException.class, () -> provisioner.delete(r, "us-east-1"));
|
||||
|
||||
assertEquals("InternalFailure", thrown.getErrorCode());
|
||||
}
|
||||
|
||||
private void stubCreateEchoingTheUsername() {
|
||||
when(cognito.adminCreateUser(anyString(), anyString(), any(), any(), any(), anyBoolean()))
|
||||
.thenAnswer(inv -> {
|
||||
CognitoUser user = new CognitoUser();
|
||||
user.setUsername(inv.getArgument(1));
|
||||
return user;
|
||||
});
|
||||
}
|
||||
|
||||
private ProvisionContext ctx(String priorPhysicalId) {
|
||||
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
|
||||
when(engine.resolve(any())).thenAnswer(inv -> {
|
||||
JsonNode node = inv.getArgument(0);
|
||||
return node == null ? null : node.asText();
|
||||
});
|
||||
when(engine.resolveNode(any())).thenAnswer(inv -> {
|
||||
JsonNode node = inv.getArgument(0);
|
||||
return node == null ? null : node.deepCopy();
|
||||
});
|
||||
return new ProvisionContext(engine, "us-east-1", "000000000000", "my-stack", priorPhysicalId);
|
||||
}
|
||||
|
||||
private JsonNode props(String json) throws Exception {
|
||||
return mapper.readTree(json);
|
||||
}
|
||||
|
||||
private static StackResource resource(String physicalId) {
|
||||
StackResource r = new StackResource();
|
||||
r.setLogicalId("User");
|
||||
r.setResourceType(TYPE);
|
||||
r.setPhysicalId(physicalId);
|
||||
r.setAttributes(new HashMap<>());
|
||||
return r;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user