Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudformation): remove the login profile when deleting an IAM user (#3975)

Priamo Germosen8af15a71c411
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationIamUserTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationIamUserTest.java
index 67cff63f5c..0170a69a39 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationIamUserTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationIamUserTest.java
@@ -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 """
{
diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md
index a7c05f3817..c36791a1b5 100644
--- a/docs/services/cloudformation.md
+++ b/docs/services/cloudformation.md
@@ -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` |
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamUserCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamUserCfnProvisioner.java
index f1a9b83a55..2a39cd0847 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamUserCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamUserCfnProvisioner.java
@@ -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");
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIamUserIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIamUserIntegrationTest.java
index 4a8f05aaab..880dd32a7a 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIamUserIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIamUserIntegrationTest.java
@@ -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);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamUserCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamUserCfnProvisionerTest.java
index e759f0e192..a1306b16b3 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamUserCfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamUserCfnProvisionerTest.java
@@ -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"))
diff --git a/tools/docs/cfn_resource_types.yaml b/tools/docs/cfn_resource_types.yaml
index 12c2e81138..0d7cf7086d 100644
--- a/tools/docs/cfn_resource_types.yaml
+++ b/tools/docs/cfn_resource_types.yaml
@@ -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