使用 AWS CLI 承擔 IAM 角色時,如何對 AWS STS 錯誤 “the security token included in the request is expired (請求中包含的安全字符已過期)” 進行疑難排解?

3 分的閱讀內容
0

我嘗試使用 AWS Command Line Interface (AWS CLI) 擔任 AWS Identity and Access Management (IAM) 角色。但收到類似於以下內容的錯誤: "The security token included in the request is expired. (請求中包含的安全字符已過期。)"

簡短描述

使用 AWS Security Token Service (AWS STS) 服務請求 IAM 使用者的臨時安全憑證。依預設,透過 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."

這些設定檔在 .aws 資料夾中定義,包含 .credentials 和 .config 檔案。

該組態檔案位於 ~/.aws/config (Linux/macOS) 和 C:\Users%USERPROFILE%.aws\config (Windows)。憑證檔案位於 ~/.aws/credentials (Linux/macOS) 和 C:\Users%USERPROFILE%.aws\credentials (Windows)。

若要檢查您的預設設定檔憑證,請執行下列命令:

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 檔案。

您可以避免在每次工作階段過期時更新組態檔案。如下所示定義 IAM 角色的設定檔,以及在 ~/.aws/config~/.aws/credentials 檔案中擔任角色的使用者:

[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) 儲存貯體,其中包含位於 ~/.aws/credentials 檔案中的 user1 憑證。

aws s3 ls --profile project1

如果您將 AWS CLI 與 source_profile 元素搭配使用,則 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 中的安全最佳實務


相關資訊

請求臨時安全憑證

如何解決在 Amazon EC2 上執行 Java 應用程式時發生的 “The security token included in the request is expired (請求中包含的安全字符已過期)” 錯誤?

AWS 官方
AWS 官方已更新 2 年前