mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): answer a blocked delete with the sentence for the dependency that blocks it (#9220)
IAM names exactly one blocking dependent and words each class with its own wholly fixed sentence, no entity name and no count, because a cleanup loop reads the sentence to choose its next step. Four of those recordings bear on this package and three of them diverged. - DeleteRole read a role's attached and inline policies together and answered the detach sentence for both, so an inline policy was told to detach something. The two are reported apart now: "Cannot delete entity, must detach all policies first." for a managed attachment and "Cannot delete entity, must delete policies first." for an embedded policy, which is the remedy that clears what is there. DeleteGroup conflated them the same way and selects the same two sentences now; DeleteUser already did and is the model both follow. - DeleteRole never looked at instance profiles, so a role a profile held was deleted and left the profile naming nothing. A role an instance profile holds is "Cannot delete entity, must remove roles from instance profile first." and the profile has to let go of it first, as on AWS. The profile is reported ahead of an attached policy, which is recorded for a role that is both held and carrying attachments, and is what a cleanup loop needs, since detaching changes nothing while the pairing is there. - DeleteInstanceProfile wording its own refusal now carries that same sentence, which is AWS's for both ends of the pairing. The new membership check and the emptiness check it joins are both check-then-act, so AddRoleToInstanceProfile takes the lock the deletes decide under, outside the profile's own monitor as every path here already does: one order throughout, and no pairing can land between a delete's read and what it does with it. Nothing takes a role out of a profile on the caller's behalf, on AWS or here. The CloudFormation role provisioner keeps clearing the policies its stack owns and lets the conflict reach the stack, with a provisioner case for it; a stack owning both resources deletes the profile first, since dependents go before what they depend on. The test fixture that tears a role down removes it from any profile first, ahead of its policies, so teardown asks for the remedies in the order the refusals name. The tolerance behind RemoveRoleFromInstanceProfile stays for state reached another way, and the test that reached it by deleting the role first now asserts the refusal and clears the pairing before the role. Which profiles hold a role is kept as an index beside the profiles themselves, written in the same two places a pairing is and read only under that lock, so the check costs a lookup rather than a read of every profile in the account while the lock is held. A scope is filled from storage the first time it is asked about, so a pairing a restart inherited is in it before the first answer. The two callers that delete a role of their own clear its policies first, so each asks about the profile before it clears anything: otherwise a role that turns out not to be deletable would be left in place with its permissions stripped by a delete that could not finish.
This commit is contained in:
1 parent
9af8ca07a9
commit
82fdb36228
10 files changed
+602
-54
No files matched your search
+27
@@ -648,6 +648,33 @@ class IamRoleCfnProvisionerTest {
|
||||
order.verify(iam).deleteRole("app-role");
|
||||
}
|
||||
|
||||
/**
|
||||
* A role an instance profile still holds refuses the delete, and that refusal reaches the
|
||||
* stack rather than being swallowed: AWS answers the same way, and taking a role out of a
|
||||
* profile this stack may not own is not the provisioner's to do. It is asked before anything
|
||||
* is cleared, so the role that stays keeps the policies it had rather than being emptied by a
|
||||
* delete that could not finish.
|
||||
*/
|
||||
@Test
|
||||
void deleteLetsAnInstanceProfileConflictReachTheStackWithNothingCleared() {
|
||||
IamRole role = new IamRole("AROAheld-role", "held-role", "/",
|
||||
"arn:aws:iam::" + ACCOUNT_ID + ":role/held-role", EMPTY_TRUST);
|
||||
role.getAttachedPolicyArns().add("arn:aws:iam::aws:policy/ReadOnlyAccess");
|
||||
role.getInlinePolicies().put("bucket-read", EMPTY_TRUST);
|
||||
when(iam.getRole("held-role")).thenReturn(role);
|
||||
doThrow(new AwsException("DeleteConflict",
|
||||
"Cannot delete entity, must remove roles from instance profile first.", 409))
|
||||
.when(iam).requireNotHeldByAnInstanceProfile("held-role");
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> provisioner.delete("AWS::IAM::Role", "held-role", "us-east-1"));
|
||||
|
||||
assertEquals("DeleteConflict", refused.getErrorCode());
|
||||
verify(iam, never()).detachRolePolicy(anyString(), anyString());
|
||||
verify(iam, never()).deleteRolePolicy(anyString(), anyString());
|
||||
verify(iam, never()).deleteRole(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteTreatsAnAlreadyMissingRoleAsDeleted() {
|
||||
when(iam.getRole("gone-role")).thenThrow(new AwsException("NoSuchEntity", "gone", 404));
|
||||
|
||||
+254
@@ -0,0 +1,254 @@
|
||||
package io.floci.aws.services.iam;
|
||||
|
||||
import io.floci.aws.testing.PartitionCleanup;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.anyOf;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
/**
|
||||
* Which dependency a blocked delete names, and in the sentence AWS was recorded answering. Three
|
||||
* sentences cover the four refusals: an instance profile still holding the role, a managed policy
|
||||
* that has to be detached, and an inline policy that has to be deleted. Each is entirely fixed
|
||||
* text, so a caller reading it to pick its next step gets the dependency that is actually there.
|
||||
*
|
||||
* <p>{@code docs/services/iam.md} carries the recordings and says which part of the ordering is a
|
||||
* reading rather than one of them.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class IamDeleteConflictDependencyIntegrationTest {
|
||||
|
||||
private static final String IAM_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20261122/us-east-1/iam/aws4_request";
|
||||
|
||||
private static final String CODE = "ErrorResponse.Error.Code";
|
||||
private static final String MESSAGE = "ErrorResponse.Error.Message";
|
||||
|
||||
private static final String MUST_DETACH_POLICIES =
|
||||
"Cannot delete entity, must detach all policies first.";
|
||||
private static final String MUST_DELETE_POLICIES =
|
||||
"Cannot delete entity, must delete policies first.";
|
||||
private static final String MUST_REMOVE_PROFILE_ROLES =
|
||||
"Cannot delete entity, must remove roles from instance profile first.";
|
||||
|
||||
private static final String TRUST_POLICY = "{\"Version\":\"2012-10-17\",\"Statement\":"
|
||||
+ "[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"ec2.amazonaws.com\"},"
|
||||
+ "\"Action\":\"sts:AssumeRole\"}]}";
|
||||
|
||||
private static final String PERMISSION_POLICY = "{\"Version\":\"2012-10-17\",\"Statement\":"
|
||||
+ "[{\"Effect\":\"Allow\",\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}";
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
/**
|
||||
* What a teardown step accepts. A test here may clear a dependency itself, to show the delete
|
||||
* going through once it is gone, so a resource that is already absent is the step's work
|
||||
* already done rather than a leak. Anything else, a refusal above all, still reports.
|
||||
*/
|
||||
private static void cleared(RequestSpecification request) {
|
||||
request.when().post("/").then().statusCode(anyOf(is(200), is(404)));
|
||||
}
|
||||
|
||||
private static RequestSpecification iam(String action) {
|
||||
return given().header("Authorization", IAM_AUTH).formParam("Action", action);
|
||||
}
|
||||
|
||||
private static String unique(String prefix) {
|
||||
return prefix + "-" + Long.toString(System.nanoTime(), 36);
|
||||
}
|
||||
|
||||
private String createRole() {
|
||||
String roleName = unique("conflict-role");
|
||||
iam("CreateRole").formParam("RoleName", roleName)
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.when().post("/").then().statusCode(200);
|
||||
cleanup.register(() -> cleared(iam("DeleteRole").formParam("RoleName", roleName)));
|
||||
return roleName;
|
||||
}
|
||||
|
||||
private String createGroup() {
|
||||
String groupName = unique("conflict-group");
|
||||
iam("CreateGroup").formParam("GroupName", groupName)
|
||||
.when().post("/").then().statusCode(200);
|
||||
cleanup.register(() -> cleared(iam("DeleteGroup").formParam("GroupName", groupName)));
|
||||
return groupName;
|
||||
}
|
||||
|
||||
private String createUser() {
|
||||
String userName = unique("conflict-user");
|
||||
iam("CreateUser").formParam("UserName", userName)
|
||||
.when().post("/").then().statusCode(200);
|
||||
cleanup.register(() -> cleared(iam("DeleteUser").formParam("UserName", userName)));
|
||||
return userName;
|
||||
}
|
||||
|
||||
private String pairedProfile(String roleName) {
|
||||
String profileName = unique("conflict-profile");
|
||||
iam("CreateInstanceProfile").formParam("InstanceProfileName", profileName)
|
||||
.when().post("/").then().statusCode(200);
|
||||
cleanup.register(() -> cleared(iam("DeleteInstanceProfile")
|
||||
.formParam("InstanceProfileName", profileName)));
|
||||
iam("AddRoleToInstanceProfile").formParam("InstanceProfileName", profileName)
|
||||
.formParam("RoleName", roleName).when().post("/").then().statusCode(200);
|
||||
cleanup.register(() -> cleared(iam("RemoveRoleFromInstanceProfile")
|
||||
.formParam("InstanceProfileName", profileName).formParam("RoleName", roleName)));
|
||||
return profileName;
|
||||
}
|
||||
|
||||
private String managedPolicy() {
|
||||
String arn = iam("CreatePolicy").formParam("PolicyName", unique("conflict-policy"))
|
||||
.formParam("PolicyDocument", PERMISSION_POLICY)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().xmlPath().getString("CreatePolicyResponse.CreatePolicyResult.Policy.Arn");
|
||||
cleanup.register(() -> cleared(iam("DeletePolicy").formParam("PolicyArn", arn)));
|
||||
return arn;
|
||||
}
|
||||
|
||||
private String attachToRole(String roleName) {
|
||||
return attach("AttachRolePolicy", "DetachRolePolicy", "RoleName", roleName);
|
||||
}
|
||||
|
||||
private String attachToGroup(String groupName) {
|
||||
return attach("AttachGroupPolicy", "DetachGroupPolicy", "GroupName", groupName);
|
||||
}
|
||||
|
||||
/** Attaches a managed policy to the entity and hands back its ARN. */
|
||||
private String attach(String action, String undo, String param, String entityName) {
|
||||
String arn = managedPolicy();
|
||||
iam(action).formParam(param, entityName).formParam("PolicyArn", arn)
|
||||
.when().post("/").then().statusCode(200);
|
||||
cleanup.register(() -> cleared(
|
||||
iam(undo).formParam(param, entityName).formParam("PolicyArn", arn)));
|
||||
return arn;
|
||||
}
|
||||
|
||||
private String inlineOnRole(String roleName) {
|
||||
return putInline("PutRolePolicy", "DeleteRolePolicy", "RoleName", roleName);
|
||||
}
|
||||
|
||||
/** Embeds an inline policy in the entity and hands back its name. */
|
||||
private String putInline(String action, String undo, String param, String entityName) {
|
||||
String policyName = unique("conflict-inline");
|
||||
iam(action).formParam(param, entityName).formParam("PolicyName", policyName)
|
||||
.formParam("PolicyDocument", PERMISSION_POLICY)
|
||||
.when().post("/").then().statusCode(200);
|
||||
cleanup.register(() -> cleared(iam(undo).formParam(param, entityName)
|
||||
.formParam("PolicyName", policyName)));
|
||||
return policyName;
|
||||
}
|
||||
|
||||
/** The role an instance profile holds: the caller is told to take it out of the profile. */
|
||||
@Test
|
||||
void aRoleAnInstanceProfileHoldsNamesTheProfile() {
|
||||
String roleName = createRole();
|
||||
pairedProfile(roleName);
|
||||
iam("DeleteRole").formParam("RoleName", roleName)
|
||||
.when().post("/").then().statusCode(409)
|
||||
.body(CODE, equalTo("DeleteConflict"))
|
||||
.body(MESSAGE, equalTo(MUST_REMOVE_PROFILE_ROLES));
|
||||
}
|
||||
|
||||
/** The profile holding it is refused with the same sentence, which is AWS's for both ends. */
|
||||
@Test
|
||||
void theProfileHoldingARoleCarriesTheSameSentence() {
|
||||
String profileName = pairedProfile(createRole());
|
||||
iam("DeleteInstanceProfile").formParam("InstanceProfileName", profileName)
|
||||
.when().post("/").then().statusCode(409)
|
||||
.body(CODE, equalTo("DeleteConflict"))
|
||||
.body(MESSAGE, equalTo(MUST_REMOVE_PROFILE_ROLES));
|
||||
}
|
||||
|
||||
/**
|
||||
* Both dependencies at once: the profile is reported, which is the precedence AWS was recorded
|
||||
* answering and the one a cleanup loop needs, since detaching the policy changes nothing here.
|
||||
*/
|
||||
@Test
|
||||
void theProfileIsNamedAheadOfTheAttachedPolicy() {
|
||||
String roleName = createRole();
|
||||
pairedProfile(roleName);
|
||||
attachToRole(roleName);
|
||||
iam("DeleteRole").formParam("RoleName", roleName)
|
||||
.when().post("/").then().statusCode(409)
|
||||
.body(MESSAGE, equalTo(MUST_REMOVE_PROFILE_ROLES));
|
||||
}
|
||||
|
||||
/** A managed policy is detached, and the sentence says so. */
|
||||
@Test
|
||||
void aRoleCarryingAManagedPolicyAsksForADetach() {
|
||||
String roleName = createRole();
|
||||
attachToRole(roleName);
|
||||
iam("DeleteRole").formParam("RoleName", roleName)
|
||||
.when().post("/").then().statusCode(409)
|
||||
.body(CODE, equalTo("DeleteConflict"))
|
||||
.body(MESSAGE, equalTo(MUST_DETACH_POLICIES));
|
||||
}
|
||||
|
||||
/** An inline policy is deleted rather than detached, and the sentence says that instead. */
|
||||
@Test
|
||||
void aRoleCarryingAnInlinePolicyAsksForADelete() {
|
||||
String roleName = createRole();
|
||||
inlineOnRole(roleName);
|
||||
iam("DeleteRole").formParam("RoleName", roleName)
|
||||
.when().post("/").then().statusCode(409)
|
||||
.body(CODE, equalTo("DeleteConflict"))
|
||||
.body(MESSAGE, equalTo(MUST_DELETE_POLICIES));
|
||||
}
|
||||
|
||||
/** The group keeps the detach sentence for a managed policy, as the user already does. */
|
||||
@Test
|
||||
void aGroupCarryingAManagedPolicyAsksForADetach() {
|
||||
String groupName = createGroup();
|
||||
attachToGroup(groupName);
|
||||
iam("DeleteGroup").formParam("GroupName", groupName)
|
||||
.when().post("/").then().statusCode(409)
|
||||
.body(CODE, equalTo("DeleteConflict"))
|
||||
.body(MESSAGE, equalTo(MUST_DETACH_POLICIES));
|
||||
}
|
||||
|
||||
/** The group's inline policy is a delete, the remedy that actually clears it. */
|
||||
@Test
|
||||
void aGroupCarryingAnInlinePolicyAsksForADelete() {
|
||||
String groupName = createGroup();
|
||||
putInline("PutGroupPolicy", "DeleteGroupPolicy", "GroupName", groupName);
|
||||
iam("DeleteGroup").formParam("GroupName", groupName)
|
||||
.when().post("/").then().statusCode(409)
|
||||
.body(CODE, equalTo("DeleteConflict"))
|
||||
.body(MESSAGE, equalTo(MUST_DELETE_POLICIES));
|
||||
}
|
||||
|
||||
/** The user path is the model the other two follow, and it answers what it already did. */
|
||||
@Test
|
||||
void aUserCarryingAnInlinePolicyStillAsksForADelete() {
|
||||
String userName = createUser();
|
||||
putInline("PutUserPolicy", "DeleteUserPolicy", "UserName", userName);
|
||||
iam("DeleteUser").formParam("UserName", userName)
|
||||
.when().post("/").then().statusCode(409)
|
||||
.body(CODE, equalTo("DeleteConflict"))
|
||||
.body(MESSAGE, equalTo(MUST_DELETE_POLICIES));
|
||||
}
|
||||
|
||||
/** The control: every dependency cleared in the order the refusals asked for, and it goes. */
|
||||
@Test
|
||||
void theRoleGoesOnceEachDependencyNamedIsCleared() {
|
||||
String roleName = createRole();
|
||||
String profileName = pairedProfile(roleName);
|
||||
String policyArn = attachToRole(roleName);
|
||||
String inlineName = inlineOnRole(roleName);
|
||||
|
||||
iam("RemoveRoleFromInstanceProfile").formParam("InstanceProfileName", profileName)
|
||||
.formParam("RoleName", roleName).when().post("/").then().statusCode(200);
|
||||
iam("DeleteInstanceProfile").formParam("InstanceProfileName", profileName)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("DetachRolePolicy").formParam("RoleName", roleName).formParam("PolicyArn", policyArn)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("DeleteRolePolicy").formParam("RoleName", roleName).formParam("PolicyName", inlineName)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("DeleteRole").formParam("RoleName", roleName).when().post("/").then().statusCode(200);
|
||||
}
|
||||
}
|
||||
+12
-8
@@ -13,8 +13,8 @@ import static org.hamcrest.Matchers.is;
|
||||
|
||||
/**
|
||||
* What the instance profile pair refuses, in the sentences AWS was recorded answering: a second
|
||||
* role names the quota that fired, and removing a role the profile never held is a 404 rather than
|
||||
* a quiet success.
|
||||
* role names the quota that fired, removing a role the profile never held is a 404 rather than a
|
||||
* quiet success, and the role a profile holds goes only after the pairing does.
|
||||
*
|
||||
* <p>{@code docs/services/iam.md} carries the recordings.
|
||||
*/
|
||||
@@ -97,14 +97,16 @@ class IamInstanceProfileRefusalIntegrationTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* A role deleted while the profile still lists it stays removable, which is the case the
|
||||
* tolerance was there for and is not the one AWS refuses. The role goes first and the pairing
|
||||
* after it, so the removal is the one that reaches a profile listing a role that is not there.
|
||||
* The pairing goes before the role does, which is what puts the state the removal tolerates
|
||||
* (a profile listing a role that is no longer there) out of reach of anything served: the
|
||||
* delete is refused while the profile holds the role, and succeeds once it does not. The
|
||||
* tolerance stays for state reached another way, and the sentence the refusal carries is
|
||||
* covered with the other three in {@link IamDeleteConflictDependencyIntegrationTest}.
|
||||
*/
|
||||
@Test
|
||||
void aRoleDeletedWhileStillListedStaysRemovable() {
|
||||
void theRoleAProfileHoldsGoesOnlyAfterThePairing() {
|
||||
String profileName = createProfile();
|
||||
String roleName = unique("profile-ghost-role");
|
||||
String roleName = unique("profile-paired-role");
|
||||
iam("CreateRole").formParam("RoleName", roleName)
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.when().post("/").then().statusCode(200);
|
||||
@@ -116,9 +118,11 @@ class IamInstanceProfileRefusalIntegrationTest {
|
||||
.formParam("InstanceProfileName", profileName)
|
||||
.formParam("RoleName", roleName)
|
||||
.when().post("/").then().statusCode(anyOf(is(200), is(404))));
|
||||
iam("DeleteRole").formParam("RoleName", roleName).when().post("/").then().statusCode(200);
|
||||
iam("DeleteRole").formParam("RoleName", roleName).when().post("/").then().statusCode(409)
|
||||
.body(CODE, equalTo("DeleteConflict"));
|
||||
iam("RemoveRoleFromInstanceProfile").formParam("InstanceProfileName", profileName)
|
||||
.formParam("RoleName", roleName).when().post("/").then().statusCode(200);
|
||||
iam("DeleteRole").formParam("RoleName", roleName).when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
/** The control: the first role still attaches, and removing it still succeeds. */
|
||||
|
||||
@@ -2939,6 +2939,59 @@ class IamServiceTest {
|
||||
assertEquals("P1", profiles.getFirst().getInstanceProfileName());
|
||||
}
|
||||
|
||||
/**
|
||||
* A role delete asks which profiles hold the role rather than reading every profile in the
|
||||
* account, so clearing out many roles while many profiles are about costs one lookup each and
|
||||
* still refuses exactly the role that is paired.
|
||||
*/
|
||||
@Test
|
||||
void deletingManyRolesWithManyProfilesRefusesOnlyThePairedOne() {
|
||||
for (int i = 0; i < 40; i++) {
|
||||
iamService.createInstanceProfile("BulkProfile" + i, "/");
|
||||
iamService.createRole("BulkRole" + i, "/", EMPTY_POLICY_DOCUMENT, null, 0, null);
|
||||
}
|
||||
iamService.addRoleToInstanceProfile("BulkProfile7", "BulkRole7");
|
||||
|
||||
for (int i = 0; i < 40; i++) {
|
||||
if (i != 7) {
|
||||
iamService.deleteRole("BulkRole" + i);
|
||||
}
|
||||
}
|
||||
assertEquals("DeleteConflict", assertThrows(AwsException.class,
|
||||
() -> iamService.deleteRole("BulkRole7")).getErrorCode());
|
||||
|
||||
iamService.removeRoleFromInstanceProfile("BulkProfile7", "BulkRole7");
|
||||
iamService.deleteRole("BulkRole7");
|
||||
}
|
||||
|
||||
/**
|
||||
* A pairing the store already carried is seen by a service that did not write it: the lookup
|
||||
* is filled from the profiles it finds, so a restart does not make a held role deletable.
|
||||
*/
|
||||
@Test
|
||||
void aPairingAlreadyInStorageStillBlocksTheDelete() {
|
||||
StorageBackend<String, IamRole> roles = new InMemoryStorage<>();
|
||||
StorageBackend<String, InstanceProfile> profiles = new InMemoryStorage<>();
|
||||
IamService before = iamServiceOn(roles, profiles);
|
||||
before.createRole("CarriedRole", "/", EMPTY_POLICY_DOCUMENT, null, 0, null);
|
||||
before.createInstanceProfile("CarriedProfile", "/");
|
||||
before.addRoleToInstanceProfile("CarriedProfile", "CarriedRole");
|
||||
|
||||
IamService after = iamServiceOn(roles, profiles);
|
||||
|
||||
assertEquals("DeleteConflict", assertThrows(AwsException.class,
|
||||
() -> after.deleteRole("CarriedRole")).getErrorCode());
|
||||
after.removeRoleFromInstanceProfile("CarriedProfile", "CarriedRole");
|
||||
after.deleteRole("CarriedRole");
|
||||
}
|
||||
|
||||
private static IamService iamServiceOn(StorageBackend<String, IamRole> roles,
|
||||
StorageBackend<String, InstanceProfile> instanceProfiles) {
|
||||
return new IamService(new InMemoryStorage<>(), new InMemoryStorage<>(), roles, new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), instanceProfiles, new InMemoryStorage<>(),
|
||||
new RegionResolver("us-east-1", "000000000000"), false);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// AWS Managed Policy Seeding
|
||||
// =========================================================================
|
||||
|
||||
@@ -28,6 +28,7 @@ import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
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;
|
||||
@@ -109,6 +110,27 @@ class SsoPermissionSetRoleProvisionerTest {
|
||||
assertEquals(List.of(cleanupFailure), List.of(thrown.getSuppressed()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Removing an assignment empties the role before deleting it, so a role an instance profile
|
||||
* holds has to be refused before that: otherwise the delete it cannot finish would leave the
|
||||
* assignment's role in place with nothing on it.
|
||||
*/
|
||||
@Test
|
||||
void anAssignmentWhoseRoleAProfileHoldsKeepsItsPolicies() {
|
||||
PermissionSetProvisioning provisioning = new PermissionSetProvisioning("permission-set", ACCOUNT, STATUS,
|
||||
ARN, "role-id");
|
||||
when(iam.findRole(ACCOUNT, NAME)).thenReturn(Optional.of(new IamRole("role-id", NAME, PATH, ARN, "{}")));
|
||||
doThrow(new AwsException("DeleteConflict",
|
||||
"Cannot delete entity, must remove roles from instance profile first.", 409))
|
||||
.when(iam).requireNotHeldByAnInstanceProfile(NAME);
|
||||
|
||||
assertEquals("DeleteConflict", assertThrows(AwsException.class,
|
||||
() -> provisioner.remove(provisioning, "us-east-1")).getErrorCode());
|
||||
|
||||
verify(iam, never()).replaceRolePolicies(anyString(), anyString(), any(), any(), any(), anyInt());
|
||||
verify(iam, never()).deleteRole(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void provisioningJsonPreservesRoleIdentityAndAcceptsLegacyRecords() throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@@ -278,14 +278,23 @@ public final class EnforcementFixtures {
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a role, and first the policies it carries: Floci answers {@code DeleteConflict},
|
||||
* "must detach all policies first", for a role holding either an inline or a managed one.
|
||||
* Deletes a role, and first what IAM wants gone before it will: any instance profile holding
|
||||
* it, then its managed attachments and its inline policies. Each of those is refused with its
|
||||
* own {@code DeleteConflict} sentence, and the profile is reported ahead of the policies, so a
|
||||
* teardown that left one in place would be told to remedy something else.
|
||||
*/
|
||||
public static void removeRole(int port, String authorization, String roleName) {
|
||||
if (iam(port, authorization, "GetRole", "RoleName", roleName)
|
||||
.extract().statusCode() == 404) {
|
||||
return;
|
||||
}
|
||||
for (String profileName : iam(port, authorization, "ListInstanceProfilesForRole",
|
||||
"RoleName", roleName).statusCode(200).extract().xmlPath()
|
||||
.getList("ListInstanceProfilesForRoleResponse.ListInstanceProfilesForRoleResult"
|
||||
+ ".InstanceProfiles.member.InstanceProfileName", String.class)) {
|
||||
iam(port, authorization, "RemoveRoleFromInstanceProfile",
|
||||
"InstanceProfileName", profileName, "RoleName", roleName).statusCode(200);
|
||||
}
|
||||
for (String policyName : iam(port, authorization, "ListRolePolicies", "RoleName", roleName)
|
||||
.statusCode(200).extract().xmlPath()
|
||||
.getList("ListRolePoliciesResponse.ListRolePoliciesResult.PolicyNames.member",
|
||||
|
||||
Reference in new issue
Block a user