| | | 1 | | import type { MapFileError } from '../domain/contract-validation-result.js'; |
| | | 2 | | import type { EnvilderOptions } from '../domain/envilder-options.js'; |
| | | 3 | | import { |
| | | 4 | | invalidEnvironmentVariableNameMessage, |
| | | 5 | | isValidEnvironmentVariableName, |
| | | 6 | | quoteName, |
| | | 7 | | } from '../domain/environment-variable-name.js'; |
| | | 8 | | |
| | | 9 | | /** |
| | | 10 | | * Checks a map file against `spec/map-file.v1.json` and the provider |
| | | 11 | | * configuration it resolves to, with the same rules and wording as |
| | | 12 | | * `envilder validate --offline`. The SDK supports fewer providers than the |
| | | 13 | | * schema declares, so `gcp`, `hashicorp` and `file` are reported as |
| | | 14 | | * unsupported. Builder overrides take precedence over `$config`, as they do |
| | | 15 | | * on a load. No mapped value ever reaches an error. |
| | | 16 | | */ |
| | | 17 | | |
| | 2 | 18 | | const SUPPORTED_PROVIDERS: readonly string[] = ['aws', 'azure']; |
| | | 19 | | |
| | 2 | 20 | | const DEFAULT_PROVIDER = 'aws'; |
| | | 21 | | |
| | 2 | 22 | | const CONFIG_FIELDS = [ |
| | | 23 | | 'provider', |
| | | 24 | | 'profile', |
| | | 25 | | 'vaultUrl', |
| | | 26 | | 'projectId', |
| | | 27 | | 'namespace', |
| | | 28 | | 'path', |
| | | 29 | | 'name', |
| | | 30 | | 'description', |
| | | 31 | | 'owner', |
| | | 32 | | 'environment', |
| | | 33 | | ]; |
| | | 34 | | |
| | 2 | 35 | | const URI_FIELDS = new Set(['vaultUrl']); |
| | | 36 | | |
| | | 37 | | /** Provider-specific `$config` fields and the providers that accept each. */ |
| | 2 | 38 | | const PROVIDER_SPECIFIC_FIELDS: Readonly<Record<string, readonly string[]>> = { |
| | | 39 | | profile: ['aws'], |
| | | 40 | | vaultUrl: ['azure', 'hashicorp'], |
| | | 41 | | projectId: ['gcp'], |
| | | 42 | | namespace: ['hashicorp'], |
| | | 43 | | path: ['file'], |
| | | 44 | | }; |
| | | 45 | | |
| | | 46 | | /** The `$config` field each supported provider cannot work without. */ |
| | 2 | 47 | | const REQUIRED_PROVIDER_FIELDS: Readonly<Record<string, string>> = { |
| | | 48 | | azure: 'vaultUrl', |
| | | 49 | | }; |
| | | 50 | | |
| | 2 | 51 | | const PROVIDER_LABELS: Readonly<Record<string, string>> = { |
| | | 52 | | aws: 'AWS', |
| | | 53 | | azure: 'Azure', |
| | | 54 | | }; |
| | | 55 | | |
| | | 56 | | /** The builder method that overrides each field, named in errors. */ |
| | 2 | 57 | | const OVERRIDE_METHODS: Readonly<Record<string, string>> = { |
| | | 58 | | provider: 'withProvider()', |
| | | 59 | | profile: 'withProfile()', |
| | | 60 | | vaultUrl: 'withVaultUrl()', |
| | | 61 | | }; |
| | | 62 | | |
| | | 63 | | /** Parses and validates the raw content of a map file. */ |
| | | 64 | | export function validateMapFileContent( |
| | | 65 | | json: string, |
| | | 66 | | overrides: EnvilderOptions, |
| | | 67 | | ): MapFileError[] { |
| | | 68 | | let document: unknown; |
| | 15 | 69 | | try { |
| | 15 | 70 | | document = JSON.parse(json); |
| | | 71 | | } catch { |
| | | 72 | | // The parser's message quotes the surrounding text, which can be a |
| | | 73 | | // secret pasted into the file, so it is not repeated. |
| | 1 | 74 | | return [{ key: '(root)', message: 'The map file is not valid JSON.' }]; |
| | | 75 | | } |
| | 14 | 76 | | return validateMapFile(document, overrides); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /** Validates a parsed map file and returns every error found. */ |
| | | 80 | | export function validateMapFile( |
| | | 81 | | document: unknown, |
| | | 82 | | overrides: EnvilderOptions, |
| | | 83 | | ): MapFileError[] { |
| | 29 | 84 | | if (!isJsonObject(document)) { |
| | 1 | 85 | | return [{ key: '(root)', message: 'The map file must be a JSON object.' }]; |
| | | 86 | | } |
| | | 87 | | |
| | 28 | 88 | | const errors: MapFileError[] = []; |
| | 28 | 89 | | let fileConfig: Record<string, unknown> = {}; |
| | 28 | 90 | | for (const [key, value] of Object.entries(document)) { |
| | 45 | 91 | | if (key === '$schema') { |
| | 1 | 92 | | errors.push(...validateUri('$schema', value)); |
| | 44 | 93 | | } else if (key === '$config') { |
| | 12 | 94 | | if (isJsonObject(value)) { |
| | 12 | 95 | | fileConfig = value; |
| | 12 | 96 | | errors.push(...validateConfigFields(value)); |
| | | 97 | | } else { |
| | 0 | 98 | | errors.push({ key: '$config', message: '$config must be an object.' }); |
| | | 99 | | } |
| | 32 | 100 | | } else if (key.startsWith('$')) { |
| | 2 | 101 | | errors.push({ |
| | | 102 | | key, |
| | | 103 | | message: `${formatKey(key)} is reserved: only $schema and $config may start with "$".`, |
| | | 104 | | }); |
| | | 105 | | } else { |
| | 30 | 106 | | errors.push(...validateMapping(key, value)); |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | |
| | 28 | 110 | | errors.push(...validateProviderConfiguration(fileConfig, overrides)); |
| | 28 | 111 | | return errors; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | function validateMapping(key: string, value: unknown): MapFileError[] { |
| | 30 | 115 | | const errors: MapFileError[] = []; |
| | 30 | 116 | | if (!isValidEnvironmentVariableName(key)) { |
| | 1 | 117 | | errors.push({ |
| | | 118 | | key, |
| | | 119 | | message: `${invalidEnvironmentVariableNameMessage(key)}.`, |
| | | 120 | | }); |
| | | 121 | | } |
| | 30 | 122 | | if (typeof value !== 'string') { |
| | | 123 | | // The type only: the value itself may be a secret pasted by mistake. |
| | 3 | 124 | | errors.push({ |
| | | 125 | | key, |
| | | 126 | | message: `${formatKey(key)} must map to a string secret path, not ${describeType(value)}.`, |
| | | 127 | | }); |
| | | 128 | | } |
| | 30 | 129 | | return errors; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | function validateConfigFields(config: Record<string, unknown>): MapFileError[] { |
| | 12 | 133 | | const errors: MapFileError[] = []; |
| | 12 | 134 | | for (const [field, value] of Object.entries(config)) { |
| | 21 | 135 | | const key = `$config.${field}`; |
| | 21 | 136 | | if (!CONFIG_FIELDS.includes(field)) { |
| | 1 | 137 | | errors.push({ |
| | | 138 | | key, |
| | | 139 | | message: `${formatKey(key)} is not a known field. Allowed fields: ${CONFIG_FIELDS.join(', ')}.`, |
| | | 140 | | }); |
| | 20 | 141 | | } else if (typeof value !== 'string') { |
| | 1 | 142 | | errors.push({ |
| | | 143 | | key, |
| | | 144 | | message: `${key} must be a string, not ${describeType(value)}.`, |
| | | 145 | | }); |
| | 19 | 146 | | } else if (URI_FIELDS.has(field)) { |
| | 5 | 147 | | errors.push(...validateUri(key, value)); |
| | | 148 | | } |
| | | 149 | | } |
| | 12 | 150 | | return errors; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | function validateUri(key: string, value: unknown): MapFileError[] { |
| | 6 | 154 | | if (typeof value !== 'string') { |
| | 0 | 155 | | return [ |
| | | 156 | | { key, message: `${key} must be a string, not ${describeType(value)}.` }, |
| | | 157 | | ]; |
| | | 158 | | } |
| | 6 | 159 | | if (!isUri(value)) { |
| | 1 | 160 | | return [{ key, message: `${key} must be an absolute URI.` }]; |
| | | 161 | | } |
| | 5 | 162 | | return []; |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | /** A configuration field with the key that named it, for the report. */ |
| | | 166 | | type SourcedValue = { readonly value: string; readonly source: string }; |
| | | 167 | | |
| | | 168 | | function validateProviderConfiguration( |
| | | 169 | | fileConfig: Record<string, unknown>, |
| | | 170 | | overrides: EnvilderOptions, |
| | | 171 | | ): MapFileError[] { |
| | 28 | 172 | | const effective = resolveEffectiveConfig(fileConfig, overrides); |
| | 28 | 173 | | const provider = effective.provider ?? { |
| | | 174 | | value: DEFAULT_PROVIDER, |
| | | 175 | | source: '$config.provider', |
| | | 176 | | }; |
| | | 177 | | |
| | 28 | 178 | | if (!SUPPORTED_PROVIDERS.includes(provider.value)) { |
| | 1 | 179 | | return [ |
| | | 180 | | { |
| | | 181 | | key: provider.source, |
| | | 182 | | message: |
| | | 183 | | `Unsupported provider: ${formatKey(provider.value)}.` + |
| | | 184 | | ` Supported providers: ${SUPPORTED_PROVIDERS.join(', ')}.`, |
| | | 185 | | }, |
| | | 186 | | ]; |
| | | 187 | | } |
| | | 188 | | |
| | 27 | 189 | | const errors: MapFileError[] = []; |
| | 27 | 190 | | for (const [field, validProviders] of Object.entries( |
| | | 191 | | PROVIDER_SPECIFIC_FIELDS, |
| | | 192 | | )) { |
| | 135 | 193 | | const setting = effective[field]; |
| | 135 | 194 | | if (setting === undefined || validProviders.includes(provider.value)) { |
| | 132 | 195 | | continue; |
| | | 196 | | } |
| | 3 | 197 | | const usable = validProviders.filter((name) => |
| | 4 | 198 | | SUPPORTED_PROVIDERS.includes(name), |
| | | 199 | | ); |
| | 3 | 200 | | errors.push({ |
| | | 201 | | key: setting.source, |
| | | 202 | | message: |
| | | 203 | | `${setting.source} is only supported with the` + |
| | | 204 | | ` ${(usable.length > 0 ? usable : validProviders).join(' or ')} provider` + |
| | | 205 | | ` (current provider: ${provider.value}).`, |
| | | 206 | | }); |
| | | 207 | | } |
| | | 208 | | |
| | 27 | 209 | | const required = REQUIRED_PROVIDER_FIELDS[provider.value]; |
| | 27 | 210 | | if (required !== undefined && effective[required] === undefined) { |
| | 3 | 211 | | errors.push({ |
| | | 212 | | key: `$config.${required}`, |
| | | 213 | | message: |
| | | 214 | | `${required} is required when using` + |
| | | 215 | | ` ${PROVIDER_LABELS[provider.value]} provider.` + |
| | | 216 | | ` Set it in $config.${required} in your map file` + |
| | | 217 | | ` or via ${OVERRIDE_METHODS[required]}.`, |
| | | 218 | | }); |
| | | 219 | | } |
| | 27 | 220 | | return errors; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | function resolveEffectiveConfig( |
| | | 224 | | fileConfig: Record<string, unknown>, |
| | | 225 | | overrides: EnvilderOptions, |
| | | 226 | | ): Record<string, SourcedValue | undefined> { |
| | 28 | 227 | | const effective: Record<string, SourcedValue | undefined> = {}; |
| | 28 | 228 | | for (const field of CONFIG_FIELDS) { |
| | 280 | 229 | | const value = fileConfig[field]; |
| | 280 | 230 | | if (typeof value === 'string') { |
| | 19 | 231 | | effective[field] = { value, source: `$config.${field}` }; |
| | | 232 | | } |
| | | 233 | | } |
| | | 234 | | // Blank overrides are ignored, as they are on a load. |
| | 28 | 235 | | for (const [field, method] of Object.entries(OVERRIDE_METHODS)) { |
| | 84 | 236 | | const value = overrides[field as keyof EnvilderOptions]?.trim(); |
| | 84 | 237 | | if (value) { |
| | 5 | 238 | | effective[field] = { |
| | | 239 | | value: field === 'provider' ? value.toLowerCase() : value, |
| | | 240 | | source: method, |
| | | 241 | | }; |
| | | 242 | | } |
| | | 243 | | } |
| | 28 | 244 | | return effective; |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | function isJsonObject(value: unknown): value is Record<string, unknown> { |
| | 41 | 248 | | return typeof value === 'object' && value !== null && !Array.isArray(value); |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | function isUri(value: string): boolean { |
| | 6 | 252 | | return /^[A-Za-z][A-Za-z0-9+.-]*:\S*$/.test(value) && URL.canParse(value); |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | function describeType(value: unknown): string { |
| | 4 | 256 | | if (value === null) { |
| | 0 | 257 | | return 'null'; |
| | | 258 | | } |
| | 4 | 259 | | if (Array.isArray(value)) { |
| | 0 | 260 | | return 'an array'; |
| | | 261 | | } |
| | 4 | 262 | | return typeof value === 'object' ? 'an object' : `a ${typeof value}`; |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | /** |
| | | 266 | | * Keys are untrusted input: anything outside a plain identifier is quoted, so |
| | | 267 | | * a key carrying a line break cannot split a message. |
| | | 268 | | */ |
| | | 269 | | function formatKey(key: string): string { |
| | 7 | 270 | | return /^[A-Za-z0-9_$./-]+$/.test(key) ? key : quoteName(key); |
| | | 271 | | } |