| | | 1 | | import type { |
| | | 2 | | ContractValidationResult, |
| | | 3 | | KeyCheck, |
| | | 4 | | MapFileError, |
| | | 5 | | } from '../domain/contract-validation-result.js'; |
| | | 6 | | import type { ParsedMapFile } from '../domain/parsed-map-file.js'; |
| | | 7 | | import type { ISecretExistenceChecker } from '../domain/ports/secret-existence-checker.js'; |
| | | 8 | | import type { SecretProviderType } from '../domain/secret-provider-type.js'; |
| | | 9 | | |
| | | 10 | | /** |
| | | 11 | | * Thrown by a load with contract validation enabled when the map file is |
| | | 12 | | * invalid or a declared secret cannot be confirmed. The message names the |
| | | 13 | | * failing keys and paths, never a secret value. |
| | | 14 | | */ |
| | | 15 | | export class ContractValidationError extends Error { |
| | | 16 | | /** The full outcome of the check that failed. */ |
| | | 17 | | readonly result: ContractValidationResult; |
| | | 18 | | |
| | | 19 | | constructor(result: ContractValidationResult) { |
| | 7 | 20 | | super(buildMessage(result)); |
| | 7 | 21 | | this.result = result; |
| | 7 | 22 | | this.name = 'ContractValidationError'; |
| | | 23 | | } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /** The result for a map file that failed validation: no provider was called. */ |
| | | 27 | | export function invalidMapFileResult( |
| | | 28 | | mapFileErrors: readonly MapFileError[], |
| | | 29 | | ): ContractValidationResult { |
| | 4 | 30 | | return { isValid: false, mapFileErrors, keys: [] }; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /** |
| | | 34 | | * The result for a provider that cannot check existence (a custom one that |
| | | 35 | | * does not implement `ISecretExistenceChecker`): every key is a provider |
| | | 36 | | * error. |
| | | 37 | | */ |
| | | 38 | | export function notCheckableResult( |
| | | 39 | | mapFile: ParsedMapFile, |
| | | 40 | | provider: SecretProviderType, |
| | | 41 | | ): ContractValidationResult { |
| | 4 | 42 | | const keys: KeyCheck[] = [...mapFile.mappings].map(([variable, path]) => ({ |
| | | 43 | | variable, |
| | | 44 | | path, |
| | | 45 | | provider, |
| | | 46 | | status: 'provider_error', |
| | | 47 | | reason: 'The provider cannot check whether a secret exists.', |
| | | 48 | | })); |
| | 2 | 49 | | return { isValid: false, mapFileErrors: [], keys }; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /** |
| | | 53 | | * Checks that every secret path declared in a valid map file exists, without |
| | | 54 | | * reading any value. A path shared by several variables is checked once. |
| | | 55 | | * Returns one check per variable, in map-file order. |
| | | 56 | | */ |
| | | 57 | | export async function checkRemoteKeys( |
| | | 58 | | mapFile: ParsedMapFile, |
| | | 59 | | checker: ISecretExistenceChecker, |
| | | 60 | | provider: SecretProviderType, |
| | | 61 | | ): Promise<ContractValidationResult> { |
| | 19 | 62 | | const mappings = [...mapFile.mappings]; |
| | | 63 | | const existence = |
| | 19 | 64 | | mappings.length === 0 |
| | | 65 | | ? new Map() |
| | | 66 | | : await checker.checkSecretsExist([ |
| | 53 | 67 | | ...new Set(mappings.map(([, path]) => path)), |
| | | 68 | | ]); |
| | 53 | 69 | | const keys: KeyCheck[] = mappings.map(([variable, path]) => ({ |
| | | 70 | | variable, |
| | | 71 | | path, |
| | | 72 | | provider, |
| | | 73 | | ...(existence.get(path) ?? { |
| | | 74 | | status: 'provider_error', |
| | | 75 | | reason: 'The provider returned no result for this secret.', |
| | | 76 | | }), |
| | | 77 | | })); |
| | 18 | 78 | | return { |
| | 40 | 79 | | isValid: keys.every((key) => key.status === 'exists'), |
| | | 80 | | mapFileErrors: [], |
| | | 81 | | keys, |
| | | 82 | | }; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | function buildMessage(result: ContractValidationResult): string { |
| | 7 | 86 | | if (result.mapFileErrors.length > 0) { |
| | 2 | 87 | | const details = result.mapFileErrors |
| | 2 | 88 | | .map((error) => `${error.key}: ${error.message}`) |
| | | 89 | | .join('; '); |
| | 2 | 90 | | return `Map file contract validation failed: ${details}`; |
| | | 91 | | } |
| | 5 | 92 | | const details = result.keys |
| | 8 | 93 | | .filter((key) => key.status !== 'exists') |
| | 6 | 94 | | .map((key) => `${key.variable} (${key.path}): ${key.status}`) |
| | | 95 | | .join(', '); |
| | 5 | 96 | | return `Map file contract validation failed: ${details}`; |
| | | 97 | | } |