mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudformation): remove the login profile when deleting an IAM user (#3975)
Since login profiles became real state, DeleteUser returns DeleteConflict while one exists, so deleting a stack whose AWS::IAM::User was given a password out of band ended in DELETE_FAILED. The provisioner now deletes the login profile before the user, tolerating a user that has none. The resource-type table notes that the template LoginProfile and PermissionsBoundary properties are ignored.
This commit is contained in:
1 parent
b1e0751764
commit
8af15a71c4
6 files changed
+120
-1
No files matched your search
+41
@@ -12,6 +12,8 @@ import software.amazon.awssdk.services.cloudformation.model.DescribeStacksReques
|
||||
import software.amazon.awssdk.services.cloudformation.model.Output;
|
||||
import software.amazon.awssdk.services.cloudformation.model.Stack;
|
||||
import software.amazon.awssdk.services.iam.IamClient;
|
||||
import software.amazon.awssdk.services.iam.model.CreateLoginProfileRequest;
|
||||
import software.amazon.awssdk.services.iam.model.DeleteLoginProfileRequest;
|
||||
import software.amazon.awssdk.services.iam.model.DeleteUserRequest;
|
||||
import software.amazon.awssdk.services.iam.model.GetUserRequest;
|
||||
import software.amazon.awssdk.services.iam.model.GetUserResponse;
|
||||
@@ -110,6 +112,45 @@ class CloudFormationIamUserTest {
|
||||
.isInstanceOf(NoSuchEntityException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Deleting a stack removes a user whose login profile was created through the IAM API")
|
||||
void deleteStackRemovesUserWithLoginProfile() throws InterruptedException {
|
||||
String profileStackName = TestFixtures.uniqueName("compat-cfn-iam-profile");
|
||||
String profileUserName = TestFixtures.uniqueName("compat-profile-user");
|
||||
try {
|
||||
cloudFormation.createStack(CreateStackRequest.builder()
|
||||
.stackName(profileStackName)
|
||||
.templateBody(template(profileUserName, "/"))
|
||||
.build());
|
||||
assertThat(waitForTerminal(profileStackName, 30)).isEqualTo("CREATE_COMPLETE");
|
||||
|
||||
iam.createLoginProfile(CreateLoginProfileRequest.builder()
|
||||
.userName(profileUserName).password("Sdk-Test-P4ssword!").build());
|
||||
|
||||
cloudFormation.deleteStack(DeleteStackRequest.builder().stackName(profileStackName).build());
|
||||
waitForDeleted(profileStackName, 30);
|
||||
|
||||
assertThatThrownBy(() -> iam.getUser(GetUserRequest.builder().userName(profileUserName).build()))
|
||||
.isInstanceOf(NoSuchEntityException.class);
|
||||
} finally {
|
||||
try {
|
||||
cloudFormation.deleteStack(DeleteStackRequest.builder().stackName(profileStackName).build());
|
||||
} catch (Exception e) {
|
||||
LOG.log(Level.WARNING, "Failed to delete CloudFormation stack during cleanup: " + profileStackName, e);
|
||||
}
|
||||
try {
|
||||
iam.deleteLoginProfile(DeleteLoginProfileRequest.builder().userName(profileUserName).build());
|
||||
} catch (Exception ignored) {
|
||||
// The stack delete normally removes the profile with the user, so this is only a fallback.
|
||||
}
|
||||
try {
|
||||
iam.deleteUser(DeleteUserRequest.builder().userName(profileUserName).build());
|
||||
} catch (Exception ignored) {
|
||||
// The stack delete normally removes the user, so this is only a fallback.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String template(String name, String path) {
|
||||
return """
|
||||
{
|
||||
|
||||
@@ -129,7 +129,7 @@ cross-resource references.
|
||||
| SNS | `Topic`, `Subscription`, `TopicPolicy` |
|
||||
| DynamoDB | `Table`, `GlobalTable` |
|
||||
| Lambda | `Function` (Zip via S3/inline `ZipFile`, and Image), `LayerVersion`, `EventSourceMapping` (SQS, Kinesis, DynamoDB Streams), `Version`, `Alias` (also what SAM's `AutoPublishAlias` expands into), `Permission`, `EventInvokeConfig`, `MicrovmImage`, `NetworkConnector`, `Url`. Inline `ZipFile` packages include the `cfn-response` (Node.js) / `cfnresponse` (Python) module AWS injects for that code path, so Solutions-style custom-resource handlers work. |
|
||||
| IAM | `Role`, `User`, `AccessKey`, `Policy`, `ManagedPolicy`, `InstanceProfile` |
|
||||
| IAM | `Role`, `User` (template `LoginProfile` and `PermissionsBoundary` are ignored; an API-created login profile is removed on delete), `AccessKey`, `Policy`, `ManagedPolicy`, `InstanceProfile` |
|
||||
| Organizations | `Organization`, `OrganizationalUnit`, `Account`, `Policy`, `ResourcePolicy` |
|
||||
| SSM | `Parameter` |
|
||||
| KMS | `Key`, `Alias` |
|
||||
|
||||
+3
@@ -314,6 +314,9 @@ public class IamUserCfnProvisioner implements CfnResourceProvisioner {
|
||||
LOG.debugv("IAM user access keys already gone, treating as deleted: {0}", physicalId);
|
||||
}
|
||||
|
||||
CfnDeletes.safeDelete("login profile on user", physicalId,
|
||||
() -> iamService.deleteLoginProfile(physicalId), "NoSuchEntity");
|
||||
|
||||
CfnDeletes.safeDelete("IAM user", physicalId,
|
||||
() -> iamService.deleteUser(physicalId), "NoSuchEntity");
|
||||
}
|
||||
|
||||
+48
@@ -94,6 +94,54 @@ class CloudFormationIamUserIntegrationTest {
|
||||
awaitStackStatus(newStackId, "DELETE_COMPLETE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteStackDeletesUserWithLoginProfileCreatedOutOfBand() throws InterruptedException {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String userName = "probe-profile-user-" + suffix;
|
||||
String stackName = "cfn-profile-stack-" + suffix;
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"ProfileUser": {
|
||||
"Type": "AWS::IAM::User",
|
||||
"Properties": {
|
||||
"UserName": "%s"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(userName);
|
||||
|
||||
String stackId = createStack(stackName, template);
|
||||
awaitStackStatus(stackId, "CREATE_COMPLETE");
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.formParam("Action", "CreateLoginProfile")
|
||||
.formParam("UserName", userName)
|
||||
.formParam("Password", "Sup3r$ecret!")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
deleteStack(stackName);
|
||||
awaitStackStatus(stackId, "DELETE_COMPLETE");
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", IAM_AUTH)
|
||||
.formParam("Action", "GetUser")
|
||||
.formParam("UserName", userName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body(containsString("NoSuchEntity"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteStackDeletesUserWithGeneratedName() throws InterruptedException {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
|
||||
+26
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.model.AccessKey;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamUser;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -21,6 +22,7 @@ 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.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -203,6 +205,30 @@ class IamUserCfnProvisionerTest {
|
||||
verify(iam).deleteUser("my-user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteRemovesLoginProfileBeforeDeletingUser() {
|
||||
IamUser user = new IamUser("AIDAuser", "my-user", "/", "arn:aws:iam::" + ACCOUNT_ID + ":user/my-user");
|
||||
when(iam.getUser("my-user")).thenReturn(user);
|
||||
|
||||
provisioner.delete("AWS::IAM::User", "my-user", "us-east-1");
|
||||
|
||||
InOrder order = inOrder(iam);
|
||||
order.verify(iam).deleteLoginProfile("my-user");
|
||||
order.verify(iam).deleteUser("my-user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteToleratesUserWithoutLoginProfile() {
|
||||
IamUser user = new IamUser("AIDAuser", "my-user", "/", "arn:aws:iam::" + ACCOUNT_ID + ":user/my-user");
|
||||
when(iam.getUser("my-user")).thenReturn(user);
|
||||
doThrow(new AwsException("NoSuchEntity", "Login Profile for User my-user cannot be found.", 404))
|
||||
.when(iam).deleteLoginProfile("my-user");
|
||||
|
||||
provisioner.delete("AWS::IAM::User", "my-user", "us-east-1");
|
||||
|
||||
verify(iam).deleteUser("my-user");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteToleratesAlreadyDeletedUser() {
|
||||
when(iam.getUser("already-gone"))
|
||||
|
||||
@@ -160,6 +160,7 @@ notes:
|
||||
AWS::Redshift::ClusterSecurityGroup: accepted; no EC2-Classic security group model
|
||||
AWS::Redshift::Cluster: single-node container; Port and non-dev DBName ignored; ManageMasterPassword unsupported
|
||||
AWS::CDK::Metadata: accepted; no-op
|
||||
AWS::IAM::User: 'template `LoginProfile` and `PermissionsBoundary` are ignored; an API-created login profile is removed on delete'
|
||||
AWS::IoT::DomainConfiguration: '`ServerCertificates` resolves to a JSON string'
|
||||
AWS::IoT::Policy: 'deleted after detaching it from its principals; on AWS the delete fails with `DeleteConflictException` while the policy is attached'
|
||||
Custom::DynamoDBReplica: applied natively against DynamoDB, not via a provider Lambda
|
||||
|
||||
Reference in new issue
Block a user