无法通过 AWS 配置文件承担角色

dav*_*nte 2 amazon-web-services amazon-iam botocore

我们总是使用以下命令在远程计算机上扮演角色超过一个小时:

# Prep environment to use roles.
unset AWS_CONFIG_FILE
unset AWS_DEFAULT_REGION
unset AWS_DEFAULT_PROFILE

CONFIG_FILE=$(mktemp)

# Creates temp file with instance profile credentials as default
# AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, ROLE_ARN are available from the environment.
printf "[default]\naws_access_key_id=$AWS_ACCESS_KEY_ID\naws_secret_access_key=$AWS_SECRET_ACCESS_KEY\n[profile role_profile]\nrole_arn = $ROLE_ARN\nsource_profile = default" > $CONFIG_FILE

# make sure instance profile takes precedence
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN

export AWS_CONFIG_FILE=$CONFIG_FILE
export AWS_DEFAULT_REGION=us-east-1
export AWS_DEFAULT_PROFILE=role_profile
Run Code Online (Sandbox Code Playgroud)

不幸的是,这种方法最近开始失效。我们只需运行以下命令即可重现故障:

aws sts get-caller-identity
Run Code Online (Sandbox Code Playgroud)

将标志添加--debug到最后一个命令:

09:11:47 2018-06-21 14:11:47,731 - MainThread - awscli.clidriver - DEBUG - CLI version: aws-cli/1.15.40 Python/2.7.12 Linux/4.9.76-3.78.amzn1.x86_64 botocore/1.10.40
...
09:11:47 2018-06-21 14:11:47,811 - MainThread - botocore.hooks - DEBUG - Event choose-signer.sts.GetCallerIdentity: calling handler <function set_operation_specific_signer at 0x7f22d19a6ed8>
09:11:47 2018-06-21 14:11:47,812 - MainThread - botocore.credentials - WARNING - Refreshing temporary credentials failed during mandatory refresh period.
09:11:47 Traceback (most recent call last):
09:11:47   File "/var/lib/jenkins/.local/lib/python2.7/site-packages/botocore/credentials.py", line 432, in _protected_refresh
...
09:11:47     raise KeyError(cache_key)
09:11:47 KeyError: 'xxxx' (redacted)
09:11:47 2018-06-21 14:11:47,814 - MainThread - awscli.clidriver - DEBUG - Exiting with rc 255
Run Code Online (Sandbox Code Playgroud)

显然,Python“缓存”字典中缺少一个键。

dav*_*nte 7

显而易见的解决方案就是找到并删除缓存:

rm ~/.aws/cli/cache/*
Run Code Online (Sandbox Code Playgroud)

但这并不能解释这种情况是如何开始发生的(以及是否会再次发生)。谁能解释一下发生了什么?