|
| 1 | +import { existsSync, readFileSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import type { IntegrationManifest } from "@openmapx/integration-framework"; |
| 4 | +import { eq } from "drizzle-orm"; |
| 5 | +import { db } from "./db"; |
| 6 | +import { integrationConfig } from "./db/schema"; |
| 7 | +import { resolveVaultSecrets } from "./services/secrets"; |
| 8 | + |
| 9 | +export type ConfigSource = "default" | "database" | "vault" | "config.json" | "env"; |
| 10 | + |
| 11 | +export interface ConfigValueWithSource { |
| 12 | + value: unknown; |
| 13 | + source: ConfigSource; |
| 14 | +} |
| 15 | + |
| 16 | +export async function resolveConfigWithSources( |
| 17 | + manifest: IntegrationManifest, |
| 18 | + directory: string, |
| 19 | +): Promise<Record<string, ConfigValueWithSource>> { |
| 20 | + const result: Record<string, ConfigValueWithSource> = {}; |
| 21 | + const schema = manifest.configSchema as Record<string, unknown> | undefined; |
| 22 | + const knownKeys = new Set<string>(); |
| 23 | + // Uppercased config key → canonical (original-case) key. Used to match env |
| 24 | + // vars like `INTEGRATION_PHOTOS_FLICKR_APIKEY` against configSchema key |
| 25 | + // `apiKey` without forcing operators to lowercase the suffix (or forcing |
| 26 | + // schema authors to pick all-lowercase keys). |
| 27 | + const upperToKey = new Map<string, string>(); |
| 28 | + |
| 29 | + if (schema) { |
| 30 | + const props = (schema.properties ?? schema) as Record<string, { default?: unknown }>; |
| 31 | + for (const [key, def] of Object.entries(props)) { |
| 32 | + if (key === "type" || key === "properties") continue; |
| 33 | + knownKeys.add(key); |
| 34 | + upperToKey.set(key.toUpperCase(), key); |
| 35 | + if (def && typeof def === "object" && "default" in def && def.default !== undefined) { |
| 36 | + result[key] = { value: def.default, source: "default" }; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if (knownKeys.size === 0) return result; |
| 42 | + |
| 43 | + try { |
| 44 | + const [row] = await db |
| 45 | + .select({ config: integrationConfig.config }) |
| 46 | + .from(integrationConfig) |
| 47 | + .where(eq(integrationConfig.integrationId, manifest.id)) |
| 48 | + .limit(1); |
| 49 | + if (row?.config && typeof row.config === "object") { |
| 50 | + for (const [key, value] of Object.entries(row.config as Record<string, unknown>)) { |
| 51 | + if (knownKeys.has(key)) result[key] = { value, source: "database" }; |
| 52 | + } |
| 53 | + } |
| 54 | + } catch { |
| 55 | + // DB not available |
| 56 | + } |
| 57 | + |
| 58 | + // 3. Apply vault secrets |
| 59 | + try { |
| 60 | + const vaultSecrets = await resolveVaultSecrets(manifest.id); |
| 61 | + for (const [key, value] of Object.entries(vaultSecrets)) { |
| 62 | + if (knownKeys.has(key)) result[key] = { value, source: "vault" }; |
| 63 | + } |
| 64 | + } catch { |
| 65 | + // vault unavailable |
| 66 | + } |
| 67 | + |
| 68 | + const configJsonPath = join(directory, "config.json"); |
| 69 | + if (existsSync(configJsonPath)) { |
| 70 | + try { |
| 71 | + const fileConfig = JSON.parse(readFileSync(configJsonPath, "utf-8")); |
| 72 | + if (typeof fileConfig === "object" && fileConfig !== null) { |
| 73 | + for (const [key, value] of Object.entries(fileConfig as Record<string, unknown>)) { |
| 74 | + if (knownKeys.has(key)) result[key] = { value, source: "config.json" }; |
| 75 | + } |
| 76 | + } |
| 77 | + } catch { |
| 78 | + // ignore |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Env layer — highest priority. Pattern: `INTEGRATION_<ID>_<KEY>` (upper-cased |
| 83 | + // id with hyphens replaced by underscores, then the upper-cased config key). |
| 84 | + // Matching is case-insensitive on the configSchema key so both snake_case |
| 85 | + // and camelCase keys work (`apiKey` matches `INTEGRATION_X_APIKEY`). |
| 86 | + const prefix = `INTEGRATION_${manifest.id.replace(/-/g, "_").toUpperCase()}_`; |
| 87 | + for (const [envKey, envVal] of Object.entries(process.env)) { |
| 88 | + if (envVal === undefined) continue; |
| 89 | + if (!envKey.startsWith(prefix)) continue; |
| 90 | + const rest = envKey.slice(prefix.length); |
| 91 | + const canonical = upperToKey.get(rest); |
| 92 | + if (canonical) result[canonical] = { value: envVal, source: "env" }; |
| 93 | + } |
| 94 | + |
| 95 | + return result; |
| 96 | +} |
| 97 | + |
| 98 | +export async function resolveConfig( |
| 99 | + manifest: { |
| 100 | + id: string; |
| 101 | + configSchema?: Record<string, unknown>; |
| 102 | + }, |
| 103 | + directory: string, |
| 104 | +): Promise<Record<string, unknown>> { |
| 105 | + const withSources = await resolveConfigWithSources(manifest as IntegrationManifest, directory); |
| 106 | + const config: Record<string, unknown> = {}; |
| 107 | + for (const [key, entry] of Object.entries(withSources)) { |
| 108 | + config[key] = entry.value; |
| 109 | + } |
| 110 | + return config; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Emits advisory warnings for required/enum config keys that violate the |
| 115 | + * manifest's `configSchema`. Never blocks load. Shared by cold start and reload. |
| 116 | + */ |
| 117 | +export function warnInvalidConfig( |
| 118 | + manifest: IntegrationManifest, |
| 119 | + config: Record<string, unknown>, |
| 120 | + id: string, |
| 121 | + warn: (msg: string) => void, |
| 122 | +): void { |
| 123 | + const configSchema = manifest.configSchema as Record<string, unknown> | undefined; |
| 124 | + if (!configSchema?.properties) return; |
| 125 | + const props = configSchema.properties as Record< |
| 126 | + string, |
| 127 | + { type?: string; enum?: unknown[]; required?: boolean } |
| 128 | + >; |
| 129 | + for (const [key, def] of Object.entries(props)) { |
| 130 | + if (def.required && config[key] === undefined) { |
| 131 | + warn(`Integration ${id}: missing required config key "${key}"`); |
| 132 | + } |
| 133 | + if (def.enum && config[key] !== undefined && !def.enum.includes(config[key])) { |
| 134 | + warn( |
| 135 | + `Integration ${id}: config "${key}" value "${config[key]}" not in allowed values: ${def.enum.join(", ")}`, |
| 136 | + ); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments