sourcebot/packages/crypto/src/tokenUtils.ts

26 lines
889 B
TypeScript
Raw Normal View History

import { Token } from "@sourcebot/schemas/v3/shared.type";
2025-11-01 23:45:09 +00:00
import { SecretManagerServiceClient } from "@google-cloud/secret-manager";
2025-11-02 00:07:10 +00:00
export const getTokenFromConfig = async (token: Token): Promise<string> => {
if ('env' in token) {
const envToken = process.env[token.env];
if (!envToken) {
throw new Error(`Environment variable ${token.env} not found.`);
}
return envToken;
2025-11-02 00:07:10 +00:00
} else if ('gcpSecretPath' in token) {
2025-11-01 23:45:09 +00:00
const client = new SecretManagerServiceClient();
2025-11-02 00:07:10 +00:00
const [response] = await client.accessSecretVersion({
name: token.gcpSecretPath,
2025-11-01 23:45:09 +00:00
});
if (!response.payload?.data) {
2025-11-02 00:07:10 +00:00
throw new Error(`Secret ${token.gcpSecretPath} not found.`);
2025-11-01 23:45:09 +00:00
}
return response.payload.data.toString();
} else {
throw new Error('Invalid token configuration');
}
};