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

Commit 275bf19

Browse files
authored
Merge pull request #792 from datafold/pass-inline-key-to-snowflake
Accept either key file path or file itself in Snowflake
2 parents b3d4223 + cf272f3 commit 275bf19

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

data_diff/databases/snowflake.py

+25-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Any, ClassVar, Union, List, Type
1+
import base64
2+
from typing import Any, ClassVar, Union, List, Type, Optional
23
import logging
34

45
import attrs
@@ -103,7 +104,7 @@ class Snowflake(Database):
103104

104105
_conn: Any
105106

106-
def __init__(self, *, schema: str, **kw):
107+
def __init__(self, *, schema: str, key: Optional[str] = None, key_content: Optional[str] = None, **kw):
107108
super().__init__()
108109
snowflake, serialization, default_backend = import_snowflake()
109110
logging.getLogger("snowflake.connector").setLevel(logging.WARNING)
@@ -113,20 +114,29 @@ def __init__(self, *, schema: str, **kw):
113114
logging.getLogger("snowflake.connector.network").disabled = True
114115

115116
assert '"' not in schema, "Schema name should not contain quotes!"
117+
if key_content and key:
118+
raise ConnectError("Only key value or key file path can be specified, not both")
119+
120+
key_bytes = None
121+
if key:
122+
with open(key, "rb") as f:
123+
key_bytes = f.read()
124+
if key_content:
125+
key_bytes = base64.b64decode(key_content)
126+
116127
# If a private key is used, read it from the specified path and pass it as "private_key" to the connector.
117-
if "key" in kw:
118-
with open(kw.get("key"), "rb") as key:
119-
if "password" in kw:
120-
raise ConnectError("Cannot use password and key at the same time")
121-
if kw.get("private_key_passphrase"):
122-
encoded_passphrase = kw.get("private_key_passphrase").encode()
123-
else:
124-
encoded_passphrase = None
125-
p_key = serialization.load_pem_private_key(
126-
key.read(),
127-
password=encoded_passphrase,
128-
backend=default_backend(),
129-
)
128+
if key_bytes:
129+
if "password" in kw:
130+
raise ConnectError("Cannot use password and key at the same time")
131+
if kw.get("private_key_passphrase"):
132+
encoded_passphrase = kw.get("private_key_passphrase").encode()
133+
else:
134+
encoded_passphrase = None
135+
p_key = serialization.load_pem_private_key(
136+
key_bytes,
137+
password=encoded_passphrase,
138+
backend=default_backend(),
139+
)
130140

131141
kw["private_key"] = p_key.private_bytes(
132142
encoding=serialization.Encoding.DER,

0 commit comments

Comments
 (0)