我正在尝试创建一个python脚本来连接到我的AWS帐户并与其进行交互。我在这里读到的https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html
我看到它从~/..aws/凭据(在Linux机器上)读取您的凭据。但是,我并没有连接到IAM用户,而是SSO用户。因此,我使用的配置文件连接数据位于~/..aws/sso/cache目录下。
在该目录中,我看到两个json文件。一个有以下钥匙:
第二个键有以下键:
我在文档中没有看到任何关于如何告诉它使用我的SSO用户的信息。
因此,当我试图运行我的脚本时,我会得到错误,例如
botocore.exceptions.ClientError: An error occurred (AuthFailure) when calling the DescribeSecurityGroups operation: AWS was not able to validate the provided access credentials
即使我可以从命令提示符运行相同的命令罚款。
发布于 2020-06-17 18:21:14
您当前的. .aws/sso/cache文件夹结构如下所示:
$ ls
botocore-client-XXXXXXXX.json cXXXXXXXXXXXXXXXXXXX.json
2 json文件包含3个有用的不同参数。
botocore-client-XXXXXXXX.json -> clientId and clientSecret
cXXXXXXXXXXXXXXXXXXX.json -> accessToken
使用cXXXXXXXXXXXXXXXXXXX.json中的访问令牌,您可以调用获取角色凭据。此命令的输出可用于创建新会话。
Python文件应该如下所示:
import json
import os
import boto3
dir = os.path.expanduser('~/.aws/sso/cache')
json_files = [pos_json for pos_json in os.listdir(dir) if pos_json.endswith('.json')]
for json_file in json_files :
path = dir + '/' + json_file
with open(path) as file :
data = json.load(file)
if 'accessToken' in data:
accessToken = data['accessToken']
client = boto3.client('sso',region_name='us-east-1')
response = client.get_role_credentials(
roleName='string',
accountId='string',
accessToken=accessToken
)
session = boto3.Session(aws_access_key_id=response['roleCredentials']['accessKeyId'], aws_secret_access_key=response['roleCredentials']['secretAccessKey'], aws_session_token=response['roleCredentials']['sessionToken'], region_name='us-east-1')
https://stackoverflow.com/questions/62311866
复制相似问题