mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): refuse a managed policy delete that leaves versions behind (#9224)
DeletePolicy read the attachment count alone, so a policy carrying a version beside its default was deleted along with its history and answered nothing. AWS refuses that delete with DeleteConflict 409 and three sentences of wholly fixed text, "This policy has more than one version. Before you delete a policy, you must delete the policy's versions. The default version is deleted with the policy.", recorded against a real account on 2026-10-29, and the operation's published description asks a caller for the same two steps: detach the policy from every user, group and role, then delete all of the policy's versions. The refusal reports an attachment first and the versions second, the order those steps are written in. Which of the two AWS names for a policy carrying both is not recorded, so the page marks that order as this package's reading rather than a recording, as it already does for attached ahead of inline. Nothing clears those versions on the caller's behalf, on AWS or here, so whoever wants the policy gone deletes them first. The CloudFormation provisioner for AWS::IAM::ManagedPolicy owns the policies its stacks create and publishes the new document as a new default version on every update, which leaves the version before it stored: the delete that brings such a stack down clears the other versions ahead of the policy, in the order the published steps give, so a stack cannot end up able to create and update a policy it can never take down again. The clearing asks the attachment question before a single version goes and answers it with the sentence the delete would, so a policy a principal the stack never recorded still holds keeps its history instead of spending it on a delete that was going to be refused anyway. The reads a delete decides on and the delete itself are under the lock every other IAM write takes, which the writes to a policy's versions now take as well, so a version published between the count and the delete is no longer deleted along with the policy it was added to, and the write back that ends a clearing cannot restore a policy another caller deleted in between. Inside it the version map keeps its own monitor, and SetDefaultPolicyVersion moves the default version id under that monitor beside the flags it already set there: between those two writes a clearing would have read the id this call had just replaced and dropped the version it had just made default. Covered over the API by the refusal, its precedence over the attachment and the control that deletes the other version and then the policy; at the service level by the clearing that keeps the default with its document, the attachment that refuses it with every version intact, and the version a SetDefault just chose being the one kept; by a trial loop over a delete racing a version publish; on the provisioner by the order its delete asks for; and end to end by a stack that is created, updated and then deleted, with the policy gone after it. One test fixture published a second version and tore the policy down with one call, which the refusal now blocks, so it deletes that version first.
This commit is contained in:
1 parent
d4af2a836e
commit
942cace935
9 files changed
+508
-85
No files matched your search
+13
-4
@@ -80,9 +80,11 @@ final class IamManagedPolicyDeletes {
|
||||
}
|
||||
|
||||
/**
|
||||
* Detaches the policy from every target, then deletes it. Deletion is idempotent: a principal,
|
||||
* attachment or the policy itself already being gone (NoSuchEntity) is tolerated, while a
|
||||
* permission or service failure propagates so the stack stays DELETE_FAILED.
|
||||
* Detaches the policy from every target, clears the versions its updates left behind and then
|
||||
* deletes it, which is the order AWS's own steps for deleting a managed policy give. Deletion
|
||||
* is idempotent: a principal, attachment or the policy itself already being gone
|
||||
* (NoSuchEntity) is tolerated, while a permission or service failure propagates so the stack
|
||||
* stays DELETE_FAILED.
|
||||
*/
|
||||
static void detachAndDeletePolicy(IamService iamService, StackResource resource) {
|
||||
String policyArn = resource.getPhysicalId();
|
||||
@@ -134,9 +136,16 @@ final class IamManagedPolicyDeletes {
|
||||
}
|
||||
}
|
||||
|
||||
/** Deletes the policy, tolerating one already gone. */
|
||||
/**
|
||||
* Clears the versions AWS asks a caller to clear and then deletes the policy, tolerating one
|
||||
* already gone. Every update of a stack-owned policy publishes the new document as a new
|
||||
* default version and leaves the version before it stored, and IAM refuses to delete a policy
|
||||
* that still has one, so a delete that cleared nothing would leave a stack able to create and
|
||||
* update a policy it could never take down again.
|
||||
*/
|
||||
static void deletePolicyTolerating(IamService iamService, String policyArn) {
|
||||
try {
|
||||
iamService.clearNonDefaultPolicyVersions(policyArn);
|
||||
iamService.deletePolicy(policyArn);
|
||||
} catch (AwsException e) {
|
||||
if (!"NoSuchEntity".equals(e.getErrorCode())) {
|
||||
|
||||
@@ -394,6 +394,22 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
"Cannot delete entity, must delete policies first.";
|
||||
private static final String MUST_REMOVE_PROFILE_ROLES_FIRST =
|
||||
"Cannot delete entity, must remove roles from instance profile first.";
|
||||
/**
|
||||
* What a managed policy's own delete is refused with while a version other than its default
|
||||
* is still stored, three sentences of entirely fixed text: no ARN, no version id, no count.
|
||||
* The last of them tells the caller what not to go looking for, the default version being
|
||||
* the one version a delete takes with it and the one {@code DeletePolicyVersion} refuses.
|
||||
*/
|
||||
private static final String MUST_DELETE_POLICY_VERSIONS_FIRST =
|
||||
"This policy has more than one version. Before you delete a policy, you must delete"
|
||||
+ " the policy's versions. The default version is deleted with the policy.";
|
||||
/**
|
||||
* What a managed policy delete answers while an entity still has it attached, Floci's own
|
||||
* wording rather than a recording. It is also what clearing the policy's other versions
|
||||
* answers, since the clearing is only ever on the way to the delete.
|
||||
*/
|
||||
private static final String POLICY_STILL_ATTACHED =
|
||||
"Cannot delete a policy attached to entities. Detach it first.";
|
||||
public static final String FEATURE_ROOT_CREDENTIALS = "RootCredentialsManagement";
|
||||
public static final String FEATURE_ROOT_SESSIONS = "RootSessions";
|
||||
|
||||
@@ -2171,15 +2187,93 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a customer-managed policy, which AWS does only once nothing is left on it: no
|
||||
* entity attached to it, and no version beside the default one the delete takes with it. A
|
||||
* policy an update published a new document for still holds the document before it, and that
|
||||
* is enough to refuse the delete, so whoever wants the policy gone clears those versions
|
||||
* first; {@link #clearNonDefaultPolicyVersions} is what Floci's own stacks do it with.
|
||||
*
|
||||
* <p>Which dependency a policy carrying both is told about follows the order AWS's own steps
|
||||
* for deleting a managed policy are written in, the attachments and then the versions. That
|
||||
* order is a reading rather than a recording, and {@code docs/services/iam.md} says so.
|
||||
*
|
||||
* <p>Both reads and the delete they decide are under {@code resourceNameLock}, which every
|
||||
* write to a policy's versions and attachments takes as well: a version published between the
|
||||
* count and the delete would otherwise be deleted along with the policy it was added to.
|
||||
*/
|
||||
public void deletePolicy(String policyArn) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
if (policy.getAttachmentCount() > 0) {
|
||||
throw new AwsException("DeleteConflict",
|
||||
"Cannot delete a policy attached to entities. Detach it first.", 409);
|
||||
synchronized (resourceNameLock) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
if (policy.getAttachmentCount() > 0) {
|
||||
throw new AwsException("DeleteConflict", POLICY_STILL_ATTACHED, 409);
|
||||
}
|
||||
if (hasAVersionBesideItsDefault(policy)) {
|
||||
throw new AwsException("DeleteConflict", MUST_DELETE_POLICY_VERSIONS_FIRST, 409);
|
||||
}
|
||||
policies.delete(policyArn);
|
||||
LOG.infov("Deleted IAM policy: {0}", policyArn);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the policy holds anything besides the version a delete would take with it. Every
|
||||
* policy carries a default version from the moment it is created and nothing can delete that
|
||||
* one on its own, so a second version is exactly what an unfinished cleanup leaves behind.
|
||||
*/
|
||||
private static boolean hasAVersionBesideItsDefault(IamPolicy policy) {
|
||||
Map<String, PolicyVersion> versions = policy.getVersions();
|
||||
synchronized (versions) {
|
||||
return versions.size() > 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops every version of a managed policy but its default, the work AWS leaves to whoever
|
||||
* wants the policy deleted: {@link #deletePolicy} refuses while another version is stored and
|
||||
* no IAM operation clears them in one go. Floci's CloudFormation provisioners own the
|
||||
* policies their stacks created and publish a new default version on every update, so this is
|
||||
* how such a stack takes its own policy down. It is refused while an entity still has the
|
||||
* policy attached, with the sentence that delete would answer: the history is worth keeping
|
||||
* for a delete that cannot go through anyway, and AWS's own steps detach before they clear.
|
||||
*
|
||||
* <p>The whole of it is under {@code resourceNameLock}, as the delete it leads to and every
|
||||
* write to a policy's versions are, so the write back cannot resurrect a policy another
|
||||
* caller deleted in between. The default is read and the rest dropped under the version map's
|
||||
* own monitor as well, so a version published while this runs is either kept as the new
|
||||
* default or dropped with the old ones, never deleted as a leftover of a default it has
|
||||
* itself replaced.
|
||||
*/
|
||||
public void clearNonDefaultPolicyVersions(String policyArn) {
|
||||
synchronized (resourceNameLock) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = requirePolicy(policyArn, PolicyNotFound.ATTACHABLE);
|
||||
// Asked before a single version goes. The delete this clears the way for is refused
|
||||
// while anything still has the policy attached, a principal the caller never recorded
|
||||
// included, and a clearing that ran first would have spent the history on a delete
|
||||
// that was never going to happen. It is the published order too: detach, then the
|
||||
// versions, then the policy.
|
||||
if (policy.getAttachmentCount() > 0) {
|
||||
throw new AwsException("DeleteConflict", POLICY_STILL_ATTACHED, 409);
|
||||
}
|
||||
Map<String, PolicyVersion> versions = policy.getVersions();
|
||||
boolean cleared;
|
||||
synchronized (versions) {
|
||||
String defaultVersionId = policy.getDefaultVersionId();
|
||||
// No default recorded at all: a policy from a store this version of Floci did not
|
||||
// write. Dropping every version would leave the policy with no document to serve.
|
||||
if (defaultVersionId == null) {
|
||||
return;
|
||||
}
|
||||
cleared = versions.keySet()
|
||||
.removeIf(versionId -> !versionId.equals(defaultVersionId));
|
||||
}
|
||||
if (cleared) {
|
||||
policies.put(policyArn, policy);
|
||||
LOG.infov("Cleared the non-default versions of IAM policy: {0}", policyArn);
|
||||
}
|
||||
}
|
||||
policies.delete(policyArn);
|
||||
LOG.infov("Deleted IAM policy: {0}", policyArn);
|
||||
}
|
||||
|
||||
/** The roles, users and groups a managed policy is currently attached to. */
|
||||
@@ -2334,46 +2428,50 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
if (heldToBeingAPolicy) {
|
||||
IamPolicyDocuments.checkManagedPolicy(document);
|
||||
}
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = requirePolicy(policyArn, PolicyNotFound.ATTACHABLE);
|
||||
Map<String, PolicyVersion> versions = policy.getVersions();
|
||||
PolicyVersion version;
|
||||
synchronized (versions) {
|
||||
if (versions.size() >= 5) {
|
||||
throw new AwsException("LimitExceeded",
|
||||
"A managed policy can have up to 5 versions.", 409);
|
||||
synchronized (resourceNameLock) {
|
||||
// Under the lock a policy delete reads the version count with, so a version
|
||||
// cannot be added between that read and the delete it decides.
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = requirePolicy(policyArn, PolicyNotFound.ATTACHABLE);
|
||||
Map<String, PolicyVersion> versions = policy.getVersions();
|
||||
PolicyVersion version;
|
||||
synchronized (versions) {
|
||||
if (versions.size() >= 5) {
|
||||
throw new AwsException("LimitExceeded",
|
||||
"A managed policy can have up to 5 versions.", 409);
|
||||
}
|
||||
// AWS version ids are monotonic and never reissued after a DeletePolicyVersion.
|
||||
// The live keys' own max is only a floor, not the source of truth: deleting the
|
||||
// highest-numbered surviving version would otherwise let a "derive from what's left"
|
||||
// computation reissue its id. The stored high-water mark is the real counter.
|
||||
int highestSurviving = versions.keySet().stream()
|
||||
.mapToInt(id -> Integer.parseInt(id.substring(1)))
|
||||
.max().orElse(0);
|
||||
int nextVersionNum;
|
||||
if (policy.getNextVersionNumber() == null) {
|
||||
// A policy persisted before this field existed: the live-key floor alone can't
|
||||
// reveal a version deleted before this field was ever recorded (e.g. v1-v4 live
|
||||
// after a pre-migration DeletePolicyVersion("v5")). Since a policy can never hold
|
||||
// more than 5 concurrent versions, no more than 5 stacked deletions could have
|
||||
// happened at the top between two persists; jump the counter past that worst
|
||||
// case rather than trust the live keys' max + 1 for an unknown history.
|
||||
nextVersionNum = highestSurviving + 1 + 5;
|
||||
} else {
|
||||
nextVersionNum = Math.max(policy.getNextVersionNumber(), highestSurviving + 1);
|
||||
}
|
||||
String versionId = "v" + nextVersionNum;
|
||||
version = new PolicyVersion(versionId, document, setAsDefault);
|
||||
if (setAsDefault) {
|
||||
versions.values().forEach(v -> v.setDefaultVersion(false));
|
||||
policy.setDefaultVersionId(versionId);
|
||||
}
|
||||
versions.put(versionId, version);
|
||||
policy.setNextVersionNumber(nextVersionNum + 1);
|
||||
}
|
||||
// AWS version ids are monotonic and never reissued after a DeletePolicyVersion.
|
||||
// The live keys' own max is only a floor, not the source of truth: deleting the
|
||||
// highest-numbered surviving version would otherwise let a "derive from what's left"
|
||||
// computation reissue its id. The stored high-water mark is the real counter.
|
||||
int highestSurviving = versions.keySet().stream()
|
||||
.mapToInt(id -> Integer.parseInt(id.substring(1)))
|
||||
.max().orElse(0);
|
||||
int nextVersionNum;
|
||||
if (policy.getNextVersionNumber() == null) {
|
||||
// A policy persisted before this field existed: the live-key floor alone can't
|
||||
// reveal a version deleted before this field was ever recorded (e.g. v1-v4 live
|
||||
// after a pre-migration DeletePolicyVersion("v5")). Since a policy can never hold
|
||||
// more than 5 concurrent versions, no more than 5 stacked deletions could have
|
||||
// happened at the top between two persists; jump the counter past that worst
|
||||
// case rather than trust the live keys' max + 1 for an unknown history.
|
||||
nextVersionNum = highestSurviving + 1 + 5;
|
||||
} else {
|
||||
nextVersionNum = Math.max(policy.getNextVersionNumber(), highestSurviving + 1);
|
||||
}
|
||||
String versionId = "v" + nextVersionNum;
|
||||
version = new PolicyVersion(versionId, document, setAsDefault);
|
||||
if (setAsDefault) {
|
||||
versions.values().forEach(v -> v.setDefaultVersion(false));
|
||||
policy.setDefaultVersionId(versionId);
|
||||
}
|
||||
versions.put(versionId, version);
|
||||
policy.setNextVersionNumber(nextVersionNum + 1);
|
||||
policy.setUpdateDate(Instant.now());
|
||||
policies.put(policyArn, policy);
|
||||
return version;
|
||||
}
|
||||
policy.setUpdateDate(Instant.now());
|
||||
policies.put(policyArn, policy);
|
||||
return version;
|
||||
}
|
||||
|
||||
public PolicyVersion getPolicyVersion(String policyArn, String versionId) {
|
||||
@@ -2388,21 +2486,23 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void deletePolicyVersion(String policyArn, String versionId) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = requirePolicy(policyArn, PolicyNotFound.ATTACHABLE);
|
||||
if (versionId.equals(policy.getDefaultVersionId())) {
|
||||
throw new AwsException("DeleteConflict",
|
||||
"Cannot delete the default version of a policy.", 409);
|
||||
}
|
||||
Map<String, PolicyVersion> versions = policy.getVersions();
|
||||
synchronized (versions) {
|
||||
if (!versions.containsKey(versionId)) {
|
||||
throw new AwsException("NoSuchEntity",
|
||||
"Policy version " + versionId + " does not exist.", 404);
|
||||
synchronized (resourceNameLock) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = requirePolicy(policyArn, PolicyNotFound.ATTACHABLE);
|
||||
if (versionId.equals(policy.getDefaultVersionId())) {
|
||||
throw new AwsException("DeleteConflict",
|
||||
"Cannot delete the default version of a policy.", 409);
|
||||
}
|
||||
versions.remove(versionId);
|
||||
Map<String, PolicyVersion> versions = policy.getVersions();
|
||||
synchronized (versions) {
|
||||
if (!versions.containsKey(versionId)) {
|
||||
throw new AwsException("NoSuchEntity",
|
||||
"Policy version " + versionId + " does not exist.", 404);
|
||||
}
|
||||
versions.remove(versionId);
|
||||
}
|
||||
policies.put(policyArn, policy);
|
||||
}
|
||||
policies.put(policyArn, policy);
|
||||
}
|
||||
|
||||
public List<PolicyVersion> listPolicyVersions(String policyArn) {
|
||||
@@ -2414,19 +2514,24 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public void setDefaultPolicyVersion(String policyArn, String versionId) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = requirePolicy(policyArn, PolicyNotFound.ATTACHABLE);
|
||||
Map<String, PolicyVersion> versions = policy.getVersions();
|
||||
synchronized (versions) {
|
||||
if (!versions.containsKey(versionId)) {
|
||||
throw new AwsException("NoSuchEntity",
|
||||
"Policy version " + versionId + " does not exist.", 404);
|
||||
synchronized (resourceNameLock) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = requirePolicy(policyArn, PolicyNotFound.ATTACHABLE);
|
||||
Map<String, PolicyVersion> versions = policy.getVersions();
|
||||
synchronized (versions) {
|
||||
if (!versions.containsKey(versionId)) {
|
||||
throw new AwsException("NoSuchEntity",
|
||||
"Policy version " + versionId + " does not exist.", 404);
|
||||
}
|
||||
versions.values().forEach(v -> v.setDefaultVersion(false));
|
||||
versions.get(versionId).setDefaultVersion(true);
|
||||
// Inside the monitor with the flags: a clearing of the other versions reads this
|
||||
// id to decide what to keep, and between the two writes it would read the one
|
||||
// this call has just replaced and drop the version it just made default.
|
||||
policy.setDefaultVersionId(versionId);
|
||||
}
|
||||
versions.values().forEach(v -> v.setDefaultVersion(false));
|
||||
versions.get(versionId).setDefaultVersion(true);
|
||||
policies.put(policyArn, policy);
|
||||
}
|
||||
policy.setDefaultVersionId(versionId);
|
||||
policies.put(policyArn, policy);
|
||||
}
|
||||
|
||||
public void tagPolicy(String policyArn, Map<String, String> newTags) {
|
||||
|
||||
Reference in new issue
Block a user