-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutil.py
405 lines (318 loc) · 12.3 KB
/
util.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
# Copyright 2022 Google Inc.
#
# 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
#
# http://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.
"""
Module for internal utilities.
"""
import os as _os
import json as _json
import typing as _typing
import dataclasses as _dataclasses
import datetime as _dt
import enum as _enum
from flask import Request as _Request
from functions_framework import logging as _logging
from firebase_admin import auth as _auth
from firebase_admin import app_check as _app_check
P = _typing.ParamSpec("P")
R = _typing.TypeVar("R")
class Sentinel:
"""Internal class for RESET_VALUE."""
def __init__(self, description):
self.description = description
def __eq__(self, other):
return isinstance(other,
Sentinel) and self.description == other.description
def copy_func_kwargs(
func_with_kwargs: _typing.Callable[P, _typing.Any], # pylint: disable=unused-argument
) -> _typing.Callable[[_typing.Callable[..., R]], _typing.Callable[P, R]]:
def return_func(func: _typing.Callable[..., R]) -> _typing.Callable[P, R]:
return _typing.cast(_typing.Callable[P, R], func)
return return_func
def set_func_endpoint_attr(
func: _typing.Callable[P, _typing.Any],
endpoint: _typing.Any,
) -> _typing.Callable[P, _typing.Any]:
setattr(func, "__firebase_endpoint__", endpoint)
return func
def set_required_apis_attr(
func: _typing.Callable[P, _typing.Any],
required_apis: list,
):
"""Set the required APIs for the current function."""
setattr(func, "__required_apis", required_apis)
return func
def prune_nones(obj: dict) -> dict:
for key in list(obj.keys()):
if obj[key] is None:
del obj[key]
elif isinstance(obj[key], dict):
prune_nones(obj[key])
return obj
def deep_merge(dict1, dict2):
result = dict1.copy()
for key, value in dict2.items():
if isinstance(value, dict):
node = result.get(key, {})
result[key] = deep_merge(node, value)
else:
result[key] = value
return result
def valid_on_call_request(request: _Request) -> bool:
"""Validate request"""
if (_on_call_valid_method(request) and
_on_call_valid_content_type(request) and
_on_call_valid_body(request)):
return True
return False
def convert_keys_to_camel_case(
data: dict[str, _typing.Any]) -> dict[str, _typing.Any]:
def snake_to_camel(word: str) -> str:
components = word.split("_")
return components[0] + "".join(x.capitalize() for x in components[1:])
return {snake_to_camel(key): value for key, value in data.items()}
def _on_call_valid_body(request: _Request) -> bool:
"""The body must not be empty."""
if request.json is None:
_logging.warning("Request is missing body.")
return False
# The body must have data.
if "data" not in request.json:
_logging.warning("Request body is missing data.", request.json)
return False
extra_keys = {
key: request.json[key] for key in request.json.keys() if key != "data"
}
if len(extra_keys) != 0:
_logging.warning(
"Request body has extra fields: %s",
"".join(f"{key}: {value}," for (key, value) in extra_keys.items()),
)
return False
return True
def _on_call_valid_method(request: _Request) -> bool:
"""Make sure it's a POST."""
if request.method != "POST":
_logging.warning("Request has invalid method. %s", request.method)
return False
return True
def _on_call_valid_content_type(request: _Request) -> bool:
"""Validate content"""
content_type: str | None = request.headers.get("Content-Type")
if content_type is None:
_logging.warning("Request is missing Content-Type.")
return False
# If it has a charset, just ignore it for now.
try:
semi_colon = content_type.index(";")
if semi_colon >= 0:
content_type = content_type[0:semi_colon].strip()
except ValueError:
pass
# Check that the Content-Type is JSON.
if content_type.lower() != "application/json":
_logging.warning("Request has incorrect Content-Type: %s", content_type)
return False
return True
class OnCallTokenState(_enum.Enum):
"""
The status of a token.
"""
MISSING = "MISSING"
"""
There is no token, e.g. unauthenticated requests.
"""
VALID = "VALID"
"""
The token is valid.
"""
INVALID = "INVALID"
"""
The token is invalid.
"""
@_dataclasses.dataclass()
class _OnCallTokenVerification:
"""
Internal class used to hold verification information of tokens used in
on_call https requests (auth + app check tokens).
"""
app: OnCallTokenState = OnCallTokenState.INVALID
app_token: dict[str, _typing.Any] | None = None
auth: OnCallTokenState = OnCallTokenState.INVALID
auth_token: dict | None = None
def as_dict(self) -> dict:
"""Set dictionary"""
return {
"app": self.app.value if self.app is not None else None,
"auth": self.auth.value if self.auth is not None else None,
}
def _on_call_check_auth_token(
request: _Request
) -> None | _typing.Literal[OnCallTokenState.INVALID] | dict[str, _typing.Any]:
"""Validates the auth token in a callable request."""
authorization = request.headers.get("Authorization")
if authorization is None:
return None
if not authorization.startswith("Bearer "):
_logging.error("Error validating token: Not a bearer token")
return OnCallTokenState.INVALID
try:
id_token = authorization.replace("Bearer ", "")
auth_token = _auth.verify_id_token(id_token)
return auth_token
# pylint: disable=broad-except
except Exception as err:
_logging.error(f"Error validating token: {err}")
return OnCallTokenState.INVALID
return OnCallTokenState.INVALID
def _on_call_check_app_token(
request: _Request
) -> None | _typing.Literal[OnCallTokenState.INVALID] | dict[str, _typing.Any]:
"""Validates the app token in a callable request."""
app_check = request.headers.get("X-Firebase-AppCheck")
if app_check is None:
return None
try:
app_token = _app_check.verify_token(app_check)
return app_token
# pylint: disable=broad-except
except Exception as err:
_logging.error(f"Error validating token: {err}")
return OnCallTokenState.INVALID
def on_call_check_tokens(request: _Request,) -> _OnCallTokenVerification:
"""Check tokens"""
verifications = _OnCallTokenVerification()
auth_token = _on_call_check_auth_token(request)
if auth_token is None:
verifications.auth = OnCallTokenState.MISSING
elif isinstance(auth_token, dict):
verifications.auth = OnCallTokenState.VALID
verifications.auth_token = auth_token
app_token = _on_call_check_app_token(request)
if app_token is None:
verifications.app = OnCallTokenState.MISSING
elif isinstance(app_token, dict):
verifications.app = OnCallTokenState.VALID
verifications.app_token = app_token
log_payload = {
**verifications.as_dict(),
"logging.googleapis.com/labels": {
"firebase-log-type": "callable-request-verification",
},
}
errs = []
if verifications.app == OnCallTokenState.INVALID:
errs.append(("AppCheck token was rejected.", log_payload))
if verifications.auth == OnCallTokenState.INVALID:
errs.append(("Auth token was rejected.", log_payload))
if len(errs) == 0:
_logging.info("Callable request verification passed: %s", log_payload)
else:
_logging.warning(f"Callable request verification failed: ${errs}",
log_payload)
return verifications
@_dataclasses.dataclass(frozen=True)
class FirebaseConfig():
"""
A collection of configuration options needed to
initialize a firebase App.
"""
storage_bucket: str | None
"""
The name of the Google Cloud Storage bucket used for storing application data.
This is the bucket name without any prefixes or additions (without "gs://").
"""
# TODO more to be added later when they are required
def firebase_config() -> None | FirebaseConfig:
config_file = _os.getenv("FIREBASE_CONFIG")
if not config_file:
return None
if config_file.startswith("{"):
json_str = config_file
else:
# Firebase Tools will always use a JSON blob in prod, but docs
# explicitly state that the user can set the env to a file:
# https://firebase.google.com/docs/admin/setup#initialize-without-parameters
try:
with open(config_file, "r", encoding="utf8") as json_file:
json_str = json_file.read()
except Exception as err:
raise ValueError(
f"Unable to read file {config_file}. {err}") from err
try:
json_data: dict = _json.loads(json_str)
except Exception as err:
raise ValueError(
f'FIREBASE_CONFIG JSON string "{json_str}" is not valid json. {err}'
) from err
return FirebaseConfig(storage_bucket=json_data.get("storageBucket"))
def nanoseconds_timestamp_conversion(time: str) -> _dt.datetime:
"""Converts a nanosecond timestamp and returns a datetime object of the current time in UTC"""
# Separate the date and time part from the nanoseconds.
datetime_str, nanosecond_str = time.replace("Z", "").replace("z",
"").split(".")
# Parse the date and time part of the string.
event_time = _dt.datetime.strptime(datetime_str, "%Y-%m-%dT%H:%M:%S")
# Add the microseconds and timezone.
event_time = event_time.replace(microsecond=int(nanosecond_str[:6]),
tzinfo=_dt.timezone.utc)
return event_time
def second_timestamp_conversion(time: str) -> _dt.datetime:
"""Converts a second timestamp and returns a datetime object of the current time in UTC"""
return _dt.datetime.strptime(
time,
"%Y-%m-%dT%H:%M:%S%z",
)
class PrecisionTimestamp(_enum.Enum):
"""
The status of a token.
"""
NANOSECONDS = "NANOSECONDS"
MICROSECONDS = "MICROSECONDS"
SECONDS = "SECONDS"
def get_precision_timestamp(time: str) -> PrecisionTimestamp:
"""Return a bool which indicates if the timestamp is in nanoseconds"""
# Split the string into date-time and fraction of second
try:
_, s_fraction = time.split(".")
except ValueError:
return PrecisionTimestamp.SECONDS
# Split the fraction from the timezone specifier ('Z' or 'z')
s_fraction, _ = s_fraction.split(
"Z") if "Z" in s_fraction else s_fraction.split("z")
# If the fraction is more than 6 digits long, it's a nanosecond timestamp
if len(s_fraction) > 6:
return PrecisionTimestamp.NANOSECONDS
else:
return PrecisionTimestamp.MICROSECONDS
def timestamp_conversion(time: str) -> _dt.datetime:
"""Converts a timestamp and returns a datetime object of the current time in UTC"""
precision_timestamp = get_precision_timestamp(time)
if precision_timestamp == PrecisionTimestamp.NANOSECONDS:
return nanoseconds_timestamp_conversion(time)
elif precision_timestamp == PrecisionTimestamp.MICROSECONDS:
return microsecond_timestamp_conversion(time)
elif precision_timestamp == PrecisionTimestamp.SECONDS:
return second_timestamp_conversion(time)
raise ValueError("Invalid timestamp")
def microsecond_timestamp_conversion(time: str) -> _dt.datetime:
"""Converts a microsecond timestamp and returns a datetime object of the current time in UTC"""
return _dt.datetime.strptime(
time,
"%Y-%m-%dT%H:%M:%S.%f%z",
)
def normalize_path(path: str) -> str:
"""
Normalize a path string to a consistent format.
"""
return path.strip("/")