| | | 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 type { ILogger } from '../../core/domain/ports/ILogger.js'; |
| | | 10 | | import type { ISecretProvider } from '../../core/domain/ports/ISecretProvider.js'; |
| | | 11 | | import type { IVariableStore } from '../../core/domain/ports/IVariableStore.js'; |
| | | 12 | | import { createAwsSecretProvider } from '../../core/infrastructure/aws/AwsSecretProviderFactory.js'; |
| | | 13 | | import { |
| | | 14 | | type AzureProviderOptions, |
| | | 15 | | createAzureSecretProvider, |
| | | 16 | | } from '../../core/infrastructure/azure/AzureSecretProviderFactory.js'; |
| | | 17 | | import { ConsoleLogger } from '../../core/infrastructure/logger/ConsoleLogger.js'; |
| | | 18 | | import { FileVariableStore } from '../../core/infrastructure/variableStore/FileVariableStore.js'; |
| | | 19 | | import { TYPES } from '../../core/types.js'; |
| | | 20 | | |
| | | 21 | | export type InfrastructureOptions = AzureProviderOptions; |
| | | 22 | | |
| | | 23 | | type ProviderFactory = ( |
| | | 24 | | config: MapFileConfig, |
| | | 25 | | options: InfrastructureOptions, |
| | | 26 | | logger: ILogger, |
| | | 27 | | ) => ISecretProvider; |
| | | 28 | | |
| | 7 | 29 | | const providerFactories: Record<string, ProviderFactory> = { |
| | 24 | 30 | | aws: (config, _options, logger) => createAwsSecretProvider(config, logger), |
| | 17 | 31 | | azure: (config, options) => createAzureSecretProvider(config, options), |
| | | 32 | | }; |
| | | 33 | | |
| | | 34 | | export function configureInfrastructureServices( |
| | | 35 | | container: Container, |
| | | 36 | | config: MapFileConfig = {}, |
| | | 37 | | options: InfrastructureOptions = {}, |
| | | 38 | | ): void { |
| | 44 | 39 | | if (!container.isBound(TYPES.ILogger)) { |
| | 39 | 40 | | container.bind<ILogger>(TYPES.ILogger).to(ConsoleLogger).inSingletonScope(); |
| | | 41 | | } |
| | | 42 | | |
| | 44 | 43 | | const logger = container.get<ILogger>(TYPES.ILogger); |
| | | 44 | | |
| | 44 | 45 | | if (!container.isBound(TYPES.IVariableStore)) { |
| | 43 | 46 | | container |
| | | 47 | | .bind<IVariableStore>(TYPES.IVariableStore) |
| | | 48 | | .to(FileVariableStore) |
| | | 49 | | .inSingletonScope(); |
| | | 50 | | } |
| | | 51 | | |
| | 44 | 52 | | const selectedProvider = config.provider?.toLowerCase() || 'aws'; |
| | | 53 | | |
| | 44 | 54 | | if (config.profile && selectedProvider !== 'aws') { |
| | 1 | 55 | | logger.warn( |
| | | 56 | | `--profile is only supported with the aws provider` + |
| | | 57 | | ` and will be ignored` + |
| | | 58 | | ` (current provider: ${selectedProvider}).`, |
| | | 59 | | ); |
| | | 60 | | } |
| | | 61 | | |
| | 44 | 62 | | const factory = providerFactories[selectedProvider]; |
| | 44 | 63 | | if (!factory) { |
| | 3 | 64 | | throw new InvalidArgumentError( |
| | | 65 | | `Unsupported provider: ${config.provider}.` + |
| | | 66 | | ` Supported providers:` + |
| | | 67 | | ` ${Object.keys(providerFactories).join(', ')}`, |
| | | 68 | | ); |
| | | 69 | | } |
| | 41 | 70 | | const secretProvider = factory(config, options, logger); |
| | | 71 | | |
| | 41 | 72 | | container |
| | | 73 | | .bind<ISecretProvider>(TYPES.ISecretProvider) |
| | | 74 | | .toConstantValue(secretProvider); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | export function configureApplicationServices(container: Container): void { |
| | 40 | 78 | | container |
| | | 79 | | .bind<PullSecretsToEnvCommandHandler>(TYPES.PullSecretsToEnvCommandHandler) |
| | | 80 | | .to(PullSecretsToEnvCommandHandler) |
| | | 81 | | .inTransientScope(); |
| | | 82 | | |
| | 40 | 83 | | container |
| | | 84 | | .bind<PushEnvToSecretsCommandHandler>(TYPES.PushEnvToSecretsCommandHandler) |
| | | 85 | | .to(PushEnvToSecretsCommandHandler) |
| | | 86 | | .inTransientScope(); |
| | | 87 | | |
| | 40 | 88 | | container |
| | | 89 | | .bind<PushSingleCommandHandler>(TYPES.PushSingleCommandHandler) |
| | | 90 | | .to(PushSingleCommandHandler) |
| | | 91 | | .inTransientScope(); |
| | | 92 | | |
| | 40 | 93 | | container |
| | | 94 | | .bind<DispatchActionCommandHandler>(TYPES.DispatchActionCommandHandler) |
| | | 95 | | .to(DispatchActionCommandHandler) |
| | | 96 | | .inTransientScope(); |
| | | 97 | | } |