Skip to content

Commit d085de8

Browse files
anandgupta42claude
andcommitted
fix: harden auth field handling for all warehouse drivers (#267)
Add config field name normalization so LLM-generated, dbt, and SDK field names all resolve to canonical snake_case before reaching drivers. - Add `normalizeConfig()` in `@altimateai/drivers` with per-driver alias maps (Snowflake, BigQuery, Databricks, Redshift, PostgreSQL, MySQL, SQL Server, Oracle) and common aliases (`username` → `user`, `dbname` → `database`) - Call `normalizeConfig()` before both `resolveConfig()` and `saveConnection()` in registry so credential store uses canonical names - Add Snowflake inline PEM support via `config.private_key` - Add BigQuery `credentials_json` (inline JSON) support - Add MySQL SSL construction from top-level `ssl_ca`/`ssl_cert`/`ssl_key` fields at connection time (not during normalization, to preserve credential store detection) - Expand `SENSITIVE_FIELDS` with `private_key`, `token`, `credentials_json`, `keyfile_json`, `ssl_key`, `ssl_cert`, `ssl_ca` - Update `sensitiveKeys` in `project-scan.ts` to match - Fix `detectAuthMethod()` to recognize inline `private_key` - Update `warehouse_add` tool description with per-warehouse field docs - Add 72 unit tests for normalization logic Closes #267 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 045b3df commit d085de8

10 files changed

Lines changed: 895 additions & 13 deletions

File tree

‎packages/drivers/src/bigquery.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
2121
async connect() {
2222
const options: Record<string, unknown> = {}
2323
if (config.project) options.projectId = config.project
24-
if (config.credentials_path) options.keyFilename = config.credentials_path
24+
if (config.credentials_json) {
25+
try {
26+
options.credentials = typeof config.credentials_json === "string"
27+
? JSON.parse(config.credentials_json as string)
28+
: config.credentials_json
29+
} catch (e) {
30+
throw new Error(`Failed to parse credentials_json: ${e}`)
31+
}
32+
} else if (config.credentials_path) {
33+
options.keyFilename = config.credentials_path
34+
}
2535
if (config.location) options.location = config.location
2636

2737
client = new BigQuery(options)

‎packages/drivers/src/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ export type { Connector, ConnectorResult, SchemaColumn, ConnectionConfig } from
44
// Re-export escape utilities
55
export { escapeSqlString, escapeSqlIdentifier } from "./sql-escape"
66

7+
// Re-export config normalization
8+
export { normalizeConfig } from "./normalize"
9+
710
// Re-export driver connect functions
811
export { connect as connectPostgres } from "./postgres"
912
export { connect as connectSnowflake } from "./snowflake"

‎packages/drivers/src/mysql.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
3030

3131
if (config.ssl !== undefined) {
3232
poolConfig.ssl = config.ssl
33+
} else if (config.ssl_ca || config.ssl_cert || config.ssl_key) {
34+
const sslObj: Record<string, unknown> = {}
35+
if (config.ssl_ca) sslObj.ca = config.ssl_ca
36+
if (config.ssl_cert) sslObj.cert = config.ssl_cert
37+
if (config.ssl_key) sslObj.key = config.ssl_key
38+
poolConfig.ssl = sslObj
3339
}
3440

3541
pool = mysql.createPool(poolConfig)

‎packages/drivers/src/normalize.ts‎

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
}

‎packages/drivers/src/snowflake.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,27 @@ export async function connect(config: ConnectionConfig): Promise<Connector> {
4141
async connect() {
4242
const options: Record<string, unknown> = {
4343
account: config.account,
44-
username: config.user ?? config.username,
44+
username: config.user,
4545
database: config.database,
4646
schema: config.schema,
4747
warehouse: config.warehouse,
4848
role: config.role,
4949
}
5050

51-
// Key-pair auth
52-
if (config.private_key_path) {
53-
const keyPath = config.private_key_path as string
54-
if (!fs.existsSync(keyPath)) {
55-
throw new Error(`Snowflake private key file not found: ${keyPath}`)
51+
// Key-pair auth (file path or inline PEM)
52+
const keyPath = config.private_key_path as string | undefined
53+
const inlinePem = config.private_key as string | undefined
54+
55+
if (keyPath || inlinePem) {
56+
let keyContent: string
57+
if (keyPath) {
58+
if (!fs.existsSync(keyPath)) {
59+
throw new Error(`Snowflake private key file not found: ${keyPath}`)
60+
}
61+
keyContent = fs.readFileSync(keyPath, "utf-8")
62+
} else {
63+
keyContent = inlinePem!
5664
}
57-
const keyContent = fs.readFileSync(keyPath, "utf-8")
5865

5966
// If key is encrypted (has ENCRYPTED in header or passphrase provided),
6067
// decrypt it using Node crypto — snowflake-sdk expects unencrypted PEM.

‎packages/opencode/src/altimate/native/connections/credential-store.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@ const SERVICE_NAME = "altimate-code"
1515
const SENSITIVE_FIELDS = new Set([
1616
"password",
1717
"private_key_passphrase",
18+
"private_key",
1819
"access_token",
20+
"token",
1921
"ssh_password",
2022
"connection_string",
23+
"credentials_json",
24+
"keyfile_json",
25+
"ssl_key",
26+
"ssl_cert",
27+
"ssl_ca",
2128
])
2229

2330
/** Cached keytar module (or null if unavailable). */

‎packages/opencode/src/altimate/native/connections/registry.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as path from "path"
1414
import * as os from "os"
1515
import { Log } from "../../../util/log"
1616
import type { ConnectionConfig, Connector } from "@altimateai/drivers"
17+
import { normalizeConfig } from "@altimateai/drivers"
1718
import { resolveConfig, saveConnection } from "./credential-store"
1819
import { startTunnel, extractSshConfig, closeTunnel } from "./ssh-tunnel"
1920
import type { WarehouseInfo } from "../types"
@@ -138,8 +139,12 @@ async function createConnector(
138139
)
139140
}
140141

142+
// Normalize field names first (camelCase → snake_case, dbt → canonical)
143+
// so credential resolution uses canonical names for keychain lookups
144+
let resolvedConfig = normalizeConfig(config)
145+
141146
// Resolve credentials from keychain
142-
let resolvedConfig = await resolveConfig(name, config)
147+
resolvedConfig = await resolveConfig(name, resolvedConfig)
143148

144149
// Handle SSH tunnel
145150
const sshConfig = extractSshConfig(resolvedConfig)
@@ -209,7 +214,7 @@ async function createConnector(
209214
export function detectAuthMethod(config: ConnectionConfig | null | undefined): string {
210215
if (!config || typeof config !== "object") return "unknown"
211216
if (config.connection_string) return "connection_string"
212-
if (config.private_key_path) return "key_pair"
217+
if (config.private_key_path || config.private_key) return "key_pair"
213218
if (config.access_token || config.token) return "token"
214219
if (config.password) return "password"
215220
const t = typeof config.type === "string" ? config.type.toLowerCase() : ""
@@ -360,8 +365,12 @@ export async function add(
360365
try {
361366
ensureLoaded()
362367

368+
// Normalize field names before saving so sensitive fields under alias
369+
// names (e.g., keyfileJson → credentials_json) are properly detected
370+
const normalized = normalizeConfig(config)
371+
363372
// Store credentials in keychain, get sanitized config
364-
const { sanitized, warnings } = await saveConnection(name, config)
373+
const { sanitized, warnings } = await saveConnection(name, normalized)
365374

366375
// Save to global config file
367376
const globalPath = globalConfigPath()

‎packages/opencode/src/altimate/tools/project-scan.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,11 @@ export async function detectEnvVars(): Promise<EnvVarConnection[]> {
227227
const matchedSignal = wh.signals.find((s) => process.env[s])
228228
if (!matchedSignal) continue
229229

230-
const sensitiveKeys = new Set(["password", "access_token", "connection_string", "private_key_path"])
230+
const sensitiveKeys = new Set([
231+
"password", "access_token", "token", "connection_string",
232+
"private_key_path", "private_key", "private_key_passphrase",
233+
"credentials_json", "keyfile_json", "ssl_key", "ssl_cert", "ssl_ca",
234+
])
231235
const config: Record<string, string> = {}
232236
for (const [key, envNames] of Object.entries(wh.configMap)) {
233237
const names = Array.isArray(envNames) ? envNames : [envNames]

‎packages/opencode/src/altimate/tools/warehouse-add.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,18 @@ export const WarehouseAddTool = Tool.define("warehouse_add", {
1010
config: z
1111
.record(z.string(), z.unknown())
1212
.describe(
13-
'Connection configuration. Must include "type" (postgres, snowflake, duckdb, etc). Example: {"type": "postgres", "host": "localhost", "port": 5432, "database": "mydb", "user": "admin", "password": "secret"}',
13+
`Connection configuration. Must include "type". Field aliases (camelCase, dbt names) are auto-normalized. Canonical fields per type:
14+
- postgres: host, port, database, user, password, ssl, connection_string, statement_timeout
15+
- snowflake: account, user, password, database, schema, warehouse, role, private_key_path, private_key_passphrase, private_key (inline PEM)
16+
- bigquery: project, credentials_path (service account JSON file), credentials_json (inline JSON), location, dataset
17+
- databricks: server_hostname, http_path, access_token, catalog, schema
18+
- redshift: host, port, database, user, password, ssl, connection_string
19+
- mysql: host, port, database, user, password, ssl (or ssl_ca, ssl_cert, ssl_key)
20+
- sqlserver: host, port, database, user, password, encrypt, trust_server_certificate
21+
- oracle: connection_string (or host, port, service_name), user, password
22+
- duckdb: path (file path or ":memory:")
23+
- sqlite: path (file path)
24+
Example: {"type": "snowflake", "account": "xy12345.us-east-1", "user": "admin", "password": "secret", "database": "MYDB", "warehouse": "COMPUTE_WH"}`,
1425
),
1526
}),
1627
async execute(args, ctx) {

0 commit comments

Comments
 (0)