从 AWS 秘密调用中获取未定义的信息

发布于 2025-01-10 04:01:30 字数 2475 浏览 0 评论 0原文

我已经看到这个问题了,但似乎没有人遇到和我一样的问题。我按照 AWS 文档并按照他们告诉我的方式实现了他们的代码,但我总是得到 undefined ?

这是代码:

// Load the AWS SDK
var AWS = require('aws-sdk'),
    region = "myRegion",
    secretName = "myARN",
    secret,
    decodedBinarySecret;

// Create a Secrets Manager client
var client = new AWS.SecretsManager({
    region: region
});

// In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
// See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
// We rethrow the exception by default.

client.getSecretValue({SecretId: secretName}, function(err, data) {
    if (err) {
        if (err.code === 'DecryptionFailureException')
            // Secrets Manager can't decrypt the protected secret text using the provided KMS key.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
        else if (err.code === 'InternalServiceErrorException')
            // An error occurred on the server side.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
        else if (err.code === 'InvalidParameterException')
            // You provided an invalid value for a parameter.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
        else if (err.code === 'InvalidRequestException')
            // You provided a parameter value that is not valid for the current state of the resource.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
        else if (err.code === 'ResourceNotFoundException')
            // We can't find the resource that you asked for.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
    }
    else {
        // Decrypts secret using the associated KMS key.
        // Depending on whether the secret is a string or binary, one of these fields will be populated.
        if ('SecretString' in data) {
            let secret = data.SecretString;
            secret = JSON.parse(secret);
        } else {
            let buff = Buffer.from(data.SecretBinary, 'base64');
            decodedBinarySecret = buff.toString('ascii');
        }
    }
    
    // Your code goes here.
    // This is where undefined gets spit out
    console.log(decodedBinarySecret);
    console.log(secret);
});

一定有什么我遗漏/没有做的事情?

I've seen this question around, but it seems nobody is having the same problem as me. I followed the AWS docs and implemented their code the way they told me to, but I keep getting undefined back?

Here's the code:

// Load the AWS SDK
var AWS = require('aws-sdk'),
    region = "myRegion",
    secretName = "myARN",
    secret,
    decodedBinarySecret;

// Create a Secrets Manager client
var client = new AWS.SecretsManager({
    region: region
});

// In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
// See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
// We rethrow the exception by default.

client.getSecretValue({SecretId: secretName}, function(err, data) {
    if (err) {
        if (err.code === 'DecryptionFailureException')
            // Secrets Manager can't decrypt the protected secret text using the provided KMS key.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
        else if (err.code === 'InternalServiceErrorException')
            // An error occurred on the server side.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
        else if (err.code === 'InvalidParameterException')
            // You provided an invalid value for a parameter.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
        else if (err.code === 'InvalidRequestException')
            // You provided a parameter value that is not valid for the current state of the resource.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
        else if (err.code === 'ResourceNotFoundException')
            // We can't find the resource that you asked for.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
    }
    else {
        // Decrypts secret using the associated KMS key.
        // Depending on whether the secret is a string or binary, one of these fields will be populated.
        if ('SecretString' in data) {
            let secret = data.SecretString;
            secret = JSON.parse(secret);
        } else {
            let buff = Buffer.from(data.SecretBinary, 'base64');
            decodedBinarySecret = buff.toString('ascii');
        }
    }
    
    // Your code goes here.
    // This is where undefined gets spit out
    console.log(decodedBinarySecret);
    console.log(secret);
});

There must be something that I'm missing / not doing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2025-01-17 04:01:30

看起来,在条件语句的外部 else 部分中,您正在重新声明您也在最顶部声明​​的 secret 变量。该秘密变量获得一个值,但仅限于嵌套的 if 主体内部,而不是外部。这使得秘密变量仍然未定义。

另外,根据您的代码,变量 secret 将具有某个值或 decodedBinarySecret。其中之一将是未定义的。

尽量不要在主体内重新声明变量。请参阅下面的代码。

 else {
    // Decrypts secret using the associated KMS key.
    // Depending on whether the secret is a string or binary...
    if ('SecretString' in data) {
        secret = data.SecretString;
        secret = JSON.parse(secret);
    } else {
        let buff = Buffer.from(data.SecretBinary, 'base64');
        decodedBinarySecret = buff.toString('ascii');
    }
}

It seems that in the outer else part of your conditional statement you're re-declaring the secret variable which you've also declared at the very top. That secret variable gets a value but only inside the nested if body not outside. Which keeps the secret variable still undefined.

Also, according to your code either variable secret will have some value or decodedBinarySecret. One of them will be undefined.

Try not to re-declare the variable inside the body. See below code.

 else {
    // Decrypts secret using the associated KMS key.
    // Depending on whether the secret is a string or binary...
    if ('SecretString' in data) {
        secret = data.SecretString;
        secret = JSON.parse(secret);
    } else {
        let buff = Buffer.from(data.SecretBinary, 'base64');
        decodedBinarySecret = buff.toString('ascii');
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文