【问题标题】:UnrecognizedClientException: The security token included in the request is invalid when calling AWS.SecretsManagerUnrecognizedClientException:调用 AWS.SecretsManager 时请求中包含的安全令牌无效
【发布时间】:2019-11-12 02:48:11
【问题描述】:

我正在实施 AWS ClientManager 以获取保存在 AWS 中的秘密变量。我的初步实现如下:

// Load the AWS SDK
var AWS = require('aws-sdk'),
    region = "us-west-2",
    secretName = "secretName",
    accessKeyId = myAccessKey,
    secretAccessKey = mySecretAccessKey,
    secret,
    decodedBinarySecret;

var client = new AWS.SecretsManager({
    region: region,
});

client.getSecretValue({SecretId: secretName}, function(err, data) {
    if (err) {
      console.log("Error Happened");
      console.log(err);
    }
    else {
        if ('SecretString' in data) {
            secret = data.SecretString;
        } else {
            let buff = new Buffer(data.SecretBinary, 'base64');
            decodedBinarySecret = buff.toString('ascii');
        }
    }
});

当我启动服务器时,它会抛出以下异常

{ UnrecognizedClientException: 请求中包含的安全令牌无效。 message: '请求中包含的安全令牌无效。', 代码:'UnrecognizedClientException', 时间:2019-07-01T12:16:00.021Z, requestId: 'c7ed53c1-fb70-4012-aa9f-5a9a3195a043', 状态码:400, 可重试:假, 重试延迟:40.923844792180674 }

【问题讨论】:

  • 您能找出问题所在吗?你是怎么解决的?
  • @Rakesh_Kumar 不,我确信这与aws console中的位置设置有关

标签: node.js aws-sdk loopback aws-secrets-manager


【解决方案1】:

您需要为该 aws 提取端点添加您使用 aws configure 定义的令牌访问权限。在创建表时添加此代码连接:

 --endpoint-url http://localhost:8000 //localhost in my case because I'm runing locally, but you can put there you domain or port server

AWS.config.update({
    region: "us-west-2",
    endpoint: "http://localhost:8000",
    accessKeyId: "your access id",
    secretAccessKey: "your acccess key"
});

【讨论】:

    【解决方案2】:

    “请求中包含的安全令牌无效”错误几乎总是意味着您的凭据有问题。 accessKeyId 或 secretAccessKey(或两者)错误。

    您可以尝试使用 AWS cli 使用 STS get caller identity 调用验证您的凭证,然后再在您的代码中使用它们。

    【讨论】:

    • 在运行 CLI aws configure 时,我无法设置令牌。我必须从我的应用程序面板手动复制 ~/.aws/credentials 文件中的令牌。
    • 我遇到了类似的问题,发现这篇文章很有帮助。 bobbyhadz.com/blog/… 在我的情况下,由于某种原因,在设置 aws-cdk 后,我的堆栈环境使用 [default] 用户,在我的情况下该用户处于非活动状态。
    猜你喜欢
    • 2016-12-12
    • 2020-05-05
    • 2020-12-09
    • 2020-03-11
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2018-04-09
    • 2021-06-22
    相关资源
    最近更新 更多