| | | 1 | | import { findCaseCollisions } from '../../domain/ChildEnvironment.js'; |
| | | 2 | | import { |
| | | 3 | | InvalidArgumentError, |
| | | 4 | | SecretsFetchError, |
| | | 5 | | } from '../../domain/errors/DomainErrors.js'; |
| | | 6 | | import type { ILogger } from '../../domain/ports/ILogger.js'; |
| | | 7 | | import type { IProcessContext } from '../../domain/ports/IProcessContext.js'; |
| | | 8 | | import type { ISecretMasker } from '../../domain/ports/ISecretMasker.js'; |
| | | 9 | | import type { ISecretProvider } from '../../domain/ports/ISecretProvider.js'; |
| | | 10 | | import type { IVariableStore } from '../../domain/ports/IVariableStore.js'; |
| | | 11 | | import { |
| | | 12 | | type FailedSecret, |
| | | 13 | | SecretResolver, |
| | | 14 | | type UnresolvedSecret, |
| | | 15 | | } from './SecretResolver.js'; |
| | | 16 | | |
| | 13 | 17 | | const NUL = '\0'; |
| | | 18 | | |
| | | 19 | | /** |
| | | 20 | | * Resolves every secret of a map file into environment variables, fail-closed: |
| | | 21 | | * it throws instead of returning a partial set. |
| | | 22 | | * |
| | | 23 | | * Shared by the modes that hand secrets to processes as environment variables |
| | | 24 | | * (exec, and the GitHub Action exporting to the job environment), where a |
| | | 25 | | * missing secret must stop the run instead of leaving a variable unset. |
| | | 26 | | */ |
| | | 27 | | export class RequiredSecretsResolver { |
| | | 28 | | private readonly secretResolver: SecretResolver; |
| | | 29 | | |
| | | 30 | | constructor( |
| | | 31 | | secretProvider: ISecretProvider, |
| | 123 | 32 | | private readonly variableStore: IVariableStore, |
| | 123 | 33 | | private readonly logger: ILogger, |
| | | 34 | | secretMasker: ISecretMasker, |
| | 123 | 35 | | private readonly processContext: IProcessContext, |
| | | 36 | | ) { |
| | 123 | 37 | | this.secretResolver = new SecretResolver( |
| | | 38 | | secretProvider, |
| | | 39 | | logger, |
| | | 40 | | secretMasker, |
| | | 41 | | ); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /** |
| | | 45 | | * Throws `SecretsFetchError` when any secret is missing, empty or |
| | | 46 | | * unreadable, and `InvalidArgumentError` when the variables cannot be set |
| | | 47 | | * on this platform. |
| | | 48 | | */ |
| | | 49 | | async resolve(mapPath: string): Promise<Record<string, string>> { |
| | 84 | 50 | | const mappings = await this.variableStore.getMapping(mapPath); |
| | 84 | 51 | | if (Object.keys(mappings).length === 0) { |
| | 1 | 52 | | this.logger.warn('Map file declares no variables.'); |
| | | 53 | | } |
| | 84 | 54 | | this.assertNoCaseCollisions(Object.keys(mappings)); |
| | | 55 | | |
| | 84 | 56 | | const { outcomes, resolved } = await this.secretResolver.resolve(mappings); |
| | 73 | 57 | | const failures = outcomes.filter( |
| | | 58 | | (outcome): outcome is UnresolvedSecret | FailedSecret => |
| | 139 | 59 | | outcome.status !== 'resolved', |
| | | 60 | | ); |
| | 73 | 61 | | if (failures.length > 0) { |
| | 10 | 62 | | this.secretResolver.logSecretsSection(resolved, []); |
| | 10 | 63 | | throw new SecretsFetchError(failures.map(SecretResolver.toFailure)); |
| | | 64 | | } |
| | | 65 | | |
| | 63 | 66 | | const secrets: Record<string, string> = {}; |
| | 63 | 67 | | for (const { envVar, value } of resolved) { |
| | 117 | 68 | | if (value.includes(NUL)) { |
| | 2 | 69 | | throw new InvalidArgumentError( |
| | | 70 | | `Secret for ${envVar} contains a NUL byte and cannot be passed as an environment variable.`, |
| | | 71 | | ); |
| | | 72 | | } |
| | 115 | 73 | | secrets[envVar] = value; |
| | | 74 | | } |
| | | 75 | | |
| | 61 | 76 | | return secrets; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /** |
| | | 80 | | * On Windows two mapped names that differ only by case are one variable, |
| | | 81 | | * so one value would be silently dropped. Refuse before resolving. |
| | | 82 | | */ |
| | | 83 | | private assertNoCaseCollisions(names: string[]): void { |
| | 84 | 84 | | const collisions = findCaseCollisions( |
| | | 85 | | names, |
| | | 86 | | this.processContext.platform(), |
| | | 87 | | ); |
| | 84 | 88 | | if (collisions.length === 0) { |
| | 81 | 89 | | return; |
| | | 90 | | } |
| | 3 | 91 | | const listed = collisions.map((group) => group.join(', ')).join('; '); |
| | 3 | 92 | | throw new InvalidArgumentError( |
| | | 93 | | `Map file declares variables that differ only by case (${listed}); Windows treats them as the same variable.`, |
| | | 94 | | ); |
| | | 95 | | } |
| | | 96 | | } |