| | | 1 | | import { inject, injectable } from 'inversify'; |
| | | 2 | | import { EnvironmentVariable } from '../../domain/EnvironmentVariable.js'; |
| | | 3 | | import type { ILogger } from '../../domain/ports/ILogger.js'; |
| | | 4 | | import type { ISecretProvider } from '../../domain/ports/ISecretProvider.js'; |
| | | 5 | | import type { IVariableStore } from '../../domain/ports/IVariableStore.js'; |
| | | 6 | | import { TYPES } from '../../types.js'; |
| | | 7 | | import type { PullSecretsToEnvCommand } from './PullSecretsToEnvCommand.js'; |
| | | 8 | | |
| | | 9 | | @injectable() |
| | 8 | 10 | | export class PullSecretsToEnvCommandHandler { |
| | 8 | 11 | | private static readonly ERROR_MESSAGES = { |
| | | 12 | | FETCH_FAILED: 'Failed to generate environment file: ', |
| | | 13 | | PARAM_NOT_FOUND: 'Some secrets could not be fetched:\n', |
| | | 14 | | NO_VALUE_FOUND: 'Warning: No value found for: ', |
| | | 15 | | ERROR_FETCHING: 'Error fetching secret: ', |
| | | 16 | | }; |
| | | 17 | | |
| | 8 | 18 | | private static readonly SUCCESS_MESSAGES = { |
| | | 19 | | ENV_GENERATED: 'Environment File generated at ', |
| | | 20 | | }; |
| | | 21 | | |
| | | 22 | | constructor( |
| | | 23 | | @inject(TYPES.ISecretProvider) |
| | 23 | 24 | | private readonly secretProvider: ISecretProvider, |
| | | 25 | | @inject(TYPES.IVariableStore) |
| | 23 | 26 | | private readonly variableStore: IVariableStore, |
| | 23 | 27 | | @inject(TYPES.ILogger) private readonly logger: ILogger, |
| | | 28 | | ) {} |
| | | 29 | | |
| | | 30 | | /** |
| | | 31 | | * Handles the PullSecretsToEnvCommand which orchestrates the process of fetching |
| | | 32 | | * environment variable values from a secret store and writing them to a local environment file. |
| | | 33 | | * |
| | | 34 | | * @param command - The PullSecretsToEnvCommand containing mapPath and envFilePath |
| | | 35 | | */ |
| | | 36 | | async handle(command: PullSecretsToEnvCommand): Promise<void> { |
| | 7 | 37 | | try { |
| | | 38 | | const { requestVariables, currentVariables } = |
| | 7 | 39 | | await this.loadVariables(command); |
| | 7 | 40 | | const envilded = await this.envild(requestVariables, currentVariables); |
| | 6 | 41 | | await this.saveEnvFile(command.envFilePath, envilded); |
| | | 42 | | |
| | 6 | 43 | | this.logger.info( |
| | | 44 | | `${PullSecretsToEnvCommandHandler.SUCCESS_MESSAGES.ENV_GENERATED}'${command.envFilePath}'`, |
| | | 45 | | ); |
| | | 46 | | } catch (_error) { |
| | | 47 | | const errorMessage = |
| | 1 | 48 | | _error instanceof Error ? _error.message : String(_error); |
| | 1 | 49 | | this.logger.error( |
| | | 50 | | `${PullSecretsToEnvCommandHandler.ERROR_MESSAGES.FETCH_FAILED}${errorMessage}`, |
| | | 51 | | ); |
| | 1 | 52 | | throw _error; |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | private async loadVariables(command: PullSecretsToEnvCommand): Promise<{ |
| | | 57 | | requestVariables: Record<string, string>; |
| | | 58 | | currentVariables: Record<string, string>; |
| | | 59 | | }> { |
| | 7 | 60 | | const requestVariables = await this.variableStore.getMapping( |
| | | 61 | | command.mapPath, |
| | | 62 | | ); |
| | 7 | 63 | | const currentVariables = await this.variableStore.getEnvironment( |
| | | 64 | | command.envFilePath, |
| | | 65 | | ); |
| | | 66 | | |
| | 7 | 67 | | return { requestVariables, currentVariables }; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | private async saveEnvFile( |
| | | 71 | | envFilePath: string, |
| | | 72 | | variables: Record<string, string>, |
| | | 73 | | ): Promise<void> { |
| | 6 | 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<Record<string, string>> { |
| | 7 | 81 | | const secretProcessingPromises = Object.entries(paramMap).map( |
| | | 82 | | async ([envVar, secretName]) => { |
| | 9 | 83 | | return this.processSecret(envVar, secretName, existingEnvVariables); |
| | | 84 | | }, |
| | | 85 | | ); |
| | | 86 | | |
| | 7 | 87 | | const results = await Promise.all(secretProcessingPromises); |
| | | 88 | | |
| | 9 | 89 | | const errors = results.filter((error) => error !== null) as string[]; |
| | | 90 | | |
| | 7 | 91 | | if (errors.length > 0) { |
| | 1 | 92 | | throw new Error( |
| | | 93 | | `${PullSecretsToEnvCommandHandler.ERROR_MESSAGES.PARAM_NOT_FOUND}${errors.join('\n')}`, |
| | | 94 | | ); |
| | | 95 | | } |
| | 6 | 96 | | return existingEnvVariables; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private async processSecret( |
| | | 100 | | envVar: string, |
| | | 101 | | secretName: string, |
| | | 102 | | existingEnvVariables: Record<string, string>, |
| | | 103 | | ): Promise<string | null> { |
| | 9 | 104 | | try { |
| | 9 | 105 | | const value = await this.secretProvider.getSecret(secretName); |
| | 8 | 106 | | if (!value) { |
| | 1 | 107 | | this.logger.warn( |
| | | 108 | | `${PullSecretsToEnvCommandHandler.ERROR_MESSAGES.NO_VALUE_FOUND}'${secretName}'`, |
| | | 109 | | ); |
| | 1 | 110 | | return null; |
| | | 111 | | } |
| | | 112 | | |
| | 7 | 113 | | existingEnvVariables[envVar] = value; |
| | | 114 | | |
| | 7 | 115 | | const envVariable = new EnvironmentVariable(envVar, value, true); |
| | 7 | 116 | | this.logger.info(`${envVariable.name}=${envVariable.maskedValue}`); |
| | | 117 | | |
| | 7 | 118 | | return null; |
| | | 119 | | } catch (_error) { |
| | 1 | 120 | | this.logger.error( |
| | | 121 | | `${PullSecretsToEnvCommandHandler.ERROR_MESSAGES.ERROR_FETCHING}'${secretName}'`, |
| | | 122 | | ); |
| | 1 | 123 | | return `ParameterNotFound: ${secretName}`; |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | } |