| | | 1 | | import type { Container } from 'inversify'; |
| | | 2 | | |
| | | 3 | | import { DispatchActionCommandHandler } from '../../core/application/dispatch/DispatchActionCommandHandler.js'; |
| | | 4 | | import { PullSecretsToEnvCommandHandler } from '../../core/application/pullSecretsToEnv/PullSecretsToEnvCommandHandler.j |
| | | 5 | | import { PushEnvToSecretsCommandHandler } from '../../core/application/pushEnvToSecrets/PushEnvToSecretsCommandHandler.j |
| | | 6 | | import { PushSingleCommandHandler } from '../../core/application/pushSingle/PushSingleCommandHandler.js'; |
| | | 7 | | import { InvalidArgumentError } from '../../core/domain/errors/DomainErrors.js'; |
| | | 8 | | import type { MapFileConfig } from '../../core/domain/MapFileConfig.js'; |
| | | 9 | | import { |
| | | 10 | | DEFAULT_PROVIDER, |
| | | 11 | | unsupportedProviderMessage, |
| | | 12 | | } from '../../core/domain/ProviderConfiguration.js'; |
| | | 13 | | import type { ILogger } from '../../core/domain/ports/ILogger.js'; |
| | | 14 | | import type { ISecretMasker } from '../../core/domain/ports/ISecretMasker.js'; |
| | | 15 | | import type { ISecretProvider } from '../../core/domain/ports/ISecretProvider.js'; |
| | | 16 | | import type { IVariableStore } from '../../core/domain/ports/IVariableStore.js'; |
| | | 17 | | import { createAwsSecretProvider } from '../../core/infrastructure/aws/AwsSecretProviderFactory.js'; |
| | | 18 | | import { createAzureSecretProvider } from '../../core/infrastructure/azure/AzureSecretProviderFactory.js'; |
| | | 19 | | import { NoOpSecretMasker } from '../../core/infrastructure/github/NoOpSecretMasker.js'; |
| | | 20 | | import { ConsoleLogger } from '../../core/infrastructure/logger/ConsoleLogger.js'; |
| | | 21 | | import { FileVariableStore } from '../../core/infrastructure/variableStore/FileVariableStore.js'; |
| | | 22 | | import { TYPES } from '../../core/types.js'; |
| | | 23 | | |
| | | 24 | | type ProviderFactory = ( |
| | | 25 | | config: MapFileConfig, |
| | | 26 | | logger: ILogger, |
| | | 27 | | ) => ISecretProvider; |
| | | 28 | | |
| | 11 | 29 | | const providerFactories: Record<string, ProviderFactory> = { |
| | 84 | 30 | | aws: (config, logger) => createAwsSecretProvider(config, logger), |
| | 24 | 31 | | azure: (config) => createAzureSecretProvider(config), |
| | | 32 | | }; |
| | | 33 | | |
| | | 34 | | /** The providers the CLI and the GitHub Action can resolve secrets from. */ |
| | | 35 | | export const SUPPORTED_PROVIDERS: readonly string[] = |
| | 11 | 36 | | Object.keys(providerFactories); |
| | | 37 | | |
| | | 38 | | export function configureInfrastructureServices( |
| | | 39 | | container: Container, |
| | | 40 | | config: MapFileConfig = {}, |
| | | 41 | | ): void { |
| | 111 | 42 | | if (!container.isBound(TYPES.ILogger)) { |
| | 53 | 43 | | container.bind<ILogger>(TYPES.ILogger).to(ConsoleLogger).inSingletonScope(); |
| | | 44 | | } |
| | | 45 | | |
| | 111 | 46 | | const logger = container.get<ILogger>(TYPES.ILogger); |
| | | 47 | | |
| | 111 | 48 | | if (!container.isBound(TYPES.IVariableStore)) { |
| | 110 | 49 | | container |
| | | 50 | | .bind<IVariableStore>(TYPES.IVariableStore) |
| | | 51 | | .to(FileVariableStore) |
| | | 52 | | .inSingletonScope(); |
| | | 53 | | } |
| | | 54 | | |
| | 111 | 55 | | if (!container.isBound(TYPES.ISecretMasker)) { |
| | 57 | 56 | | container |
| | | 57 | | .bind<ISecretMasker>(TYPES.ISecretMasker) |
| | | 58 | | .to(NoOpSecretMasker) |
| | | 59 | | .inSingletonScope(); |
| | | 60 | | } |
| | | 61 | | |
| | 111 | 62 | | const selectedProvider = config.provider?.toLowerCase() || DEFAULT_PROVIDER; |
| | | 63 | | |
| | 111 | 64 | | if (config.profile && selectedProvider !== 'aws') { |
| | 1 | 65 | | logger.warn( |
| | | 66 | | `--profile is only supported with the aws provider` + |
| | | 67 | | ` and will be ignored` + |
| | | 68 | | ` (current provider: ${selectedProvider}).`, |
| | | 69 | | ); |
| | | 70 | | } |
| | | 71 | | |
| | 111 | 72 | | const factory = providerFactories[selectedProvider]; |
| | 111 | 73 | | if (!factory) { |
| | 3 | 74 | | throw new InvalidArgumentError( |
| | | 75 | | unsupportedProviderMessage(`${config.provider}`, SUPPORTED_PROVIDERS), |
| | | 76 | | ); |
| | | 77 | | } |
| | 108 | 78 | | const secretProvider = factory(config, logger); |
| | | 79 | | |
| | 108 | 80 | | container |
| | | 81 | | .bind<ISecretProvider>(TYPES.ISecretProvider) |
| | | 82 | | .toConstantValue(secretProvider); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | export function configureApplicationServices(container: Container): void { |
| | 94 | 86 | | container |
| | | 87 | | .bind<PullSecretsToEnvCommandHandler>(TYPES.PullSecretsToEnvCommandHandler) |
| | | 88 | | .to(PullSecretsToEnvCommandHandler) |
| | | 89 | | .inTransientScope(); |
| | | 90 | | |
| | 94 | 91 | | container |
| | | 92 | | .bind<PushEnvToSecretsCommandHandler>(TYPES.PushEnvToSecretsCommandHandler) |
| | | 93 | | .to(PushEnvToSecretsCommandHandler) |
| | | 94 | | .inTransientScope(); |
| | | 95 | | |
| | 94 | 96 | | container |
| | | 97 | | .bind<PushSingleCommandHandler>(TYPES.PushSingleCommandHandler) |
| | | 98 | | .to(PushSingleCommandHandler) |
| | | 99 | | .inTransientScope(); |
| | | 100 | | |
| | 94 | 101 | | container |
| | | 102 | | .bind<DispatchActionCommandHandler>(TYPES.DispatchActionCommandHandler) |
| | | 103 | | .to(DispatchActionCommandHandler) |
| | | 104 | | .inTransientScope(); |
| | | 105 | | } |