fix(iam): refuse the instance profile pair as AWS words it (#8876)

An instance profile holds one role and both halves of that were wrong.
Adding a second answered prose where AWS names the quota that fired,
'Cannot exceed quota for InstanceSessionsPerInstanceProfile: 1', which is
the form this package already writes for nine other quotas. Removing a
role the profile never held removed nothing and answered 200 where AWS
answers 404 NoSuchEntity naming the role and the profile.

The tolerance the remove path was written for is kept and pinned: a role
deleted while the profile still lists it stays removable, which the
profile's own list decides. Whether the pairing is there and the removal
of it are one decision, taken on the monitor the attach path already
held, so a concurrent attach cannot land between them.

Both CloudFormation callers remove roles they read from the profile's
list, and a caller outside the stack can detach one in between, so each
tolerates a pairing that is already gone rather than leaving a stack
delete or a reconcile part way through. A profile that has itself gone is
a different case and still propagates, so the delete ends where it always
did.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-18 11:13:31 -04:00
1 parent 0c1ee1ea32
commit 0842636ed9
5 files changed
+239 -7

No files matched your search

@@ -35,7 +35,8 @@ import static org.mockito.Mockito.when;
* and Roles are applied, a role swap reconciles in place, a createOnly change (name, or path on an
* auto-named profile, or a dropped name) replaces the profile and marks the old one for cleanup, a
* path change that keeps an explicit name collides with EntityAlreadyExists, a missing Roles is
* rejected, and delete detaches roles before removing the profile.
* rejected, and delete detaches roles before removing the profile, tolerating a pairing another
* caller detached first while still stopping where the profile itself has gone.
*/
class IamInstanceProfileCfnProvisionerTest {
@@ -287,6 +288,54 @@ class IamInstanceProfileCfnProvisionerTest {
assertEquals("DeleteConflict", failure.getErrorCode());
}
@Test
void deleteToleratesAPairingDetachedWhileTheProfileStands() {
InstanceProfile withRole = profile("web", "/");
withRole.getRoleNames().add("app-role");
when(iam.getInstanceProfile("web")).thenReturn(withRole);
doThrow(new AwsException("NoSuchEntity",
"Role app-role in Instance Profile web cannot be found.", 404))
.when(iam).removeRoleFromInstanceProfile("web", "app-role");
assertDoesNotThrow(() -> provisioner.delete(TYPE, "web", "us-east-1"));
verify(iam).deleteInstanceProfile("web");
}
@Test
void deleteStopsWhereTheProfileWentWithThePairing() {
InstanceProfile withRole = profile("web", "/");
withRole.getRoleNames().add("app-role");
when(iam.getInstanceProfile("web")).thenReturn(withRole)
.thenThrow(new AwsException("NoSuchEntity", "gone", 404));
doThrow(new AwsException("NoSuchEntity", "gone", 404))
.when(iam).removeRoleFromInstanceProfile("web", "app-role");
assertDoesNotThrow(() -> provisioner.delete(TYPE, "web", "us-east-1"));
verify(iam, never()).deleteInstanceProfile("web");
}
@Test
void aPairingDetachedDuringAnUpdateDoesNotStopTheReconcile() throws Exception {
InstanceProfile existing = profile("web", "/");
existing.getRoleNames().add("old-role");
when(iam.createInstanceProfile("web", "/"))
.thenThrow(new AwsException("EntityAlreadyExists", "exists", 409));
when(iam.getInstanceProfile("web")).thenReturn(existing);
doThrow(new AwsException("NoSuchEntity",
"Role old-role in Instance Profile web cannot be found.", 404))
.when(iam).removeRoleFromInstanceProfile("web", "old-role");
StackResource r = resource();
r.setPhysicalId("web");
provisioner.provision(r, props("""
{"InstanceProfileName": "web", "Roles": ["new-role"]}
"""), ctx("web"));
verify(iam).addRoleToInstanceProfile("web", "new-role");
}
private ProvisionContext ctx(String priorPhysicalId) {
return ctx("my-stack", priorPhysicalId);
}
@@ -0,0 +1,134 @@
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;
/**
* 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.
*
* <p>{@code docs/services/iam.md} carries the recordings.
*/
@QuarkusTest
class IamInstanceProfileRefusalIntegrationTest {
private static final String IAM_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20261118/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 TRUST_POLICY = "{\"Version\":\"2012-10-17\",\"Statement\":"
+ "[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"ec2.amazonaws.com\"},"
+ "\"Action\":\"sts:AssumeRole\"}]}";
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
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("profile-role");
iam("CreateRole").formParam("RoleName", roleName)
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
.when().post("/").then().statusCode(200);
cleanup.register(() -> iam("DeleteRole").formParam("RoleName", roleName)
.when().post("/").then().statusCode(200));
return roleName;
}
private String createProfile() {
String profileName = unique("profile");
iam("CreateInstanceProfile").formParam("InstanceProfileName", profileName)
.when().post("/").then().statusCode(200);
cleanup.register(() -> iam("DeleteInstanceProfile")
.formParam("InstanceProfileName", profileName)
.when().post("/").then().statusCode(200));
return profileName;
}
private void addRole(String profileName, String roleName) {
iam("AddRoleToInstanceProfile").formParam("InstanceProfileName", profileName)
.formParam("RoleName", roleName).when().post("/").then().statusCode(200);
cleanup.register(() -> iam("RemoveRoleFromInstanceProfile")
.formParam("InstanceProfileName", profileName)
.formParam("RoleName", roleName).when().post("/").then().statusCode(200));
}
/** The second role names the quota that fired, in the form the other nine quotas use. */
@Test
void aSecondRoleNamesTheQuotaThatFired() {
String profileName = createProfile();
addRole(profileName, createRole());
iam("AddRoleToInstanceProfile").formParam("InstanceProfileName", profileName)
.formParam("RoleName", createRole())
.when().post("/").then().statusCode(409)
.body(CODE, equalTo("LimitExceeded"))
.body(MESSAGE, equalTo(
"Cannot exceed quota for InstanceSessionsPerInstanceProfile: 1"));
}
/** Removing a role the profile never held is refused rather than answered 200. */
@Test
void removingARoleTheProfileNeverHeldIsRefused() {
String profileName = createProfile();
String roleName = createRole();
iam("RemoveRoleFromInstanceProfile").formParam("InstanceProfileName", profileName)
.formParam("RoleName", roleName)
.when().post("/").then().statusCode(404)
.body(CODE, equalTo("NoSuchEntity"))
.body(MESSAGE, equalTo("Role " + roleName + " in Instance Profile "
+ profileName + " cannot be found."));
}
/**
* 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.
*/
@Test
void aRoleDeletedWhileStillListedStaysRemovable() {
String profileName = createProfile();
String roleName = unique("profile-ghost-role");
iam("CreateRole").formParam("RoleName", roleName)
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
.when().post("/").then().statusCode(200);
cleanup.register(() -> iam("DeleteRole").formParam("RoleName", roleName)
.when().post("/").then().statusCode(anyOf(is(200), is(404))));
iam("AddRoleToInstanceProfile").formParam("InstanceProfileName", profileName)
.formParam("RoleName", roleName).when().post("/").then().statusCode(200);
cleanup.register(() -> iam("RemoveRoleFromInstanceProfile")
.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("RemoveRoleFromInstanceProfile").formParam("InstanceProfileName", profileName)
.formParam("RoleName", roleName).when().post("/").then().statusCode(200);
}
/** The control: the first role still attaches, and removing it still succeeds. */
@Test
void theFirstRoleStillAttachesAndDetaches() {
String profileName = createProfile();
String roleName = createRole();
iam("AddRoleToInstanceProfile").formParam("InstanceProfileName", profileName)
.formParam("RoleName", roleName).when().post("/").then().statusCode(200);
iam("RemoveRoleFromInstanceProfile").formParam("InstanceProfileName", profileName)
.formParam("RoleName", roleName).when().post("/").then().statusCode(200);
}
}