代码之家  ›  专栏  ›  技术社区  ›  bibi

如何修复格式错误的PolicyDocument:使用terraform时生成的策略中的语法错误

  •  0
  • bibi  · 技术社区  · 2 年前

    我正在创建IAM策略,以强制用户在访问AWS资源之前首先使用MFA登录进行身份验证。当我运行terraform apply时,我得到了格式错误的PolicyDocument:策略中的语法错误。任何帮助都将不胜感激。谢谢

    { “版本”:“2012-10-17”, “声明”:[ { “Sid”:“AllowViewAccountInfo”, “效果”:“允许”, “操作”:[ “iam:GetAccountPasswordPolicy”,

                "iam:ListVirtualMFADevices"
            ],
            "Resource": "*"
        },       
            "Sid": "AllowManageOwnAccessKeys",
            "Effect": "Allow",
            "Action": [
                "iam:CreateAccessKey",
                "iam:DeleteAccessKey",
                "iam:ListAccessKeys",
                "iam:UpdateAccessKey"
            ],
            "Resource": "arn:aws:iam::*:user/${aws:username}"
        },
        {
            "Sid": "AllowManageOwnSSHPublicKeys",
            "Effect": "Allow",
            "Action": [
                "iam:DeleteSSHPublicKey",
                "iam:GetSSHPublicKey",
                "iam:ListSSHPublicKeys",
                "iam:UpdateSSHPublicKey",
                "iam:UploadSSHPublicKey"
            ],
            "Resource": "arn:aws:iam::*:user/${aws:username}"
        },
        {
            "Sid": "AllowManageOwnGitCredentials",
            "Effect": "Allow",
            "Action": [
                "iam:CreateServiceSpecificCredential",
                "iam:DeleteServiceSpecificCredential",
                "iam:ListServiceSpecificCredentials",
                "iam:ResetServiceSpecificCredential",
                "iam:UpdateServiceSpecificCredential"
            ],
            "Resource": "arn:aws:iam::*:user/${aws:username}"
        },
        {
            "Sid": "AllowManageOwnVirtualMFADevice",
            "Effect": "Allow",
            "Action": [
                "iam:CreateVirtualMFADevice",
                "iam:DeleteVirtualMFADevice"
            ],
            "Resource": "arn:aws:iam::*:mfa/${aws:username}"
        },
        {
            "Sid": "AllowManageOwnUserMFA",
            "Effect": "Allow",
            "Action": [
                "iam:DeactivateMFADevice",
                "iam:EnableMFADevice",
                "iam:ListMFADevices",
                "iam:ResyncMFADevice"
            ],
            "Resource": "arn:aws:iam::*:user/${aws:username}"
        },
        {
            "Sid": "DenyAllExceptListedIfNoMFA",
            "Effect": "Deny",
            "NotAction": [
                "iam:CreateVirtualMFADevice",
                "iam:EnableMFADevice",
                "iam:GetUser",
                "iam:ListMFADevices",
                "iam:ListVirtualMFADevices",
                "iam:ResyncMFADevice",
                "sts:GetSessionToken"
            ],
            "Resource": "*",
            "Condition": {
                "BoolIfExists": {
                    "aws:MultiFactorAuthPresent": "false"
                }
            }
        }
    ]
    

    }

    0 回复  |  直到 2 年前
        1
  •  1
  •   Riz    2 年前

    你的 json 有一个 { 丢失的如果你拿着这个 json 并尝试在控制台中制定策略,它将为您提供出现问题的确切位置。 就在之前 "Sid": "AllowManageOwnAccessKeys", 你失踪了 {

    更新日期: 以下是aws官方文档中关于使用变量的示例。

    {
      "Version": "2012-10-17",
      "Statement": [{
        "Action": ["iam:*AccessKey*"],
        "Effect": "Allow",
        "Resource": ["arn:aws:iam::account-id:user/${aws:username}"]
      }]
    }
    

    你可以据此更改你的政策。