【问题标题】:Policy contains a statement with one or more invalid principals - AWS Cloudformation error策略包含具有一个或多个无效委托人的语句 - AWS Cloudformation 错误
【发布时间】:2020-04-11 15:09:20
【问题描述】:

我有一个如下所示的 CF 模板

 AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: gtm platform Lampda application deployment for ELasticCloud
Parameters:
  SystemUserAccount:
    Description: The syatem user account used to assume deployment role
    Type: String
    Default: usr-test1
  DeploymentRoleName:
    Description: The deployment role used to deploy cloudformation template
    Type: String
    Default: gtm-platform-deployment-role
  GTMPlatformLambdaRoleName:
    Description: The execution role for gtm platform
    Type: String
    Default: gtm-platform-lambda-role
  GTMPlatformKMSKeyAliasName:
    Description: The lambda function name for gtm platform
    Type: String
    Default: gtm-platform-kms-key
Resources:
  GTMPlatformLambdaRole:
    Type: AWS::IAM::Role
    DependsOn:
      - GTMPlatformKMSKey
    Properties:
      RoleName: !Ref GTMPlatformLambdaRoleName
      AssumeRolePolicyDocument:
        Version: '2008-10-17'
        Statement:
          - Sid: ''
            Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      Path: /
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/CloudWatchFullAccess
        - arn:aws:iam::aws:policy/AmazonVPCFullAccess
      Policies:
        - PolicyName: GTMPlatformLambdaPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action: cloudwatch:*
                Resource: "*"
              - Effect: Allow
                Action:
                  - kms:EnableKeyRotation
                  - kms:EnableKey
                  - kms:ImportKeyMaterial
                  - kms:Decrypt
                  - kms:UntagResource
                  - kms:UpdateKeyDescription
                  - kms:GetKeyPolicy
                  - kms:GenerateDataKeyWithoutPlaintext
                  - kms:CancelKeyDeletion
                  - kms:ListResourceTags
                  - kms:DeleteImportedKeyMaterial
                  - kms:DisableKey
                  - kms:DisableKeyRotation
                  - kms:ListGrants
                  - kms:UpdateAlias
                  - kms:GetParametersForImport
                  - kms:TagResource
                  - kms:Encrypt
                  - kms:GetKeyRotationStatus
                  - kms:ScheduleKeyDeletion
                  - kms:CreateAlias
                  - kms:DescribeKey
                  - kms:DeleteAlias
                Resource: !GetAtt GTMPlatformKMSKey.Arn
              - Effect: Allow
                Action:
                  - kms:GenerateRandom
                  - kms:GenerateDataKey
                  - kms:ReEncryptTo
                  - kms:ReEncryptFrom
                Resource: "*"
  GTMPlatformKMSKey:
    Type: AWS::KMS::Key
    Properties:
      Description: Key used to encrypt decrypt EBS volumes at rest
      Enabled: true
      KeyPolicy:
        Version: '2012-10-17'
        Statement:
          - Sid: Enable permissions for admin
            Effect: Allow
            Principal:
              AWS: !Join
                - ''
                - - 'arn:aws:iam::'
                  - !Ref 'AWS::AccountId'
                  - ':root'
            Action:
              - 'kms:*'
            Resource: '*'
          - Sid: Allow access for Key Administrators
            Effect: Allow
            Principal:
              AWS:
                - !Sub
                  - 'arn:aws:iam::${accountId}:role/${gtmDeploymentRoleName}'
                  - accountId: !Ref 'AWS::AccountId'
                    gtmDeploymentRoleName: !Ref 'DeploymentRoleName'
            Action:
              - kms:Create*
              - kms:Describe*
              - kms:Enable*
              - kms:List*
              - kms:Put*
              - kms:Update*
              - kms:Revoke*
              - kms:Disable*
              - kms:Get*
              - kms:Delete*
              - kms:TagResource
              - kms:UntagResource
            Resource: "*"
          - Sid: Allow use of the key
            Effect: Allow
            Principal:
              AWS:
                - !Sub
                  - 'arn:aws:iam::${accountId}:role/${gtmPlatformLambdaRoleName}'
                  - accountId: !Ref 'AWS::AccountId'
                    gtmPlatformLambdaRoleName: !Ref 'GTMPlatformLambdaRoleName'
            Action:
              - kms:Encrypt
              - kms:Decrypt
              - kms:ReEncrypt*
              - kms:GenerateDataKey*
              - kms:DescribeKey
            Resource: "*"
          - Sid: Allow attachment of persistent resources
            Effect: Allow
            Principal:
              AWS:
                - !Sub
                  - 'arn:aws:iam::${accountId}:role/${gtmPlatformLambdaRoleName}'
                  - accountId: !Ref 'AWS::AccountId'
                    gtmPlatformLambdaRoleName: !Ref 'GTMPlatformLambdaRoleName'
            Action:
              - kms:CreateGrant
              - kms:ListGrants
              - kms:RevokeGrant
            Resource: "*"
            Condition:
              Bool:
                kms:GrantIsForAWSResource: 'true'
  GTMPlatformKMSKeyAlias:
    Type: AWS::KMS::Alias
    DependsOn:
      - GTMPlatformKMSKey
    Properties:
      AliasName: !Join ['/', ['alias', !Ref GTMPlatformKMSKeyAliasName]]
      TargetKeyId: !GetAtt GTMPlatformKMSKey.Arn

