3
3
from http import HTTPStatus
4
4
import json
5
5
from json .decoder import JSONDecodeError
6
- from logging import Logger
7
6
from typing import Any , Callable , Dict , List , NamedTuple , Optional , Union
8
7
import uuid
9
8
10
9
from dateutil import parser as date_parser
11
- from flask import request
10
+ from flask import current_app , request
12
11
from flask .wrappers import Request , Response
13
12
from flask_restful import abort , Resource
14
13
from sqlalchemy .orm .query import Query
@@ -1111,7 +1110,6 @@ class ApiBase(Resource):
1111
1110
def __init__ (
1112
1111
self ,
1113
1112
config : PbenchServerConfig ,
1114
- logger : Logger ,
1115
1113
* schemas : ApiSchema ,
1116
1114
always_enabled : bool = False ,
1117
1115
):
@@ -1130,7 +1128,6 @@ def __init__(
1130
1128
"""
1131
1129
super ().__init__ ()
1132
1130
self .config = config
1133
- self .logger = logger
1134
1131
self .schemas = ApiSchemaSet (* schemas )
1135
1132
self .always_enabled = always_enabled
1136
1133
@@ -1222,14 +1219,14 @@ def _check_authorization(self, mode: ApiAuthorization):
1222
1219
if user :
1223
1220
username = user .username
1224
1221
else :
1225
- self .logger .error ("User ID {} not found" , user_id )
1222
+ current_app .logger .error ("User ID {} not found" , user_id )
1226
1223
1227
1224
# The ADMIN authorization doesn't involve a target resource owner or
1228
1225
# access, so take care of that first as a special case. If there is
1229
1226
# an authenticated user, and that user holds ADMIN access rights, the
1230
1227
# check passes. Otherwise raise an "admin access" failure.
1231
1228
if mode .type == ApiAuthorizationType .ADMIN :
1232
- self .logger .debug (
1229
+ current_app .logger .debug (
1233
1230
"Authorizing {} access for {} to an administrative resource" ,
1234
1231
role ,
1235
1232
authorized_user ,
@@ -1244,7 +1241,7 @@ def _check_authorization(self, mode: ApiAuthorization):
1244
1241
1245
1242
access = mode .access
1246
1243
1247
- self .logger .debug (
1244
+ current_app .logger .debug (
1248
1245
"Authorizing {} access for {} to user {} ({}) with access {} using {}" ,
1249
1246
role ,
1250
1247
authorized_user ,
@@ -1274,7 +1271,7 @@ def _check_authorization(self, mode: ApiAuthorization):
1274
1271
if authorized_user is None :
1275
1272
# An unauthenticated user is never allowed to access private
1276
1273
# data nor to perform an potential mutation of data: REJECT
1277
- self .logger .warning (
1274
+ current_app .logger .warning (
1278
1275
"Attempt to {} user {} data without login" , role , username
1279
1276
)
1280
1277
raise UnauthorizedAccess (
@@ -1287,7 +1284,7 @@ def _check_authorization(self, mode: ApiAuthorization):
1287
1284
elif role != OperationCode .READ and user_id is None :
1288
1285
# No target user is specified, so we won't allow mutation of
1289
1286
# data: REJECT
1290
- self .logger .warning (
1287
+ current_app .logger .warning (
1291
1288
"Unauthorized attempt by {} to {} data with defaulted user" ,
1292
1289
authorized_user ,
1293
1290
role ,
@@ -1303,7 +1300,7 @@ def _check_authorization(self, mode: ApiAuthorization):
1303
1300
# We are mutating data, or reading private data, so the
1304
1301
# authenticated user must either be the owner of the data or
1305
1302
# must have ADMIN role: REJECT
1306
- self .logger .warning (
1303
+ current_app .logger .warning (
1307
1304
"Unauthorized attempt by {} to {} user {} data" ,
1308
1305
authorized_user ,
1309
1306
role ,
@@ -1391,7 +1388,7 @@ def _build_sql_query(
1391
1388
is_admin = authorized_user .is_admin () if authorized_user else False
1392
1389
query = base_query
1393
1390
1394
- self .logger .debug (
1391
+ current_app .logger .debug (
1395
1392
"QUERY auth ID {}, user {!r}, access {!r}, admin {}" ,
1396
1393
authorized_id ,
1397
1394
owner_id ,
@@ -1422,24 +1419,28 @@ def _build_sql_query(
1422
1419
owner_id and owner_id != authorized_id and not is_admin
1423
1420
):
1424
1421
query = query .filter (Dataset .access == Dataset .PUBLIC_ACCESS )
1425
- self .logger .debug ("QUERY: not self public" )
1422
+ current_app .logger .debug ("QUERY: not self public" )
1426
1423
elif access :
1427
1424
query = query .filter (Dataset .access == access )
1428
1425
if not owner_id and access == Dataset .PRIVATE_ACCESS and not is_admin :
1429
1426
query = query .filter (Dataset .owner_id == authorized_id )
1430
1427
user_term = True
1431
- self .logger .debug ("QUERY: user: {}, access: {}" , authorized_id , access )
1428
+ current_app .logger .debug (
1429
+ "QUERY: user: {}, access: {}" , authorized_id , access
1430
+ )
1432
1431
elif not owner_id and not is_admin :
1433
1432
query = query .filter (
1434
1433
(Dataset .owner_id == authorized_id )
1435
1434
| (Dataset .access == Dataset .PUBLIC_ACCESS )
1436
1435
)
1437
1436
user_term = True
1438
- self .logger .debug ("QUERY: self ({}) + public" , authorized_user .username )
1437
+ current_app .logger .debug (
1438
+ "QUERY: self ({}) + public" , authorized_user .username
1439
+ )
1439
1440
else :
1440
1441
# Either "user" was specified and will be added to the filter,
1441
1442
# or client is ADMIN and no access restrictions are required.
1442
- self .logger .debug (
1443
+ current_app .logger .debug (
1443
1444
"QUERY: default, user: {}" , owner_id if owner_id else authorized_user
1444
1445
)
1445
1446
@@ -1513,7 +1514,7 @@ def _dispatch(
1513
1514
1514
1515
api_name = self .__class__ .__name__
1515
1516
1516
- self .logger .info ("In {} {}: mime {}" , method , api_name , request .mimetype )
1517
+ current_app .logger .info ("In {} {}: mime {}" , method , api_name , request .mimetype )
1517
1518
1518
1519
if method is ApiMethod .GET :
1519
1520
execute = self ._get
@@ -1576,15 +1577,15 @@ def _dispatch(
1576
1577
ApiParams (body = body_params , query = query_params , uri = uri_params ),
1577
1578
)
1578
1579
except APIInternalError as e :
1579
- self .logger .exception ("{} {}" , api_name , e .details )
1580
+ current_app .logger .exception ("{} {}" , api_name , e .details )
1580
1581
abort (e .http_status , message = str (e ))
1581
1582
except APIAbort as e :
1582
1583
abort (e .http_status , message = str (e ))
1583
1584
except Exception :
1584
1585
# Construct an APIInternalError to get the UUID and standard return
1585
1586
# message.
1586
1587
x = APIInternalError ("Unexpected validation exception" )
1587
- self .logger .exception ("{} {}" , api_name , x .details )
1588
+ current_app .logger .exception ("{} {}" , api_name , x .details )
1588
1589
abort (x .http_status , message = str (x ))
1589
1590
1590
1591
# Automatically authorize the operation only if the API schema for the
@@ -1598,16 +1599,16 @@ def _dispatch(
1598
1599
try :
1599
1600
self ._check_authorization (auth_params )
1600
1601
except UnauthorizedAccess as e :
1601
- self .logger .warning ("{}: {}" , api_name , e )
1602
+ current_app .logger .warning ("{}: {}" , api_name , e )
1602
1603
abort (e .http_status , message = str (e ))
1603
1604
except APIInternalError as e :
1604
- self .logger .exception ("{} {}" , api_name , e .details )
1605
+ current_app .logger .exception ("{} {}" , api_name , e .details )
1605
1606
abort (e .http_status , message = str (e ))
1606
1607
except Exception :
1607
1608
# Construct an APIInternalError to get the UUID and standard return
1608
1609
# message.
1609
1610
x = APIInternalError ("Unexpected authorize exception" )
1610
- self .logger .exception ("{} {}" , api_name , x .details )
1611
+ current_app .logger .exception ("{} {}" , api_name , x .details )
1611
1612
abort (x .http_status , message = str (x ))
1612
1613
1613
1614
audit = None
@@ -1653,7 +1654,7 @@ def _dispatch(
1653
1654
)
1654
1655
return response
1655
1656
except APIInternalError as e :
1656
- self .logger .exception ("{} {}" , api_name , e .details )
1657
+ current_app .logger .exception ("{} {}" , api_name , e .details )
1657
1658
abort (e .http_status , message = str (e ))
1658
1659
except APIAbort as e :
1659
1660
if auditing ["finalize" ]:
@@ -1666,12 +1667,12 @@ def _dispatch(
1666
1667
attributes = attr ,
1667
1668
)
1668
1669
except Exception :
1669
- self .logger .error (
1670
+ current_app .logger .error (
1670
1671
"Unexpected exception on audit: {}" , json .dumps (auditing )
1671
1672
)
1672
1673
abort (e .http_status , message = str (e ))
1673
1674
except Exception as e :
1674
- self .logger .exception (
1675
+ current_app .logger .exception (
1675
1676
"Exception {} API error: {}: {!r}" , api_name , e , json .dumps (auditing )
1676
1677
)
1677
1678
if auditing ["finalize" ]:
0 commit comments