-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathsnowflake.ts
More file actions
325 lines (297 loc) · 12.7 KB
/
snowflake.ts
File metadata and controls
325 lines (297 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* Snowflake driver using the `snowflake-sdk` package.
*/
import * as fs from "fs"
import type { ConnectionConfig, Connector, ConnectorResult, ExecuteOptions, SchemaColumn } from "./types"
export async function connect(config: ConnectionConfig): Promise<Connector> {
let snowflake: any
try {
snowflake = await import("snowflake-sdk")
snowflake = snowflake.default || snowflake
} catch {
throw new Error(
"Snowflake driver not installed. Run: npm install snowflake-sdk",
)
}
// Suppress snowflake-sdk's Winston console logging — it writes JSON log
// lines into the interactive TUI output and corrupts the display.
if (typeof snowflake.configure === "function") {
try {
snowflake.configure({
logLevel: "OFF",
additionalLogToConsole: false,
})
} catch {
// Older SDK versions may not support these options; ignore.
}
}
let connection: any
function escapeSqlIdentifier(value: string): string {
return value.replace(/"/g, '""')
}
function executeQuery(sql: string, binds?: any[]): Promise<{ columns: string[]; rows: any[][] }> {
return new Promise((resolve, reject) => {
const options: Record<string, any> = {
sqlText: sql,
complete(err: Error | null, _stmt: any, rows: any[]) {
if (err) return reject(err)
if (!rows || rows.length === 0) {
return resolve({ columns: [], rows: [] })
}
const rawColumns = Object.keys(rows[0])
const columns = rawColumns.map((col) => col.toLowerCase())
const mapped = rows.map((row) =>
rawColumns.map((col) => row[col]),
)
resolve({ columns, rows: mapped })
},
}
if (binds && binds.length > 0) options.binds = binds
connection.execute(options)
})
}
return {
async connect() {
const options: Record<string, unknown> = {
account: config.account,
username: config.user ?? config.username,
database: config.database,
schema: config.schema,
warehouse: config.warehouse,
role: config.role,
}
// ---------------------------------------------------------------
// Normalize field names: accept snake_case (dbt), camelCase (SDK),
// and common LLM-generated variants so auth "just works".
// Note: normalizeConfig() in normalize.ts handles most aliases
// upstream, but these fallbacks provide defense-in-depth.
// ---------------------------------------------------------------
const keyPath = (config.private_key_path ?? config.privateKeyPath) as string | undefined
const inlineKey = (config.private_key ?? config.privateKey) as string | undefined
const keyPassphrase = (config.private_key_passphrase ?? config.privateKeyPassphrase ?? config.privateKeyPass) as string | undefined
const oauthToken = (config.token ?? config.access_token) as string | undefined
const oauthClientId = (config.oauth_client_id ?? config.oauthClientId) as string | undefined
const oauthClientSecret = (config.oauth_client_secret ?? config.oauthClientSecret) as string | undefined
const authenticator = (config.authenticator as string | undefined)?.trim()
const authUpper = authenticator?.toUpperCase()
const passcode = config.passcode as string | undefined
// ---------------------------------------------------------------
// 1. Key-pair auth (SNOWFLAKE_JWT)
// Accepts: private_key_path (file), private_key (inline PEM or
// file path auto-detected), privateKey, privateKeyPath.
// ---------------------------------------------------------------
// Resolve private_key: could be a file path or PEM content
let resolvedKeyPath = keyPath
let resolvedInlineKey = inlineKey
if (!resolvedKeyPath && resolvedInlineKey && !resolvedInlineKey.includes("-----BEGIN")) {
// Looks like a file path, not PEM content
if (fs.existsSync(resolvedInlineKey)) {
resolvedKeyPath = resolvedInlineKey
resolvedInlineKey = undefined
} else {
throw new Error(
`Snowflake private key: '${resolvedInlineKey}' is not a valid file path or PEM content. ` +
`Use 'private_key_path' for file paths or provide PEM content starting with '-----BEGIN PRIVATE KEY-----'.`,
)
}
}
if (resolvedKeyPath || resolvedInlineKey) {
let keyContent: string
if (resolvedKeyPath) {
if (!fs.existsSync(resolvedKeyPath)) {
throw new Error(`Snowflake private key file not found: ${resolvedKeyPath}`)
}
keyContent = fs.readFileSync(resolvedKeyPath, "utf-8")
} else {
keyContent = resolvedInlineKey!
// Normalize escaped newlines from env vars / JSON configs
if (keyContent.includes("\\n")) {
keyContent = keyContent.replace(/\\n/g, "\n")
}
}
// If key is encrypted, decrypt using Node crypto —
// snowflake-sdk expects unencrypted PKCS#8 PEM.
let privateKey: string
if (keyPassphrase || keyContent.includes("ENCRYPTED")) {
const crypto = await import("crypto")
try {
const keyObject = crypto.createPrivateKey({
key: keyContent,
format: "pem",
passphrase: keyPassphrase || undefined,
})
privateKey = keyObject
.export({ type: "pkcs8", format: "pem" })
.toString()
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
throw new Error(
`Snowflake: Failed to decrypt private key. Verify the passphrase and key format (must be PEM/PKCS#8). ${msg}`,
)
}
} else {
privateKey = keyContent
}
options.authenticator = "SNOWFLAKE_JWT"
options.privateKey = privateKey
// ---------------------------------------------------------------
// 2. External browser SSO
// Interactive — opens user's browser for IdP login. Requires
// connectAsync() instead of connect().
// ---------------------------------------------------------------
} else if (authUpper === "EXTERNALBROWSER") {
options.authenticator = "EXTERNALBROWSER"
// ---------------------------------------------------------------
// 3. Okta native SSO (authenticator is an Okta URL)
// ---------------------------------------------------------------
} else if (authenticator && /^https?:\/\/.+\.okta\.com/i.test(authenticator)) {
options.authenticator = authenticator
if (config.password) options.password = config.password
// ---------------------------------------------------------------
// 4. OAuth token auth
// Triggered by: authenticator="oauth", OR token/access_token
// present without a password.
// ---------------------------------------------------------------
} else if (authUpper === "OAUTH" || (oauthToken && !config.password)) {
if (!oauthToken) {
throw new Error(
"Snowflake OAuth authenticator specified but no token provided (expected 'token' or 'access_token')",
)
}
options.authenticator = "OAUTH"
options.token = oauthToken
// ---------------------------------------------------------------
// 5. JWT / Programmatic access token (pre-generated)
// The Node.js snowflake-sdk only accepts pre-generated tokens
// via the OAUTH authenticator. SNOWFLAKE_JWT expects a privateKey
// for self-signing, and PROGRAMMATIC_ACCESS_TOKEN is not recognized.
// Alias both to OAUTH so the token is passed correctly.
// ---------------------------------------------------------------
} else if (authUpper === "JWT" || authUpper === "PROGRAMMATIC_ACCESS_TOKEN") {
if (!oauthToken) {
throw new Error(`Snowflake ${authenticator} authenticator specified but no token provided (expected 'token' or 'access_token')`)
}
options.authenticator = "OAUTH"
options.token = oauthToken
// ---------------------------------------------------------------
// 7. Username + password + MFA
// ---------------------------------------------------------------
} else if (authUpper === "USERNAME_PASSWORD_MFA") {
if (!config.password) {
throw new Error("Snowflake USERNAME_PASSWORD_MFA authenticator requires 'password'")
}
options.authenticator = "USERNAME_PASSWORD_MFA"
options.password = config.password
if (passcode) options.passcode = passcode
// ---------------------------------------------------------------
// 8. Plain password auth (default)
// ---------------------------------------------------------------
} else if (config.password) {
options.password = config.password
}
// Use connectAsync for browser-based auth (SSO/Okta), connect for everything else
const isOktaUrl = authenticator && /^https?:\/\/.+\.okta\.com/i.test(authenticator)
const useBrowserAuth = authUpper === "EXTERNALBROWSER" || isOktaUrl
connection = await new Promise<any>((resolve, reject) => {
const conn = snowflake.createConnection(options)
if (useBrowserAuth) {
if (typeof conn.connectAsync !== "function") {
reject(new Error("Snowflake browser/SSO auth requires snowflake-sdk with connectAsync support. Upgrade snowflake-sdk."))
return
}
conn.connectAsync((err: Error | null) => {
if (err) reject(err)
else resolve(conn)
}).catch(reject)
} else {
conn.connect((err: Error | null) => {
if (err) reject(err)
else resolve(conn)
})
}
})
},
async execute(sql: string, limit?: number, binds?: any[], options?: ExecuteOptions): Promise<ConnectorResult> {
const effectiveLimit = options?.noLimit ? 0 : (limit ?? 1000)
let query = sql
const isSelectLike = /^\s*(SELECT|WITH|VALUES|SHOW)\b/i.test(sql)
if (
isSelectLike &&
effectiveLimit &&
!/\bLIMIT\b/i.test(sql)
) {
query = `${sql.replace(/;\s*$/, "")} LIMIT ${effectiveLimit + 1}`
}
const result = await executeQuery(query, binds)
const truncated = effectiveLimit > 0 && result.rows.length > effectiveLimit
const rows = truncated
? result.rows.slice(0, effectiveLimit)
: result.rows
return {
columns: result.columns,
rows,
row_count: rows.length,
truncated,
}
},
async listSchemas(): Promise<string[]> {
const result = await executeQuery("SHOW SCHEMAS")
// SHOW SCHEMAS returns rows with a "name" column
const nameIdx = result.columns.indexOf("name")
if (nameIdx < 0) return result.rows.map((r) => String(r[0]))
return result.rows.map((r) => String(r[nameIdx]))
},
async listTables(
schema: string,
): Promise<Array<{ name: string; type: string }>> {
const result = await executeQuery(
`SHOW TABLES IN SCHEMA "${escapeSqlIdentifier(schema)}"`,
)
const nameIdx = result.columns.indexOf("name")
const kindIdx = result.columns.indexOf("kind")
return result.rows.map((r) => ({
name: String(r[nameIdx >= 0 ? nameIdx : 0]),
type: kindIdx >= 0 && String(r[kindIdx]).toLowerCase() === "view"
? "view"
: "table",
}))
},
async describeTable(
schema: string,
table: string,
): Promise<SchemaColumn[]> {
const result = await executeQuery(
`SHOW COLUMNS IN TABLE "${escapeSqlIdentifier(schema)}"."${escapeSqlIdentifier(table)}"`,
)
const nameIdx = result.columns.indexOf("column_name")
const typeIdx = result.columns.indexOf("data_type")
const nullIdx = result.columns.indexOf("is_nullable")
return result.rows.map((r) => {
let dataType = String(r[typeIdx >= 0 ? typeIdx : 1])
// Snowflake SHOW COLUMNS returns JSON in data_type, parse it
try {
const parsed = JSON.parse(dataType)
dataType = parsed.type ?? dataType
} catch {
// not JSON, use as-is
}
return {
name: String(r[nameIdx >= 0 ? nameIdx : 0]),
data_type: dataType,
nullable:
nullIdx >= 0 ? String(r[nullIdx]).toUpperCase() === "YES" : true,
}
})
},
async close() {
if (connection) {
await new Promise<void>((resolve) => {
connection.destroy((err: Error | null) => {
resolve()
})
})
connection = null
}
},
}
}