python - 用户无权担任 IAM 角色

标签 python sqlalchemy amazon-redshift aws-cloudformation

我有一个 bash 脚本,它从 CloudFormation 堆栈中提取信息并将其放入 Python 脚本中。 Python 脚本旨在连接到 Redshift 数据库、创建表,然后从从 S3 存储桶下载的文件导入数据。

第一部分有效:

endpoint=$(aws cloudformation describe-stacks --stack-name=BallotOnlineRS --region=us-west-2 \
            --query 'Stacks[0].Outputs[?OutputKey==`RedshiftClusterEndpointAddress`].OutputValue' \
            --output text)

role=$(aws cloudformation describe-stack-resources --stack-name=BallotOnlineRS --region=us-west-2 \
            --logical-resource-id RawDataBucketAccessRole |grep 'PhysicalResourceId' |cut -d: -f2 |sed 's/^ //; s/,//; s/"//g')

sed -i "s/ENDPOINT/${endpoint}/; s/ROLE/${role}/" /tmp/ballotonline_data.py

这会导致必要的字符串到达​​它们需要的地方:

HOST = 'ballotonliners-redshiftcluster-xxxxxxxxxxxx.cgsdneukfjtv.us-west-2.redshift.amazonaws.com'

ARN_CREDENTIALS = 'arn:aws:iam::xxxxxxxxxxx:role/BallotOnlineRS-RawDataBucketAccessRole-xxxxxxxxxxxx

但是,当我执行 Python 脚本时,我从 sqlalchemy 收到以下错误:

sqlalchemy.exc.InternalError: (psycopg2.errors.InternalError_) User arn:aws:redshift:us-west-2:xxxxxxxxxxx:dbuser:ballotonliners-redshiftcluster-1qb2hxkta10t9/admin is not authorized to assume IAM Role arn:aws:iam::xxxxxxxxxxx:role/BallotOnlineRS-RawDataBucketAccessRole-1U56NHFY528VW
DETAIL:
  -----------------------------------------------
  error:  User arn:aws:redshift:us-west-2:xxxxxxxxxxx:dbuser:ballotonliners-redshiftcluster-xxxxxxxxxxxx/admin is not authorized to assume IAM Role arn:aws:iam::xxxxxxxxxxx:role/BallotOnlineRS-RawDataBucketAccessRole-xxxxxxxxxxxx
  code:      8001
  context:   IAM Role=arn:aws:iam::xxxxxxxxxxx:role/BallotOnlineRS-RawDataBucketAccessRole-xxxxxxxxxxxx
  query:     76
  location:  xen_aws_credentials_mgr.cpp:272
  process:   padbmaster [pid=13823]
  -----------------------------------------------

错误中提到的 admin 用户是在创建 Redshift 堆栈时创建的,如果我通过登录查询编辑器通过 Redshift UI 访问数据库,则使用同一帐户不会收到错误。这种情况的不同之处在于,我使用用户名:密码登录,而不是(我猜)假设角色。具体来说,就是错误中的那个。

Redshift 堆栈是使用模板 provided by Thorntech 构建的。执行时也是成功的。

对这个问题有什么想法吗?

编辑: 我在 AWS Developer forum 上发现了应该是相同的问题这表明我不应该使用物理资源 ID,而应该使用逻辑 ID。在这种情况下,我应该使用 RawDataBucketAccessRole,而不是 BallotOnlineRS-RawDataBucketAccessRole-xxxxxxxxxxxx。但是,这会导致相同的错误,区别在于逻辑 ID 替换了物理 ID。

编辑2: 为了回答 John Rotenstein 关于关联角色的问题,以下是定义集群及其相关组件的部分:

Resources:
  RedshiftCluster: 
    Type: AWS::Redshift::Cluster
    Properties: 
      ClusterSubnetGroupName: !Ref RedshiftClusterSubnetGroup
      ClusterType: !If [ SingleNode, single-node, multi-node ]  
      NumberOfNodes: !If [ SingleNode, !Ref 'AWS::NoValue', !Ref RedshiftNodeCount ] #'
      DBName: !Sub ${DatabaseName}
      IamRoles:
        - !GetAtt RawDataBucketAccessRole.Arn
      MasterUserPassword: !Ref MasterUserPassword
      MasterUsername: !Ref MasterUsername
      PubliclyAccessible: true
      NodeType: dc2.large
      Port: 5439
      VpcSecurityGroupIds: 
        - !Sub ${RedshiftSecurityGroup}
      PreferredMaintenanceWindow: Sun:09:15-Sun:09:45
  DataBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub ${DataBucketName}
  RawDataBucketAccessRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement: 
          - 
            Effect: Allow
            Principal:
              Service:
                - redshift.amazonaws.com
            Action:
              - sts:AssumeRole
  RawDataBucketRolePolicy:
    Type: AWS::IAM::Policy
    Properties:
      PolicyName: RawDataBucketRolePolicy
      PolicyDocument: 
        Version: 2012-10-17
        Statement:
          - 
            Effect: Allow
            Action: s3:ListAllMyBuckets
            Resource: arn:aws:s3:::*
          - 
            Effect: Allow
            Action: 
              - 's3:Get*'
              - 's3:List*'
            Resource: '*'
          - 
            Effect: Allow
            Action: cloudwatch:*
            Resource: "*"
      Roles:
        - !Ref RawDataBucketAccessRole  

集群在角色之前定义重要吗?我怀疑它不会,因为该角色列在 CF UI 中堆栈的“资源”选项卡下。

编辑3: 我找到了另一个,similar issue但对于一个产品来说。 OP 表示他找到了解决方案。因为他使用了错误的角色进行身份验证。当我研究解决我的问题的途径时,它似乎并不适用,因为我只有一个角色。

最佳答案

我已经到了把东西扔到墙上看看什么会粘住的地步。最终我让它发挥作用。我不知道的是,我一次所做的最后两项更改中的哪一项解决了问题(或原因):将 Path: 属性添加到 IAM::Role 定义或将策略与角色定义内联并删除外部策略定义。我最终得到的是(将其与我在上面问题正文中放置的内容进行比较)

  RawDataBucketAccessRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - 
            Effect: Allow
            Principal:
              Service:
                - 'redshift.amazonaws.com'
            Action:
              - 'sts:AssumeRole'
      Path: '/'
      RoleName: RawDataBucketAccessRole
      Policies:
        - 
          PolicyName: RawDataBucketRolePolicy
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - 
                Effect: Allow
                Action:
                  - 's3:Get*'
                  - 's3:List*'
                Resource: '*'

这允许admin用户(在MasterUsername参数中定义)承担该角色并访问S3存储桶。

关于python - 用户无权担任 IAM 角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55778161/

相关文章:

postgresql - 将数据从 PostgreSQL 移动到 AWS S3 并使用 RedShift Spectrum 进行分析

python - Byte Embedding in MLSTM概念之争

python - 如何使用 skimage 获取 hough 线峰的 extream x,y 坐标

python - 在 SQLAlchemy 中查询浮点值

python - 过滤 SQLAlchemy 关系

sql - 将文件名合并到 Redshift COPY 中

sql - 我如何找到一个组在 redshift 中有什么特权?

python - 如何在 Cygwin 上运行可执行文件

python - 打开作为记录存储在 Django 数据库中的文本文件

python - 使用 AttributeExtension 自动更新非规范化属性