Skip to content

Commit

Permalink
style: more pythonic code
Browse files Browse the repository at this point in the history
  • Loading branch information
fstagni committed Feb 14, 2025
1 parent 2b00d2b commit 9ebbbcb
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 36 deletions.
12 changes: 5 additions & 7 deletions src/DIRAC/Core/LCG/GOCDBClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
6 changes: 1 addition & 5 deletions src/DIRAC/Core/Security/IAMService.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
9 changes: 1 addition & 8 deletions src/DIRAC/Core/Security/Locations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Collection of utilities for locating certs, proxy, CAs
"""
import os

import DIRAC
from DIRAC import gConfig

Expand All @@ -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"""
Expand Down Expand Up @@ -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():
Expand All @@ -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):
Expand Down
6 changes: 2 additions & 4 deletions src/DIRAC/Core/Security/VOMSService.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
7 changes: 2 additions & 5 deletions src/DIRAC/FrameworkSystem/Client/BundleDeliveryClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions src/DIRAC/Resources/Storage/StorageElement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/System/client_core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9ebbbcb

Please sign in to comment.