| | | 1 | | import { SSMClient } from '@aws-sdk/client-ssm'; |
| | | 2 | | import { fromIni } from '@aws-sdk/credential-providers'; |
| | | 3 | | import { DefaultAzureCredential } from '@azure/identity'; |
| | | 4 | | import { SecretClient } from '@azure/keyvault-secrets'; |
| | | 5 | | import type { EnvilderOptions } from '../domain/envilder-options.js'; |
| | | 6 | | import type { MapFileConfig } from '../domain/map-file-config.js'; |
| | | 7 | | import type { ISecretProvider } from '../domain/ports/secret-provider.js'; |
| | | 8 | | import { SecretProviderType } from '../domain/secret-provider-type.js'; |
| | | 9 | | import { AwsSsmSecretProvider } from './aws/aws-ssm-secret-provider.js'; |
| | | 10 | | import { AzureKeyVaultSecretProvider } from './azure/azure-key-vault-secret-provider.js'; |
| | | 11 | | |
| | | 12 | | export function createSecretProvider( |
| | | 13 | | config: MapFileConfig, |
| | | 14 | | options?: EnvilderOptions, |
| | | 15 | | ): ISecretProvider { |
| | 9 | 16 | | const provider = options?.provider ?? config.provider; |
| | 9 | 17 | | const profile = normalize(options?.profile ?? config.profile); |
| | 9 | 18 | | const vaultUrl = normalize(options?.vaultUrl ?? config.vaultUrl); |
| | 9 | 19 | | const isAzure = provider === SecretProviderType.Azure; |
| | | 20 | | |
| | 9 | 21 | | if (isAzure && profile) { |
| | 1 | 22 | | throw new Error('AWS profile cannot be used with Azure Key Vault provider'); |
| | | 23 | | } |
| | | 24 | | |
| | 8 | 25 | | if (!isAzure && vaultUrl) { |
| | 1 | 26 | | throw new Error('Vault URL cannot be used with AWS SSM provider'); |
| | | 27 | | } |
| | | 28 | | |
| | 7 | 29 | | if (isAzure) { |
| | 3 | 30 | | return createAzureProvider(vaultUrl); |
| | | 31 | | } |
| | | 32 | | |
| | 4 | 33 | | return createAwsProvider(profile); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | function createAzureProvider( |
| | | 37 | | vaultUrl: string | undefined, |
| | | 38 | | ): AzureKeyVaultSecretProvider { |
| | 3 | 39 | | if (!vaultUrl?.trim()) { |
| | 1 | 40 | | throw new Error('Vault URL must be provided for Azure Key Vault provider'); |
| | | 41 | | } |
| | | 42 | | |
| | 2 | 43 | | const credential = new DefaultAzureCredential(); |
| | 2 | 44 | | const client = new SecretClient(vaultUrl, credential); |
| | 2 | 45 | | return new AzureKeyVaultSecretProvider(client); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | function createAwsProvider(profile: string | undefined): AwsSsmSecretProvider { |
| | 4 | 49 | | const clientOptions = profile ? { credentials: fromIni({ profile }) } : {}; |
| | 4 | 50 | | const client = new SSMClient(clientOptions); |
| | 4 | 51 | | return new AwsSsmSecretProvider(client); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | function normalize(value: string | undefined): string | undefined { |
| | 18 | 55 | | const trimmed = value?.trim(); |
| | 18 | 56 | | return trimmed || undefined; |
| | | 57 | | } |