21
21
import DIRAC
22
22
from DIRAC import gConfig , gLogger , S_OK , S_ERROR
23
23
from DIRAC .Core .Security .Locations import getDefaultProxyLocation , getCertificateAndKeyLocation
24
+ from DIRAC .Core .Security .VOMS import VOMS
24
25
from DIRAC .Core .Security .ProxyFile import writeToProxyFile
25
26
from DIRAC .Core .Security .ProxyInfo import getProxyInfo , formatProxyInfoAsString
26
27
from DIRAC .Core .Security .X509Chain import X509Chain # pylint: disable=import-error
33
34
readTokenFromFile ,
34
35
getTokenFileLocation ,
35
36
)
37
+ from DIRAC .ConfigurationSystem .Client .Helpers .Registry import (
38
+ getGroupOption ,
39
+ getVOMSAttributeForGroup ,
40
+ getVOMSVOForGroup ,
41
+ findDefaultGroupForDN ,
42
+ )
36
43
37
44
# This value shows what authorization way will be by default
38
45
DEFAULT_AUTH_WAY = "certificate" # possible values are "certificate", "diracas"
@@ -240,8 +247,7 @@ def loginWithCertificate(self):
240
247
241
248
chain = X509Chain ()
242
249
# Load user cert and key
243
- result = chain .loadChainFromFile (self .certLoc )
244
- if result ["OK" ]:
250
+ if (result := chain .loadChainFromFile (self .certLoc ))["OK" ]:
245
251
result = chain .loadKeyFromFile (
246
252
self .keyLoc , password = prompt ("Enter Certificate password: " , is_password = True )
247
253
)
@@ -259,16 +265,29 @@ def loginWithCertificate(self):
259
265
260
266
# Create local proxy with group
261
267
self .outputFile = self .outputFile or getDefaultProxyLocation ()
262
- result = chain .generateProxyToFile (self .outputFile , int (self .lifetime or 12 ) * 3600 , self .group )
268
+ parameters = (self .outputFile , int (self .lifetime or 12 ) * 3600 , self .group )
269
+
270
+ # Add a VOMS extension if the group requires it
271
+ if (result := chain .generateProxyToFile (* parameters ))["OK" ] and (result := self .__enableCS ())["OK" ]:
272
+ if not self .group and (result := findDefaultGroupForDN (credentials ["DN" ]))["OK" ]:
273
+ self .group = result ["Value" ] # Use default group if user don't set it
274
+ # based on the configuration we decide whether to add VOMS extensions
275
+ if getGroupOption (self .group , "AutoAddVOMS" , False ):
276
+ if not (vomsAttr := getVOMSAttributeForGroup (self .group )):
277
+ print (HTML (f"<yellow>No VOMS attribute foud for { self .group } </yellow>" ))
278
+ else :
279
+ vo = getVOMSVOForGroup (self .group )
280
+ if not (result := VOMS ().setVOMSAttributes (chain , attribute = vomsAttr , vo = vo ))["OK" ]:
281
+ return S_ERROR (f"Failed adding VOMS attribute: { result ['Message' ]} " )
282
+ chain = result ["Value" ]
283
+ result = chain .generateProxyToFile (* parameters )
263
284
if not result ["OK" ]:
264
285
return S_ERROR (f"Couldn't generate proxy: { result ['Message' ]} " )
265
286
266
287
if self .enableCS :
267
288
# After creating the proxy, we can try to connect to the server
268
- result = Script .enableCS ()
269
- if not result ["OK" ]:
270
- return S_ERROR (f"Cannot contact CS: { result ['Message' ]} " )
271
- gConfig .forceRefresh ()
289
+ if not (result := self .__enableCS ())["OK" ]:
290
+ return result
272
291
273
292
# Step 2: Upload proxy to DIRAC server
274
293
result = gProxyManager .getUploadedProxyLifeTime (credentials ["subject" ])
@@ -282,6 +301,11 @@ def loginWithCertificate(self):
282
301
return gProxyManager .uploadProxy (proxy )
283
302
return S_OK ()
284
303
304
+ def __enableCS (self ):
305
+ if not (result := Script .enableCS ())["OK" ] or not (result := gConfig .forceRefresh ())["OK" ]:
306
+ return S_ERROR (f"Cannot contact CS: { result ['Message' ]} " )
307
+ return result
308
+
285
309
def howToSwitch (self ) -> bool :
286
310
"""Helper message, how to switch access type(proxy or access token)"""
287
311
if "DIRAC_USE_ACCESS_TOKEN" in self .ENV :
@@ -303,13 +327,10 @@ def howToSwitch(self) -> bool:
303
327
304
328
def getAuthStatus (self ):
305
329
"""Try to get user authorization status.
306
-
307
330
:return: S_OK()/S_ERROR()
308
331
"""
309
- result = Script .enableCS ()
310
- if not result ["OK" ]:
311
- return S_ERROR ("Cannot contact CS." )
312
- gConfig .forceRefresh ()
332
+ if not (result := self .__enableCS ())["OK" ]:
333
+ return result
313
334
314
335
if self .response == "proxy" :
315
336
result = getProxyInfo (self .outputFile )
0 commit comments