Skip to content

Instantly share code, notes, and snippets.

@ValentaTomas
Created March 21, 2021 15:33
Show Gist options
  • Save ValentaTomas/dddf5198e8015eb7d1a424729908698a to your computer and use it in GitHub Desktop.
Save ValentaTomas/dddf5198e8015eb7d1a424729908698a to your computer and use it in GitHub Desktop.
Retrieves and prints the database URL from the Google Secret Manager
#!/usr/bin/env node
const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
const secretManager = new SecretManagerServiceClient();
async function getSecret(name) {
const [secretVersion] = await secretManager.accessSecretVersion({
name: `${name}/versions/latest`,
});
const secretValue = secretVersion.payload.data.toString();
if (!secretValue) throw new Error(`'Cannot retrive secret "${name}" from the Google Secret Manager.'`);
return secretValue;
}
const databaseURLName = 'projects/<id>/secrets/<secretName>';
const devDatabaseURLName = 'projects/<id>/secrets/<devSecretName>';
async function printDatabaseURL() {
const environment = process.argv[2] === 'production' ? 'production' : 'development';
if (environment === 'production') {
console.log(await getSecret(databaseURLName));
} else if (environment === 'development') {
console.log(await getSecret(devDatabaseURLName));
}
}
printDatabaseURL();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment