Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
How to extract fields from this AWS SecretsManager JSON Object?

I am using AWS Secrets manager to protect the database credits of my REST API. I am using AWS Lambda, API Gateway and RDS (MySQL). Below is how I get them.

// 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);
    });
};

Below is the output

INFO    {"username":"***","password":"***","engine":"mysql","host":"***.***.us-east-1.rds.amazonaws.com","port":3306,"dbname":"***","dbInstanceIdentifier":"***"}

I tried to extract the password like below

let pass = secret.password;
        console.log(pass);

It gave the following

INFO    undefined

How can I extract fields such as password, username, databasename etc?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

At first, you get back secret as data.SecretString, then now secret just is a normal string. In your case, it is a JSON string, you must cast your string to a JSON object, then you can access the information by attribute name easily.

To do that, you can use JSON.parse method to convert a json string to json object:

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)

over 4 years ago · Santiago Trujillo Relatório

0

This Output is a JSON object literal so You Can access each object with bellow format :

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>

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda