Skip to content

Commit 0b037c8

Browse files
committed
fix: AREX with tokens
1 parent 8b1be49 commit 0b037c8

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/DIRAC/Resources/Computing/AREXComputingElement.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ def __init__(self, ceUniqueID):
5252
self.timeout = 5.0
5353
# Request session
5454
self.session = None
55-
self.headers = {}
55+
self.headers = {
56+
"Accept": "application/json",
57+
"Content-Type": "application/json",
58+
}
5659
# URL used to communicate with the REST interface
5760
self.base_url = ""
5861

@@ -90,13 +93,6 @@ def _reset(self):
9093
# Set up the request framework
9194
self.session = requests.Session()
9295
self.session.verify = Locations.getCAsLocation()
93-
self.headers = {
94-
"Accept": "application/json",
95-
"Content-Type": "application/json",
96-
}
97-
# Attach the token to the headers if present
98-
if os.environ.get("BEARER_TOKEN"):
99-
self.headers["Authorization"] = "Bearer " + os.environ["BEARER_TOKEN"]
10096

10197
return S_OK()
10298

@@ -186,11 +182,22 @@ def _checkSession(self):
186182
if not self.session:
187183
return S_ERROR("REST interface not initialised.")
188184

189-
# Get a proxy
185+
# Reinitialize the authentication parameters
186+
self.session.cert = None
187+
self.headers.pop("Authorization", None)
188+
189+
# Get a proxy: still mandatory, even if tokens are used to authenticate
190190
result = self._prepareProxy()
191191
if not result["OK"]:
192192
self.log.error("Failed to set up proxy", result["Message"])
193193
return result
194+
195+
if self.token:
196+
# Attach the token to the headers if present
197+
self.headers["Authorization"] = "Bearer " + self.token["access_token"]
198+
return S_OK()
199+
200+
# Attach the proxy to the session, only if the token is unavailable
194201
self.session.cert = Locations.getProxyLocation()
195202
return S_OK()
196203

@@ -236,9 +243,12 @@ def __uploadCertificate(self, delegationID, csrContent):
236243

237244
# Get a proxy and sign the CSR
238245
proxy = X509Chain()
239-
result = proxy.loadProxyFromFile(self.session.cert)
246+
proxyFile = Locations.getProxyLocation()
247+
if not proxyFile:
248+
return S_ERROR(f"No proxy available")
249+
result = proxy.loadProxyFromFile(proxyFile)
240250
if not result["OK"]:
241-
return S_ERROR(f"Can't load {self.session.cert}: {result['Message']}")
251+
return S_ERROR(f"Can't load {proxyFile}: {result['Message']}")
242252
result = proxy.generateChainFromRequestString(csrContent)
243253
if not result["OK"]:
244254
self.log.error("Problem with the Certificate Signing Request:", result["Message"])

0 commit comments

Comments
 (0)