创建资源GTMPlatformKMSKey 时出现错误。它失败并显示CREATE_FAILED 和错误消息

Policy contains a statement with one or more invalid principals. (Service: AWSKMS; Status Code: 400; Error Code: MalformedPolicyDocumentException; Request ID: 5673456f-b458-45c6-854b-9ed63c737772)

如果我从 GTMPlatformKMSKey 中删除 Sid Allow use of the keyAllow attachment of persistent resources,则模板运行良好。不知道我在这里缺少什么。非常感谢任何帮助

附: - 资源SystemUserAccountDeploymentRoleName 已经存在于环境中

编辑 - 根据建议将模板缩减为仅包含失败的资源

【问题讨论】:

  • sid可以包含空格吗?给 SID 一个不带空格的值
  • 您能否将模板缩减为最相关的部分? (也就是说,删除不影响您的特定问题的部分。)这样,我们可以尝试诊断或重现。有关提出好问题的提示,请参阅:How do I ask a good question?
  • @JohnRotenstein 我把模板缩小了,你现在可以看一下吗
  • 哪个特定策略产生了错误?
  • 您为什么将!Sub 与地图/数组一起使用?像String 一样传递它,例如:` !Sub 'arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${vpc}' `。错误在于您尝试构建主体的方式,如下所示:``` Principal: AWS: - !Sub - 'arn:aws:iam::${accountId}:role/${gtmDeploymentRoleName}' - accountId : !Ref 'AWS::AccountId' gtmDeploymentRoleName: !Ref 'DeploymentRoleName' ``

标签: aws-lambda amazon-cloudformation aws-serverless aws-kms


【解决方案1】:

就我而言,我试图部署一个具有多个帐户的阶段的CdkPipeline 堆栈。我需要在部署我的堆栈的每个帐户和区域上运行 cdk bootstrap ${account}/${region}

cdk bootstrap 123456789012/us-west-2
cdk bootstrap 123456789012/us-east-1
cdk bootstrap 987654321098/us-east-1

我还必须将第二个帐户权限授予per this link

  1. 转到 IAM 中的角色
  2. 选择信任关系选项卡...
  3. 然后编辑信任关系以包括codepipeline
  4. 同时添加其他账户根目录的arn
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": [
          "codepipeline.amazonaws.com"
        ]
      },
      "Action": "sts:AssumeRole"
    },
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "arn:aws:iam::123456789012:root",
          "arn:aws:iam::987654321098:root"
        ]
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

【讨论】:

    【解决方案2】:

    您需要确认您向其分配资源的所有委托人 arn 在您的 AWS 账户中可用或已被正确引用,或者它们在模板中的拼写正确。至少这些是我经历过的。

    【讨论】:

      【解决方案3】:

      您可以在 cli 中使用 cdk bootstrap 调用中的标志设置信任关系:

      npx cdk bootstrap \
      --profile PROFILE2 \
      --trust ACCOUNT1 \
      --cloudformation-execution-policies arn:aws:iam::aws:policy/AdministratorAccess aws://ACCOUNT2/us-east-1
      

      Reference article

      【讨论】:

        【解决方案4】:

        请查看建云模板中提到的KMS用户。这可能是原因之一。

        【讨论】:

          【解决方案5】:

          在我的情况下,我显然没有在外部账户中创建 IAM 角色,这就是为什么我的主账户中的 KMS 抱怨“策略包含具有一个或多个无效委托人的声明”。

          在外部帐户中创建角色后,CloudFormation 成功使用 KMS 更新堆栈。

          【讨论】:

            猜你喜欢
            • 2023-04-05
            • 1970-01-01
            • 2013-06-08
            • 2020-03-29
            • 1970-01-01
            • 1970-01-01
            • 2018-05-22
            • 1970-01-01
            • 2020-04-24
            相关资源
            最近更新 更多