Skip to content

Commit 2bbfec8

Browse files
anandgupta42claude
andcommitted
feat: [Phase 2] connection manager + 10 Node.js database drivers
Replace Python ConnectionRegistry, credential store, SSH tunneling, Docker discovery, and dbt profiles parser with native TypeScript implementations. Core infrastructure: - connections/registry.ts — ConnectionRegistry with lazy driver loading - connections/credential-store.ts — keytar with graceful fallback - connections/ssh-tunnel.ts — ssh2-based tunnel with process exit cleanup - connections/docker-discovery.ts — detect postgres/mysql/mssql containers - connections/dbt-profiles.ts — parse ~/.dbt/profiles.yml with Jinja env_var() - connections/types.ts — shared Connector interface - connections/register.ts — registers 9 dispatcher methods 10 database drivers (all lazy-loaded via dynamic import): - postgres.ts (pg), redshift.ts (pg), mysql.ts (mysql2) - snowflake.ts (snowflake-sdk), bigquery.ts (@google-cloud/bigquery) - databricks.ts (@databricks/sql), sqlserver.ts (mssql) - oracle.ts (oracledb thin), duckdb.ts (duckdb), sqlite.ts (better-sqlite3) Dispatcher methods registered: - sql.execute, sql.explain, warehouse.list, warehouse.test - warehouse.add, warehouse.remove, warehouse.discover - schema.inspect, dbt.profiles Add yaml dependency for dbt profiles parsing. Add 36 unit tests covering registry, credentials, dbt profiles, docker discovery, dispatcher integration, and DuckDB driver. Closes #217 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 13681c9 commit 2bbfec8

21 files changed

Lines changed: 2949 additions & 0 deletions

