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

Lambda无法访问KMS密钥

  •  0
  • holtc  · 技术社区  · 6 年前

    The ciphertext refers to a customer master key that does not exist, does not exist in this region, or you are not allowed to access.

    我基本上都是跟着 this 使用aws sam cli创建堆栈,模板的相关部分在代码下面。

    相关代码为:

    const ssm = new AWS.SSM();
    const param = {
        Name: "param1",
        WithDecryption: true
    };
    const secret = await ssm.getParameter(param).promise();
    

    KeyAlias:
        Type: AWS::KMS::Alias
        Properties:
          AliasName: 'param1Key'
          TargetKeyId: !Ref Key
    Key:
        Type: AWS::KMS::Key
        Properties:
          KeyPolicy:
            Id: default
            Statement:
            - Effect: Allow
              Principal:
                AWS: !Sub arn:aws:iam::${AWS::AccountId}:root
              Action:
              - 'kms:Create*'
              - 'kms:Encrypt'
              - 'kms:Describe*'
              - 'kms:Enable*'
              - 'kms:List*'
              - 'kms:Put*'
              - 'kms:Update*'
              - 'kms:Revoke*'
              - 'kms:Disable*'
              - 'kms:Get*'
              - 'kms:Delete*'
              - 'kms:ScheduleKeyDeletion'
              - 'kms:CancelKeyDeletion'
              Resource: '*'
              Sid: Allow root account all permissions except to decrypt the key
            Version: 2012-10-17
    
    LambdaFunction:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: ../
          Handler: app.lambda
          Runtime: nodejs8.10
          Policies:
          - DynamoDBReadPolicy:
              TableName: !Ref Table
          - KMSDecryptPolicy:
              KeyId: !Ref Key
          - Statement:
             - Action:
               - "ssm:GetParameter"
               Effect: Allow
               Resource: !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/param1"
    

    是吗 KMSDecryptPolicy 不允许使用钥匙?我错过了什么?谢谢!

    编辑:改变模板到下面的作品,但我真的很想使用 KMSDecryptPolicy公司

    LambdaFunction:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: ../
          Handler: app.lambda
          Runtime: nodejs8.10
          Policies:
          - DynamoDBReadPolicy:
              TableName: !Ref Table
          - KMSDecryptPolicy:
              KeyId: !Ref Key
          - Statement:
             - Action:
               - "ssm:GetParameter"
               Effect: Allow
               Resource: !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/param1"
    
    Key:
        Type: AWS::KMS::Key
        Properties:
          KeyPolicy:
            Id: default
            Statement:
            - Effect: Allow
              Principal:
                AWS: !Sub arn:aws:iam::${AWS::AccountId}:root
              Action:
              - 'kms:Create*'
              - 'kms:Encrypt'
              - 'kms:Describe*'
              - 'kms:Enable*'
              - 'kms:List*'
              - 'kms:Put*'
              - 'kms:Update*'
              - 'kms:Revoke*'
              - 'kms:Disable*'
              - 'kms:Get*'
              - 'kms:Delete*'
              - 'kms:ScheduleKeyDeletion'
              - 'kms:CancelKeyDeletion'
              Resource: '*'
              Sid: Allow root account all permissions except to decrypt the key
            - Sid: 'Allow use of the key for decryption by the LambdaFunction'
              Effect: Allow
              Principal:
                AWS: !GetAtt LambdaFunctionRole.Arn
              Action:
              - 'kms:Decrypt'
              Resource: '*'        
            Version: 2012-10-17
    
    0 回复  |  直到 6 年前
        1
  •  3
  •   SanD    6 年前

    以下是AWS官方资源,了解为什么会发生这种情况- https://docs.aws.amazon.com/kms/latest/developerguide/iam-policies.html

    根据这个

    所有KMS CMK都有一个密钥策略,您必须使用它来控制对CMK的访问。IAM策略本身不足以允许访问CMK,不过您可以将它们与CMK的密钥策略结合使用。