| | | 1 | | import { |
| | | 2 | | type EnvFileKeyComparison, |
| | | 3 | | type EnvValueCheck, |
| | | 4 | | type EnvValueStatus, |
| | | 5 | | summarizeEnvFileFailures, |
| | | 6 | | summarizeValueFailures, |
| | | 7 | | } from '../../../core/domain/EnvFileComparison.js'; |
| | | 8 | | import type { MapFileViolation } from '../../../core/domain/MapFileValidator.js'; |
| | | 9 | | import { |
| | | 10 | | type RemoteKeyCheck, |
| | | 11 | | type SecretExistenceStatus, |
| | | 12 | | summarizeFailures, |
| | | 13 | | } from '../../../core/domain/SecretExistence.js'; |
| | | 14 | | import { describeError } from '../../../core/infrastructure/describeError.js'; |
| | | 15 | | |
| | | 16 | | /** |
| | | 17 | | * What `envilder validate` found, step by step. A step that did not run, for |
| | | 18 | | * example the remote check with `--offline` or after an invalid map file, is |
| | | 19 | | * left out. |
| | | 20 | | */ |
| | | 21 | | export type ValidationResults = { |
| | | 22 | | offline: boolean; |
| | | 23 | | mapFile?: { |
| | | 24 | | readonly path: string; |
| | | 25 | | readonly violations: readonly MapFileViolation[]; |
| | | 26 | | }; |
| | | 27 | | keys?: { |
| | | 28 | | readonly provider: string; |
| | | 29 | | readonly checks: readonly RemoteKeyCheck[]; |
| | | 30 | | }; |
| | | 31 | | envFile?: { |
| | | 32 | | readonly path: string; |
| | | 33 | | readonly comparison: EnvFileKeyComparison; |
| | | 34 | | }; |
| | | 35 | | values?: readonly EnvValueCheck[]; |
| | | 36 | | }; |
| | | 37 | | |
| | | 38 | | export type ValidationJsonKey = { |
| | | 39 | | name: string; |
| | | 40 | | provider: string; |
| | | 41 | | path: string; |
| | | 42 | | status: SecretExistenceStatus; |
| | | 43 | | reason?: string; |
| | | 44 | | }; |
| | | 45 | | |
| | | 46 | | export type ValidationJsonEnvFileKey = { |
| | | 47 | | name: string; |
| | | 48 | | status: 'present' | 'missing'; |
| | | 49 | | value?: EnvValueStatus; |
| | | 50 | | reason?: string; |
| | | 51 | | }; |
| | | 52 | | |
| | | 53 | | /** |
| | | 54 | | * The document `envilder validate --format json` prints. A section is left |
| | | 55 | | * out when its step did not run, and `error` when nothing stopped the run. It never carries a secret value: value |
| | | 56 | | * comparison only reports `matches`, `differs` or `unreadable`. |
| | | 57 | | */ |
| | | 58 | | export type ValidationJson = { |
| | | 59 | | valid: boolean; |
| | | 60 | | offline: boolean; |
| | | 61 | | mapFile?: { |
| | | 62 | | path: string; |
| | | 63 | | valid: boolean; |
| | | 64 | | errors: { key: string; message: string }[]; |
| | | 65 | | }; |
| | | 66 | | keys?: ValidationJsonKey[]; |
| | | 67 | | envFile?: { |
| | | 68 | | path: string; |
| | | 69 | | valid: boolean; |
| | | 70 | | keys: ValidationJsonEnvFileKey[]; |
| | | 71 | | duplicates: { name: string; count: number }[]; |
| | | 72 | | undeclared: string[]; |
| | | 73 | | }; |
| | | 74 | | error?: { message: string }; |
| | | 75 | | }; |
| | | 76 | | |
| | | 77 | | /** |
| | | 78 | | * Shapes the validation results as JSON. `error` is what stopped the run |
| | | 79 | | * before every step finished, such as expired credentials; a failed check is |
| | | 80 | | * not an error. |
| | | 81 | | */ |
| | | 82 | | export function toValidationJson( |
| | | 83 | | results: ValidationResults, |
| | | 84 | | error?: unknown, |
| | | 85 | | ): ValidationJson { |
| | 15 | 86 | | const mapFile = results.mapFile && { |
| | | 87 | | path: results.mapFile.path, |
| | | 88 | | valid: results.mapFile.violations.length === 0, |
| | 2 | 89 | | errors: results.mapFile.violations.map(({ key, message }) => ({ |
| | | 90 | | key, |
| | | 91 | | message, |
| | | 92 | | })), |
| | | 93 | | }; |
| | 15 | 94 | | const keys = results.keys?.checks.map((check) => |
| | 15 | 95 | | toKey(check, results.keys?.provider ?? ''), |
| | | 96 | | ); |
| | 15 | 97 | | const envFile = results.envFile && toEnvFile(results.envFile, results.values); |
| | | 98 | | |
| | 15 | 99 | | return { |
| | | 100 | | valid: |
| | | 101 | | error === undefined && |
| | | 102 | | mapFile?.valid === true && |
| | | 103 | | summarizeFailures(results.keys?.checks ?? []) === '' && |
| | | 104 | | (envFile?.valid ?? true), |
| | | 105 | | offline: results.offline, |
| | | 106 | | ...(mapFile !== undefined && { mapFile }), |
| | | 107 | | ...(keys !== undefined && { keys }), |
| | | 108 | | ...(envFile !== undefined && { envFile }), |
| | | 109 | | ...(error !== undefined && { error: { message: describeError(error) } }), |
| | | 110 | | }; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | function toKey(check: RemoteKeyCheck, provider: string): ValidationJsonKey { |
| | 15 | 114 | | return { |
| | | 115 | | name: check.variable, |
| | | 116 | | provider, |
| | | 117 | | path: check.path, |
| | | 118 | | status: check.status, |
| | | 119 | | ...(check.reason !== undefined && { reason: check.reason }), |
| | | 120 | | }; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | function toEnvFile( |
| | | 124 | | envFile: NonNullable<ValidationResults['envFile']>, |
| | | 125 | | values: readonly EnvValueCheck[] | undefined, |
| | | 126 | | ): NonNullable<ValidationJson['envFile']> { |
| | 3 | 127 | | const { comparison } = envFile; |
| | 4 | 128 | | const valueChecks = new Map(values?.map((check) => [check.variable, check])); |
| | 3 | 129 | | const present = comparison.present.map((name) => { |
| | 5 | 130 | | const value = valueChecks.get(name); |
| | 5 | 131 | | return { |
| | | 132 | | name, |
| | | 133 | | status: 'present' as const, |
| | | 134 | | ...(value !== undefined && { value: value.status }), |
| | | 135 | | ...(value?.reason !== undefined && { reason: value.reason }), |
| | | 136 | | }; |
| | | 137 | | }); |
| | 3 | 138 | | const missing = comparison.missing.map((name) => ({ |
| | | 139 | | name, |
| | | 140 | | status: 'missing' as const, |
| | | 141 | | })); |
| | | 142 | | |
| | 3 | 143 | | return { |
| | | 144 | | path: envFile.path, |
| | | 145 | | valid: |
| | | 146 | | summarizeEnvFileFailures(comparison, envFile.path) === '' && |
| | | 147 | | summarizeValueFailures(values ?? []) === '', |
| | | 148 | | keys: [...present, ...missing], |
| | 1 | 149 | | duplicates: comparison.duplicates.map(({ key, count }) => ({ |
| | | 150 | | name: key, |
| | | 151 | | count, |
| | | 152 | | })), |
| | | 153 | | undeclared: [...comparison.undeclared], |
| | | 154 | | }; |
| | | 155 | | } |