< Summary - Envilder Node.js SDK

Information
Class: src/sdks/nodejs/src/infrastructure/secret-provider-factory.ts
Assembly: Default
File(s): src/sdks/nodejs/src/infrastructure/secret-provider-factory.ts
Tag: 427_29134414720
Line coverage
100%
Covered lines: 28
Uncovered lines: 0
Coverable lines: 28
Total lines: 72
Line coverage: 100%
Branch coverage
96%
Covered branches: 27
Total branches: 28
Branch coverage: 96.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/sdks/nodejs/src/infrastructure/secret-provider-factory.ts

#LineLine coverage
 1import { SSMClient } from '@aws-sdk/client-ssm';
 2import { DefaultAzureCredential } from '@azure/identity';
 3import { SecretClient } from '@azure/keyvault-secrets';
 4import type { EnvilderOptions } from '../domain/envilder-options.js';
 5import type { MapFileConfig } from '../domain/map-file-config.js';
 6import type { ISecretProvider } from '../domain/ports/secret-provider.js';
 7import { SecretProviderType } from '../domain/secret-provider-type.js';
 8import { AwsSsmSecretProvider } from './aws/aws-ssm-secret-provider.js';
 9import { AzureKeyVaultSecretProvider } from './azure/azure-key-vault-secret-provider.js';
 10
 11export function createSecretProvider(
 12  config: MapFileConfig,
 13  options?: EnvilderOptions,
 14): ISecretProvider {
 1015  const provider = options?.provider ?? config.provider;
 1016  const profile = normalize(options?.profile ?? config.profile);
 1017  const vaultUrl = normalize(options?.vaultUrl ?? config.vaultUrl);
 1018  const isAzure = provider === SecretProviderType.Azure;
 19
 1020  if (isAzure && profile) {
 121    throw new Error('AWS profile cannot be used with Azure Key Vault provider');
 22  }
 23
 924  if (!isAzure && vaultUrl) {
 125    throw new Error('Vault URL cannot be used with AWS SSM provider');
 26  }
 27
 828  if (isAzure) {
 329    return createAzureProvider(vaultUrl);
 30  }
 31
 532  return createAwsProvider(profile);
 33}
 34
 35function createAzureProvider(
 36  vaultUrl: string | undefined,
 37): AzureKeyVaultSecretProvider {
 338  if (!vaultUrl?.trim()) {
 139    throw new Error('Vault URL must be provided for Azure Key Vault provider');
 40  }
 41
 242  const credential = new DefaultAzureCredential();
 243  const client = new SecretClient(vaultUrl, credential);
 244  return new AzureKeyVaultSecretProvider(client);
 45}
 46
 47export async function resolveRegionWithFallback(
 48  profile?: string,
 49): Promise<string> {
 950  if (process.env.AWS_REGION) {
 651    return process.env.AWS_REGION;
 52  }
 353  if (process.env.AWS_DEFAULT_REGION) {
 154    return process.env.AWS_DEFAULT_REGION;
 55  }
 256  try {
 257    return await new SSMClient(profile ? { profile } : {}).config.region();
 58  } catch {
 159    return 'us-east-1';
 60  }
 61}
 62
 63function createAwsProvider(profile: string | undefined): AwsSsmSecretProvider {
 564  const region = () => resolveRegionWithFallback(profile);
 565  const client = new SSMClient(profile ? { profile, region } : { region });
 566  return new AwsSsmSecretProvider(client, profile);
 67}
 68
 69function normalize(value: string | undefined): string | undefined {
 2070  const trimmed = value?.trim();
 2071  return trimmed || undefined;
 72}