【问题标题】:The security token included in the request is expired请求中包含的安全令牌已过期
【发布时间】:2016-06-11 16:13:46
【问题描述】:

我有一个脚本,可以从 Cloudwatch 中提取大量指标用于我们自己的内部报告。

该脚本迭代特定区域中的所有 EC2 实例,并询问过去 2 周的 5 个 cloudwatch 指标(所有可用的统计数据)(每次 5 天,间隔 5 分钟,正好是 1440 配额)。我正在使用假定会话:

session = Session(aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, region_name=regionName)
sts = session.client('sts')
response = sts.assume_role(
    RoleArn=arn, # External role arn
    RoleSessionName='role-name',
    ExternalId='<some-id-here>',
)
tempAccessKeyId = response['Credentials']['AccessKeyId']
tempSecretAccessKey = response['Credentials']['SecretAccessKey']
tempSessionToken = response['Credentials']['SessionToken']
assumedSession = Session(
    aws_access_key_id=tempAccessKeyId,
    aws_secret_access_key=tempSecretAccessKey,
    aws_session_token=tempSessionToken,
    region_name=regionName)

在运行脚本时,我遇到了这个异常:

botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the GetMetricStatistics operation: The security token included in the request is expired

有没有办法确保令牌在运行脚本时不会过期?我正在使用 boto3。

【问题讨论】:

    标签: amazon-web-services boto3


    【解决方案1】:

    您使用的假设角色方法返回临时安全凭证。以下摘自official documentation

    临时安全凭证在您调用 AssumeRole 时指定的持续时间有效,可以从 900 秒(15 分钟)到 3600 秒(1 小时)。默认为 1 小时。

    由于您没有使用 DurationSeconds 关键字参数,因此返回的凭据在默认 1 小时内有效。您必须确保获得新的凭据才能在 1 小时后提出请求。请参阅Temporary Security Credentials official documentation 中的以下内容:

    当临时安全凭证到期时(甚至之前),用户可以请求新凭证,只要请求它们的用户仍然有权这样做。

    【讨论】:

      【解决方案2】:

      就我而言,问题是,我的 .aws/configure 中有凭据,并试图从中进行配置,但我没有意识到我还有另一对凭据 AWS_SESSION_TOKEN AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY 设置在环境变量中。

      您可以这样做(这将从环境中删除凭据)。

      unset AWS_SESSION_TOKEN AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY
      

      现在您将只有一组访问密钥,即.aws/configure,并且我能够成功建立连接。

      aws configure
      aws sts get-caller-identity
      

      如果您使用非默认配置文件,请在上述命令中使用--profile 标志。

      【讨论】:

        【解决方案3】:

        我发现AWS Premium Support answer 这个问题非常简洁和相关

        另外需要注意的是,当服务器时间偏离正确时间时有时会出现错误(10-15分钟会导致错误)

        【讨论】:

          【解决方案4】:

          我也遇到了这个错误,甚至我检查了我的.aws/credential 文件。它包含凭据,但我仍然建议执行以下步骤:

          在做任何事情之前,首先你必须运行以下命令

          aws sts get-caller-identity
          

          如果您在 aws 凭据中拥有多个配置文件,则使用该配置文件

          您可以在以下文件.aws/credential 中查看您的个人资料。如果您只有[default],则无需提及个人资料

          aws --profile NAME_OF_YOUR_PROFILE  sts get-caller-identity
          

          现在的问题是上面的命令能解决问题吗?

          不,但至少它会让您知道您的凭据是否正确。我遇到了同样的错误,当我运行上面的命令时,它给了我以下错误

          An error occurred (SignatureDoesNotMatch) when calling the GetCallerIdentity operation: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

          所以至少我发现我使用了错误的凭据。我只需更换凭证,我的问题就解决了。

          【讨论】:

            猜你喜欢
            • 2020-01-11
            • 2015-07-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-12-12
            • 2018-04-09
            • 2021-06-22
            • 1970-01-01
            相关资源
            最近更新 更多