77@import_helper ("snowflake" )
88def import_snowflake ():
99 import snowflake .connector
10+ from cryptography .hazmat .primitives import serialization
11+ from cryptography .hazmat .backends import default_backend
1012
11- return snowflake
13+ return snowflake , serialization , default_backend
1214
1315
1416class Snowflake (Database ):
@@ -26,7 +28,7 @@ class Snowflake(Database):
2628 ROUNDS_ON_PREC_LOSS = False
2729
2830 def __init__ (self , * , schema : str , ** kw ):
29- snowflake = import_snowflake ()
31+ snowflake , serialization , default_backend = import_snowflake ()
3032 logging .getLogger ("snowflake.connector" ).setLevel (logging .WARNING )
3133
3234 # Got an error: snowflake.connector.network.RetryRequest: could not find io module state (interpreter shutdown?)
@@ -35,10 +37,23 @@ def __init__(self, *, schema: str, **kw):
3537 logging .getLogger ("snowflake.connector.network" ).disabled = True
3638
3739 assert '"' not in schema , "Schema name should not contain quotes!"
38- self ._conn = snowflake .connector .connect (
39- schema = f'"{ schema } "' ,
40- ** kw ,
41- )
40+ if (
41+ "key" in kw
42+ ): # if private keys are used for Snowflake connection, read in key from path specified and pass as "private_key" to connector.
43+ with open (kw .get ("key" ), "rb" ) as key :
44+ p_key = serialization .load_pem_private_key (
45+ key .read (),
46+ password = kw .pop ("password" , None ),
47+ backend = default_backend (),
48+ )
49+
50+ kw ["private_key" ] = p_key .private_bytes (
51+ encoding = serialization .Encoding .DER ,
52+ format = serialization .PrivateFormat .PKCS8 ,
53+ encryption_algorithm = serialization .NoEncryption (),
54+ )
55+
56+ self ._conn = snowflake .connector .connect (schema = f'"{ schema } "' , ** kw )
4257
4358 self .default_schema = schema
4459
0 commit comments