mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): roll back in-place Cognito user pool and client updates (#5006)
* fix(cloudformation): roll back in-place Cognito user pool and client updates A stack update that changed a user pool or user pool client in place and then failed on a later resource ended in UPDATE_ROLLBACK_FAILED, because the provisioner kept no snapshot to put them back. It now snapshots the settings an update can change before the update call and restores them on rollback, spending the snapshot only once the restore succeeds. UpdateUserPool and UpdateUserPoolClient only apply what a request names, so they cannot clear a setting the update added. CognitoService gains userPoolSettings/restoreUserPoolSettings and their client counterparts, which copy exactly those settings, leave the pool's signing keys and the client's secrets out of the snapshot, and put the settings back on the stored entity. * fix(cloudformation): drop the Cognito update snapshot when the update commits Greptile on #5006: once a stack update commits, the engine calls completeUpdate for every resource but clearUpdate only after a replacement's cleanup, so an in-place pool or client update left its settings snapshot in the stored stack metadata until the resource was next provisioned. completeUpdate now drops it before delegating to the replacement cleanup; the update committed and nothing will roll it back. Also orders the test's org imports (org.junit before org.mockito), which were already out of order on main.
This commit is contained in:
1 parent
0740adf9ae
commit
7ffe43f48f
6 files changed
+447
-9
No files matched your search
+147
@@ -0,0 +1,147 @@
|
||||
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.Response;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* A stack update that changes a Cognito user pool and its client in place and then fails on a later
|
||||
* resource rolls both back: the pool and the client carry the settings they had before the update,
|
||||
* including a setting the update added being cleared again, and the stack ends in
|
||||
* UPDATE_ROLLBACK_COMPLETE rather than UPDATE_ROLLBACK_FAILED.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationCognitoUpdateRollbackIntegrationTest {
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
|
||||
private static final String COGNITO_CONTENT_TYPE = "application/x-amz-json-1.1";
|
||||
|
||||
private static final String TEMPLATE = """
|
||||
{
|
||||
"Resources": {
|
||||
"Pool": {
|
||||
"Type": "AWS::Cognito::UserPool",
|
||||
"Properties": {
|
||||
"UserPoolName": "%s",
|
||||
"UserPoolTags": {"team": "%s"}%s
|
||||
}
|
||||
},
|
||||
"Client": {
|
||||
"Type": "AWS::Cognito::UserPoolClient",
|
||||
"Properties": {"UserPoolId": {"Ref": "Pool"}, "ClientName": "%s"}
|
||||
}%s
|
||||
},
|
||||
"Outputs": {
|
||||
"PoolId": {"Value": {"Ref": "Pool"}},
|
||||
"ClientId": {"Value": {"Ref": "Client"}}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
private static final String FAILING_RESOURCE = """
|
||||
,
|
||||
"BadSecret": {
|
||||
"Type": "AWS::SecretsManager::Secret",
|
||||
"DependsOn": "Client",
|
||||
"Properties": {
|
||||
"Name": "cognito-rollback-%s",
|
||||
"SecretString": "explicit",
|
||||
"GenerateSecretString": {"PasswordLength": 32}
|
||||
}
|
||||
}""";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailedUpdatePutsThePoolAndTheClientBack() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stack = "cfn-cognito-rb-" + suffix;
|
||||
String poolName = "pool-rb-" + suffix;
|
||||
|
||||
cloudFormation(stack, "CreateStack", TEMPLATE.formatted(poolName, "auth", "", "web", ""));
|
||||
Map<String, String> outputs = outputs(describeStacks(stack, "CREATE_COMPLETE"));
|
||||
String poolId = outputs.get("PoolId");
|
||||
String clientId = outputs.get("ClientId");
|
||||
|
||||
String failingUpdate = TEMPLATE.formatted(poolName + "-v2", "data",
|
||||
",\n \"AutoVerifiedAttributes\": [\"email\"]", "web-v2", FAILING_RESOURCE.formatted(suffix));
|
||||
cloudFormation(stack, "UpdateStack", failingUpdate);
|
||||
describeStacks(stack, "UPDATE_ROLLBACK_COMPLETE");
|
||||
|
||||
describeUserPool(poolId).then()
|
||||
.statusCode(200)
|
||||
.body("UserPool.Name", equalTo(poolName))
|
||||
.body("UserPool.UserPoolTags.team", equalTo("auth"))
|
||||
.body("UserPool.AutoVerifiedAttributes", empty());
|
||||
describeUserPoolClient(poolId, clientId).then()
|
||||
.statusCode(200)
|
||||
.body("UserPoolClient.ClientName", equalTo("web"));
|
||||
|
||||
cloudFormation(stack, "DeleteStack", null);
|
||||
CfnStackWaits.awaitStackDeleted(stack);
|
||||
}
|
||||
|
||||
private static void cloudFormation(String stack, String action, String templateBody) {
|
||||
if (templateBody == null) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", action)
|
||||
.formParam("StackName", stack)
|
||||
.when().post("/").then().statusCode(200);
|
||||
return;
|
||||
}
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", action)
|
||||
.formParam("StackName", stack)
|
||||
.formParam("TemplateBody", templateBody)
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static String describeStacks(String stack, String expectedStatus) {
|
||||
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 Map<String, String> outputs(String describeStacksXml) {
|
||||
return XmlParser.extractPairs(describeStacksXml, "Outputs", "OutputKey", "OutputValue");
|
||||
}
|
||||
|
||||
private static Response describeUserPool(String poolId) {
|
||||
return given()
|
||||
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.DescribeUserPool")
|
||||
.contentType(COGNITO_CONTENT_TYPE)
|
||||
.body("{\"UserPoolId\": \"" + poolId + "\"}")
|
||||
.when().post("/");
|
||||
}
|
||||
|
||||
private static Response describeUserPoolClient(String poolId, String clientId) {
|
||||
return given()
|
||||
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.DescribeUserPoolClient")
|
||||
.contentType(COGNITO_CONTENT_TYPE)
|
||||
.body("{\"UserPoolId\": \"" + poolId + "\", \"ClientId\": \"" + clientId + "\"}")
|
||||
.when().post("/");
|
||||
}
|
||||
}
|
||||
+94
-5
@@ -10,8 +10,9 @@ import io.github.hectorvent.floci.services.cognito.CognitoService;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPool;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPoolClient;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPoolDomain;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -35,6 +36,7 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -807,14 +809,101 @@ class CognitoCfnProvisionerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInPlaceUserPoolClientUpdateCannotBeRolledBack() {
|
||||
void anInPlaceUserPoolClientUpdateIsRolledBackFromTheSnapshotTakenBeforeIt() {
|
||||
when(cognito.describeUserPoolClient(CLIENT_ID)).thenReturn(client("web", null));
|
||||
stubClientUpdate(client("web", null));
|
||||
UserPoolClient before = client("web", null);
|
||||
before.setCallbackURLs(List.of("https://before.example.com/callback"));
|
||||
when(cognito.userPoolClientSettings(POOL_ID, CLIENT_ID)).thenReturn(before);
|
||||
stubClientUpdate(client("web-renamed", null));
|
||||
StackResource r = resource(USER_POOL_CLIENT, "Client");
|
||||
r.setPhysicalId(CLIENT_ID);
|
||||
provisioner.provision(r, mapper.createObjectNode().put("UserPoolId", POOL_ID), ctx(CLIENT_ID));
|
||||
provisioner.provision(r, mapper.createObjectNode().put("UserPoolId", POOL_ID).put("ClientName", "web-renamed"),
|
||||
ctx(CLIENT_ID));
|
||||
|
||||
assertFalse(provisioner.rollbackUpdate(r));
|
||||
InOrder order = inOrder(cognito);
|
||||
order.verify(cognito).userPoolClientSettings(POOL_ID, CLIENT_ID);
|
||||
order.verify(cognito).updateUserPoolClient(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
|
||||
ArgumentCaptor<UserPoolClient> restored = ArgumentCaptor.forClass(UserPoolClient.class);
|
||||
verify(cognito).restoreUserPoolClientSettings(restored.capture());
|
||||
assertEquals(CLIENT_ID, restored.getValue().getClientId());
|
||||
assertEquals("web", restored.getValue().getClientName());
|
||||
assertEquals(List.of("https://before.example.com/callback"), restored.getValue().getCallbackURLs());
|
||||
assertFalse(r.getAttributes().containsKey(CfnRollback.COGNITO_UPDATE_SNAPSHOT_ATTR), "the snapshot is spent");
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInPlaceUserPoolUpdateIsRolledBackFromTheSnapshotTakenBeforeIt() {
|
||||
UserPool before = pool(POOL_ID, "my-pool");
|
||||
before.setMfaConfiguration("OFF");
|
||||
when(cognito.userPoolSettings(POOL_ID)).thenReturn(before);
|
||||
when(cognito.updateUserPool(any(), eq(REGION))).thenReturn(pool(POOL_ID, "renamed-pool"));
|
||||
StackResource r = resource(USER_POOL, "Pool");
|
||||
r.setPhysicalId(POOL_ID);
|
||||
provisioner.provision(r, mapper.createObjectNode().put("UserPoolName", "renamed-pool")
|
||||
.put("MfaConfiguration", "OPTIONAL"), ctx(POOL_ID));
|
||||
|
||||
InOrder order = inOrder(cognito);
|
||||
order.verify(cognito).userPoolSettings(POOL_ID);
|
||||
order.verify(cognito).updateUserPool(any(), eq(REGION));
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(r));
|
||||
|
||||
ArgumentCaptor<UserPool> restored = ArgumentCaptor.forClass(UserPool.class);
|
||||
verify(cognito).restoreUserPoolSettings(restored.capture());
|
||||
assertEquals(POOL_ID, restored.getValue().getId());
|
||||
assertEquals("my-pool", restored.getValue().getName());
|
||||
assertEquals("OFF", restored.getValue().getMfaConfiguration());
|
||||
assertFalse(r.getAttributes().containsKey(CfnRollback.COGNITO_UPDATE_SNAPSHOT_ATTR), "the snapshot is spent");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailedRestoreKeepsTheSnapshotForTheNextAttempt() {
|
||||
when(cognito.userPoolSettings(POOL_ID)).thenReturn(pool(POOL_ID, "my-pool"));
|
||||
when(cognito.updateUserPool(any(), eq(REGION))).thenReturn(pool(POOL_ID, "renamed-pool"));
|
||||
when(cognito.restoreUserPoolSettings(any()))
|
||||
.thenThrow(new AwsException("InternalErrorException", "storage unavailable", 500));
|
||||
StackResource r = resource(USER_POOL, "Pool");
|
||||
r.setPhysicalId(POOL_ID);
|
||||
provisioner.provision(r, mapper.createObjectNode().put("UserPoolName", "renamed-pool"), ctx(POOL_ID));
|
||||
|
||||
assertThrows(AwsException.class, () -> provisioner.rollbackUpdate(r));
|
||||
|
||||
assertTrue(r.getAttributes().containsKey(CfnRollback.COGNITO_UPDATE_SNAPSHOT_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInPlaceUpdateDropsItsSnapshotWhenTheUpdateCommits() {
|
||||
// The engine's post-commit cleanup calls completeUpdate for every resource, but clearUpdate
|
||||
// only after a replacement, so an in-place update relies on completeUpdate.
|
||||
when(cognito.userPoolSettings(POOL_ID)).thenReturn(pool(POOL_ID, "my-pool"));
|
||||
when(cognito.updateUserPool(any(), eq(REGION))).thenReturn(pool(POOL_ID, "renamed-pool"));
|
||||
StackResource r = resource(USER_POOL, "Pool");
|
||||
r.setPhysicalId(POOL_ID);
|
||||
provisioner.provision(r, mapper.createObjectNode().put("UserPoolName", "renamed-pool"), ctx(POOL_ID));
|
||||
assertTrue(r.getAttributes().containsKey(CfnRollback.COGNITO_UPDATE_SNAPSHOT_ATTR));
|
||||
|
||||
assertFalse(provisioner.completeUpdate(r).applicable(), "an in-place update owes no replacement cleanup");
|
||||
|
||||
assertFalse(r.getAttributes().containsKey(CfnRollback.COGNITO_UPDATE_SNAPSHOT_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCommittedUpdateDropsTheSnapshotAndTheNextProvisionStartsWithoutOne() {
|
||||
when(cognito.userPoolSettings(POOL_ID)).thenReturn(pool(POOL_ID, "my-pool"));
|
||||
when(cognito.updateUserPool(any(), eq(REGION))).thenReturn(pool(POOL_ID, "renamed-pool"));
|
||||
StackResource r = resource(USER_POOL, "Pool");
|
||||
r.setPhysicalId(POOL_ID);
|
||||
provisioner.provision(r, mapper.createObjectNode().put("UserPoolName", "renamed-pool"), ctx(POOL_ID));
|
||||
|
||||
provisioner.clearUpdate(r);
|
||||
|
||||
assertFalse(r.getAttributes().containsKey(CfnRollback.COGNITO_UPDATE_SNAPSHOT_ATTR));
|
||||
assertTrue(provisioner.rollbackUpdate(r), "nothing to undo once the update committed");
|
||||
verify(cognito, never()).restoreUserPoolSettings(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -171,6 +171,43 @@ class CognitoServiceTest {
|
||||
assertEquals(List.of("email"), pool.getUsernameAttributes());
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreUserPoolSettingsPutsBackWhatAnUpdateChangedAndKeepsTheSigningKeys() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "before"), "us-east-1");
|
||||
String signingKeyId = service.describeUserPool(pool.getId()).getSigningKeyId();
|
||||
UserPool settings = service.userPoolSettings(pool.getId());
|
||||
assertNull(settings.getSigningPrivateKey(), "the settings copy carries no signing material");
|
||||
|
||||
service.updateUserPool(Map.of("UserPoolId", pool.getId(), "PoolName", "after",
|
||||
"AutoVerifiedAttributes", List.of("email"), "MfaConfiguration", "OPTIONAL"), "us-east-1");
|
||||
service.restoreUserPoolSettings(settings);
|
||||
|
||||
UserPool restored = service.describeUserPool(pool.getId());
|
||||
assertEquals("before", restored.getName());
|
||||
assertEquals(List.of(), restored.getAutoVerifiedAttributes(), "a setting the update added is cleared");
|
||||
assertEquals("OFF", restored.getMfaConfiguration());
|
||||
assertEquals(signingKeyId, restored.getSigningKeyId(), "the pool keeps the keys it signs with");
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreUserPoolClientSettingsPutsBackWhatAnUpdateChanged() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "pool"), "us-east-1");
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "web", false, false, List.of(), List.of());
|
||||
UserPoolClient settings = service.userPoolClientSettings(pool.getId(), client.getClientId());
|
||||
// The stored client is updated in place, so the expectation is copied before the update.
|
||||
List<String> flowsBefore = List.copyOf(client.getExplicitAuthFlows());
|
||||
|
||||
service.updateUserPoolClient(pool.getId(), client.getClientId(), "web-v2", null, null, null, null,
|
||||
null, null, List.of("ALLOW_USER_PASSWORD_AUTH"), null, null, null, null, null, null, null,
|
||||
null, null, null, null);
|
||||
service.restoreUserPoolClientSettings(settings);
|
||||
|
||||
UserPoolClient restored = service.describeUserPoolClient(pool.getId(), client.getClientId());
|
||||
assertEquals("web", restored.getClientName());
|
||||
assertEquals(flowsBefore, restored.getExplicitAuthFlows());
|
||||
assertEquals(client.getClientId(), restored.getClientId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void initiateAuthRejectsUnrecognizedAuthFlowWithoutIssuingTokens() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
|
||||
Reference in new issue
Block a user