Skip to content
Snippets Groups Projects
Commit d9c674d1 authored by roshan's avatar roshan
Browse files

gcp secrete key

parent 92255603
No related branches found
No related tags found
No related merge requests found
......@@ -3,14 +3,16 @@ import { SecretManagerServiceClient } from '@google-cloud/secret-manager';
import * as fs from 'fs';
import * as path from 'path';
async function createSecretManagerClient(): Promise<{
export async function createSecretManagerClient(): Promise<{
client: SecretManagerServiceClient;
projectId: string;
}> {
try {
const keyPath = path.resolve(__dirname, 'gcp-key.json');
if (fs.existsSync(keyPath)) {
const keyPath = path.resolve(process.cwd(), 'gcp-key.json'); // or wherever it is
const credentials = JSON.parse(fs.readFileSync(keyPath, 'utf8'));
if (credentials) {
const client = new SecretManagerServiceClient({
credentials: {
client_email: credentials.client_email,
......@@ -18,19 +20,21 @@ async function createSecretManagerClient(): Promise<{
},
projectId: credentials.project_id,
});
console.log('Using service account from local JSON file');
console.log('Using credentials object from memory');
return { client, projectId: credentials.project_id };
} else {
throw new Error('No local key file found');
}
throw new Error('No credentials set; falling back...');
} catch {
const client = new SecretManagerServiceClient();
const projectId = await client.getProjectId();
console.log('Using Google Application Default Credentials');
console.log('Using Application Default Credentials');
return { client, projectId };
}
}
function toEnvKey(secretId: string): string {
return secretId.toUpperCase().replace(/[^A-Z0-9_]/gi, '_');
}
......@@ -77,6 +81,4 @@ async function fetchAllSecretsAndWriteEnv() {
console.log(`All secrets written to ${envFilePath}`);
}
fetchAllSecretsAndWriteEnv().catch(err => {
console.error('Error fetching secrets:', err);
});
export { fetchAllSecretsAndWriteEnv };
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment