Skip to content

Instantly share code, notes, and snippets.

@shashkiranr
Last active Mar 9, 2020
Embed
What would you like to do?
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