Skip to content

Commit 684d715

Browse files
committed
fix: simplify setting of the full component name
1 parent c120f70 commit 684d715

File tree

1 file changed

+16
-38
lines changed

1 file changed

+16
-38
lines changed

src/DIRAC/Core/Tornado/Server/private/BaseRequestHandler.py

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
from tornado.web import RequestHandler, HTTPError
1818
from tornado.ioloop import IOLoop
1919

20-
import DIRAC
21-
2220
from DIRAC import gConfig, gLogger, S_OK, S_ERROR
2321
from DIRAC.ConfigurationSystem.Client.Helpers.Operations import Operations
2422
from DIRAC.Core.Utilities import DErrno
@@ -176,10 +174,6 @@ class BaseRequestHandler(RequestHandler):
176174
Override the class variable ``SUPPORTED_METHODS`` by writing down the necessary methods there.
177175
Note that by default all HTTP methods are supported.
178176
179-
It is important to understand that the handler belongs to the system.
180-
The class variable ``SYSTEM_NAME`` displays the system name. By default it is taken from the module name.
181-
This value is used to generate the full component name, see :py:meth:`_getFullComponentName` method
182-
183177
This class also defines some variables for writing your handler's methods:
184178
185179
- ``DEFAULT_AUTHORIZATION`` describes the general authorization rules for the entire handler
@@ -200,7 +194,7 @@ class BaseRequestHandler(RequestHandler):
200194
Also, if necessary, you can create a new type of authorization by simply creating the appropriate method::
201195
202196
def _authzMYAUTH(self):
203-
'''Another authorization algoritm.'''
197+
'''Another authorization algorithm.'''
204198
# Do somthing
205199
return S_OK(credentials) # return user credentials as a dictionary
206200
@@ -212,13 +206,12 @@ def _authzMYAUTH(self):
212206
The class contains methods that require implementation:
213207
214208
- :py:meth:`_pre_initialize`
215-
- :py:meth:`_getCSAuthorizarionSection`
209+
- :py:meth:`_getCSAuthorizationSection`
216210
- :py:meth:`_getMethod`
217211
- :py:meth:`_getMethodArgs`
218212
219213
Some methods have basic behavior, but developers can rewrite them:
220214
221-
- :py:meth:`_getFullComponentName`
222215
- :py:meth:`_getComponentInfoDict`
223216
- :py:meth:`_monitorRequest`
224217
@@ -239,20 +232,20 @@ def _authzMYAUTH(self):
239232
At startup, :py:class:`HandlerManager <DIRAC.Core.Tornado.Server.HandlerManager.HandlerManager>` call :py:meth:`__pre_initialize`
240233
handler method that inspects the handler and its methods to generate tornados URLs of access to it:
241234
242-
- specifies the full name of the component, including the name of the system to which it belongs, see :py:meth:`_getFullComponentName`.
235+
- specifies the full name of the component, including the name of the system to which it belongs as <System>/<Component>.
243236
- initialization of the main authorization class, see :py:class:`AuthManager <DIRAC.Core.DISET.AuthManager.AuthManager>` for more details.
244237
- call :py:meth:`__pre_initialize` that should explore the handler, prepare all the necessary attributes and most importantly - return the list of URL tornadoes
245238
246239
The first request starts the process of initializing the handler, see the :py:meth:`initialize` method:
247240
248241
- load all registered identity providers for authentication with access token, see :py:meth:`__loadIdPs`.
249242
- create a ``cls.log`` logger that should be used in the children classes instead of directly ``gLogger`` (this allows to carry the ``tornadoComponent`` information, crutial for centralized logging)
250-
- initialization of the monitoring specific to this handler, see :py:meth:`__initMonitoring`.
243+
- initialization of the monitoring specific to this handler, see :py:meth:`_initMonitoring`.
251244
- initialization of the target handler that inherit this one, see :py:meth:`initializeHandler`.
252245
253246
Next, first of all the tornados prepare method is called which does the following:
254247
255-
- determines determines the name of the target method and checks its presence, see :py:meth:`_getMethod`.
248+
- determines the name of the target method and checks its presence, see :py:meth:`_getMethod`.
256249
- request monitoring, see :py:meth:`_monitorRequest`.
257250
- authentication request using one of the available algorithms called ``DEFAULT_AUTHENTICATION``, see :py:meth:`_gatherPeerCredentials` for more details.
258251
- and finally authorizing the request to access the component, see :py:meth:`authQuery <DIRAC.Core.DISET.AuthManager.AuthManager.authQuery>` for more details.
@@ -262,7 +255,7 @@ def _authzMYAUTH(self):
262255
263256
- execute the target method in an executor a separate thread.
264257
- defines the arguments of the target method, see :py:meth:`_getMethodArgs`.
265-
- initialization of the each request, see :py:meth:`initializeRequest`.
258+
- initialization of each request, see :py:meth:`initializeRequest`.
266259
- the result of the target method is processed in the main thread and returned to the client, see :py:meth:`__execute`.
267260
268261
"""
@@ -330,20 +323,18 @@ def __pre_initialize(cls) -> list:
330323
:returns: a list of URL (not the string with "https://..." but the tornado object)
331324
see http://www.tornadoweb.org/en/stable/web.html#tornado.web.URLSpec
332325
"""
333-
# Set full component name, e.g.: <System>/<Component>
334-
cls._fullComponentName = cls._getFullComponentName()
335326

