我想在本地使用 moto 测试我的 sts 设置。
get_aws_temp_credentials.py
import boto3
from botocore.exceptions import ClientError
def assume_role(aws_arn=None, aws_session_name=None):
if aws_arn is not None and aws_session_name is not None:
try:
local_session_name = aws_session_name[:64]
client = boto3.client('sts', region_name=Constants.pubAwsRegion)
response = client.assume_role(RoleArn=aws_arn,
RoleSessionName=local_session_name,
DurationSeconds=900)
return response
except ClientError as error:
logger.info({"message": "get_aws_temp_credentails.py: Can not assume Role"})
return 25
if __name__ == '__main__':
assume_role()
test_assume_role.py :
import os
import pytest
from moto import mock_sts
@pytest.fixture
def aws_credentials():
"""Mocked AWS Credentials for moto."""
os.environ['AWS_ACCESS_KEY_ID'] = 'testing'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing'
os.environ['AWS_SECURITY_TOKEN'] = 'testing'
os.environ['AWS_SESSION_TOKEN'] = 'testing'
@pytest.fixture
def sts(aws_credentials):
with mock_sts():
yield boto3.client('sts', region_name=Constants.AwsRegion)
def test_temp_credentials(sts):
from get_aws_temp_credentials import *
response = assume_role(aws_arn=Constants.awsArn_to_assume_not_exists,
aws_session_name="My Dummy session")
assert response == 25
根据moto,这应该可以。但它没有返回任何响应,也没有对assume_role
呼叫做出反应。