Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions secret-manager/getSecret.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,54 @@

'use strict';

async function main(name = 'projects/my-project/secrets/my-secret') {
// [START secretmanager_get_secret]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const name = 'projects/my-project/secrets/my-secret';
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');

// Imports the Secret Manager library
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');

// Instantiates a client
// [START secretmanager_get_secret]
/**
* Get metadata about a secret.
*
* @param {string} projectId The ID of the Google Cloud project.
* @param {string} secretId The ID of the secret to retrieve.
*/
async function getSecret(projectId, secretId) {
const client = new SecretManagerServiceClient();

async function getSecret() {
const name = `projects/${projectId}/secrets/${secretId}`;

Check failure on line 29 in secret-manager/getSecret.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed

Check failure on line 29 in secret-manager/getSecret.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
try {
const [secret] = await client.getSecret({
name: name,
});

const policy = secret.replication.replication;
if (secret.replication && secret.replication.replication) {
const policy = secret.replication.replication;
console.info(
`Found secret ${secret.name} with replication policy ${policy}`
);

Check failure on line 39 in secret-manager/getSecret.js

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`

console.info(`Found secret ${secret.name} (${policy})`);
} else {
console.info(`Found secret ${secret.name} with no replication policy.`);
}
return secret;
} catch (err) {
console.error('Failed to retrieve secret ${name}:', err);
} finally {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There are a couple of issues in the catch block:

  1. The error message uses template literal syntax (${name}) inside a single-quoted string. This prevents string interpolation, so the literal text ${name} will be logged instead of the secret's name. To fix this, you should use backticks (`) to create a template literal string.
  2. The catch block logs the error but doesn't re-throw it. This causes the getSecret function to implicitly return undefined on failure. When run from the command line, the script will then exit with a success code (0) even though an error occurred. The error should be re-thrown to ensure the process exits with a non-zero status code, correctly indicating failure.
  } catch (err) {
    console.error(`Failed to retrieve secret '${name}':`, err);
    throw err;
  }

await client.close();
}
}
// [END secretmanager_get_secret]

async function main() {
const projectId = process.argv[2] || process.env.PROJECT_ID;
const secretId = process.argv[3] || process.env.SECRET_ID;

await getSecret(projectId, secretId);
}
Comment thread
iennae marked this conversation as resolved.

getSecret();
// [END secretmanager_get_secret]
if (require.main === module) {
main().catch(err => {
console.error(err.message);
process.exit(1);

Check failure on line 63 in secret-manager/getSecret.js

View workflow job for this annotation

GitHub Actions / lint

Don't use process.exit(); throw an error instead
});
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
module.exports.getSecret = getSecret;
Loading