336327
# Define base request path
337328
if not cls.DEFAULT_LOCATION:
338-
# By default use full component name as location
329+
# By default, use the full component name as location
339330
cls.DEFAULT_LOCATION = cls._fullComponentName
340331

341332
# SUPPORTED_METHODS should be a tuple
342333
if not isinstance(cls.SUPPORTED_METHODS, tuple):
343334
raise TypeError("SUPPORTED_METHODS should be a tuple")
344335

345336
# authorization manager initialization
346-
cls._authManager = AuthManager(cls._getCSAuthorizarionSection(cls._fullComponentName))
337+
cls._authManager = AuthManager(cls._getCSAuthorizationSection(cls._fullComponentName))
347338

348339
if not (urls := cls._pre_initialize()):
349340
cls.log.warn("no target method found", f"{cls.__name__}")
@@ -374,32 +365,19 @@ def _pre_initialize(cls) -> list:
374365
raise NotImplementedError("Please, create the _pre_initialize class method")
375366

376367
@classmethod
377-
def __initMonitoring(cls, fullComponentName: str, fullUrl: str) -> dict:
368+
def _initMonitoring(cls, fullComponentName: str, fullUrl: str) -> dict:
378369
"""
379370
Initialize the monitoring specific to this handler
380371
This has to be called only by :py:meth:`.__initialize`
381372
to ensure thread safety and unicity of the call.
382373
383-
:param componentName: relative URL ``/<System>/<Component>``
374+
:param fullComponentName: relative URL ``<System>/<Component>``
384375
:param fullUrl: full URl like ``https://<host>:<port>/<System>/<Component>``
385376
"""
386377
cls._stats = {"requests": 0, "monitorLastStatsUpdate": time.time()}
387378

388379
return S_OK()
389380

390-
@classmethod
391-
def _getFullComponentName(cls) -> str:
392-
"""Search the full name of the component, including the name of the system to which it belongs.
393-
CAN be implemented by developer.
394-
"""
395-
if cls.SYSTEM_NAME is None:
396-
# If the system name is not specified, it is taken from the module.
397-
cls.SYSTEM_NAME = ([m[:-6] for m in cls.__module__.split(".") if m.endswith("System")] or [None]).pop()
398-
if cls.COMPONENT_NAME is None:
399-
# If the service name is not specified, it is taken from the handler.
400-
cls.COMPONENT_NAME = cls.__name__[: -len("Handler")]
401-
return f"{cls.SYSTEM_NAME}/{cls.COMPONENT_NAME}" if cls.SYSTEM_NAME else cls.COMPONENT_NAME
402-
403381
@classmethod
404382
def __loadIdPs(cls) -> None:
405383
"""Load identity providers that will be used to verify tokens"""
@@ -412,24 +390,24 @@ def __loadIdPs(cls) -> None:
412390
if result["OK"]:
413391
cls._idp[result["Value"].issuer.strip("/")] = result["Value"]
414392
else:
415-
cls.log.error("Error getting IDP", f"{providerName}: {result['Message']}")
393+
cls.log.error("Error getting Identity Provider", f"{providerName}: {result['Message']}")
416394

417395
@classmethod
418-
def _getCSAuthorizarionSection(cls, fullComponentName: str) -> str:
396+
def _getCSAuthorizationSection(cls, fullComponentName: str) -> str:
419397
"""Search component authorization section in CS.
420398
SHOULD be implemented by developer.
421399
422-
:param fullComponentName: full component name, see :py:meth:`_getFullComponentName`
400+
:param fullComponentName: full component name <System>/<Component>
423401
"""
424-
raise NotImplementedError("Please, create the _getCSAuthorizarionSection class method")
402+
raise NotImplementedError("Please, create the _getCSAuthorizationSection class method")
425403

426404
@classmethod
427405
def _getComponentInfoDict(cls, fullComponentName: str, fullURL: str) -> dict:
428406
"""Fills the dictionary with information about the current component,
429407
e.g.: 'serviceName', 'serviceSectionPath', 'csPaths'.
430408
SHOULD be implemented by developer.
431409
432-
:param fullComponentName: full component name, see :py:meth:`_getFullComponentName`
410+
:param fullComponentName: full component name <System>/<Component>
433411
:param fullURL: incoming request path
434412
"""
435413
raise NotImplementedError("Please, create the _getComponentInfoDict class method")
@@ -470,7 +448,7 @@ def __initialize(cls, request):
470448
cls.log.info("Initializing method for first use", f"{cls._fullComponentName}, initializing..")
471449

472450
# component monitoring initialization
473-
cls.__initMonitoring(cls._fullComponentName, absoluteUrl)
451+
cls._initMonitoring(cls._fullComponentName, absoluteUrl)
474452

475453
cls._componentInfoDict = cls._getComponentInfoDict(cls._fullComponentName, absoluteUrl)
476454

0 commit comments

Comments
 (0)