‎bun.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/opencode/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
"web-tree-sitter": "0.25.10",
131131
"which": "6.0.1",
132132
"xdg-basedir": "5.1.0",
133+
"yaml": "2.8.2",
133134
"yargs": "18.0.0",
134135
"zod": "catalog:",
135136
"zod-to-json-schema": "3.24.5"
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Credential management for connection configs.
3+
*
4+
* Tries to use `keytar` for OS keychain storage. If keytar is unavailable,
5+
* falls back to storing credentials in the config JSON with a warning.
6+
*/
7+
8+
import { Log } from "../../../util/log"
9+
import type { ConnectionConfig } from "./types"
10+
11+
const SERVICE_NAME = "altimate-code"
12+
13+
const SENSITIVE_FIELDS = new Set([
14+
"password",
15+
"private_key_passphrase",
16+
"access_token",
17+
"ssh_password",
18+
"connection_string",
19+
])
20+
21+
/** Cached keytar module (or null if unavailable). */
22+
let keytarModule: any | null | undefined = undefined
23+
24+
async function getKeytar(): Promise<any | null> {
25+
if (keytarModule !== undefined) return keytarModule
26+
try {
27+
// @ts-expect-error — optional dependency, loaded at runtime
28+
keytarModule = await import("keytar")
29+
return keytarModule
30+
} catch {
31+
Log.Default.warn(
32+
"keytar not available — credentials will be stored in config JSON (not encrypted)",
33+
)
34+
keytarModule = null
35+
return null
36+
}
37+
}
38+
39+
/** Store a single credential in the OS keychain (or return false if unavailable). */
40+
export async function storeCredential(
41+
connectionName: string,
42+
field: string,
43+
value: string,
44+
): Promise<boolean> {
45+
const keytar = await getKeytar()
46+
if (!keytar) return false
47+
const account = `${connectionName}/${field}`
48+
await keytar.setPassword(SERVICE_NAME, account, value)
49+
return true
50+
}
51+
52+
/** Retrieve a single credential from the OS keychain (or return null). */
53+
export async function getCredential(
54+
connectionName: string,
55+
field: string,
56+
): Promise<string | null> {
57+
const keytar = await getKeytar()
58+
if (!keytar) return null
59+
const account = `${connectionName}/${field}`
60+
return keytar.getPassword(SERVICE_NAME, account)
61+
}
62+
63+
/** Delete a single credential from the OS keychain. */
64+
export async function deleteCredential(
65+
connectionName: string,
66+
field: string,
67+
): Promise<boolean> {
68+
const keytar = await getKeytar()
69+
if (!keytar) return false
70+
const account = `${connectionName}/${field}`
71+
return keytar.deletePassword(SERVICE_NAME, account)
72+
}
73+
74+
/**
75+
* Resolve a connection config by pulling sensitive fields from the keychain.
76+
* If keytar is unavailable, returns the config as-is (credentials stay in JSON).
77+
*/
78+
export async function resolveConfig(
79+
name: string,
80+
config: ConnectionConfig,
81+
): Promise<ConnectionConfig> {
82+
const resolved = { ...config }
83+
for (const field of SENSITIVE_FIELDS) {
84+
if (resolved[field]) continue // already present in config
85+
const stored = await getCredential(name, field)
86+
if (stored) {
87+
resolved[field] = stored
88+
}
89+
}
90+
return resolved
91+
}
92+
93+
/**
94+
* Save a connection config, extracting sensitive fields to the keychain.
95+
* Returns the sanitized config (sensitive fields removed if stored in keychain).
96+
*/
97+
export async function saveConnection(
98+
name: string,
99+
config: ConnectionConfig,
100+
): Promise<ConnectionConfig> {
101+
const sanitized = { ...config }
102+
for (const field of SENSITIVE_FIELDS) {
103+
const value = config[field]
104+
if (typeof value !== "string" || !value) continue
105+
const stored = await storeCredential(name, field, value)
106+
if (stored) {
107+
delete sanitized[field]
108+
}
109+
// If keytar unavailable, credential stays in the JSON config
110+
}
111+
return sanitized
112+
}
113+
114+
/** Check if a field is sensitive. */
115+
export function isSensitiveField(field: string): boolean {
116+
return SENSITIVE_FIELDS.has(field)
117+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/**
2+
* dbt profiles.yml parser.
3+
*
4+
* Reads ~/.dbt/profiles.yml and converts dbt connection configs
5+
* into altimate connection configs.
6+
*/
7+
8+
import * as fs from "fs"
9+
import * as path from "path"
10+
import * as os from "os"
11+
import type { DbtProfileConnection } from "../../bridge/protocol"
12+
import type { ConnectionConfig } from "./types"
13+
14+
/** Map dbt adapter types to altimate connector types. */
15+
const ADAPTER_TYPE_MAP: Record<string, string> = {
16+
postgres: "postgres",
17+
redshift: "redshift",
18+
snowflake: "snowflake",
19+
bigquery: "bigquery",
20+
databricks: "databricks",
21+
duckdb: "duckdb",
22+
mysql: "mysql",
23+
sqlserver: "sqlserver",
24+
oracle: "oracle",
25+
sqlite: "sqlite",
26+
spark: "databricks",
27+
trino: "postgres", // wire-compatible
28+
}
29+
30+
/** Map dbt config keys to altimate config keys. */
31+
const KEY_MAP: Record<string, string> = {
32+
dbname: "database",
33+
db: "database",
34+
server: "host",
35+
hostname: "host",
36+
server_hostname: "server_hostname",
37+
http_path: "http_path",
38+
token: "access_token",
39+
private_key_path: "private_key_path",
40+
private_key_passphrase: "private_key_passphrase",
41+
keyfile: "credentials_path",
42+
keyfile_json: "credentials_json",
43+
project: "project",
44+
dataset: "dataset",
45+
location: "location",
46+
threads: "", // skip — empty string signals "don't map"
47+
method: "", // skip
48+
}
49+
50+
/** Resolve Jinja {{ env_var('NAME') }} and {{ env_var('NAME', 'default') }} patterns. */
51+
function resolveEnvVars(value: unknown): unknown {
52+
if (typeof value !== "string") return value
53+
return value.replace(
54+
/\{\{\s*env_var\s*\(\s*'([^']+)'\s*(?:,\s*'([^']*)'\s*)?\)\s*\}\}/g,
55+
(_match, envName: string, defaultValue?: string) => {
56+
return process.env[envName] ?? defaultValue ?? ""
57+
},
58+
)
59+
}
60+
61+
/** Recursively resolve env vars in a config object. */
62+
function resolveEnvVarsDeep(obj: Record<string, unknown>): Record<string, unknown> {
63+
const result: Record<string, unknown> = {}
64+
for (const [key, value] of Object.entries(obj)) {
65+
if (value && typeof value === "object" && !Array.isArray(value)) {
66+
result[key] = resolveEnvVarsDeep(value as Record<string, unknown>)
67+
} else {
68+
result[key] = resolveEnvVars(value)
69+
}
70+
}
71+
return result
72+
}
73+
74+
/** Convert a dbt output config to an altimate ConnectionConfig. */
75+
function mapConfig(
76+
dbtType: string,
77+
dbtConfig: Record<string, unknown>,
78+
): ConnectionConfig {
79+
const type = ADAPTER_TYPE_MAP[dbtType] ?? dbtType
80+
const config: ConnectionConfig = { type }
81+
82+
for (const [dbtKey, value] of Object.entries(dbtConfig)) {
83+
if (value === undefined || value === null) continue
84+
const mappedKey = KEY_MAP[dbtKey]
85+
if (mappedKey === "" || (mappedKey === undefined && dbtKey in KEY_MAP)) continue // explicitly skipped
86+
const targetKey = mappedKey ?? dbtKey
87+
config[targetKey] = resolveEnvVars(value)
88+
}
89+
90+
// Ensure type is set from mapping
91+
config.type = type
92+
93+
return config
94+
}
95+
96+
/**
97+
* Parse dbt profiles.yml and return discovered connections.
98+
*
99+
* @param profilesPath - Path to profiles.yml. Defaults to ~/.dbt/profiles.yml
100+
*/
101+
export async function parseDbtProfiles(
102+
profilesPath?: string,
103+
): Promise<DbtProfileConnection[]> {
104+
const resolvedPath =
105+
profilesPath ?? path.join(os.homedir(), ".dbt", "profiles.yml")
106+
107+
if (!fs.existsSync(resolvedPath)) {
108+
return []
109+
}
110+
111+
let parseYaml: (content: string) => any
112+
try {
113+
// Try `yaml` package first (more common in Node/Bun ecosystems)
114+
const yamlMod = await import("yaml")
115+
const yamlLib = yamlMod.default || yamlMod
116+
parseYaml = (content: string) => yamlLib.parse(content)
117+
} catch {
118+
try {
119+
// Fall back to `js-yaml`
120+
// @ts-expect-error — optional fallback dependency
121+
const jsYaml = await import("js-yaml")
122+
const jsYamlLib = jsYaml.default || jsYaml
123+
parseYaml = (content: string) => jsYamlLib.load(content)
124+
} catch {
125+
return []
126+
}
127+
}
128+
129+
const content = fs.readFileSync(resolvedPath, "utf-8")
130+
let profiles: Record<string, any>
131+
try {
132+
profiles = parseYaml(content) as Record<string, any>
133+
} catch {
134+
return []
135+
}
136+
137+
if (!profiles || typeof profiles !== "object") return []
138+
139+
const connections: DbtProfileConnection[] = []
140+
141+
for (const [profileName, profile] of Object.entries(profiles)) {
142+
if (!profile || typeof profile !== "object") continue
143+
// Skip config key (not a profile)
144+
if (profileName === "config") continue
145+
146+
const outputs = (profile as Record<string, any>).outputs
147+
if (!outputs || typeof outputs !== "object") continue
148+
149+
for (const [outputName, output] of Object.entries(
150+
outputs as Record<string, any>,
151+
)) {
152+
if (!output || typeof output !== "object") continue
153+
const rawConfig = resolveEnvVarsDeep(output as Record<string, unknown>)
154+
const dbtType = (rawConfig.type as string) ?? "unknown"
155+
const config = mapConfig(dbtType, rawConfig)
156+
157+
connections.push({
158+
name: `${profileName}_${outputName}`,
159+
type: config.type,
160+
config: config as Record<string, unknown>,
161+
})
162+
}
163+
}
164+
165+
return connections
166+
}
167+
168+
/**
169+
* Convert DbtProfileConnection array to a map of ConnectionConfigs.
170+
*/
171+
export function dbtConnectionsToConfigs(
172+
connections: DbtProfileConnection[],
173+
): Record<string, ConnectionConfig> {
174+
const result: Record<string, ConnectionConfig> = {}
175+
for (const conn of connections) {
176+
result[conn.name] = conn.config as ConnectionConfig
177+
}
178+
return result
179+
}

0 commit comments

Comments
 (0)