UNPKG

adk-typescript

Version:

TypeScript port of Google's Agent Development Kit (ADK)

79 lines (78 loc) 3.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SecretManagerClient = void 0; const axios_1 = __importDefault(require("axios")); /** * A client for interacting with Google Cloud Secret Manager. * * This class provides a simplified interface for retrieving secrets from * Secret Manager, handling authentication using either a service account * JSON keyfile (passed as a string) or a pre-existing authorization token. */ class SecretManagerClient { /** * Initializes the SecretManagerClient. * * @param params Configuration parameters * @param params.serviceAccountJson The content of a service account JSON keyfile (as a string), not the file path. Must be valid JSON. * @param params.authToken An existing Google Cloud authorization token. * @throws Error if neither serviceAccountJson nor authToken is provided, or if both are provided. Also raised if the serviceAccountJson is not valid JSON. */ constructor(params) { this.credentials = null; if (params.serviceAccountJson && params.authToken) { throw new Error("Must provide either 'serviceAccountJson' or 'authToken', not both."); } if (params.authToken) { this.credentials = { token: params.authToken }; } else if (params.serviceAccountJson) { try { const serviceAccount = JSON.parse(params.serviceAccountJson); // In a TypeScript environment, we can't directly use the Google Auth library the same way // We'd need to use something like the Google Cloud client libraries or custom implementation this.credentials = { serviceAccount }; } catch (e) { throw new Error(`Invalid service account JSON: ${e}`); } } else { throw new Error("Must provide either 'serviceAccountJson' or 'authToken'"); } } /** * Retrieves a secret from Google Cloud Secret Manager. * * @param resourceName The full resource name of the secret. * Usually you want the "latest" version, e.g., "projects/my-project/secrets/my-secret/versions/latest". * @returns The secret payload as a string. * @throws Error if the Secret Manager API returns an error */ async getSecret(resourceName) { // This is a simplified implementation since the full implementation would require Google Cloud SDK // In a real environment, you would use the @google-cloud/secret-manager package if (!this.credentials.token) { throw new Error("Authentication token required to access Secret Manager"); } try { const url = `https://secretmanager.googleapis.com/v1/${resourceName}:access`; const response = await axios_1.default.get(url, { headers: { 'Authorization': `Bearer ${this.credentials.token}`, 'Content-Type': 'application/json' } }); // The actual response format would depend on the Secret Manager API // This is a simplified implementation return response.data.payload?.data || ''; } catch (e) { throw new Error(`Failed to access secret: ${e}`); } } } exports.SecretManagerClient = SecretManagerClient;