1
- from typing import Any , Union , List
1
+ from typing import Any , Union , List , Optional
2
2
import logging
3
3
4
4
import attrs
@@ -162,7 +162,7 @@ class Snowflake(Database):
162
162
163
163
_conn : Any
164
164
165
- def __init__ (self , * , schema : str , ** kw ):
165
+ def __init__ (self , * , schema : str , key : Optional [ str ] = None , key_content : Optional [ bytes ] = None , ** kw ):
166
166
super ().__init__ ()
167
167
snowflake , serialization , default_backend = import_snowflake ()
168
168
logging .getLogger ("snowflake.connector" ).setLevel (logging .WARNING )
@@ -172,20 +172,26 @@ def __init__(self, *, schema: str, **kw):
172
172
logging .getLogger ("snowflake.connector.network" ).disabled = True
173
173
174
174
assert '"' not in schema , "Schema name should not contain quotes!"
175
+ if key_content and key :
176
+ raise ConnectError ("Only key value or key file path can be specified, not both" )
177
+
178
+ if key :
179
+ with open (key , "rb" ) as f :
180
+ key_content = f .read ()
181
+
175
182
# 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_content :
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_content ,
192
+ password = encoded_passphrase ,
193
+ backend = default_backend (),
194
+ )
189
195
190
196
kw ["private_key" ] = p_key .private_bytes (
191
197
encoding = serialization .Encoding .DER ,
0 commit comments