Skip to content

Instantly share code, notes, and snippets.

@shashkiranr
Last active March 9, 2020 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shashkiranr/df00780520c918842d4071083c3890c1 to your computer and use it in GitHub Desktop.
Save shashkiranr/df00780520c918842d4071083c3890c1 to your computer and use it in GitHub Desktop.
GCP Secret Manager to get runtime environment credentials
import {SecretManagerServiceClient} from '@google-cloud/secret-manager';
import * as admin from 'firebase-admin';
const client = new SecretManagerServiceClient();
let credentails: admin.app.App;
export const db = async (): Promise<admin.app.App> => {
if (credentails) {
return credentails;
} else {
const [version] = await client.accessSecretVersion({
name: <resource_id>, // enter the copied resource id here
});
const result: any = JSON.parse(version.payload.data.toString());
const params = {
type: result.type,
projectId: result.project_id,
privateKeyId: result.private_key_id,
privateKey: result.private_key,
clientEmail: result.client_email,
clientId: result.client_id,
authUri: result.auth_uri,
tokenUri: result.token_uri,
authProviderX509CertUrl: result.auth_provider_x509_cert_url,
clientC509CertUrl: result.client_x509_cert_url,
};
credentails = admin.initializeApp({
credential: admin.credential.cert(params),
storageBucket: `gs://${result.project_id}.appspot.com`,
});
return credentails;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment