簡體   English   中英

AWS 承擔的角色無法與 boto3 一起按預期工作

[英]AWS assume role not working as expected with boto3

我想使用 aws SSM 在 ec2 實例 (i-0691847a77) 上執行 ssm:DescribeInstanceInformation,方法是假設一個定義了以下策略的 IAM 角色 (iam_ssm_role)。

兩個 IAM 角色都在同一個 aws 帳戶上,並且 iam_base_role arn 已添加為 iam_ssm_role 中的可信策略。

     {
        "Sid": "VisualEditor1",
        "Effect": "Allow",
        "Action": [
            "ssm:*",
            "ec2:DescribeImages",
            "cloudwatch:PutMetricData",
            "ec2:DescribeInstances",
            "lambda:InvokeFunction",
            "ec2:DescribeTags",
            "ec2:DescribeVpcs",
            "cloudwatch:GetMetricStatistics",
            "ec2:DescribeSubnets",
            "ec2:DescribeKeyPairs",
            "cloudwatch:ListMetrics",
            "ec2:DescribeSecurityGroups"
        ],
        "Resource": "*"
    }

我在具有 IAM 角色 (iam_base_role) 的 ec2 實例上運行以下代碼

import boto3
from boto3.session import Session


def assume_role(arn, session_name):
    client = boto3.client('sts')
    response = client.assume_role(RoleArn=arn, RoleSessionName=session_name)
    session = Session(aws_access_key_id=response['Credentials']['AccessKeyId'],
                  aws_secret_access_key=response['Credentials']['SecretAccessKey'],
                  aws_session_token=response['Credentials']['SessionToken'])
    client = session.client('sts')
    account_id = client.get_caller_identity()["Account"]
    print(response['AssumedRoleUser']['AssumedRoleId'])
assume_role('arn:aws:iam::000001:role/iam_ssm_role', 'ssm_session')


client = boto3.client('ssm', region_name = 'us-east-1')
ssm_response = client.describe_instance_information(
    InstanceInformationFilterList=[
        {
            'key': 'InstanceIds',
            'valueSet': [
                'i-0f0099877fgg'
            ]
        }
    ]
)

print(ssm_response)

我收到拒絕訪問錯誤,假定角色顯示為“iam_ssm_role”,但看起來 SSM 正在使用 iam_base_role 而不是 iam_ssm_role

AROAV6BDS6PTVQBU:iam_ssm_role

botocore.exceptions.ClientError: An error occurred (AccessDeniedException) when calling the DescribeInstanceInformation operation: User: arn:aws:sts::000001:assumed-role/iam_base_role/i-0691847a77 is not authorized to perform: ssm:DescribeInstanceInformation on resource: arn:aws:ssm:us-east-1:000001:*

好的,我發現我之前的代碼存在問題,我沒有在 boto3.client SSM 部分使用假定的 iam 角色憑據。

我現在可以成功運行代碼,我現在正在使用下面的代碼。

import boto3

boto_sts=boto3.client('sts')
stsresponse = boto_sts.assume_role(
    RoleArn="arn:aws:iam::000001:role/iam_ssm_role",
    RoleSessionName='newsession'
)

newsession_id = stsresponse["Credentials"]["AccessKeyId"]
newsession_key = stsresponse["Credentials"]["SecretAccessKey"]
newsession_token = stsresponse["Credentials"]["SessionToken"]


client = boto3.client('ssm', 
                      region_name = 'us-east-1',
                      aws_access_key_id=newsession_id,
                      aws_secret_access_key=newsession_key,
                      aws_session_token=newsession_token)

ssm_response = client.describe_instance_information(
    InstanceInformationFilterList=[
        {
            'key': 'InstanceIds',
            'valueSet': [
                'i-0f0099877fgg'
            ]
        }
    ]
)

print(ssm_response)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM