【问题标题】:How to extract fields from this AWS SecretsManager JSON Object?如何从此 AWS SecretsManager JSON 对象中提取字段?
【发布时间】: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

如何提取passwordusernamedatabasename等字段?

【问题讨论】:

    标签: javascript node.js amazon-web-services aws-lambda aws-secrets-manager


    【解决方案1】:

    一开始,你返回secretdata.SecretString,然后现在secret 只是一个普通的字符串。在您的情况下,它是一个 JSON 字符串,您必须将您的字符串转换为 JSON 对象,然后您可以轻松地通过属性名称访问信息。

    为此,您可以使用JSON.parse 方法将json字符串转换为json对象:

    var secret = `{"username":"***","password":"***","engine":"mysql","host":"***.***.us-east-1.rds.amazonaws.com","port":3306,"dbname":"***","dbInstanceIdentifier":"***"}
    `;
    
    const secretObj = JSON.parse(secret);
    
    console.log(secretObj.host)

    【讨论】:

      【解决方案2】:

      此输出是 JSON 对象文字,因此您可以使用以下格式访问每个对象:

      secret.password
      

      const secret= {"username":"yourusername","password":"Pa$$w0rd","engine":"mysql","host":"***.***.us-east-1.rds.amazonaws.com","port":3306,"dbname":"***","dbInstanceIdentifier":"***"};
      console.log(secret.password);
      console.log(secret.username);
      document.getElementById("secret").innerHTML = secret.password;
      <p>
      Your Password is : <span id="secret"></span>
      </p>

      【讨论】:

      • 不,不是。它返回INFO Undefined。这也与 HTML 无关。
      • @JustCause 我在示例中使用 html 以获得简单和更好的视图!我知道你不会在 html 中使用它!你是认真的为此投反对票吗! “INFO”在你的输出中还是你自己在输出之前写这个字符串?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      相关资源
      最近更新 更多