| | | 1 | | import { inject, injectable } from 'inversify'; |
| | | 2 | | import type { IEnvironmentExporter } from '../../domain/ports/IEnvironmentExporter.js'; |
| | | 3 | | import type { ILogger } from '../../domain/ports/ILogger.js'; |
| | | 4 | | import type { IProcessContext } from '../../domain/ports/IProcessContext.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 { RequiredSecretsResolver } from '../secretResolution/RequiredSecretsResolver.js'; |
| | | 10 | | import type { ExportSecretsToEnvironmentCommand } from './ExportSecretsToEnvironmentCommand.js'; |
| | | 11 | | |
| | | 12 | | /** |
| | | 13 | | * Exports the secrets of a map file as environment variables for the |
| | | 14 | | * processes the host starts afterwards (the later steps of a GitHub Actions |
| | | 15 | | * job), without writing a `.env` file. |
| | | 16 | | * |
| | | 17 | | * Fail-closed like exec mode: nothing is exported when a secret is missing, |
| | | 18 | | * empty or unreadable, or when a value could not be registered for masking. |
| | | 19 | | */ |
| | | 20 | | @injectable() |
| | 4 | 21 | | export class ExportSecretsToEnvironmentCommandHandler { |
| | | 22 | | private readonly requiredSecrets: RequiredSecretsResolver; |
| | | 23 | | |
| | | 24 | | constructor( |
| | | 25 | | @inject(TYPES.ISecretProvider) |
| | | 26 | | secretProvider: ISecretProvider, |
| | | 27 | | @inject(TYPES.IVariableStore) |
| | | 28 | | variableStore: IVariableStore, |
| | 29 | 29 | | @inject(TYPES.ILogger) private readonly logger: ILogger, |
| | | 30 | | @inject(TYPES.ISecretMasker) |
| | 29 | 31 | | private readonly secretMasker: ISecretMasker, |
| | | 32 | | @inject(TYPES.IProcessContext) |
| | | 33 | | processContext: IProcessContext, |
| | | 34 | | @inject(TYPES.IEnvironmentExporter) |
| | 29 | 35 | | private readonly environmentExporter: IEnvironmentExporter, |
| | | 36 | | ) { |
| | 29 | 37 | | this.requiredSecrets = new RequiredSecretsResolver( |
| | | 38 | | secretProvider, |
| | | 39 | | variableStore, |
| | | 40 | | logger, |
| | | 41 | | secretMasker, |
| | | 42 | | processContext, |
| | | 43 | | ); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | async handle(command: ExportSecretsToEnvironmentCommand): Promise<void> { |
| | 8 | 47 | | const secrets = await this.requiredSecrets.resolve(command.mapPath); |
| | | 48 | | // Later steps print whatever they like: export only once every value is |
| | | 49 | | // registered for masking, and nothing when a registration failed. |
| | 5 | 50 | | await this.secretMasker.flush(); |
| | 4 | 51 | | await this.environmentExporter.export(secrets); |
| | 4 | 52 | | this.logger.info( |
| | | 53 | | ExportSecretsToEnvironmentCommandHandler.buildSummary( |
| | | 54 | | Object.keys(secrets), |
| | | 55 | | ), |
| | | 56 | | ); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | private static buildSummary(names: string[]): string { |
| | 4 | 60 | | const noun = names.length === 1 ? 'secret' : 'secrets'; |
| | 4 | 61 | | const listed = names.length > 0 ? ` (${names.join(', ')})` : ''; |
| | 4 | 62 | | return `envilder: ${names.length} ${noun} exported to the job environment${listed}`; |
| | | 63 | | } |
| | | 64 | | } |