Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
57 changes: 38 additions & 19 deletions secret-manager/getSecret.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,51 @@

'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}`;
try {
const [secret] = await client.getSecret({
name: name,
});

const policy = secret.replication.replication;

console.info(`Found secret ${secret.name} (${policy})`);
if (secret.replication && secret.replication.replication) {
const policy = secret.replication.replication;
console.info(
`Found secret ${secret.name} with replication policy ${policy}`
);
} else {
console.info(`Found secret ${secret.name} with no replication policy.`);
}
return secret;
} catch (err) {
console.error(`Failed to retrieve secret ${name}:`, err);
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Dismissed
} finally {
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);
});
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
module.exports.getSecret = getSecret;
4 changes: 2 additions & 2 deletions secret-manager/test/secretmanager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ describe('Secret Manager samples', () => {
assert.match(output, new RegExp(`${regionalSecret.name}`));
});

it('gets a secret', async () => {
const output = execSync(`node getSecret.js ${secret.name}`);
it('gets metadata about a secret', async () => {
const output = execSync(`node getSecret.js ${projectId} ${secretId}`);
assert.match(output, new RegExp(`Found secret ${secret.name}`));
});

Expand Down
Loading