Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 5b5c35e

Browse files
committed
accept either key file path or file itself
1 parent 57b889d commit 5b5c35e

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

data_diff/databases/snowflake.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class Snowflake(Database):
162162

163163
_conn: Any
164164

165-
def __init__(self, *, schema: str, **kw):
165+
def __init__(self, *, schema: str, key_file_path: str | None = None, key: bytes | None = None, **kw):
166166
super().__init__()
167167
snowflake, serialization, default_backend = import_snowflake()
168168
logging.getLogger("snowflake.connector").setLevel(logging.WARNING)
@@ -172,20 +172,26 @@ def __init__(self, *, schema: str, **kw):
172172
logging.getLogger("snowflake.connector.network").disabled = True
173173

174174
assert '"' not in schema, "Schema name should not contain quotes!"
175+
if key and key_file_path:
176+
raise ConnectError("Only key value or key file path can be specified, not both")
177+
178+
if key_file_path:
179+
with open(key_file_path, "rb") as f:
180+
key = f.read()
181+
175182
# If a private key is used, read it from the specified path and pass it as "private_key" to the connector.
176-
if "key" in kw:
177-
with open(kw.get("key"), "rb") as key:
178-
if "password" in kw:
179-
raise ConnectError("Cannot use password and key at the same time")
180-
if kw.get("private_key_passphrase"):
181-
encoded_passphrase = kw.get("private_key_passphrase").encode()
182-
else:
183-
encoded_passphrase = None
184-
p_key = serialization.load_pem_private_key(
185-
key.read(),
186-
password=encoded_passphrase,
187-
backend=default_backend(),
188-
)
183+
if key:
184+
if "password" in kw:
185+
raise ConnectError("Cannot use password and key at the same time")
186+
if kw.get("private_key_passphrase"):
187+
encoded_passphrase = kw.get("private_key_passphrase").encode()
188+
else:
189+
encoded_passphrase = None
190+
p_key = serialization.load_pem_private_key(
191+
key,
192+
password=encoded_passphrase,
193+
backend=default_backend(),
194+
)
189195

190196
kw["private_key"] = p_key.private_bytes(
191197
encoding=serialization.Encoding.DER,

data_diff/dbt_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def set_connection(self):
355355
if credentials.get("private_key_path") is not None:
356356
if credentials.get("password") is not None:
357357
raise DataDiffDbtSnowflakeSetConnectionError("Cannot use password and key at the same time")
358-
conn_info["key"] = credentials.get("private_key_path")
358+
conn_info["key_file_path"] = credentials.get("private_key_path")
359359
conn_info["private_key_passphrase"] = credentials.get("private_key_passphrase")
360360
elif credentials.get("authenticator") is not None:
361361
conn_info["authenticator"] = credentials.get("authenticator")

0 commit comments

Comments
 (0)