mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
0c1ee1ea32
commit
0842636ed9
5 files changed
+239
-7
No files matched your search
+19
-2
@@ -147,7 +147,7 @@ public class IamInstanceProfileCfnProvisioner implements CfnResourceProvisioner
|
||||
private void reconcileRoles(String name, List<String> current, List<String> desired) {
|
||||
for (String roleName : new ArrayList<>(current)) {
|
||||
if (!desired.contains(roleName)) {
|
||||
iamService.removeRoleFromInstanceProfile(name, roleName);
|
||||
detachPairing(name, roleName);
|
||||
}
|
||||
}
|
||||
for (String roleName : desired) {
|
||||
@@ -157,6 +157,23 @@ public class IamInstanceProfileCfnProvisioner implements CfnResourceProvisioner
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Detaches one role, tolerating a pairing that is already gone: the operation refuses a role
|
||||
* the profile does not hold, and a caller outside the stack can detach between the list being
|
||||
* read here and this call, which must not leave a deletion or a reconcile part way through. A
|
||||
* profile that has itself gone is a different case and still propagates, so the deletion ends
|
||||
* where it always did rather than carrying on against a profile that is not there.
|
||||
*/
|
||||
private void detachPairing(String name, String roleName) {
|
||||
try {
|
||||
iamService.removeRoleFromInstanceProfile(name, roleName);
|
||||
} catch (AwsException e) {
|
||||
if (!"NoSuchEntity".equals(e.getErrorCode()) || findProfile(name) == null) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String normalizePath(String path) {
|
||||
if (path == null || path.isEmpty()) {
|
||||
return "/";
|
||||
@@ -197,7 +214,7 @@ public class IamInstanceProfileCfnProvisioner implements CfnResourceProvisioner
|
||||
CfnDeletes.safeDelete("IAM instance profile", physicalId, () -> {
|
||||
InstanceProfile profile = iamService.getInstanceProfile(physicalId);
|
||||
for (String roleName : new ArrayList<>(profile.getRoleNames())) {
|
||||
iamService.removeRoleFromInstanceProfile(physicalId, roleName);
|
||||
detachPairing(physicalId, roleName);
|
||||
}
|
||||
iamService.deleteInstanceProfile(physicalId);
|
||||
}, "NoSuchEntity");
|
||||
|
||||
@@ -2919,8 +2919,10 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
synchronized (roleNames) {
|
||||
if (!roleNames.contains(roleName)) {
|
||||
if (!roleNames.isEmpty()) {
|
||||
// The quota name is what says which of the operation's limits fired, and it is
|
||||
// this one rather than a roles-per-profile quota, which AWS publishes nowhere.
|
||||
throw new AwsException("LimitExceeded",
|
||||
"An instance profile can contain at most 1 role.", 409);
|
||||
"Cannot exceed quota for InstanceSessionsPerInstanceProfile: 1", 409);
|
||||
}
|
||||
roleNames.add(roleName);
|
||||
instanceProfiles.put(instanceProfileName, profile);
|
||||
@@ -2931,10 +2933,19 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
public void removeRoleFromInstanceProfile(String instanceProfileName, String roleName) {
|
||||
validateRoleName(roleName);
|
||||
InstanceProfile profile = getInstanceProfile(instanceProfileName);
|
||||
// Tolerates an already-deleted role, so guard only what is still there.
|
||||
// Tolerates an already-deleted role that the profile still lists, so guard only what is
|
||||
// still there; what the profile never held is a different case and AWS refuses it.
|
||||
roles.get(roleName).ifPresent(role -> requireNotServiceLinked(role, roleName));
|
||||
profile.getRoleNames().remove(roleName);
|
||||
instanceProfiles.put(instanceProfileName, profile);
|
||||
List<String> roleNames = profile.getRoleNames();
|
||||
// Whether the pairing is there and the removal of it are one decision, taken on the
|
||||
// monitor the attach path holds, so a concurrent attach cannot land between them.
|
||||
synchronized (roleNames) {
|
||||
if (!roleNames.remove(roleName)) {
|
||||
throw new AwsException("NoSuchEntity", "Role " + roleName + " in Instance Profile "
|
||||
+ instanceProfileName + " cannot be found.", 404);
|
||||
}
|
||||
instanceProfiles.put(instanceProfileName, profile);
|
||||
}
|
||||
}
|
||||
|
||||
public List<InstanceProfile> listInstanceProfilesForRole(String roleName) {
|
||||
|
||||
Reference in new issue
Block a user