-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_auth_provider.py
More file actions
515 lines (446 loc) · 16.1 KB
/
_auth_provider.py
File metadata and controls
515 lines (446 loc) · 16.1 KB
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
import errno
import json
import os
import platform
import stat
import sys
import time
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Dict
from urllib.parse import parse_qs
import jwt
import msal
import tenacity as tn
from azure.identity import ManagedIdentityCredential
from msal_extensions.persistence import FilePersistence
from msal_extensions.token_cache import PersistedTokenCache
from ._retry_strategy import _log_retry_info, _return_last_value
if not sys.platform.startswith("linux"):
from msal_extensions import build_encrypted_persistence
def scope_for_resource(resource_id):
return f"{resource_id}/.default"
def _maybe_nfs_exception(exception):
return isinstance(exception, OSError) and (
exception.errno in (errno.EAGAIN, errno.ESTALE)
)
def get_token_dir():
return os.path.expanduser("~/.sumo")
def get_token_path(resource_id, suffix, case_uuid=None):
if case_uuid is not None:
return os.path.join(
get_token_dir(),
str(resource_id) + "+" + str(case_uuid) + suffix,
)
else:
return os.path.join(get_token_dir(), str(resource_id) + suffix)
class AuthProvider:
def __init__(self, resource_id):
self._resource_id = resource_id
self._scope = scope_for_resource(resource_id)
self._app = None
self._login_timeout_minutes = 5
os.system("") # Ensure color init on all platforms (win10)
return
@tn.retry(
retry=tn.retry_if_exception(_maybe_nfs_exception),
stop=tn.stop_after_attempt(6),
wait=(
tn.wait_exponential(multiplier=0.5, exp_base=2)
+ tn.wait_random_exponential(multiplier=0.5, exp_base=2)
),
retry_error_callback=_return_last_value,
before_sleep=_log_retry_info,
)
def get_token(self):
accounts = self._app.get_accounts()
if len(accounts) == 0:
return None
result = self._app.acquire_token_silent([self._scope], accounts[0])
if result is None:
return None
# ELSE
return result["access_token"]
def get_authorization(self) -> Dict:
token = self.get_token()
if token is None:
return {}
return {"Authorization": "Bearer " + token}
def store_shared_access_key_for_case(self, case_uuid, token):
os.makedirs(get_token_dir(), mode=0o700, exist_ok=True)
with open(
get_token_path(self._resource_id, ".sharedkey", case_uuid),
"w",
) as f:
f.write(token)
protect_token_cache(self._resource_id, ".sharedkey", case_uuid)
return
def has_case_token(self, case_uuid):
return os.path.exists(
get_token_path(self._resource_id, ".sharedkey", case_uuid)
)
pass
class AuthProviderNone(AuthProvider):
def get_token(self):
raise Exception("No valid authorization provider found.")
pass
class AuthProviderSilent(AuthProvider):
def __init__(self, client_id, authority, resource_id):
super().__init__(resource_id)
cache = get_token_cache(resource_id, ".token")
self._app = msal.PublicClientApplication(
client_id=client_id, authority=authority, token_cache=cache
)
self._resource_id = resource_id
self._scope = scope_for_resource(resource_id)
class AuthProviderAccessToken(AuthProvider):
def __init__(self, access_token):
self._access_token = access_token
payload = jwt.decode(access_token, options={"verify_signature": False})
self._expires = payload["exp"]
self._resource_id = payload["aud"]
return
def get_token(self):
if time.time() >= self._expires:
raise ValueError("Access token has expired.")
# ELSE
return self._access_token
pass
class AuthProviderRefreshToken(AuthProvider):
def __init__(self, refresh_token, client_id, authority, resource_id):
super().__init__(resource_id)
self._app = msal.PublicClientApplication(
client_id=client_id, authority=authority
)
self._scope = scope_for_resource(resource_id)
self._app.acquire_token_by_refresh_token(refresh_token, [self._scope])
return
pass
@tn.retry(
retry=tn.retry_if_exception(_maybe_nfs_exception),
stop=tn.stop_after_attempt(6),
wait=(
tn.wait_exponential(multiplier=0.5, exp_base=2)
+ tn.wait_random_exponential(multiplier=0.5, exp_base=2)
),
retry_error_callback=_return_last_value,
before_sleep=_log_retry_info,
)
def get_token_cache(resource_id, suffix):
# https://github.com/AzureAD/microsoft-authentication-extensions-\
# for-python
# Encryption not supported on linux servers like rgs, and
# neither is common usage from many cluster nodes.
# Encryption is supported on Windows and Mac.
cache = None
token_path = get_token_path(resource_id, suffix)
if sys.platform.startswith("linux"):
persistence = FilePersistence(token_path)
cache = PersistedTokenCache(persistence)
else:
if os.path.exists(token_path):
encrypted_persistence = build_encrypted_persistence(token_path)
try:
token = encrypted_persistence.load()
except Exception:
# This code will encrypt an unencrypted existing file
token = FilePersistence(token_path).load()
with open(token_path, "w") as f:
f.truncate()
pass
encrypted_persistence.save(token)
pass
pass
persistence = build_encrypted_persistence(token_path)
cache = PersistedTokenCache(persistence)
pass
return cache
@tn.retry(
retry=tn.retry_if_exception(_maybe_nfs_exception),
stop=tn.stop_after_attempt(6),
wait=(
tn.wait_exponential(multiplier=0.5, exp_base=2)
+ tn.wait_random_exponential(multiplier=0.5, exp_base=2)
),
retry_error_callback=_return_last_value,
before_sleep=_log_retry_info,
)
def protect_token_cache(resource_id, suffix, case_uuid=None):
token_path = get_token_path(resource_id, suffix, case_uuid)
if sys.platform.startswith("linux") or sys.platform == "darwin":
filemode = stat.filemode(os.stat(token_path).st_mode)
if filemode != "-rw-------":
os.chmod(token_path, 0o600)
folder = os.path.dirname(token_path)
foldermode = stat.filemode(os.stat(folder).st_mode)
if foldermode != "drwx------":
os.chmod(os.path.dirname(token_path), 0o700)
pass
pass
return
pass
class AuthProviderInteractive(AuthProvider):
def __init__(self, client_id, authority, resource_id):
super().__init__(resource_id)
cache = get_token_cache(resource_id, ".token")
self._app = msal.PublicClientApplication(
client_id=client_id, authority=authority, token_cache=cache
)
self._resource_id = resource_id
self._scope = scope_for_resource(resource_id)
if self.get_token() is None:
self.login()
pass
return
@tn.retry(
retry=tn.retry_if_exception(_maybe_nfs_exception),
stop=tn.stop_after_attempt(6),
wait=(
tn.wait_exponential(multiplier=0.5, exp_base=2)
+ tn.wait_random_exponential(multiplier=0.5, exp_base=2)
),
retry_error_callback=_return_last_value,
before_sleep=_log_retry_info,
)
def login(self):
scopes = [self._scope + " offline_access"]
print(
"\n\n \033[31m NOTE! \033[0m"
+ " Please login to Equinor Azure to enable Sumo access: "
+ "we are opening a login web-page for you in your browser."
+ "\nYou should complete your login within "
+ str(self._login_timeout_minutes)
+ " minutes, "
+ "that is before "
+ str(
(
datetime.now()
+ timedelta(minutes=self._login_timeout_minutes)
).strftime("%H:%M:%S")
)
)
try:
result = self._app.acquire_token_interactive(
scopes, timeout=(self._login_timeout_minutes * 60)
)
if "error" in result:
print(
"\n\n \033[31m Error during Equinor Azure login "
"for Sumo access: \033[0m"
)
print("Err: ", json.dumps(result, indent=4))
return
except Exception:
print(
"\n\n \033[31m Failed Equinor Azure login for Sumo access, "
"one possible reason is timeout \033[0m"
)
return
protect_token_cache(self._resource_id, ".token")
print(
"Equinor Azure login for Sumo access was successful (interactive)"
)
return
pass
class AuthProviderDeviceCode(AuthProvider):
def __init__(self, client_id, authority, resource_id):
super().__init__(resource_id)
cache = get_token_cache(resource_id, ".token")
self._app = msal.PublicClientApplication(
client_id=client_id, authority=authority, token_cache=cache
)
self._resource_id = resource_id
self._scope = scope_for_resource(resource_id)
if self.get_token() is None:
self.login()
pass
return
@tn.retry(
retry=tn.retry_if_exception(_maybe_nfs_exception),
stop=tn.stop_after_attempt(6),
wait=(
tn.wait_exponential(multiplier=0.5, exp_base=2)
+ tn.wait_random_exponential(multiplier=0.5, exp_base=2)
),
retry_error_callback=_return_last_value,
before_sleep=_log_retry_info,
)
def login(self):
try:
scopes = [self._scope + " offline_access"]
flow = self._app.initiate_device_flow(scopes)
if "error" in flow:
print(
"\n\n \033[31m"
+ "Failed to initiate device-code login. Err: %s"
+ "\033[0m" % json.dumps(flow, indent=4)
)
return
flow["expires_at"] = (
int(time.time()) + self._login_timeout_minutes * 60
)
print(
"\033[31m"
+ " NOTE! Please login to Equinor Azure to enable Sumo access:"
+ flow["message"]
+ " \033[0m"
+ "\nYou should complete your login within a few minutes"
)
result = self._app.acquire_token_by_device_flow(flow)
if "error" in result:
print(
"\n\n \033[31m Error during Equinor Azure login "
"for Sumo access: \033[0m"
)
print("Err: ", json.dumps(result, indent=4))
return
except Exception:
print(
"\n\n \033[31m Failed Equinor Azure login for Sumo access, "
"one possible reason is timeout \033[0m"
)
return
protect_token_cache(self._resource_id, ".token")
print(
"Equinor Azure login for Sumo access was successful (device-code)"
)
return
pass
class AuthProviderManaged(AuthProvider):
def __init__(self, resource_id):
super().__init__(resource_id)
self._app = ManagedIdentityCredential()
self._scope = scope_for_resource(resource_id)
return
@tn.retry(
retry=tn.retry_if_exception(_maybe_nfs_exception),
stop=tn.stop_after_attempt(6),
wait=(
tn.wait_exponential(multiplier=0.5, exp_base=2)
+ tn.wait_random_exponential(multiplier=0.5, exp_base=2)
),
retry_error_callback=_return_last_value,
before_sleep=_log_retry_info,
)
def get_token(self):
return self._app.get_token(self._scope).token
pass
class AuthProviderSumoToken(AuthProvider):
@tn.retry(
retry=tn.retry_if_exception(_maybe_nfs_exception),
stop=tn.stop_after_attempt(6),
wait=(
tn.wait_exponential(multiplier=0.5, exp_base=2)
+ tn.wait_random_exponential(multiplier=0.5, exp_base=2)
),
retry_error_callback=_return_last_value,
before_sleep=_log_retry_info,
)
def __init__(self, resource_id, case_uuid=None):
super().__init__(resource_id)
protect_token_cache(resource_id, ".sharedkey", case_uuid)
token_path = get_token_path(resource_id, ".sharedkey", case_uuid)
with open(token_path, "r") as f:
self._token = f.readline().strip()
return
def get_token(self):
return self._token
def get_authorization(self):
return {"X-SUMO-Token": self._token}
@tn.retry(
retry=tn.retry_if_exception(_maybe_nfs_exception),
stop=tn.stop_after_attempt(6),
wait=(
tn.wait_exponential(multiplier=0.5, exp_base=2)
+ tn.wait_random_exponential(multiplier=0.5, exp_base=2)
),
retry_error_callback=_return_last_value,
before_sleep=_log_retry_info,
)
def get_auth_provider(
client_id,
authority,
resource_id,
interactive=False,
access_token=None,
refresh_token=None,
devicecode=False,
case_uuid=None,
) -> AuthProvider:
if refresh_token:
return AuthProviderRefreshToken(
refresh_token, client_id, authority, resource_id
)
# ELSE
if access_token:
return AuthProviderAccessToken(access_token)
# ELSE
if os.path.exists(get_token_path(resource_id, ".sharedkey", case_uuid)):
return AuthProviderSumoToken(resource_id, case_uuid)
# ELSE
if os.path.exists(get_token_path(resource_id, ".sharedkey")):
return AuthProviderSumoToken(resource_id)
# ELSE
if os.path.exists(get_token_path(resource_id, ".token")):
auth_silent = AuthProviderSilent(client_id, authority, resource_id)
token = auth_silent.get_token()
if token is not None:
return auth_silent
pass
# ELSE
if interactive:
lockfile_path = Path.home() / ".config/chromium/SingletonLock"
if Path(lockfile_path).is_symlink() and not str(
Path(lockfile_path).resolve()
).__contains__(platform.node()):
# https://github.com/equinor/sumo-wrapper-python/issues/193
print(
"\n\n\033[1mDetected chromium lockfile for different node; using firefox to authenticate.\033[0m"
)
os.environ["BROWSER"] = "firefox"
pass
return AuthProviderInteractive(client_id, authority, resource_id)
# ELSE
if devicecode:
# Potential issues with device-code
# under Equinor compliant device policy
return AuthProviderDeviceCode(client_id, authority, resource_id)
# ELSE
if all(
os.getenv(x)
for x in [
"AZURE_FEDERATED_TOKEN_FILE",
"AZURE_TENANT_ID",
"AZURE_CLIENT_ID",
"AZURE_AUTHORITY_HOST",
]
):
return AuthProviderManaged(resource_id)
# ELSE
return AuthProviderNone(resource_id)
def cleanup_shared_keys():
tokendir = get_token_dir()
if not os.path.exists(tokendir):
return
for f in os.listdir(tokendir):
ff = os.path.join(tokendir, f)
if os.path.isfile(ff):
(_, ext) = os.path.splitext(ff)
if ext.lower() == ".sharedkey":
try:
with open(ff, "r") as file:
token = file.read()
pq = parse_qs(token)
se = pq["se"][0]
end = datetime.strptime(se, "%Y-%m-%dT%H:%M:%S.%fZ")
now = datetime.now(timezone.utc)
if now.timestamp() > end.timestamp():
os.unlink(ff)
pass
pass
pass
except Exception:
pass
pass
pass
return