< 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: 299_25910610327
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 57
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
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 { fromIni } from '@aws-sdk/credential-providers';
 3import { DefaultAzureCredential } from '@azure/identity';
 4import { SecretClient } from '@azure/keyvault-secrets';
 5import type { EnvilderOptions } from '../domain/envilder-options.js';
 6import type { MapFileConfig } from '../domain/map-file-config.js';
 7import type { ISecretProvider } from '../domain/ports/secret-provider.js';
 8import { SecretProviderType } from '../domain/secret-provider-type.js';
 9import { AwsSsmSecretProvider } from './aws/aws-ssm-secret-provider.js';
 10import { AzureKeyVaultSecretProvider } from './azure/azure-key-vault-secret-provider.js';
 11
 12export function createSecretProvider(
 13  config: MapFileConfig,
 14  options?: EnvilderOptions,
 15): ISecretProvider {
 916  const provider = options?.provider ?? config.provider;
 917  const profile = normalize(options?.profile ?? config.profile);
 918  const vaultUrl = normalize(options?.vaultUrl ?? config.vaultUrl);
 919  const isAzure = provider === SecretProviderType.Azure;
 20
 921  if (isAzure && profile) {
 122    throw new Error('AWS profile cannot be used with Azure Key Vault provider');
 23  }
 24
 825  if (!isAzure && vaultUrl) {
 126    throw new Error('Vault URL cannot be used with AWS SSM provider');
 27  }
 28
 729  if (isAzure) {
 330    return createAzureProvider(vaultUrl);
 31  }
 32
 433  return createAwsProvider(profile);
 34}
 35
 36function createAzureProvider(
 37  vaultUrl: string | undefined,
 38): AzureKeyVaultSecretProvider {
 339  if (!vaultUrl?.trim()) {
 140    throw new Error('Vault URL must be provided for Azure Key Vault provider');
 41  }
 42
 243  const credential = new DefaultAzureCredential();
 244  const client = new SecretClient(vaultUrl, credential);
 245  return new AzureKeyVaultSecretProvider(client);
 46}
 47
 48function createAwsProvider(profile: string | undefined): AwsSsmSecretProvider {
 449  const clientOptions = profile ? { credentials: fromIni({ profile }) } : {};
 450  const client = new SSMClient(clientOptions);
 451  return new AwsSsmSecretProvider(client);
 52}
 53
 54function normalize(value: string | undefined): string | undefined {
 1855  const trimmed = value?.trim();
 1856  return trimmed || undefined;
 57}