Created
March 21, 2021 15:33
-
-
Save ValentaTomas/dddf5198e8015eb7d1a424729908698a to your computer and use it in GitHub Desktop.
Retrieves and prints the database URL from the Google Secret Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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