@enspirit/emb
Version:
A replacement for our Makefile-for-monorepos
39 lines (38 loc) • 1.2 kB
JavaScript
/**
* Abstract base class for secret providers.
* Implementations should handle specific secret management systems
* (e.g., HashiCorp Vault, 1Password).
*/
export class AbstractSecretProvider {
config;
cache = new Map();
constructor(config) {
this.config = config;
}
/**
* Get a secret value, using cache if available.
* @param ref Reference to the secret
* @returns The secret value (entire record if no key specified, or specific field value)
*/
async get(ref) {
const cacheKey = `${ref.path}:${ref.version || 'latest'}`;
if (!this.cache.has(cacheKey)) {
this.cache.set(cacheKey, await this.fetchSecret(ref));
}
const cached = this.cache.get(cacheKey);
if (ref.key) {
if (!(ref.key in cached)) {
const availableKeys = Object.keys(cached).join(', ') || 'none';
throw new Error(`Key '${ref.key}' not found in secret '${ref.path}'. Available keys: ${availableKeys}`);
}
return cached[ref.key];
}
return cached;
}
/**
* Clear the cache.
*/
clearCache() {
this.cache.clear();
}
}