Created
May 3, 2022 22:13
-
-
Save rafalsep/87794f57c95702b3e417bb1673bee42f to your computer and use it in GitHub Desktop.
Example snippet describing how to retrieve secret from GCP secret manager using @google-cloud/secret-manager library
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
import { SecretManagerServiceClient } from '@google-cloud/secret-manager'; | |
const secretManagerServiceClient = new SecretManagerServiceClient(); | |
let secretCredentials; | |
const initSecretCredentials = async () => { | |
if (process.env.NODE_ENV === 'production') { | |
const [secret] = await secretManagerServiceClient.accessSecretVersion({ | |
name: `projects/${process.env.GCP_PROJECT}/secrets/secret-credentials/versions/latest`, | |
}); | |
secretCredentials = JSON.parse(secret.payload.data.toString()); | |
} else { | |
secretCredentials = (await import('./secretCredentials.json')).default; | |
} | |
return secretCredentials; | |
}; | |
const retrieveSecretCredentials = () => secretCredentials; | |
export { initSecretCredentials, retrieveSecretCredentials }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment