This repository was archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
283 lines (234 loc) · 8.87 KB
/
utils.py
File metadata and controls
283 lines (234 loc) · 8.87 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
"""
Copyright 2023 Cognitive Scale, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import json
import base64
import hashlib
import logging
import urllib.parse
from collections import namedtuple
from datetime import datetime, timezone
from pathlib import Path
from typing import Union
import jwt
from pytimeparse2 import parse
from requests.exceptions import HTTPError
from requests import request
from .exceptions import BadTokenException, AuthenticationHeaderError
def md5sum(file_name, blocksize=65536):
"""
Computes md5sum on a fileself.
:param file_name: The name of the file on which to compute the md5sum.
:param blocksize: Blocksize used to compute md5 sum (default: 65536)
:return: Hexdigest of md5sum for file.
"""
md5 = hashlib.md5()
with open(file_name, "rb") as filed:
for block in iter(lambda: filed.read(blocksize), b""):
md5.update(block)
return md5.hexdigest()
def is_notebook() -> bool:
"""
Checks if the shell is in a notebook.
:return: `True` if the shell is for a notebook, `False` otherwise
"""
try:
from IPython import get_ipython # pylint: disable=import-outside-toplevel
shell = get_ipython().__class__.__name__
if shell == "ZMQInteractiveShell":
return True # Jupyter notebook or console
if shell == "TerminalInteractiveShell":
return False # Terminal running IPython
return False # Other type (?)
except (NameError, ImportError):
return False
def log_message(msg: str, log: logging.Logger, *args, level=logging.INFO, **kwargs):
"""
Logs a message.
:param msg: Message to log
:param log: logger where message should be logged
:param level: optional log level, defaults to INFO
:param args: a tuple of arguments passed to the logger
:param kwargs: a dictionary of keyword arguments passed to the logger
"""
if is_notebook():
log.debug(msg, *args, **kwargs)
else:
log.log(level, msg, *args, **kwargs)
def b64encode(byts: bytes) -> str:
"""
Returns a string from an iterable collection of bytes.
"""
encoded = base64.b64encode(byts)
return encoded.decode("utf-8")
def b64decode(string: str) -> bytes:
"""
Returns an iterable collection of bytes representing a base-64 encoding of a given string.
"""
return base64.decodebytes(string.encode("utf-8"))
def named_dict(obj):
"""
Returns a named tuple for the given object if it's a dictionary or list;
otherwise it returns the object itself.
"""
if isinstance(obj, dict):
for key, value in obj.items():
obj[key] = named_dict(value)
return namedtuple("NamedDict", obj.keys())(**obj)
if isinstance(obj, list):
return [named_dict(item) for item in obj]
return obj
def decode_JWT(*args) -> tuple:
# pylint: disable=invalid-name
"""
thin wrapper around jwt.decode. This function exists for better error handling of the
jwt exceptions.
"""
invalid_token_msg = "Your Cortex Token is invalid: "
try:
headers = jwt.get_unverified_header(*args)
payload = jwt.decode(*args, options={"verify_signature": False ,})
# there are places in the sdk where we try to decode 'any ol token' before sending the token
# to auth to get verified therefore, here we have some reasonable checks to make sure that
# this is a cortex token by checking the JWT keys exist
if not payload.get("aud") or not payload.get("sub") or not payload.get("exp"):
raise BadTokenException(invalid_token_msg)
return (headers, payload)
except jwt.exceptions.DecodeError as err: # pylint: disable=protected-access
raise BadTokenException(invalid_token_msg.format(err)) from err
def verify_JWT(token, config=None):
# pylint: disable=invalid-name
"""
thin wrapper around jwt.decode. This function exists for better error handling of the
jwt exceptions.
"""
try:
decode_JWT(token)
return token
except BadTokenException:
return generate_token(config)
def _get_fabric_info(config: dict, verify_ssl_cert: Union[bool, str]=True):
uri = config.get("url") + "/fabric/v4/info"
headers = {"Content-Type": "application/json"}
return request("GET", uri, headers=headers, verify=verify_ssl_cert,).json()
def _get_fabric_server_ts(config: dict, verify_ssl_cert: Union[bool, str]=True):
return _get_fabric_info(config, verify_ssl_cert).get("serverTs")
def generate_token(config, verify_ssl_cert: Union[bool, str, None]=True, validity: Union[int, str, None]='2m'):
"""
Use the Personal Access Token (JWK) obtained from Cortex's console
to generate JWTs to access cortex services..
:param verify_ssl_cert: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
:param validity: (optional) Token validity duration. Defaults to `2m`
"""
try:
jwk = config.get("jwk")
server_ts = int(
_get_fabric_server_ts(config, verify_ssl_cert) / 1000
) # fabric info returns serverTs in milliseconds
# key = jwkLib.JWK.from_json(json.dumps(config.get("jwk")))
alg = 'EdDSA'
key = jwt.get_algorithm_by_name(alg).from_jwk(jwk)
server_ts_dt = datetime.fromtimestamp(server_ts, tz=timezone.utc)
expiry = server_ts_dt + parse(validity, as_timedelta=True)
token_payload = {
"iss": config.get("issuer"),
"aud": config.get("audience"),
"sub": config.get("username"),
"iat": server_ts / 1000,
"exp": expiry
}
token = jwt.encode(
payload=token_payload,
key=key,
headers={
"alg": alg,
"kid": jwk['kid']
},
)
return token
except Exception as err:
gen_token_msg = (
"Unable to generate a JWT token, " "check PAT config or cortex profile: {}"
).format(err)
raise BadTokenException(gen_token_msg) from err
def get_cortex_profile(profile_name=None):
"""
Gets the current cortex profile or the profile that matches the optional given name.
"""
cortex_config_path = Path.home() / ".cortex" / "config"
if cortex_config_path.exists():
with cortex_config_path.open() as filed:
cortex_config = json.load(filed)
if profile_name is None:
profile_name = cortex_config.get("currentProfile")
return cortex_config.get("profiles", {}).get(profile_name, {})
return {}
def get_logger(name):
"""
Gets a logger with the given name.
"""
log = logging.getLogger(name)
formatter = logging.Formatter(
"%(asctime)s - %(levelname)s - %(name)s/%(module)s: %(message)s"
)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
log.addHandler(handler)
log.setLevel(logging.INFO)
return log
def json_str(val):
"""
Get the string representation of a json object.
"""
try:
return json.dumps(val)
except TypeError:
return str(val)
def base64decode_jsonstring(base64encoded_jsonstring: str):
"""
Loads a json from a base64 encoded json string.
:param base64encoded_jsonstring:
:return:
"""
return json.loads(
base64.urlsafe_b64decode(base64encoded_jsonstring).decode("utf-8")
)
def raise_for_status_with_detail(resp):
"""
wrap raise_for_status and attempt give detailed reason for api failure
re-raise HTTPError for normal flow
:param resp: python request resp
:return:
"""
try:
resp.raise_for_status()
except HTTPError as http_exception:
try:
log_message(
msg=resp.text, log=get_logger("http_status"), level=logging.ERROR
)
finally:
raise http_exception
if resp.status_code == 302:
raise AuthenticationHeaderError(
f'Authentication error: {resp.headers.get("X-Auth-Error")}'
)
def parse_string(string: str):
"""
parse a given string and apply common encoding/substitution rules
:param string: the string to parse
:return:
"""
# Replaces special characters like / with %2F (URL encoding)
return urllib.parse.quote(string, safe="")