|
| 1 | +import { Logger } from '@aws-lambda-powertools/logger'; |
| 2 | +import { BaseProvider } from '@aws-lambda-powertools/parameters/base'; |
| 3 | +import Vault from 'hashi-vault-js'; |
| 4 | +import { isErrorResponse } from './customProviderVaultTypes'; |
| 5 | +import type { |
| 6 | + HashiCorpVaultProviderOptions, |
| 7 | + HashiCorpVaultGetOptions, |
| 8 | +} from './customProviderVaultTypes'; |
| 9 | + |
| 10 | +class HashiCorpVaultProvider extends BaseProvider { |
| 11 | + public client: Vault; |
| 12 | + readonly #token: string; |
| 13 | + readonly #logger: Logger; |
| 14 | + |
| 15 | + /** |
| 16 | + * It initializes the HashiCorpVaultProvider class. |
| 17 | + * |
| 18 | + * @param {HashiCorpVaultProviderOptions} config - The configuration object. |
| 19 | + */ |
| 20 | + public constructor(config: HashiCorpVaultProviderOptions) { |
| 21 | + super(); |
| 22 | + |
| 23 | + const { url, token, clientConfig, vaultClient } = config; |
| 24 | + if (vaultClient) { |
| 25 | + if (vaultClient instanceof Vault) { |
| 26 | + this.client = vaultClient; |
| 27 | + } else { |
| 28 | + throw Error('Not a valid Vault client provided'); |
| 29 | + } |
| 30 | + } else { |
| 31 | + const config: Vault.VaultConfig = { |
| 32 | + baseUrl: url, |
| 33 | + ...(clientConfig ?? { |
| 34 | + timeout: 10000, |
| 35 | + rootPath: '', |
| 36 | + }), |
| 37 | + }; |
| 38 | + this.client = new Vault(config); |
| 39 | + } |
| 40 | + this.#token = token; |
| 41 | + this.#logger = new Logger({ |
| 42 | + serviceName: 'HashiCorpVaultProvider', |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Retrieve a secret from HashiCorp Vault. |
| 48 | + * |
| 49 | + * You can customize the retrieval of the secret by passing options to the function: |
| 50 | + * * `maxAge` - The maximum age of the value in cache before fetching a new one (in seconds) (default: 5) |
| 51 | + * * `forceFetch` - Whether to always fetch a new value from the store regardless if already available in cache |
| 52 | + * * `sdkOptions` - Extra options to pass to the HashiCorp Vault SDK, e.g. `mount` or `version` |
| 53 | + * |
| 54 | + * @param {string} name - The name of the secret |
| 55 | + * @param {HashiCorpVaultGetOptions} options - Options to customize the retrieval of the secret |
| 56 | + */ |
| 57 | + public async get( |
| 58 | + name: string, |
| 59 | + options?: HashiCorpVaultGetOptions |
| 60 | + ): Promise<Record<string, unknown> | undefined> { |
| 61 | + return super.get(name, options) as Promise< |
| 62 | + Record<string, unknown> | undefined |
| 63 | + >; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Retrieving multiple parameter values is not supported with HashiCorp Vault. |
| 68 | + */ |
| 69 | + public async getMultiple(path: string, _options?: unknown): Promise<void> { |
| 70 | + await super.getMultiple(path); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Retrieve a secret from HashiCorp Vault. |
| 75 | + * |
| 76 | + * @param {string} name - The name of the secret |
| 77 | + * @param {HashiCorpVaultGetOptions} options - Options to customize the retrieval of the secret |
| 78 | + */ |
| 79 | + protected async _get( |
| 80 | + name: string, |
| 81 | + options?: HashiCorpVaultGetOptions |
| 82 | + ): Promise<Record<string, unknown>> { |
| 83 | + const mount = options?.sdkOptions?.mount ?? 'secret'; |
| 84 | + const version = options?.sdkOptions?.version; |
| 85 | + |
| 86 | + const response = await this.client.readKVSecret( |
| 87 | + this.#token, |
| 88 | + name, |
| 89 | + version, |
| 90 | + mount |
| 91 | + ); |
| 92 | + |
| 93 | + if (isErrorResponse(response)) { |
| 94 | + this.#logger.error('An error occurred', { |
| 95 | + error: response.vaultHelpMessage, |
| 96 | + }); |
| 97 | + throw response; |
| 98 | + } else { |
| 99 | + return response.data; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Retrieving multiple parameter values from HashiCorp Vault is not supported. |
| 105 | + * |
| 106 | + * @throws Not Implemented Error. |
| 107 | + */ |
| 108 | + protected async _getMultiple( |
| 109 | + _path: string, |
| 110 | + _options?: unknown |
| 111 | + ): Promise<void> { |
| 112 | + throw new Error('Method not implemented.'); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export { HashiCorpVaultProvider }; |
0 commit comments