使用 AWS CLI 代入 IAM 角色时,如何对 AWS STS 错误“the security token included in the request is expired”(请求中包含的安全令牌已过期)进行故障排除?
我尝试使用 AWS Command Line Interface (AWS CLI) 代入 AWS Identity and Access Management (IAM) 角色。但是,我收到了与以下内容类似的错误消息: “The security token included in the request is expired.”(请求中包含的安全令牌已过期。)
简短描述
IAM 用户的临时安全凭证使用 AWS Security Token Service (AWS STS) 进行请求。默认情况下,使用 AssumeRole API 操作创建的临时凭证的持续时间为一小时。临时凭证过期后,无法重复使用。有关更多信息,请参阅 IAM 中的临时安全凭证。
解决方法
针对您的使用案例使用以下故障排除步骤。
如果在运行 AWS CLI 命令时收到错误,请确保您使用的是最新版本的 AWS CLI。
确保您的临时安全凭证请求可以到达 AWS 端点
为角色建立凭证需要访问密钥 ID、秘密访问密钥和会话令牌。发送的请求必须在请求时间戳五分钟内到达 AWS 端点,否则请求将被拒绝。有关更多信息,请参阅为什么为请求签名。
使用配置文件代入 IAM 角色
命名的配置文件是可以应用于 AWS CLI 命令的设置和凭证的集合。您必须验证您在使用正确的凭证。
以下 AWS CLI 命令使用默认的配置文件凭证:
aws s3 ls
此示例命令使用 .config 文件中配置的 project1 配置文件凭证:
aws s3 ls --profile project1
使用过期凭证的示例输出:
"An error occurred (ExpiredToken) when calling the ListBuckets operation: The provided token has expired."
这些配置文件在包含 .credentials 和 .config 文件的 .aws 文件夹中定义。
对于 Linux/macOS,配置文件位于 ~/.aws/config,对于 Windows,配置文件位于 C:\Users%USERPROFILE%.aws\config。对于 Linux/macOS,凭证文件位于 ~/.aws/credentials,对于 Windows,凭证文件位于 C:\Users%USERPROFILE%.aws\credentials。
要检查您的默认配置文件凭证,请运行以下命令:
aws configure list --profile default
示例输出:
Name Value Type Location ---- ----- ---- -------- profile default manual —profile access_key TGN7 shared-credentials-file secret_key SbXb shared-credentials-file region us-east-1 config-file ~/.aws/config
要确认是否为配置文件 project1 使用了相同的凭证,请运行以下命令:
aws configure list --profile project1
示例输出:
Name Value Type Location ---- ----- ---- -------- profile project1 manual —profile access_key QN2X config-file secret_key LPYI config-file region eu-west-1 config-file ~/.aws/config
请注意,在示例输出中,可能会为默认配置文件和 project1 配置文件配置不同的凭证。
您可以使用以下格式在 .aws/config 文件中创建配置文件:
[profile project1] region = eu-west-1 aws_access_key_id = <access-Key-for-an-IAM-role> aws_secret_access_key = <secret-access-Key-for-an-IAM-role> aws_session_token = <session-token>
这些凭证在您运行与以下内容类似的 AWS STS assume-role 命令时向您提供:
aws sts assume-role --role-arn arn:aws:iam::<account-number>:role/Prod-Role --role-session-name environment-prod
示例输出:
{ "AssumedRoleUser": { "AssumedRoleId": "AROAXXXXXXXXXXXX:environment-prod", "Arn": "arn:aws:sts::<account-number>:assumed-role/Prod-Role/environment-prod" }, "Credentials": { "SecretAccessKey": "<secret-access-Key-for-an-IAM-role>, "SessionToken": "<session-token>", "Expiration": "2020-03-31T17:17:53Z", "AccessKeyId": "<access-Key-for-an-IAM-role>" }
注意: 您可以使用适合您的使用案例的 DurationSeconds 参数为 IAM 角色的临时凭证延长最大会话持续时间有效期。
然后,新的 assume-role API 调用将检索一组新的有效凭证。在 API 调用之后,您必须使用新的临时凭证手动更新 ~/.aws/config 文件。
您可以避免在每次会话过期时更新 config 文件。在与以下内容类似的 ~/.aws/config 或 ~/.aws/credentials 文件中为 IAM 角色以及代入角色的用户定义一个配置文件:
[profile project1] role_arn = <arn-of-IAM-role> source_profile = user1 region = <region>
请注意,user1 在与以下内容类似的 ~/.aws/credentials 文件中定义:
[user1] aws_access_key_id=AKIAIOSFODNN7EXAMPLE aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
定义 source_profile 意味着您不必在 ~/.aws/config 或 ~/.aws/credentials 文件中更新临时凭证。
以下 AWS CLI 命令列出了 Amazon Simple Storage Service (Amazon S3) 存储桶,其 user1 凭证位于 ~/.aws/credentials 文件中。
aws s3 ls --profile project1
如果您使用包含 source_profile 元素的 AWS CLI,则 API 调用 assume-role 会将凭证放在 .aws/cli/cache 文件中。过期的凭证会在 .aws/cli/cache 文件中自动更新。如果您收到过期凭证的错误消息,则可以使用以下命令清除缓存:
Linux/macOS:
$ rm -r ~/.aws/cli/cache
Windows:
C:\> del /s /q %UserProfile%\.aws\cli\cache
AWS CLI 会在缓存中创建新的凭证。
创建环境变量以代入 IAM 角色,然后验证访问权限
您可以使用 IAM 角色凭证创建三个环境变量以代入与以下内容类似的 IAM 角色:
Linux/macOS:
export AWS_ACCESS_KEY_ID=RoleAccessKeyID export AWS_SECRET_ACCESS_KEY=RoleSecretKey export AWS_SESSION_TOKEN=RoleSessionToken
Windows:
C:\> setx AWS_ACCESS_KEY_ID RoleAccessKeyID C:\> setx AWS_SECRET_ACCESS_KEY RoleSecretKey C:\> setx AWS_SESSION_TOKEN RoleSessionToken
要验证您是否代入了正确的 IAM 角色,请运行以下命令:
aws sts get-caller-identity
get-caller-identity 命令显示有关用于对请求进行身份验证的 IAM 身份的信息。有关更多信息,请参阅如何使用 AWS CLI 代入 IAM 角色?
即使临时缓存的凭证过期后,环境变量也会保留这些凭证,并且不会自动续订。使用以下命令检查是否设置了凭证环境变量:
Linux/macOS:
$ printenv | grep AWS
Windows:
C:\>set AWS
您可以使用以下命令删除过期的环境变量:
Linux/macOS:
$ unset AWS_ACCESS_KEY_ID $ unset AWS_SECRET_ACCESS_KEY $ unset AWS_SESSION_TOKEN
Windows:
C:\>set AWS_ACCESS_KEY_ID= C:\>set AWS_SECRET_ACCESS_KEY= C:\>set AWS_SESSION_TOKEN=
现在,您可以再次使用 assume-role API 调用来获取新的有效凭证并重新设置环境变量。
重要事项: .aws/credentials 和 .aws/config 文件包含 IAM 实体的凭证详细信息。管理凭证时,请确保遵循 IAM 中的安全最佳实践。
相关信息
相关内容
- AWS 官方已更新 1 年前
- AWS 官方已更新 1 年前
- AWS 官方已更新 2 年前