2025-06-06 17:50:13 +00:00
|
|
|
import { Token } from "@sourcebot/schemas/v3/shared.type";
|
2025-11-01 23:45:09 +00:00
|
|
|
import { SecretManagerServiceClient } from "@google-cloud/secret-manager";
|
2025-06-06 17:50:13 +00:00
|
|
|
|
2025-10-31 21:33:28 +00:00
|
|
|
export const getTokenFromConfig = async (token: Token) => {
|
|
|
|
|
if ('env' in token) {
|
2025-06-06 17:50:13 +00:00
|
|
|
const envToken = process.env[token.env];
|
|
|
|
|
if (!envToken) {
|
|
|
|
|
throw new Error(`Environment variable ${token.env} not found.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return envToken;
|
2025-11-01 23:45:09 +00:00
|
|
|
} else if ('gcpSecretName' in token) {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const client = new SecretManagerServiceClient();
|
|
|
|
|
const [ response ] = await client.accessSecretVersion({
|
|
|
|
|
name: token.gcpSecretName,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.payload?.data) {
|
|
|
|
|
throw new Error(`Secret ${token.gcpSecretName} not found.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.payload.data.toString();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log("HERE IN THE EXCEPTION HANDLER");
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2025-06-06 17:50:13 +00:00
|
|
|
} else {
|
|
|
|
|
throw new Error('Invalid token configuration');
|
|
|
|
|
}
|
|
|
|
|
};
|