如何在命令行中一条命令删除多个版本的iam policy?

How to delete an iam policy with multiple versions on the command line with one command?

我正在尝试使用多个版本的命令行删除策略,如下所示:

function iam-list-versions () {
  aws iam list-policy-versions --query "Versions[].VersionId" --policy-arn  --output text 
}

function iam-delete-policy-versions () {
  iam-list-versions  | xargs -n 1 -I{} aws iam delete-policy-version --policy-arn  --version-id {}
}

function iam-delete-policy () {
  iam-delete-policy-versions 
  aws iam delete-policy --policy-arn 
}

然后运行iam-delete-policy arn:aws:iam::123456789012:policy/... 但我不断收到错误消息:

An error occurred (DeleteConflict) when calling the DeletePolicyVersion operation: Cannot delete the default version of a policy.

An error occurred (DeleteConflict) when calling the DeletePolicy operation: 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.

看来我的 iam-delete-policy-versions 功能不起作用。希望他们能简单地添加一个 --force 标志。

错误消息提示:

  • 您不能删除策略的默认版本。相反,删除策略本身。
  • 当存在多个版本时,您无法删除策略。

我还注意到 list-policy-versions returns 一个名为 IsDefaultVersion 的字段,它指示策略是否为默认版本。

因此,您需要执行以下操作:

  • 致电list-policy-versions
  • 对于 IsDefaultVersion = False 的每个响应,调用 delete-policy-version
  • 删除所有版本后,为每个策略调用 delete-policy(或者,为每个 IsDefaultVersion = True

这在 Python 脚本中可能会更容易。

@John Rotenstein 实际上在这里给了我这个问题的答案:

因为版本没有正确迭代需要:setopt shwordsplit 在我的 zshell 中,这个删除版本命令将 运行 这样:aws iam delete-policy-version --policy-arn --version-id v3 v2 v1 只会尝试删除v3.

因为 v3 是该角色的默认版本,此命令将失败,导致:

An error occurred (DeleteConflict) when calling the DeletePolicyVersion operation: Cannot delete the default version of a policy.

随后删除该策略将失败,因为该策略上还有其他版本,因为先前的命令无效。

我会接受约翰的回答,因为他配得上所有的分数!下面是最终脚本:

setopt shwordsplit

function iam-list-versions () {
  aws iam list-policy-versions --query "Versions[?@.IsDefaultVersion == \`false\`].VersionId" --policy-arn  --output text
}

function iam-delete-policy-versions () {
  iam-list-versions  | xargs -n 1 -I{} aws iam delete-policy-version --policy-arn  --version-id {}
}

function iam-delete-policy () {
  iam-delete-policy-versions 
  aws iam delete-policy --policy-arn 
}


请使用以下boto3 代码立即删除策略列表。请传递变量 pol_list=[] 中的策略列表。也不要忘记在策略 ARN 中添加您的账户 ID。此脚本将删除策略及其所有版本。

import boto3
client = boto3.client("iam")
###Pass the list of IAM policy name on the following variable
pol_list=[]
for arn in pol_list:
    print(arn)
    try:
        response = client.list_policy_versions(
            PolicyArn="arn:aws:iam::accountID:policy/" + arn

        )

        # print(response)
        for ver in response['Versions']:
            # print(ver['VersionId'])
            if ver['IsDefaultVersion'] is True:
                pass
            else:
                delete = client.delete_policy_version(
                    PolicyArn="arn:aws:iam::accountID:policy/" + arn,
                    VersionId=ver['VersionId']
                )
                print(delete)

        pol_delete = client.delete_policy(
            PolicyArn="arn:aws:iam::accountID:policy/" + arn
        )
        print("Policy Deleted Successfully!!")
    except Exception as E:
        print("Already Deleted!")