From 9ebbbcb875ab11235dbb13f85c7a6ee06920dcbe Mon Sep 17 00:00:00 2001 From: Federico Stagni Date: Fri, 14 Feb 2025 16:55:13 +0100 Subject: [PATCH] style: more pythonic code --- src/DIRAC/Core/LCG/GOCDBClient.py | 12 +++++------- src/DIRAC/Core/Security/IAMService.py | 6 +----- src/DIRAC/Core/Security/Locations.py | 9 +-------- src/DIRAC/Core/Security/VOMSService.py | 6 ++---- .../FrameworkSystem/Client/BundleDeliveryClient.py | 7 ++----- .../OccupancyPlugins/WLCGAccountingHTTPJson.py | 4 +--- src/DIRAC/Resources/Storage/StorageElement.py | 5 ++--- tests/System/client_core.sh | 2 +- 8 files changed, 15 insertions(+), 36 deletions(-) diff --git a/src/DIRAC/Core/LCG/GOCDBClient.py b/src/DIRAC/Core/LCG/GOCDBClient.py index d773ca4fbe6..99e89046039 100644 --- a/src/DIRAC/Core/LCG/GOCDBClient.py +++ b/src/DIRAC/Core/LCG/GOCDBClient.py @@ -209,11 +209,11 @@ def getHostnameDowntime(self, hostname, startDate=None, ongoing=False): if ongoing: params += "&ongoing_only=yes" - caPath = getCAsLocation() - try: response = requests.get( - "https://goc.egi.eu/gocdbpi/public/?method=get_downtime&topentity=" + params, verify=caPath, timeout=20 + "https://goc.egi.eu/gocdbpi/public/?method=get_downtime&topentity=" + params, + verify=getCAsLocation(), + timeout=20, ) response.raise_for_status() except requests.exceptions.RequestException as e: @@ -268,8 +268,7 @@ def _downTimeCurlDownload(self, entity=None, startDate=None): gocdb_ep = gocdb_ep + "&topentity=" + entity gocdb_ep = gocdb_ep + when + gocdbpi_startDate + "&scope=" - caPath = getCAsLocation() - dtPage = requests.get(gocdb_ep, verify=caPath, timeout=20) + dtPage = requests.get(gocdb_ep, verify=getCAsLocation(), timeout=20) dt = dtPage.text @@ -293,8 +292,7 @@ def _getServiceEndpointCurlDownload(self, granularity, entity): # GOCDB-PI query gocdb_ep = "https://goc.egi.eu/gocdbpi/public/?method=get_service_endpoint&" + granularity + "=" + entity - caPath = getCAsLocation() - service_endpoint_page = requests.get(gocdb_ep, verify=caPath, timeout=20) + service_endpoint_page = requests.get(gocdb_ep, verify=getCAsLocation(), timeout=20) return service_endpoint_page.text diff --git a/src/DIRAC/Core/Security/IAMService.py b/src/DIRAC/Core/Security/IAMService.py index 0cc9559dbb0..a95a81a3d58 100644 --- a/src/DIRAC/Core/Security/IAMService.py +++ b/src/DIRAC/Core/Security/IAMService.py @@ -3,11 +3,7 @@ import requests -from DIRAC import gConfig, gLogger, S_OK, S_ERROR -from DIRAC.Core.Utilities import DErrno -from DIRAC.Core.Security.Locations import getProxyLocation, getCAsLocation -from DIRAC.Core.Utilities.Decorators import deprecated -from DIRAC.ConfigurationSystem.Client.Helpers.Registry import getVOOption +from DIRAC import S_OK, gConfig, gLogger from DIRAC.ConfigurationSystem.Client.Helpers.CSGlobals import getVO diff --git a/src/DIRAC/Core/Security/Locations.py b/src/DIRAC/Core/Security/Locations.py index b576f58a00e..6c5a3a28252 100644 --- a/src/DIRAC/Core/Security/Locations.py +++ b/src/DIRAC/Core/Security/Locations.py @@ -1,6 +1,7 @@ """ Collection of utilities for locating certs, proxy, CAs """ import os + import DIRAC from DIRAC import gConfig @@ -20,12 +21,6 @@ def getProxyLocation(): if os.path.isfile(f"/tmp/{proxyName}"): return f"/tmp/{proxyName}" - # No gridproxy found - return False - - -# Retrieve CA's location - def getCAsLocation(): """Retrieve the CA's files location""" @@ -57,7 +52,6 @@ def getCAsLocation(): casPath = f"{DIRAC.rootPath}/etc/grid-security/certificates" if os.path.isdir(casPath): return casPath - return False def getVOMSLocation(): @@ -84,7 +78,6 @@ def getVOMSLocation(): vomsPath = f"{DIRAC.rootPath}/etc/grid-security/vomsdir" if os.path.isdir(vomsPath): return vomsPath - return False def getHostCertificateAndKeyLocation(specificLocation=None): diff --git a/src/DIRAC/Core/Security/VOMSService.py b/src/DIRAC/Core/Security/VOMSService.py index a4a25f2d9c4..a100c54808d 100644 --- a/src/DIRAC/Core/Security/VOMSService.py +++ b/src/DIRAC/Core/Security/VOMSService.py @@ -86,8 +86,6 @@ def getUsers(self): if not self.urls: return S_ERROR(DErrno.ENOAUTH, "No VOMS server defined") - userProxy = getProxyLocation() - caPath = getCAsLocation() rawUserList = [] result = None for url in self.urls: @@ -101,8 +99,8 @@ def getUsers(self): result = requests.get( url, headers={"X-VOMS-CSRF-GUARD": "y"}, - cert=userProxy, - verify=caPath, + cert=getProxyLocation(), + verify=getCAsLocation(), params={"startIndex": str(startIndex), "pageSize": "100"}, ) except requests.ConnectionError as exc: diff --git a/src/DIRAC/FrameworkSystem/Client/BundleDeliveryClient.py b/src/DIRAC/FrameworkSystem/Client/BundleDeliveryClient.py index 29c268d62ed..4765328b9d2 100644 --- a/src/DIRAC/FrameworkSystem/Client/BundleDeliveryClient.py +++ b/src/DIRAC/FrameworkSystem/Client/BundleDeliveryClient.py @@ -134,11 +134,9 @@ def syncCAs(self): if "X509_CERT_DIR" in os.environ: X509_CERT_DIR = os.environ["X509_CERT_DIR"] del os.environ["X509_CERT_DIR"] - casLocation = Locations.getCAsLocation() - result = self.syncDir("CAs", casLocation) if X509_CERT_DIR: os.environ["X509_CERT_DIR"] = X509_CERT_DIR - return result + return self.syncDir("CAs", Locations.getCAsLocation()) def syncCRLs(self): """Synchronize CRLs @@ -149,10 +147,9 @@ def syncCRLs(self): if "X509_CERT_DIR" in os.environ: X509_CERT_DIR = os.environ["X509_CERT_DIR"] del os.environ["X509_CERT_DIR"] - result = self.syncDir("CRLs", Locations.getCAsLocation()) if X509_CERT_DIR: os.environ["X509_CERT_DIR"] = X509_CERT_DIR - return result + return self.syncDir("CRLs", Locations.getCAsLocation()) def getCAs(self): """This method can be used to create the CAs. If the file can not be created, diff --git a/src/DIRAC/Resources/Storage/OccupancyPlugins/WLCGAccountingHTTPJson.py b/src/DIRAC/Resources/Storage/OccupancyPlugins/WLCGAccountingHTTPJson.py index 8e1566abc95..8045b7fe8d6 100644 --- a/src/DIRAC/Resources/Storage/OccupancyPlugins/WLCGAccountingHTTPJson.py +++ b/src/DIRAC/Resources/Storage/OccupancyPlugins/WLCGAccountingHTTPJson.py @@ -41,9 +41,7 @@ def _downloadJsonFile(self, occupancyLFN, filePath): """ try: with open(filePath, "w") as fd: - caPath = getCAsLocation() - userProxy = getProxyLocation() - res = requests.get(occupancyLFN, cert=userProxy, verify=caPath, timeout=30) + res = requests.get(occupancyLFN, cert=getProxyLocation(), verify=getCAsLocation(), timeout=30) res.raise_for_status() fd.write(res.text) except Exception as e: diff --git a/src/DIRAC/Resources/Storage/StorageElement.py b/src/DIRAC/Resources/Storage/StorageElement.py index e7e8e1424dc..6ac1003ea28 100755 --- a/src/DIRAC/Resources/Storage/StorageElement.py +++ b/src/DIRAC/Resources/Storage/StorageElement.py @@ -65,14 +65,13 @@ def __call__(self, name, protocolSections=None, vo=None, hideExceptions=False): # Because the gfal2 context caches the proxy location, # we also use the proxy location as a key. # In practice, there should almost always be one, except for the REA - # If we see its memory consumtpion exploding, this might be a place to look - proxyLoc = getProxyLocation() + # If we see its memory consumption exploding, this might be a place to look # ensure protocolSections is hashable! (tuple) if isinstance(protocolSections, list): protocolSections = tuple(protocolSections) - argTuple = (tId, name, protocolSections, vo, proxyLoc) + argTuple = (tId, name, protocolSections, vo, getProxyLocation()) seObj = self.seCache.get(argTuple) if not seObj: diff --git a/tests/System/client_core.sh b/tests/System/client_core.sh index 6b9b693479b..3cf81f00017 100755 --- a/tests/System/client_core.sh +++ b/tests/System/client_core.sh @@ -70,7 +70,7 @@ if ! dirac-apptainer-exec dirac-platform; then exit 1 fi -echo " "dira +echo " " echo "====== dirac-apptainer-exec dirac-proxy-info --help" if ! dirac-apptainer-exec dirac-proxy-info --help; then exit 1