|
| 1 | +/** |
| 2 | + * Config field name normalization for warehouse drivers. |
| 3 | + * |
| 4 | + * The warehouse_add tool takes a generic Record<string, unknown> config. |
| 5 | + * LLMs, dbt profiles, and SDK conventions use different field names for the |
| 6 | + * same config values. This module normalizes them to canonical snake_case |
| 7 | + * names before the config reaches the driver. |
| 8 | + */ |
| 9 | + |
| 10 | +import type { ConnectionConfig } from "./types" |
| 11 | + |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | +// Per-driver alias maps |
| 14 | +// --------------------------------------------------------------------------- |
| 15 | +// Key = canonical field name, Value = list of aliases (checked in order). |
| 16 | +// First-value-wins: if the canonical field is already present, aliases are |
| 17 | +// ignored. |
| 18 | + |
| 19 | +type AliasMap = Record<string, string[]> |
| 20 | + |
| 21 | +/** Aliases common to most drivers. */ |
| 22 | +const COMMON_ALIASES: AliasMap = { |
| 23 | + user: ["username"], |
| 24 | + database: ["dbname", "db"], |
| 25 | +} |
| 26 | + |
| 27 | +const SNOWFLAKE_ALIASES: AliasMap = { |
| 28 | + ...COMMON_ALIASES, |
| 29 | + private_key_path: ["privateKeyPath"], |
| 30 | + private_key_passphrase: ["privateKeyPassphrase"], |
| 31 | + private_key: ["privateKey"], |
| 32 | +} |
| 33 | + |
| 34 | +const BIGQUERY_ALIASES: AliasMap = { |
| 35 | + project: ["projectId", "project_id"], |
| 36 | + credentials_path: ["keyfile", "keyFilename", "key_file", "keyFile"], |
| 37 | + credentials_json: ["keyfile_json", "keyfileJson"], |
| 38 | + dataset: ["defaultDataset", "default_dataset"], |
| 39 | +} |
| 40 | + |
| 41 | +const DATABRICKS_ALIASES: AliasMap = { |
| 42 | + server_hostname: ["host", "serverHostname"], |
| 43 | + http_path: ["httpPath"], |
| 44 | + access_token: ["token", "personal_access_token", "personalAccessToken"], |
| 45 | +} |
| 46 | + |
| 47 | +const POSTGRES_ALIASES: AliasMap = { |
| 48 | + ...COMMON_ALIASES, |
| 49 | + connection_string: ["connectionString"], |
| 50 | +} |
| 51 | + |
| 52 | +const REDSHIFT_ALIASES: AliasMap = { |
| 53 | + ...COMMON_ALIASES, |
| 54 | + connection_string: ["connectionString"], |
| 55 | +} |
| 56 | + |
| 57 | +const MYSQL_ALIASES: AliasMap = { |
| 58 | + ...COMMON_ALIASES, |
| 59 | +} |
| 60 | + |
| 61 | +const SQLSERVER_ALIASES: AliasMap = { |
| 62 | + ...COMMON_ALIASES, |
| 63 | + host: ["server", "serverName", "server_name"], |
| 64 | + trust_server_certificate: ["trustServerCertificate"], |
| 65 | +} |
| 66 | + |
| 67 | +const ORACLE_ALIASES: AliasMap = { |
| 68 | + ...COMMON_ALIASES, |
| 69 | + connection_string: ["connectString", "connect_string", "connectionString"], |
| 70 | + service_name: ["serviceName"], |
| 71 | +} |
| 72 | + |
| 73 | +/** Map of warehouse type to its alias map. */ |
| 74 | +const DRIVER_ALIASES: Record<string, AliasMap> = { |
| 75 | + snowflake: SNOWFLAKE_ALIASES, |
| 76 | + bigquery: BIGQUERY_ALIASES, |
| 77 | + databricks: DATABRICKS_ALIASES, |
| 78 | + postgres: POSTGRES_ALIASES, |
| 79 | + postgresql: POSTGRES_ALIASES, |
| 80 | + redshift: REDSHIFT_ALIASES, |
| 81 | + mysql: MYSQL_ALIASES, |
| 82 | + mariadb: MYSQL_ALIASES, |
| 83 | + sqlserver: SQLSERVER_ALIASES, |
| 84 | + mssql: SQLSERVER_ALIASES, |
| 85 | + oracle: ORACLE_ALIASES, |
| 86 | + // duckdb and sqlite have simple configs — no aliases needed |
| 87 | +} |
| 88 | + |
| 89 | +// --------------------------------------------------------------------------- |
| 90 | +// Core logic |
| 91 | +// --------------------------------------------------------------------------- |
| 92 | + |
| 93 | +/** |
| 94 | + * Apply alias mappings to a config object. |
| 95 | + * For each canonical field: if it is not already set, check each alias in |
| 96 | + * order and use the first non-undefined value. Consumed aliases are removed |
| 97 | + * from the output to avoid passing unexpected fields to the driver. |
| 98 | + */ |
| 99 | +function applyAliases(config: ConnectionConfig, aliases: AliasMap): ConnectionConfig { |
| 100 | + const result = { ...config } |
| 101 | + |
| 102 | + for (const [canonical, aliasList] of Object.entries(aliases)) { |
| 103 | + let resolved = result[canonical] !== undefined |
| 104 | + for (const alias of aliasList) { |
| 105 | + if (result[alias] !== undefined) { |
| 106 | + if (!resolved) { |
| 107 | + result[canonical] = result[alias] |
| 108 | + resolved = true |
| 109 | + } |
| 110 | + delete result[alias] |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return result |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Handle Snowflake private_key: if the value doesn't look like a file path, |
| 120 | + * keep it as private_key (inline PEM). If it looks like a path, move it to |
| 121 | + * private_key_path. |
| 122 | + */ |
| 123 | +function normalizeSnowflakePrivateKey(config: ConnectionConfig): ConnectionConfig { |
| 124 | + const pk = config.private_key |
| 125 | + if (typeof pk !== "string" || !pk) return config |
| 126 | + if (config.private_key_path) return config // already have a path |
| 127 | + |
| 128 | + // Inline PEM starts with "-----BEGIN" or contains newlines |
| 129 | + if (pk.includes("-----BEGIN") || pk.includes("\n")) { |
| 130 | + return config // keep as private_key (inline PEM) |
| 131 | + } |
| 132 | + |
| 133 | + // Looks like a file path |
| 134 | + if (pk.includes("/") || pk.includes("\\") || pk.endsWith(".pem") || pk.endsWith(".p8")) { |
| 135 | + const result = { ...config } |
| 136 | + result.private_key_path = pk |
| 137 | + delete result.private_key |
| 138 | + return result |
| 139 | + } |
| 140 | + |
| 141 | + return config |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * Normalize a connection config by resolving field name aliases. |
| 146 | + * Returns a new config object; does not mutate the input. |
| 147 | + * Unknown types pass through unchanged. |
| 148 | + */ |
| 149 | +export function normalizeConfig(config: ConnectionConfig): ConnectionConfig { |
| 150 | + const type = config.type?.toLowerCase() |
| 151 | + if (!type) return config |
| 152 | + |
| 153 | + const aliases = DRIVER_ALIASES[type] |
| 154 | + let result = aliases ? applyAliases(config, aliases) : { ...config } |
| 155 | + |
| 156 | + // Type-specific post-processing |
| 157 | + // Note: MySQL SSL fields (ssl_ca, ssl_cert, ssl_key) are NOT constructed |
| 158 | + // into an ssl object here. They stay as top-level fields so the credential |
| 159 | + // store can detect and strip them. The MySQL driver constructs the ssl |
| 160 | + // object at connection time. |
| 161 | + if (type === "snowflake") { |
| 162 | + result = normalizeSnowflakePrivateKey(result) |
| 163 | + } |
| 164 | + |
| 165 | + return result |
| 166 | +} |
0 commit comments