| | | 1 | | import { DefaultAzureCredential } from '@azure/identity'; |
| | | 2 | | import { SecretClient } from '@azure/keyvault-secrets'; |
| | | 3 | | import { DependencyMissingError } from '../../domain/errors/DomainErrors.js'; |
| | | 4 | | import type { MapFileConfig } from '../../domain/MapFileConfig.js'; |
| | | 5 | | import type { ISecretProvider } from '../../domain/ports/ISecretProvider.js'; |
| | | 6 | | import { AzureKeyVaultSecretProvider } from './AzureKeyVaultSecretProvider.js'; |
| | | 7 | | import { |
| | | 8 | | DEFAULT_VAULT_HOSTS, |
| | | 9 | | validateAzureVaultUrl, |
| | | 10 | | } from './AzureVaultUrlValidator.js'; |
| | | 11 | | |
| | | 12 | | export { DEFAULT_VAULT_HOSTS } from './AzureVaultUrlValidator.js'; |
| | | 13 | | |
| | | 14 | | export type AzureProviderOptions = { |
| | | 15 | | allowedVaultHosts?: string[]; |
| | | 16 | | disableChallengeResourceVerification?: boolean; |
| | | 17 | | }; |
| | | 18 | | |
| | | 19 | | export function createAzureSecretProvider( |
| | | 20 | | config: MapFileConfig, |
| | | 21 | | options?: AzureProviderOptions, |
| | | 22 | | ): ISecretProvider { |
| | 19 | 23 | | const { vaultUrl } = config; |
| | 19 | 24 | | if (!vaultUrl) { |
| | 2 | 25 | | throw new DependencyMissingError( |
| | | 26 | | 'vaultUrl is required when using Azure provider.' + |
| | | 27 | | ' Set it in $config.vaultUrl in your map file' + |
| | | 28 | | ' or via --vault-url flag.', |
| | | 29 | | ); |
| | | 30 | | } |
| | 17 | 31 | | const allowedVaultHosts = options?.allowedVaultHosts ?? DEFAULT_VAULT_HOSTS; |
| | | 32 | | const disableChallengeResourceVerification = |
| | 19 | 33 | | options?.disableChallengeResourceVerification ?? false; |
| | 19 | 34 | | validateAzureVaultUrl(vaultUrl, allowedVaultHosts); |
| | 19 | 35 | | const credential = new DefaultAzureCredential(); |
| | 19 | 36 | | const client = new SecretClient(vaultUrl, credential, { |
| | | 37 | | disableChallengeResourceVerification, |
| | | 38 | | }); |
| | 19 | 39 | | return new AzureKeyVaultSecretProvider(client); |
| | | 40 | | } |