| | | 1 | | import { |
| | | 2 | | invalidEnvironmentVariableNameMessage, |
| | | 3 | | isValidEnvironmentVariableName, |
| | | 4 | | } from '../domain/environment-variable-name.js'; |
| | | 5 | | import type { MapFileConfig } from '../domain/map-file-config.js'; |
| | | 6 | | import type { ParsedMapFile } from '../domain/parsed-map-file.js'; |
| | | 7 | | import { SecretProviderType } from '../domain/secret-provider-type.js'; |
| | | 8 | | |
| | 4 | 9 | | const CONFIG_KEY = '$config'; |
| | | 10 | | |
| | 4 | 11 | | const PROVIDER_MAP: Record<string, SecretProviderType> = { |
| | | 12 | | aws: SecretProviderType.Aws, |
| | | 13 | | azure: SecretProviderType.Azure, |
| | | 14 | | }; |
| | | 15 | | |
| | | 16 | | /** |
| | | 17 | | * Parses a JSON map-file string into a {@link ParsedMapFile}. |
| | | 18 | | */ |
| | | 19 | | export class MapFileParser { |
| | | 20 | | /** |
| | | 21 | | * Parse a JSON map-file string, extracting `$config` and variable mappings. |
| | | 22 | | * |
| | | 23 | | * @param json - Raw JSON string of the map file. |
| | | 24 | | * @returns Parsed config and variable mappings. |
| | | 25 | | */ |
| | | 26 | | parse(json: string): ParsedMapFile { |
| | 54 | 27 | | const raw = this.parseRawJson(json); |
| | 54 | 28 | | const mappings = new Map<string, string>(); |
| | 54 | 29 | | let config: MapFileConfig = {}; |
| | | 30 | | |
| | 54 | 31 | | for (const [key, value] of Object.entries(raw)) { |
| | 75 | 32 | | if (key.startsWith('$')) { |
| | 10 | 33 | | if (key === CONFIG_KEY) { |
| | 7 | 34 | | config = this.parseConfig(value); |
| | | 35 | | } |
| | 9 | 36 | | continue; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | // Before the non-string filter, so the name rule covers every mapping |
| | | 40 | | // key the file declares, matching the CLI and the published schema. |
| | 65 | 41 | | if (!isValidEnvironmentVariableName(key)) { |
| | 35 | 42 | | throw new Error(invalidEnvironmentVariableNameMessage(key)); |
| | | 43 | | } |
| | | 44 | | |
| | 30 | 45 | | if (typeof value === 'string') { |
| | 28 | 46 | | mappings.set(key, value); |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | 17 | 50 | | return { config, mappings }; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | private parseRawJson(json: string): Record<string, unknown> { |
| | | 54 | | let raw: unknown; |
| | 54 | 55 | | try { |
| | 54 | 56 | | raw = JSON.parse(json); |
| | | 57 | | } catch { |
| | 0 | 58 | | throw new Error('Invalid map file: content is not valid JSON'); |
| | | 59 | | } |
| | 54 | 60 | | if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) { |
| | 1 | 61 | | throw new Error('Invalid map file: root must be a JSON object'); |
| | | 62 | | } |
| | 53 | 63 | | return raw as Record<string, unknown>; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | private parseConfig(value: unknown): MapFileConfig { |
| | 7 | 67 | | if (typeof value !== 'object' || value === null) { |
| | 0 | 68 | | return {}; |
| | | 69 | | } |
| | 7 | 70 | | const obj = value as Record<string, unknown>; |
| | | 71 | | const providerStr = |
| | 7 | 72 | | typeof obj.provider === 'string' ? obj.provider.toLowerCase() : undefined; |
| | 7 | 73 | | const config: MapFileConfig = { |
| | | 74 | | provider: providerStr ? PROVIDER_MAP[providerStr] : undefined, |
| | | 75 | | vaultUrl: typeof obj.vaultUrl === 'string' ? obj.vaultUrl : undefined, |
| | | 76 | | profile: typeof obj.profile === 'string' ? obj.profile : undefined, |
| | | 77 | | }; |
| | | 78 | | |
| | 7 | 79 | | if (providerStr && !config.provider) { |
| | 1 | 80 | | throw new Error( |
| | | 81 | | `Unknown provider: '${obj.provider}'. Supported: aws, azure`, |
| | | 82 | | ); |
| | | 83 | | } |
| | | 84 | | |
| | 6 | 85 | | return config; |
| | | 86 | | } |
| | | 87 | | } |