11try :
22 import snowflake .connector
3+ from cryptography .hazmat .primitives .serialization import load_pem_private_key
34
45 enabled = True
56except ImportError :
67 enabled = False
78
89
10+ from base64 import b64decode
11+
912from redash import __version__
1013from redash .query_runner import (
1114 TYPE_BOOLEAN ,
@@ -43,6 +46,8 @@ def configuration_schema(cls):
4346 "account" : {"type" : "string" },
4447 "user" : {"type" : "string" },
4548 "password" : {"type" : "string" },
49+ "private_key_File" : {"type" : "string" },
50+ "private_key_pwd" : {"type" : "string" },
4651 "warehouse" : {"type" : "string" },
4752 "database" : {"type" : "string" },
4853 "region" : {"type" : "string" , "default" : "us-west" },
@@ -57,13 +62,15 @@ def configuration_schema(cls):
5762 "account" ,
5863 "user" ,
5964 "password" ,
65+ "private_key_File" ,
66+ "private_key_pwd" ,
6067 "warehouse" ,
6168 "database" ,
6269 "region" ,
6370 "host" ,
6471 ],
65- "required" : ["user" , "password" , " account" , "database" , "warehouse" ],
66- "secret" : ["password" ],
72+ "required" : ["user" , "account" , "database" , "warehouse" ],
73+ "secret" : ["password" , "private_key_File" , "private_key_pwd" ],
6774 "extra_options" : [
6875 "host" ,
6976 ],
@@ -88,22 +95,37 @@ def _get_connection(self):
8895 if region == "us-west" :
8996 region = None
9097
91- if self .configuration .__contains__ ("host" ):
98+ if self .configuration .get ("host" ):
9299 host = self .configuration .get ("host" )
93100 else :
94101 if region :
95102 host = "{}.{}.snowflakecomputing.com" .format (account , region )
96103 else :
97104 host = "{}.snowflakecomputing.com" .format (account )
98105
99- connection = snowflake .connector .connect (
100- user = self .configuration ["user" ],
101- password = self .configuration ["password" ],
102- account = account ,
103- region = region ,
104- host = host ,
105- application = "Redash/{} (Snowflake)" .format (__version__ .split ("-" )[0 ]),
106- )
106+ params = {
107+ "user" : self .configuration ["user" ],
108+ "account" : account ,
109+ "region" : region ,
110+ "host" : host ,
111+ "application" : "Redash/{} (Snowflake)" .format (__version__ .split ("-" )[0 ]),
112+ }
113+
114+ if self .configuration .get ("password" ):
115+ params ["password" ] = self .configuration ["password" ]
116+ elif self .configuration .get ("private_key_File" ):
117+ private_key_b64 = self .configuration .get ("private_key_File" )
118+ private_key_bytes = b64decode (private_key_b64 )
119+ if self .configuration .get ("private_key_pwd" ):
120+ private_key_pwd = self .configuration .get ("private_key_pwd" ).encode ()
121+ else :
122+ private_key_pwd = None
123+ private_key_pem = load_pem_private_key (private_key_bytes , private_key_pwd )
124+ params ["private_key" ] = private_key_pem
125+ else :
126+ raise Exception ("Neither password nor private_key_b64 is set." )
127+
128+ connection = snowflake .connector .connect (** params )
107129
108130 return connection
109131
0 commit comments