Skip to content

Commit c086c40

Browse files
committed
Add certificates handling to Tier0Handler
1 parent f6e0fc4 commit c086c40

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

CondCore/Utilities/python/tier0.py

+37-18
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, message):
2323

2424
def unique(seq, keepstr=True):
2525
t = type(seq)
26-
if t in (unicode, str):
26+
if t is str:
2727
t = (list, t('').join)[bool(keepstr)]
2828
try:
2929
remaining = set(seq)
@@ -90,29 +90,51 @@ def unsetDebug( self ):
9090
def setProxy( self, proxy ):
9191
self._proxy = proxy
9292

93+
def _getCerts( self ) -> str:
94+
cert_path = os.getenv('X509_USER_CERT', '')
95+
key_path = os.getenv('X509_USER_KEY', '')
96+
97+
certs = ""
98+
if cert_path:
99+
certs += f' --cert {cert_path}'
100+
else:
101+
logging.warn("No certificate, nor proxy provided for Tier0 access")
102+
if key_path:
103+
certs += f' --key {key_path}'
104+
return certs
105+
106+
def _curlQueryTier0( self, url:str, force_debug:bool = False) -> int:
107+
userAgent = "User-Agent: ConditionWebServices/1.0 python/%d.%d.%d PycURL/%s" \
108+
% ( sys.version_info[ :3 ] + ( pycurl.version_info()[ 1 ], ) )
109+
debug = "-v" if self._debug or force_debug else "-s -S"
110+
111+
proxy = ""
112+
certs = ""
113+
if self._proxy:
114+
proxy = f"--proxy {self._proxy}"
115+
else:
116+
certs = self._getCerts()
117+
118+
cmd = '/usr/bin/curl -k -L --user-agent "%s" %s --connect-timeout %i --retry %i %s %s %s' \
119+
% (userAgent, proxy, self._timeOut, self._retries, debug, url, certs)
120+
121+
# time the curl to understand if re-tries have been carried out
122+
start = time.time()
123+
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
124+
(stdoutdata, stderrdata) = process.communicate()
125+
return process.returncode, stdoutdata, stderrdata
126+
93127
def _queryTier0DataSvc( self, url ):
94128
"""
95129
Queries Tier0DataSvc.
96130
url: Tier0DataSvc URL.
97131
@returns: dictionary, from whence the required information must be retrieved according to the API call.
98132
Raises if connection error, bad response, or timeout after retries occur.
99133
"""
100-
101-
userAgent = "User-Agent: ConditionWebServices/1.0 python/%d.%d.%d PycURL/%s" % ( sys.version_info[ :3 ] + ( pycurl.version_info()[ 1 ], ) )
102-
103-
proxy = ""
104-
if self._proxy: proxy = ' --proxy=%s ' % self._proxy
105-
106-
debug = " -s -S "
107-
if self._debug: debug = " -v "
108-
109-
cmd = '/usr/bin/curl -k -L --user-agent "%s" %s --connect-timeout %i --retry %i %s %s ' % (userAgent, proxy, self._timeOut, self._retries, debug, url)
110134

111135
# time the curl to understand if re-tries have been carried out
112136
start = time.time()
113-
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
114-
(stdoutdata, stderrdata) = process.communicate()
115-
retcode = process.returncode
137+
retcode, stdoutdata, stderrdata = self._curlQueryTier0(url)
116138
end = time.time()
117139

118140
if retcode != 0 or stderrdata:
@@ -123,10 +145,7 @@ def _queryTier0DataSvc( self, url ):
123145
logging.error(msg)
124146

125147
time.sleep(10)
126-
cmd = '/usr/bin/curl -k -L --user-agent "%s" %s --connect-timeout %i --retry %i %s %s ' % (userAgent, proxy, self._timeOut, self._retries, "-v", url)
127-
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
128-
(stdoutdata, stderrdata) = process.communicate()
129-
retcode = process.returncode
148+
retcode, stdoutdata, stderrdata = self._curlQueryTier0(url, force_debug=True)
130149
if retcode != 0:
131150
msg = "looks like curl returned an error for the second time: retcode=%s" % (retcode,)
132151
msg += ' msg = "'+str(stderrdata)+'"'

0 commit comments

Comments
 (0)