| | | 1 | | import { inject, injectable } from 'inversify'; |
| | | 2 | | import type { EnvValueCheck } from '../../domain/EnvFileComparison.js'; |
| | | 3 | | import { EnvironmentFileError } 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 { |
| | | 10 | | type SecretOutcome, |
| | | 11 | | SecretResolver, |
| | | 12 | | } from '../secretResolution/SecretResolver.js'; |
| | | 13 | | import type { CompareEnvFileValuesCommand } from './CompareEnvFileValuesCommand.js'; |
| | | 14 | | |
| | | 15 | | /** |
| | | 16 | | * Compares the local value of each requested variable with its remote value |
| | | 17 | | * and reports only `matches`, `differs` or `unreadable`. Remote values are |
| | | 18 | | * read through the same resolver pull and exec use, so they reach the secret |
| | | 19 | | * masker, and are dropped as soon as they are compared. Neither value ever |
| | | 20 | | * reaches the result. When a key repeats in the env file, the last assignment |
| | | 21 | | * is compared, as dotenv would load it. |
| | | 22 | | */ |
| | | 23 | | @injectable() |
| | 9 | 24 | | export class CompareEnvFileValuesCommandHandler { |
| | | 25 | | private readonly secretResolver: SecretResolver; |
| | | 26 | | |
| | | 27 | | constructor( |
| | | 28 | | @inject(TYPES.ISecretProvider) secretProvider: ISecretProvider, |
| | | 29 | | @inject(TYPES.IVariableStore) |
| | 16 | 30 | | private readonly variableStore: IVariableStore, |
| | | 31 | | @inject(TYPES.ILogger) logger: ILogger, |
| | | 32 | | @inject(TYPES.ISecretMasker) secretMasker: ISecretMasker, |
| | | 33 | | ) { |
| | 16 | 34 | | this.secretResolver = new SecretResolver( |
| | | 35 | | secretProvider, |
| | | 36 | | logger, |
| | | 37 | | secretMasker, |
| | | 38 | | ); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | async handle(command: CompareEnvFileValuesCommand): Promise<EnvValueCheck[]> { |
| | 16 | 42 | | const mapping = await this.variableStore.getMapping(command.mapPath); |
| | 16 | 43 | | const entries = await this.variableStore.readEnvironmentEntries( |
| | | 44 | | command.envFilePath, |
| | | 45 | | ); |
| | 16 | 46 | | if (entries === null) { |
| | 0 | 47 | | throw new EnvironmentFileError( |
| | | 48 | | `Env file not found: ${command.envFilePath}`, |
| | | 49 | | ); |
| | | 50 | | } |
| | 23 | 51 | | const localValues = new Map(entries.map(({ key, value }) => [key, value])); |
| | 16 | 52 | | const requested = new Set(command.variables); |
| | 16 | 53 | | const selected = Object.fromEntries( |
| | 23 | 54 | | Object.entries(mapping).filter(([variable]) => requested.has(variable)), |
| | | 55 | | ); |
| | | 56 | | |
| | 16 | 57 | | const { outcomes } = await this.secretResolver.resolve(selected); |
| | 15 | 58 | | return outcomes.map((outcome) => |
| | 18 | 59 | | toValueCheck( |
| | | 60 | | outcome, |
| | | 61 | | selected[outcome.envVar], |
| | | 62 | | localValues.get(outcome.envVar), |
| | | 63 | | ), |
| | | 64 | | ); |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | function toValueCheck( |
| | | 69 | | outcome: SecretOutcome, |
| | | 70 | | path: string, |
| | | 71 | | localValue: string | undefined, |
| | | 72 | | ): EnvValueCheck { |
| | 18 | 73 | | const variable = outcome.envVar; |
| | 18 | 74 | | if (outcome.status === 'error') { |
| | 1 | 75 | | return { variable, path, status: 'unreadable', reason: outcome.reason }; |
| | | 76 | | } |
| | 17 | 77 | | if (outcome.status === 'warning' && outcome.reason === 'not-found') { |
| | 1 | 78 | | return { variable, path, status: 'unreadable', reason: 'not found' }; |
| | | 79 | | } |
| | 16 | 80 | | const remoteValue = outcome.status === 'resolved' ? outcome.value : ''; |
| | 18 | 81 | | return { |
| | | 82 | | variable, |
| | | 83 | | path, |
| | | 84 | | status: localValue === remoteValue ? 'matches' : 'differs', |
| | | 85 | | }; |
| | | 86 | | } |