【发布时间】:2021-09-29 12:10:06
【问题描述】:
我正在使用 AWS Secrets Manager 来保护我的 REST API 的数据库信用。我正在使用 AWS Lambda、API Gateway 和 RDS (MySQL)。以下是我获取它们的方法。
// Load the AWS SDK
var AWS = require('aws-sdk'),
region = "us-east-1",
secretName = "test-secret",
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.
exports.handler = (event, context, callback) => {
client.getSecretValue({
SecretId: secretName
}, function(err, data) {
if (err) {
throw err
} else {
// Decrypts secret using the associated KMS CMK.
// Depending on whether the secret is a string or binary, one of these fields will be populated.
if ('SecretString' in data) {
secret = data.SecretString;
} else {
let buff = new Buffer(data.SecretBinary, 'base64');
decodedBinarySecret = buff.toString('ascii');
}
}
// Your code goes here.
console.log(secret);
});
};
下面是输出
INFO {"username":"***","password":"***","engine":"mysql","host":"***.***.us-east-1.rds.amazonaws.com","port":3306,"dbname":"***","dbInstanceIdentifier":"***"}
我尝试像下面这样提取密码
let pass = secret.password;
console.log(pass);
它给出了以下内容
INFO undefined
如何提取password、username、databasename等字段?
【问题讨论】:
标签: javascript node.js amazon-web-services aws-lambda aws-secrets-manager