1- from typing import Any , Union , List
1+ import base64
2+ from typing import Any , Union , List , Optional
23import logging
34
45import attrs
@@ -162,7 +163,7 @@ class Snowflake(Database):
162163
163164 _conn : Any
164165
165- def __init__ (self , * , schema : str , ** kw ):
166+ def __init__ (self , * , schema : str , key : Optional [ str ] = None , key_content : Optional [ str ] = None , ** kw ):
166167 super ().__init__ ()
167168 snowflake , serialization , default_backend = import_snowflake ()
168169 logging .getLogger ("snowflake.connector" ).setLevel (logging .WARNING )
@@ -172,20 +173,29 @@ def __init__(self, *, schema: str, **kw):
172173 logging .getLogger ("snowflake.connector.network" ).disabled = True
173174
174175 assert '"' not in schema , "Schema name should not contain quotes!"
176+ if key_content and key :
177+ raise ConnectError ("Only key value or key file path can be specified, not both" )
178+
179+ key_bytes = None
180+ if key :
181+ with open (key , "rb" ) as f :
182+ key_bytes = f .read ()
183+ if key_content :
184+ key_bytes = base64 .b64decode (key_content )
185+
175186 # 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- )
187+ if key_bytes :
188+ if "password" in kw :
189+ raise ConnectError ("Cannot use password and key at the same time" )
190+ if kw .get ("private_key_passphrase" ):
191+ encoded_passphrase = kw .get ("private_key_passphrase" ).encode ()
192+ else :
193+ encoded_passphrase = None
194+ p_key = serialization .load_pem_private_key (
195+ key_bytes ,
196+ password = encoded_passphrase ,
197+ backend = default_backend (),
198+ )
189199
190200 kw ["private_key" ] = p_key .private_bytes (
191201 encoding = serialization .Encoding .DER ,
0 commit comments