当我跑步时:笔记本电脑没有问题,但当我从我的服务器运行时来自我的公司它挂在STSConnection()。get_session_token
这个tempCredentials = sts_connection.get_session_token需要打开任何端口
import boto
import datetime
from datetime import date, timedelta
import subprocess
import os
import argparse
from boto.s3.connection import S3Connection
from boto.sts import STSConnection
import shutil
#command line arguments
parser = argparse.ArgumentParser(description='To create Temp credentials through STS function and upload to ~/.aws/credentials')
parser.add_argument('-d', '--device_id', help='acc xxxxxxxx neumerical value', required=True)
parser.add_argument('-u', '--user_id', help='user id like xxx ', required=True)
parser.add_argument('-p', '--parent_profile', help='parent profile ', required=True)
parser.add_argument('-m', '--mfa_profile', help='profile', required=True)
args = parser.parse_args()
deviceId = args.device_id
userID = args.user_id
parentProfile = args.parent_profile
mfaProfile = args.mfa_profile
# Prompt for MFA time-based one-time password (TOTP)
mfa_TOTP = raw_input("Enter the MFA code: ")
print "STS connection"
#sts connection
sts_connection = STSConnection(profile_name=parentProfile)
print "STS connection temp credentials"
tempCredentials = sts_connection.get_session_token(
duration=43200,
mfa_serial_number="arn:aws:iam::" + deviceId + ":mfa/" + userID,
mfa_token=mfa_TOTP
)
print "STS connection temp credentials closed"
print str(tempCredentials.access_key)
答案 0 :(得分:0)
AWS实施REST API。这意味着端口443需要为出站打开。返回端口可以是1024到65534之间的任何内容。
尝试使用wget或curl向知名网站(https://www.amazon.com)发出正常的HTTPS请求,并确保不会阻止此操作。如果这返回有效的网页,那么AWS REST API就可以了。
仔细检查您是否正在使用STS的全局连接。此链接将为您提供更多信息。尝试wget https://sts.amazonaws.com
以确保STS端点响应。
Activating and Deactivating AWS STS in an AWS Region
最后不要使用您的ROOT凭据。创建IAM用户以分配临时凭证。
使用boto3的最终解决方案:
import boto3
#sts connection
session = boto3.Session(profile_name=parentProfile)
sts_client = session.client('sts')
tempCredentials = sts_client.get_session_token(
DurationSeconds=43200,
SerialNumber="arn:aws:iam::" + deviceId + ":mfa/" + userID,
TokenCode=mfa_TOTP
)
print "STS connection temp credentials closed"
print(tempCredentials)