| | | 1 | | import { inject, injectable } from 'inversify'; |
| | | 2 | | import type { ISecretProvider } from '../../domain/ports/ISecretProvider.js'; |
| | | 3 | | import type { IVariableStore } from '../../domain/ports/IVariableStore.js'; |
| | | 4 | | import type { RemoteKeyCheck } from '../../domain/SecretExistence.js'; |
| | | 5 | | import { TYPES } from '../../types.js'; |
| | | 6 | | import type { ValidateRemoteKeysCommand } from './ValidateRemoteKeysCommand.js'; |
| | | 7 | | |
| | | 8 | | /** |
| | | 9 | | * Checks that every secret path declared in a map file exists in the |
| | | 10 | | * provider, without reading any value. A path shared by several variables is |
| | | 11 | | * checked once. Returns one check per variable, in map-file order. Run it on a |
| | | 12 | | * map file that passed offline validation. |
| | | 13 | | */ |
| | | 14 | | @injectable() |
| | 8 | 15 | | export class ValidateRemoteKeysCommandHandler { |
| | | 16 | | constructor( |
| | | 17 | | @inject(TYPES.ISecretProvider) |
| | 21 | 18 | | private readonly secretProvider: ISecretProvider, |
| | | 19 | | @inject(TYPES.IVariableStore) |
| | 21 | 20 | | private readonly variableStore: IVariableStore, |
| | | 21 | | ) {} |
| | | 22 | | |
| | | 23 | | async handle(command: ValidateRemoteKeysCommand): Promise<RemoteKeyCheck[]> { |
| | 21 | 24 | | const mappings = Object.entries( |
| | | 25 | | await this.variableStore.getMapping(command.mapPath), |
| | | 26 | | ); |
| | 21 | 27 | | if (typeof this.secretProvider.logIdentity === 'function') { |
| | 4 | 28 | | await this.secretProvider.logIdentity(); |
| | | 29 | | } |
| | 21 | 30 | | const existence = await this.secretProvider.checkSecretsExist([ |
| | 36 | 31 | | ...new Set(mappings.map(([, path]) => path)), |
| | | 32 | | ]); |
| | 33 | 33 | | return mappings.map(([variable, path]) => ({ |
| | | 34 | | variable, |
| | | 35 | | path, |
| | | 36 | | ...(existence.get(path) ?? { |
| | | 37 | | status: 'provider_error', |
| | | 38 | | reason: 'The provider returned no result for this secret.', |
| | | 39 | | }), |
| | | 40 | | })); |
| | | 41 | | } |
| | | 42 | | } |