-
Notifications
You must be signed in to change notification settings - Fork 2
/
snowsight_basic.py
1358 lines (1099 loc) · 51.7 KB
/
snowsight_basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Basic example implementation of logging into Snowsight using Username/Password, SSO and Duo (with passcode).
#
# This script should work with Python 3.5+ without needing additional libraries; however, for private key
# authentication, you will need to install `cryptography` and pyjwt`: `pip install cryptography pyjwt`.
# > Note that the Python Snowflake Connector comes with these packages already installed.
#
# Usage: python3 snowsight_basic.py -m METHOD -a ACCOUNT_IDENTIFIER -u LOGIN_NAME [-p PASSWORD] [-l {DEBUG,INFO}]
#
# Information about account identifiers can be found at https://docs.snowflake.com/en/user-guide/admin-account-identifier
#
# LOGIN_NAME is the login_name from Snowflake, which can be different to the username (e.g. SSO can use an email address
# for the login_name, but a username for the username).
#
# Password is optional unless using password login method.
#
# Supported login methods (-m):
# - password - Login using username and password.
# - sso [Federated/SSO](https://docs.snowflake.com/en/user-guide/admin-security-fed-auth-overview)
# - duo [MFA (Duo, Push Notification)](https://docs.snowflake.com/en/user-guide/security-mfa)
# - duo_passcode [MFA (Duo, Passcode)](https://docs.snowflake.com/en/user-guide/security-mfa#using-mfa-with-snowsql)
# - private key [Key-pair authentication (Authenticating with your private key)](https://docs.snowflake.com/en/user-guide/key-pair-auth)
#
# Log level can be either debug or info, defaults to info. Debug shows verbose request/response information.
import argparse
import datetime
import http.cookiejar
import json
import logging
import re
import socket
import sys
import urllib.request
import urllib.error
import urllib.parse
from urllib.parse import urlparse, parse_qs
from typing import Optional
def fake_user_agent() -> str:
"""
User agent to use when sending requests, this is needed otherwise the default urllib user agent is blocked by Cloudflare,
which Snowflake uses for their app server.
I just used mine - Firefox 121, on macOS.
Returns:
A user agent string to use for requests.
"""
return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0"
def snowsight_client_app_version() -> int:
"""
Snowsight uses this as of January 2024 on app.snowflake.com for the CLIENT_APP_VERSION when the
CLIENT_APP_ID is set to "Snowflake UI".
Returns:
An integer representing the current date/time in the format of YYYYMMDDHHMMSS.
"""
return int(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
def validate_snowflake_url(account_identifier: str) -> dict:
"""
Validates a Snowflake account using the account identifier,
and returns the account name, region and app server URL as a dict.
Args:
account_identifier:
Snowflake account identifier, it's usually `https://<account_identifier>.snowflakecomputing.com`.
Most of the time it is `<USERNAME>.<ACCOUNT_NAME>`.
Information about account identifiers can be found at
https://docs.snowflake.com/en/user-guide/admin-account-identifier
Returns:
dict with the response from the Snowflake API
"""
url = f"https://app.snowflake.com/v0/validate-snowflake-url?url={account_identifier}&isSecondaryAccount=false"
headers = {"User-Agent": fake_user_agent()}
logging.debug("[GET REQUEST] URL: %s - Headers: %s", url, headers)
req = urllib.request.Request(
url,
method="GET",
headers=headers,
)
# Perform the request and extracts the account name, region and app server URL.
try:
with urllib.request.urlopen(req) as response:
response_data = json.loads(response.read().decode())
logging.debug("[RESPONSE] - %s", response_data)
# account - The account name, e.g. `MYACCOUNT`
# region - the region of the snowflake account (us-east-1, etc.)
# instance_url - The URL to the user's instance, usually `https://<ACCOUNT_NAME>.<REGION>.snowflakecomputing.com`
# app_server_url - the snowsight API URL, `https://apps-api.c1.<REGION>.aws.app.snowflake.com`
# valid - True if the account is valid, False if it's not.
return {
"account": response_data["account"],
"region": response_data["region"],
"instance_url": response_data["url"],
"app_server_url": response_data["appServerUrl"],
"valid": response_data["valid"],
}
except urllib.error.URLError as e:
logging.error(e.reason)
print(e.reason)
sys.exit(1)
def snowsight_bootstrap(
app_server_url: str,
instance_url: str,
name: Optional[str] = None,
cookies: Optional[str] = None,
) -> dict:
"""
There are two different bootstrap methods, one for authenticated users, and one for unauthenticated users.
If performing an unauthenticated request, then the `name` and `cookies` parameters should be None.
Snowflake requires an `csrfToken` and an ` OrganizationID ` (if authenticated) for interacting with endpoints,
which you can retrieve from the `bootstrap` endpoint.
Args:
app_server_url: The app server URL from validate-url (Usually `'https://apps-api.c1.ap-southeast-2.aws.app.snowflake.com')
instance_url: The instance URL from validate-url (Usually `https://<ACCOUNT_NAME>.<REGION>.snowflakecomputing.com')
name: The login name to use, if you're logged in, this is the username, otherwise it's None.
cookies: The list of cookies to use for authentication
Returns:
A dict containing the `csrf_token` and `org_id` (if it's an authenticated request) for the Snowflake account.
"""
headers = {
"Content-Type": "application/json",
"User-Agent": fake_user_agent(),
}
if name is not None and instance_url is not None:
headers["X-Snowflake-Context"] = f"{name.upper()}::{instance_url}"
if cookies is not None:
headers["Cookie"] = cookies
url = f"{app_server_url}/bootstrap"
logging.debug("[GET REQUEST] URL: %s Headers: %s", url, headers)
req = urllib.request.Request(url, headers=headers, method="GET")
# Perform the request and extract the `csrfToken` and `OrganizationID` (if authenticated).
try:
with urllib.request.urlopen(req) as response:
response_data = json.loads(response.read().decode())
logging.debug("[RESPONSE] - %s", response_data)
csrf_token = response_data["PageParams"]["csrfToken"]
# Unauthenticated responses don't have the org (as they don't have a user, as they're not logged in)
if response_data.get("User", None) is None:
logging.debug("Has no user, csrf_token %s", csrf_token)
return {"csrf_token": csrf_token, "org_id": None}
# `OrganizationID` is from either:
#
# 1. response_data["Org"]["id"].
# 2. That value can be null/empty, fall back to response_data["User"]["defaultOrgId"]
org_id = response_data.get("Org", {}).get("id", None) or response_data[
"User"
].get("defaultOrgId", None)
logging.debug("csrf_token - %s, org_id - %s", csrf_token, org_id)
return {"csrf_token": csrf_token, "org_id": org_id}
except urllib.error.URLError as e:
logging.error(e.reason)
print(e.reason)
sys.exit(1)
### OAUTH
def start_oauth(app_server_url: str, instance_url: str, csrf_token: str) -> dict:
"""
Starts the OAuth flow, returning the redirect URL and cookies.
Args:
app_server_url: The app server URL from validate-url (Usually `'https://apps-api.c1.ap-southeast-2.aws.app.snowflake.com')
instance_url: The instance URL from validate-url (Usually `https://<ACCOUNT_NAME>.<REGION>.snowflakecomputing.com')
csrf_token: The csrf token from the bootstrap request
Returns:
A dict containing the redirect URL and cookies.
"""
# The state needs to be these values, otherwise the login will fail.
# Passing additional values will cause the login to fail.
# If new values are added/removed in future, you'll need to update this.
state = '{{"csrf":"{0}","url":"{1}","browserUrl":"{2}"}}'.format(
csrf_token, instance_url, "https://app.snowflake.com/"
)
instance_url_encoded = urllib.parse.quote_plus(instance_url)
state_encoded = urllib.parse.quote_plus(state)
url = f"{app_server_url}/start-oauth/snowflake?accountUrl={instance_url_encoded}&&state={state_encoded}"
headers = {
"User-Agent": fake_user_agent(),
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Referer": "https://app.snowflake.com/"
}
logging.debug("[GET REQUEST] URL: %s - Headers: %s", url, headers)
req = urllib.request.Request(url, headers=headers, method="GET")
# get cookies from req
cookie_jar = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie_jar))
# Create an opener that will use the cookie jar
try:
with opener.open(req) as response:
# Open the URL, we don't care about the response, we just want the cookies.
logging.debug("[RESPONSE] - %s", response.read().decode())
# Extract cookies from the cookie jar
cookies_list = []
for cookie in [cookie for cookie in cookie_jar]:
cookies_list.append(cookie.name + "=" + cookie.value.replace('"', ""))
cookies = "; ".join(cookies_list)
logging.debug("Extracted cookies: %s", cookies)
final_url = response.url
parsed_url = urlparse(final_url)
params = parse_qs(parsed_url.query)
scope = params["scope"][0]
client_id = params["client_id"][0]
response_type = params["response_type"][0]
code_challenge = params["code_challenge"][0]
code_challenge_method = params["code_challenge_method"][0]
redirect_uri = params["redirect_uri"][0]
returned_state = params["state"][0]
state_dict = json.loads(returned_state)
originator = state_dict["originator"]
return {
"cookies": cookies,
"oauth_nonce": state_dict["oauthNonce"],
"scope": scope,
"client_id": client_id,
"response_type": response_type,
"code_challenge": code_challenge,
"code_challenge_method": code_challenge_method,
"redirect_uri": redirect_uri,
"originator": originator,
}
except urllib.error.URLError as e:
logging.error(e.reason)
print(e.reason)
sys.exit(1)
def build_authenticate_request_payload(
instance_url: str,
account_name: str,
login_name: str,
oauth_nonce: str,
csrf: str,
response_type: str,
code_challenge: str,
code_challenge_method: str,
client_id: str,
scope: str,
redirect_uri: str,
originator: str,
password: Optional[str] = None,
private_key_token: Optional[str] = None,
inflight_ctx: Optional[str] = None,
proof_key: Optional[str] = None,
token: Optional[str] = None,
) -> dict:
"""
Builds the authenticate request payload, which is sent to the session/authenticate-request endpoint.
Depending on your authentication method, you will need the following;
Username/Password authentication: password
SSO authentication: None - Just the login name
Duo authentication (pass code): Duo passcode
Duo authentication (push): None - Just the login name
Private key authentication: Private key as a string
Args:
instance_url: The instance URL from validate-url (Usually `https://<ACCOUNT_NAME>.<REGION>.snowflakecomputing.com')
account_name: The account name from validate-url (Usually `ACCOUNT_NAME`)
login_name: The login name to authenticate with, this is usually the username, but can be different for SSO.
oauth_nonce: The oauth nonce from the bootstrap request
csrf: The csrf token from the bootstrap request
response_type: The response type from the `/start-oauth/snowflake` endpoint
code_challenge: The code challenge from the `/start-oauth/snowflake` endpoint
code_challenge_method: The code challenge method from the `/start-oauth/snowflake` endpoint
client_id: The client ID from the `/start-oauth/snowflake` endpoint
scope: The scope from the `/start-oauth/snowflake` endpoint
redirect_uri: The redirect URI from the `/start-oauth/snowflake` endpoint
originator: The originator from the `/start-oauth/snowflake` endpoint
password: The password to authenticate with, if using username/password authentication.
private_key_token: The JWT from the private_key to authenticate with, if using private key authentication.
inflight_ctx: The inflight_ctx from the login-request endpoint, if using Duo authentication.
proof_key: When logging in via SSO, the proof key returned from the `/session/authenticator-request` endpoint.
token: When logging in via SSO, the token returned from the `/session/authenticator-request` endpoint.
Returns:
Payload to send to the session/authenticate-request endpoint.
"""
# The state needs to be these values, otherwise the login will fail.
# Passing additional values will cause the login to fail.
# If new values are added/removed in future, you'll need to update this.
state = '{{"csrf":"{0}","url":"{1}","browserUrl":"{2}","originator":"{3}","oauthNonce":"{4}"}}'.format(
csrf, instance_url, "https://app.snowflake.com/", originator, oauth_nonce
)
payload = {
"data": {
"ACCOUNT_NAME": account_name.upper(),
"LOGIN_NAME": login_name,
"clientId": client_id,
"redirectUri": redirect_uri,
"responseType": response_type,
"state": state,
"scope": scope,
"codeChallenge": code_challenge,
"codeChallengeMethod": code_challenge_method,
"CLIENT_APP_ID": "Snowflake UI",
"CLIENT_APP_VERSION": snowsight_client_app_version(),
}
}
if password and not inflight_ctx:
payload["data"]["PASSWORD"] = password
if private_key_token:
# add AUTHENTICATOR
payload["data"]["AUTHENTICATOR"] = "SNOWFLAKE_JWT"
# add TOKEN
payload["data"]["TOKEN"] = private_key_token
if inflight_ctx:
payload["inFlightCtx"] = inflight_ctx
# remove LOGIN_NAME & ACCOUNT_NAME from data, as they're not needed
payload["data"].pop("LOGIN_NAME", None)
payload["data"].pop("ACCOUNT_NAME", None)
if proof_key and token:
payload["data"]["AUTHENTICATOR"] = "EXTERNALBROWSER"
payload["data"]["TOKEN"] = token
payload["data"]["PROOF_KEY"] = proof_key
return payload
def authenticate_request(instance_url: str, payload: dict) -> str:
"""
Sends a POST request to the session/authenticate-request endpoint with the login payload, returning the masterToken,
which is used in the authorization request.
This function does not perform any error handling, so if the login fails, it will just exit.
Args:
instance_url: The instance URL from validate-url (Usually `https://<ACCOUNT_NAME>.<REGION>.snowflakecomputing.com')
payload: The payload to send to the session/authenticate-request endpoint, built using build_authenticate_request_payload
Returns:
The masterToken, which is used in the authorization request.
"""
data_json = json.dumps(payload).encode("utf-8")
headers = {"Content-Type": "application/json", "User-Agent": fake_user_agent()}
url = f"{instance_url}/session/authenticate-request?__uiAppName=Login"
logging.debug(f"[REQUEST] URL: {url} data: {payload} headers: {headers}")
req = urllib.request.Request(url, data=data_json, headers=headers, method="POST")
# Perform the request and extract the 'masterToken' from the request, if this
# doesn't exist, then the login failed - you'll need to implement better error handling.
try:
with urllib.request.urlopen(req) as response:
response_data = json.loads(response.read().decode())
logging.debug("[RESPONSE] - %s", response_data)
return response_data["data"]["masterToken"]
except urllib.error.URLError as e:
logging.error(e.reason)
print(e.reason)
sys.exit(1)
def authorization_request(
instance_url: str,
master_token: str,
oauth_nonce: str,
csrf: str,
response_type: str,
code_challenge: str,
code_challenge_method: str,
client_id: str,
scope: str,
redirect_uri: str,
originator: str,
) -> str:
"""
Sends a POST request to the oauth/authorization-request endpoint with the masterToken, returning the redirectUrl.
The oauth/authorization-request endpoint is used to convert the masterToken into a redirectUrl,
which is then used to complete the OAuth flow.
Args:
instance_url: The instance URL from validate-url (Usually `https://<ACCOUNT_NAME>.<REGION>.snowflakecomputing.com')
master_token: The masterToken from the session/authenticate-request endpoint
oauth_nonce: The oauth nonce from the bootstrap request
csrf: The unauthenticated CSRF token from the bootstrap request
response_type: The response type from the start-oauth request
code_challenge: The code challenge from the start-oauth request
code_challenge_method: The code challenge method from the start-oauth request
client_id: The client ID from the start-oauth request
scope: The scope from the start-oauth request
redirect_uri: The redirect URI from the start-oauth request
originator: The originator from the start-oauth request
Returns:
The redirectUrl, which is used to complete the OAuth flow.
"""
headers = {"Content-Type": "application/json", "User-Agent": fake_user_agent()}
# The state needs to be these values, otherwise the login will fail.
# Passing additional values will cause the login to fail.
# If new values are added/removed in future, you'll need to update this.
state = '{{"csrf":"{0}","url":"{1}","browserUrl":"{2}","originator":"{3}","oauthNonce":"{4}"}}'.format(
csrf, instance_url, "https://app.snowflake.com/", originator, oauth_nonce
)
url = f"{instance_url}/oauth/authorization-request"
data = {
"masterToken": master_token,
"clientId": client_id,
"redirectUri": redirect_uri,
"responseType": response_type,
"state": state,
"scope": scope,
"codeChallenge": code_challenge,
"codeChallengeMethod": code_challenge_method,
}
data_json = json.dumps(data).encode("utf-8")
logging.debug(
"authorization_request - URL: %s data: %s headers: %s", url, data, headers
)
req = urllib.request.Request(url, data=data_json, headers=headers, method="POST")
# Perform the request and extract the 'redirectURI'.
try:
with urllib.request.urlopen(req) as response:
response_data = json.loads(response.read().decode())
logging.debug("authorization_request - %s", response_data)
return response_data["data"]["redirectUrl"]
except urllib.error.URLError as e:
logging.error(e.reason)
print(e.reason)
sys.exit(1)
def complete_oauth(
redirect_url: str, csrf: str, instance_url: str, oauth_nonce: str, cookies: str
) -> str:
"""
Completes the OAuth flow, returning the cookies as a string that can be used to authenticate with Snowsight.
We need to get the `S8_SESSION_` and `user-` cookies for future requests.
Args:
redirect_url: The redirect URL returned from the login request
csrf: The csrf token from the bootstrap request
instance_url: The instance URL from the bootstrap request
oauth_nonce: The oauth nonce from the bootstrap request
cookies: The cookies from the bootstrap request
Returns:
Cookies, as a string, to use for authentication in Snowsight, prefixed as `S8_SESSION_` and `user-`.
"""
headers = {
"Content-Type": "application/json",
"User-Agent": fake_user_agent(),
"Cookie": cookies,
}
req = urllib.request.Request(redirect_url, headers=headers, method="GET")
# get cookies from req
cookie_jar = http.cookiejar.CookieJar()
# Create an opener that will use the cookie jar
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie_jar))
logging.debug("complete_oauth [REQUEST] URL: %s headers: %s", redirect_url, headers)
try:
with opener.open(req) as response:
# It's HTML, not JSON
html_response = response.read().decode()
logging.debug("complete_oauth [RESPONSE] - %s", html_response)
cookies_list = []
for cookie in [cookie for cookie in cookie_jar]:
cookies_list.append(cookie.name + "=" + cookie.value.replace('"', ""))
snowsight_authed_cookies = "; ".join(cookies_list)
logging.debug("complete_oauth - cookies: %s", snowsight_authed_cookies)
# Snowflake returns initial bootstrap data in the HTML, but it's not JSON, so we can't parse it directly...
# You could use regex to extract the data, but it's not ideal, as a change of the HTML could break this.
# We don't really need the information from this anyway, as we get the login name from the cookies.
# account_params = re.search(r"var params = (.*?);", html_response).group(1)
# account_params_dict = json.loads(account_params)
return snowsight_authed_cookies
except urllib.error.URLError as e:
logging.error(e.reason)
print(e.reason)
sys.exit(1)
def login_request(base_url: str, login_payload: dict) -> dict:
"""
For Duo passcode authentication (https://duo.com/product/multi-factor-authentication-mfa/authentication-methods/tokens-and-passcodes),
a user can generate a passcode, valid for a single login.
This returns a inFlightCtx, which we need to send again to the `login-request` endpoint.
For Duo Push Notifications, the `/session/v1/login-request` endpoint needs to be required first, as
this endpoint sends a Push notification to the Duo app on the users phone.
Once the user has approved the login, a `masterToken` is returned, which is valid for 1 hour.
This masterToken can then be used with the `oauth/authorization-request` endpoint, bypassing the
`session/authenticate-request` endpoint, as this endpoint acts as the authentication method.
Args:
base_url: The base URL to authenticate to
login_payload: The login payload, a Python dict.
Returns: A dict containing the redirect_uri and the name for the header.
"""
# Convert the data to JSON
data_json = json.dumps(login_payload).encode("utf-8")
# Headers
headers = {
"Content-Type": "application/json",
"User-Agent": fake_user_agent(),
}
# Create a connection - replace with proxy details if needed
url = f"{base_url}/session/v1/login-request?__uiAppName=Login"
logging.debug(
"[POST REQUEST] URL: %s - Headers: %s - Body: %s", url, headers, login_payload
)
req = urllib.request.Request(url, data=data_json, headers=headers, method="POST")
# Perform the request and extract the 'redirectURI' and userName
try:
with urllib.request.urlopen(req) as response:
response_data = json.loads(response.read().decode())
logging.debug("[RESPONSE] - %s", response_data)
# check if ["data"]["nextAction"] is EXT_AUTHN_DUO_BEYOND, if so return the inFlightCtx
next_action = response_data["data"].get("nextAction", None)
inflight_ctx = response_data["data"].get("inFlightCtx", None)
# https://docs.snowflake.com/en/user-guide/security-mfa#mfa-error-codes
# Check response for code 390128 (EXT_AUTHN_SUCCEEDED)
if (
response_data.get("code", None) == "390128"
and next_action == "EXT_AUTHN_DUO_BEYOND"
and inflight_ctx is not None
):
return {
"redirect_uri": None,
"name": None,
"master_token": None,
"inFlightCtx": response_data["data"]["inFlightCtx"],
}
elif response_data["data"].get("redirectURI", None) is not None:
return {
"redirect_uri": response_data["data"]["redirectURI"],
"name": response_data["data"]["authnEvent"]["userName"],
"inFlightCtx": None,
"master_token": None,
}
elif response_data["data"].get("masterToken", None) is not None:
return {
"redirect_uri": None,
"name": None,
"inFlightCtx": None,
"master_token": response_data["data"]["masterToken"],
}
# Snowflake returns success false if the login fails, but also returns false when authenticating with Duo,
# so we need to check the code due to this "bug"?
elif response_data.get("success") is False:
return_message = "Authentication Failed - "
# is there a code?
if response_data.get("code"):
return_message += response_data["code"] + " - "
# is there a message?
if response_data.get("message"):
return_message += response_data["message"] + " - "
# raise
raise Exception(return_message)
if response_data.get("data") is None:
raise Exception("Authentication Failed - No data returned")
# nothing found?
raise Exception("Authentication Failed - No redirectURI returned")
except urllib.error.URLError as e:
logging.info(e.reason)
# Authenticated Endpoints (Internal Snowsight API)
def snowsight_entities(
app_server_url: str,
instance_url: str,
name: str,
org_id: str,
csrf_token: str,
cookies: str,
) -> list:
"""
Returns a list of worksheets for a Snowflake account.
This example does not perform pagination, so if you have more than 500 worksheets, you will need to
implement that yourself.
Args:
app_server_url: The app server URL from validate-url (Usually `'https://apps-api.c1.ap-southeast-2.aws.app.snowflake.com')
instance_url: The instance URL from validate-url (Usually `https://<ACCOUNT_NAME>.<REGION>.snowflakecomputing.com')
name: The login name to use, if you're logged in, this is the username, otherwise it's None.
org_id: The org_id returned from the bootstrap request.
csrf_token: The authenticated csrf_token
cookies: The cookies to use for authentication, returned from complete-oauth
Returns:
A list of worksheets for the Snowflake account.
"""
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"X-CSRF-Token": csrf_token,
"X-Snowflake-Context": f"{name.upper()}::{instance_url}",
"Cookie": cookies,
"User-Agent": fake_user_agent(),
}
url = f"{app_server_url}/v0/organizations/{org_id}/entities/list"
post_data = "options=%7B%22sort%22%3A%7B%22col%22%3A%22modified%22%2C%22dir%22%3A%22desc%22%7D%2C%22limit%22%3A500%2C%22owner%22%3Anull%2C%22types%22%3A%5B%22query%22%5D%2C%22showNeverViewed%22%3A%22if-invited%22%7D&location=worksheets".encode(
"utf-8"
)
logging.debug(
"[POST REQUEST] URL: %s - Headers: %s - Body: %s", url, headers, post_data
)
req = urllib.request.Request(
url,
headers=headers,
method="POST",
data=post_data,
)
# Perform the request and extract all queries.
try:
with urllib.request.urlopen(req) as response:
response_data = json.loads(response.read().decode())
logging.debug("[RESPONSE] - %s", response_data)
queries = []
logging.info("Found %s queries", len(response_data["models"]["queries"]))
for q in response_data["models"].get("queries", {}).values():
queries.append(q)
return queries
except urllib.error.URLError as e:
logging.error(e.reason)
print(e.reason)
sys.exit(1)
def generate_jwt_token_from_private_key(
account_identifier: str,
login_name: str,
private_key_path: str,
private_key_password: None,
) -> str:
"""
Generate a JWT token from a private key file, taken pretty much verbatim from the Snowflake documentation:
https://docs.snowflake.com/en/developer-guide/sql-api/authenticating
You will need both `cryptography` and `pyjwt` installed for this to work, if using pip - `pip install cryptography pyjwt`.
If you are using Snowflake Connector for Python, you should already have these installed, as it's
a dependency of the connector.
Args:
account_identifier:
Snowflake account identifier, it's usually `https://<account_identifier>.snowflakecomputing.com`.
Most of the time it is `<USERNAME>.<ACCOUNT_NAME>`.
Information about account identifiers can be found at
https://docs.snowflake.com/en/user-guide/admin-account-identifier
login_name: The login name to authenticate with, this is usually the username.
private_key_path: Path to the private key file.
private_key_password: Password for the private key file, if it has one.
Returns:
The JWT token as a string.
"""
# This script should work with Python 3.5+ without needing additional libraries; however, for private key
# authentication, you will need to install `cryptography` and pyjwt`: `pip install cryptography pyjwt`.
import pkg_resources
required = {"pyjwt", "cryptography"}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed
if missing:
raise ImportError(f"Missing required packages: {', '.join(missing)}")
from datetime import timedelta, timezone, datetime
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from cryptography.hazmat.primitives.serialization import Encoding
from cryptography.hazmat.primitives.serialization import PublicFormat
from cryptography.hazmat.backends import default_backend
import base64
import hashlib
import jwt
# Get the account identifier without the region, cloud provider, or subdomain.
if not ".global" in account_identifier:
idx = account_identifier.find(".")
if idx > 0:
account_identifier = account_identifier[0:idx]
else:
# Handle the replication case.
idx = account_identifier.find("-")
if idx > 0:
account_identifier = account_identifier[0:idx]
qualified_username = account_identifier + "." + login_name
with open(private_key_path, "rb") as pem_in:
pemlines = pem_in.read()
if private_key_password is None:
private_key = load_pem_private_key(pemlines, None, default_backend())
else:
private_key = load_pem_private_key(
pemlines, private_key_password, default_backend()
)
# Get the raw bytes of the public key.
public_key_raw = private_key.public_key().public_bytes(
Encoding.DER, PublicFormat.SubjectPublicKeyInfo
)
# Get the sha256 hash of the raw bytes.
sha256hash = hashlib.sha256()
sha256hash.update(public_key_raw)
# Base64-encode the value and prepend the prefix 'SHA256:'.
public_key_fp = "SHA256:" + base64.b64encode(sha256hash.digest()).decode("utf-8")
logging.debug("Public key fingerprint is ", public_key_fp)
# Specify the length of time during which the JWT will be valid. You can specify at most 1 hour.
lifetime = timedelta(minutes=60)
now = datetime.now(timezone.utc)
payload = {
"iss": qualified_username.upper() + "." + public_key_fp,
"sub": qualified_username.upper(),
"iat": now,
"exp": now + lifetime,
}
encoding_algorithm = "RS256"
token = jwt.encode(payload, key=private_key, algorithm=encoding_algorithm)
logging.debug("Generated a JWT with the following payload:\n{}".format(payload))
# Double-check that we can decode the token.
# If you are using a version of PyJWT prior to 2.0, jwt.encode returns a byte string, rather than a string.
# If the token is a byte string, convert it to a string.
if isinstance(token, bytes):
token = token.decode("utf-8")
decoded_token = jwt.decode(
token, key=private_key.public_key(), algorithms=[encoding_algorithm]
)
logging.debug(
"Generated a JWT with the following payload:\n{}".format(decoded_token)
)
return token
def sso_authenticator_request(
instance_url: str, account_name: str, login_name: str
) -> dict:
"""
When logging in with SSO, a local web server is started on a random port, which listens for a redirect from the IdP.
A request is sent to the `/session/authenticator-request` endpoint, which returns a ssoUrl and proofKey, which the
user then opens the provided ssoUrl in their browser and authenticates with their IdP.
Once this has been done, the IdP then makes a POST request to the `fed/login` endpoint with the SAML response,
which Snowflake then validates. The user is then redirected back to the local web server, with the URL
being in the url - `http://localhost:XXX/?token=ABC`.
We then extract the token from the URL, which is passed to `session/authenticate-request`, with the
`TOKEN` as the returned token, `PROOF_KEY` as the proof key from the initial step (to ensure it's a valid request),
and `AUTHENTICATOR` set to `EXTERNALBROWSER`.
Args:
instance_url: The instance_url returned from validate-url
account_name: The Snowflake account name, without the region.
login_name: The login name to authenticate with, this must be the LOGIN_NAME of the user in Snowflake,
and the email address of the user in the IdP.
Returns:
Dict of token, proof_key - The proof key is used in the final step.
"""
# First, we need to listen on localhost:{PORT} for the redirect from the IdP. The port doesn't
# really matter, we can try and bind on a dynamic port.
# Create a socket and bind to a random port
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("localhost", 0))
# Accept connections, we don't care about backlog.
server_socket.listen(0)
# The callback port is needed, as we need to pass it to Snowflake in the login request (BROWSER_MODE_REDIRECT_PORT).
callback_port = server_socket.getsockname()[1]
logging.debug("Listening on http://localhost:%s", callback_port)
authenticator_request_payload = {
"data": {
"CLIENT_APP_ID": "Snowflake UI",
"CLIENT_APP_VERSION": snowsight_client_app_version(),
"ACCOUNT_NAME": account_name.upper(),
"LOGIN_NAME": login_name,
"AUTHENTICATOR": "EXTERNALBROWSER",
"BROWSER_MODE_REDIRECT_PORT": str(callback_port),
}
}
data_json = json.dumps(authenticator_request_payload).encode("utf-8")
headers = {
"Content-Type": "application/json",
"User-Agent": fake_user_agent(),
}
url = f"{instance_url}/session/authenticator-request?__uiAppName=Login"
logging.debug(
"[POST REQUEST] URL: %s - Headers: %s - Body: %s",
url,
headers,
authenticator_request_payload,
)
req = urllib.request.Request(url, data=data_json, headers=headers, method="POST")
try:
with urllib.request.urlopen(req) as response:
response_data = json.loads(response.read().decode())
logging.debug("[RESPONSE] - %s", response_data)
# SSO URL is the URL to redirect the user to, so they can authenticate with their IdP.
# print this for the user to open it in their browser, as the default browser might be wrong.
sso_url = response_data["data"]["ssoUrl"]
# The proof key is used to verify the response from the IdP, we need to store this for later.
proof_key = response_data["data"]["proofKey"]
logging.debug("Proof key - %s", proof_key)
print(
"Please open the following URL in your browser and authenticate with your IdP."
)
print(sso_url)
# This snippet has been shamelessly borrowed from Snowflakes Python connector, as it's a nice way to handle
# the redirect from the IdP.
# [See webbrowser.py](https://github.com/snowflakedb/snowflake-connector-python/blob/main/src/snowflake/connector/auth/webbrowser.py#L117)
logging.debug("Waiting for redirect from IdP..")
token = token_socket_listener(server_socket)
logging.debug("found token for SSO - %s", token)
return {"token": token, "proof_key": proof_key}
except urllib.error.URLError as e:
logging.debug(e.reason)
def token_socket_listener(server_socket: socket) -> str:
"""
Listens on the provided socket for a redirect from the IdP, returning the token.
Args:
server_socket: The socket to listen on.
Returns:
The token from the URL, if it exists.
"""
# This needs to be in a function, so when we have the token we can "return".
while True:
socket_client, _ = server_socket.accept()
try:
data = socket_client.recv(16384).decode("utf-8").split("\r\n")
logging.debug("Received data: %s", data)
# This returns as a list, as it's chunked.
logging.debug("Finding token..")
for line in data:
logging.debug("Line: %s", line)
if line.startswith("GET /?token="):
token = line.split(" ")[1].split("=")[1]
logging.info("found token? %s", token)
return token
finally:
socket_client.shutdown(socket.SHUT_RDWR)
socket_client.close()
def snowsight_login(
login_method: str,
account_identifier: str,
login_name: str,
password: Optional[str] = None,
duo_passcode: Optional[str] = None,
private_key_path: Optional[str] = None,
private_key_password: Optional[str] = None,
) -> dict:
"""
Logs into Snowsight using the specified login method.
Args:
login_method: The login method to use, one of: `password`, `sso`, `duo`, `duo_passcode`, `private_key`
account_identifier: The account identifier to use, usually `https://<ACCOUNT_NAME>.<REGION>.snowflakecomputing.com`
login_name: The login name to use, usually the username, but can be different for SSO.
password: The password to use, if using username/password authentication.
duo_passcode: The Duo passcode to use, if using Duo authentication.
private_key_path: Path to the private key file, if using private key authentication.
private_key_password: Password for the private key file, if it has one.