| | | 1 | | import { inject, injectable } from 'inversify'; |
| | | 2 | | import pc from 'picocolors'; |
| | | 3 | | import { SecretsFetchError } from '../../domain/errors/DomainErrors.js'; |
| | | 4 | | import type { ILogger } from '../../domain/ports/ILogger.js'; |
| | | 5 | | import type { ISecretMasker } from '../../domain/ports/ISecretMasker.js'; |
| | | 6 | | import type { ISecretProvider } from '../../domain/ports/ISecretProvider.js'; |
| | | 7 | | import type { IVariableStore } from '../../domain/ports/IVariableStore.js'; |
| | | 8 | | import { TYPES } from '../../types.js'; |
| | | 9 | | import { SecretResolver } from '../secretResolution/SecretResolver.js'; |
| | | 10 | | import type { PullSecretsToEnvCommand } from './PullSecretsToEnvCommand.js'; |
| | | 11 | | |
| | | 12 | | @injectable() |
| | 12 | 13 | | export class PullSecretsToEnvCommandHandler { |
| | | 14 | | private readonly secretResolver: SecretResolver; |
| | | 15 | | |
| | | 16 | | constructor( |
| | | 17 | | @inject(TYPES.ISecretProvider) |
| | | 18 | | secretProvider: ISecretProvider, |
| | | 19 | | @inject(TYPES.IVariableStore) |
| | 93 | 20 | | private readonly variableStore: IVariableStore, |
| | 93 | 21 | | @inject(TYPES.ILogger) private readonly logger: ILogger, |
| | | 22 | | @inject(TYPES.ISecretMasker) |
| | | 23 | | secretMasker: ISecretMasker, |
| | | 24 | | ) { |
| | 93 | 25 | | this.secretResolver = new SecretResolver( |
| | | 26 | | secretProvider, |
| | | 27 | | logger, |
| | | 28 | | secretMasker, |
| | | 29 | | ); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /** |
| | | 33 | | * Handles the PullSecretsToEnvCommand which orchestrates the process of fetching |
| | | 34 | | * environment variable values from a secret store and writing them to a local environment file. |
| | | 35 | | * |
| | | 36 | | * @param command - The PullSecretsToEnvCommand containing mapPath and envFilePath |
| | | 37 | | */ |
| | | 38 | | async handle(command: PullSecretsToEnvCommand): Promise<void> { |
| | | 39 | | const { requestVariables, currentVariables } = |
| | 17 | 40 | | await this.loadVariables(command); |
| | 16 | 41 | | const { variables, resolvedCount, totalCount } = await this.envild( |
| | | 42 | | requestVariables, |
| | | 43 | | currentVariables, |
| | | 44 | | ); |
| | 11 | 45 | | await this.saveEnvFile(command.envFilePath, variables); |
| | | 46 | | |
| | 11 | 47 | | this.logger.info( |
| | | 48 | | PullSecretsToEnvCommandHandler.buildSummary( |
| | | 49 | | resolvedCount, |
| | | 50 | | totalCount, |
| | | 51 | | command.envFilePath, |
| | | 52 | | ), |
| | | 53 | | ); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | private async loadVariables(command: PullSecretsToEnvCommand): Promise<{ |
| | | 57 | | requestVariables: Record<string, string>; |
| | | 58 | | currentVariables: Record<string, string>; |
| | | 59 | | }> { |
| | 17 | 60 | | const requestVariables = await this.variableStore.getMapping( |
| | | 61 | | command.mapPath, |
| | | 62 | | ); |
| | 16 | 63 | | const currentVariables = await this.variableStore.getEnvironment( |
| | | 64 | | command.envFilePath, |
| | | 65 | | ); |
| | | 66 | | |
| | 16 | 67 | | return { requestVariables, currentVariables }; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | private async saveEnvFile( |
| | | 71 | | envFilePath: string, |
| | | 72 | | variables: Record<string, string>, |
| | | 73 | | ): Promise<void> { |
| | 11 | 74 | | await this.variableStore.saveEnvironment(envFilePath, variables); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | private async envild( |
| | | 78 | | paramMap: Record<string, string>, |
| | | 79 | | existingEnvVariables: Record<string, string>, |
| | | 80 | | ): Promise<{ |
| | | 81 | | variables: Record<string, string>; |
| | | 82 | | resolvedCount: number; |
| | | 83 | | totalCount: number; |
| | | 84 | | }> { |
| | | 85 | | const { resolved, warnings, errors } = |
| | 16 | 86 | | await this.secretResolver.resolve(paramMap); |
| | | 87 | | |
| | 14 | 88 | | for (const outcome of resolved) { |
| | 8 | 89 | | existingEnvVariables[outcome.envVar] = outcome.value; |
| | | 90 | | } |
| | | 91 | | |
| | 14 | 92 | | this.secretResolver.logSecretsSection(resolved, warnings); |
| | | 93 | | |
| | 14 | 94 | | if (errors.length > 0) { |
| | 3 | 95 | | throw new SecretsFetchError(errors.map(SecretResolver.toFailure)); |
| | | 96 | | } |
| | | 97 | | |
| | 11 | 98 | | return { |
| | | 99 | | variables: existingEnvVariables, |
| | | 100 | | resolvedCount: resolved.length, |
| | | 101 | | totalCount: Object.keys(paramMap).length, |
| | | 102 | | }; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | private static buildSummary( |
| | | 106 | | resolvedCount: number, |
| | | 107 | | totalCount: number, |
| | | 108 | | envFilePath: string, |
| | | 109 | | ): string { |
| | 11 | 110 | | return `\n${pc.bold(pc.green('\u2B50 LEVEL CLEARED'))}${pc.dim( |
| | | 111 | | ` \u2014 ${resolvedCount}/${totalCount} secrets loaded \u00B7 `, |
| | | 112 | | )}${pc.bold(envFilePath)}${pc.dim(' written')}\n`; |
| | | 113 | | } |
| | | 114 | | } |