1
1
try :
2
2
import snowflake .connector
3
+ from cryptography .hazmat .primitives .serialization import load_pem_private_key
3
4
4
5
enabled = True
5
6
except ImportError :
6
7
enabled = False
7
8
8
9
10
+ from base64 import b64decode
11
+
9
12
from redash import __version__
10
13
from redash .query_runner import (
11
14
TYPE_BOOLEAN ,
@@ -43,6 +46,8 @@ def configuration_schema(cls):
43
46
"account" : {"type" : "string" },
44
47
"user" : {"type" : "string" },
45
48
"password" : {"type" : "string" },
49
+ "private_key_File" : {"type" : "string" },
50
+ "private_key_pwd" : {"type" : "string" },
46
51
"warehouse" : {"type" : "string" },
47
52
"database" : {"type" : "string" },
48
53
"region" : {"type" : "string" , "default" : "us-west" },
@@ -57,13 +62,15 @@ def configuration_schema(cls):
57
62
"account" ,
58
63
"user" ,
59
64
"password" ,
65
+ "private_key_File" ,
66
+ "private_key_pwd" ,
60
67
"warehouse" ,
61
68
"database" ,
62
69
"region" ,
63
70
"host" ,
64
71
],
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" ],
67
74
"extra_options" : [
68
75
"host" ,
69
76
],
@@ -88,22 +95,37 @@ def _get_connection(self):
88
95
if region == "us-west" :
89
96
region = None
90
97
91
- if self .configuration .__contains__ ("host" ):
98
+ if self .configuration .get ("host" ):
92
99
host = self .configuration .get ("host" )
93
100
else :
94
101
if region :
95
102
host = "{}.{}.snowflakecomputing.com" .format (account , region )
96
103
else :
97
104
host = "{}.snowflakecomputing.com" .format (account )
98
105
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 )
107
129
108
130
return connection
109
131
0 commit comments