Skip to content

Instantly share code, notes, and snippets.

@rafalsep
Created May 3, 2022 22:13
Show Gist options
  • Save rafalsep/87794f57c95702b3e417bb1673bee42f to your computer and use it in GitHub Desktop.
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
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