Skip to content

Commit 87a5beb

Browse files
TaykYokufstagni
andcommitted
refactor: add nocs to dirac-login
Co-Authored-By: fstagni <[email protected]>
1 parent 2690517 commit 87a5beb

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

src/DIRAC/FrameworkSystem/scripts/dirac_login.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ def __init__(self):
5252
self.keyLoc = None
5353
self.result = "proxy"
5454
self.authWith = "certificate"
55+
self.enableCS = True
56+
57+
def disableCS(self, _arg) -> S_OK:
58+
"""Set issuer
59+
60+
:param arg: issuer
61+
"""
62+
self.enableCS = False
63+
return S_OK()
5564

5665
def setIssuer(self, arg: str) -> S_OK:
5766
"""Set issuer
@@ -85,6 +94,9 @@ def setCertificate(self, arg: str) -> S_OK:
8594
8695
:param arg: path
8796
"""
97+
if not os.path.exists(arg):
98+
DIRAC.gLogger.error(f"{arg} is not exist.")
99+
DIRAC.exit(1)
88100
self.useCertificate(None)
89101
self.certLoc = arg
90102
return S_OK()
@@ -94,6 +106,9 @@ def setPrivateKey(self, arg: str) -> S_OK:
94106
95107
:param arg: path
96108
"""
109+
if not os.path.exists(arg):
110+
DIRAC.gLogger.error(f"{arg} is not exist.")
111+
DIRAC.exit(1)
97112
self.useCertificate(None)
98113
self.keyLoc = arg
99114
return S_OK()
@@ -160,19 +175,20 @@ def registerCLISwitches(self):
160175
)
161176
Script.registerSwitch("I:", "issuer=", "set issuer.", self.setIssuer)
162177
Script.registerSwitch(
163-
" ",
178+
"",
164179
"use-certificate",
165180
"in case you want to generate a proxy using a certificate. By default.",
166181
self.useCertificate,
167182
)
168183
Script.registerSwitch(
169-
" ", "use-diracas", "in case you want to authorize with DIRAC Authorization Server.", self.useDIRACAS
184+
"", "use-diracas", "in case you want to authorize with DIRAC Authorization Server.", self.useDIRACAS
170185
)
171186
Script.registerSwitch("C:", "certificate=", "user certificate location", self.setCertificate)
172187
Script.registerSwitch("K:", "key=", "user key location", self.setPrivateKey)
173-
Script.registerSwitch(" ", "proxy", "return proxy in case of successful authorization", self.setProxy)
174-
Script.registerSwitch(" ", "token", "return tokens in case of successful authorization", self.setToken)
175-
Script.registerSwitch(" ", "status", "print user authorization status", self.authStatus)
188+
Script.registerSwitch("", "proxy", "return proxy in case of successful authorization", self.setProxy)
189+
Script.registerSwitch("", "token", "return tokens in case of successful authorization", self.setToken)
190+
Script.registerSwitch("", "status", "print user authorization status", self.authStatus)
191+
Script.registerSwitch("", "nocs", "disable CS.", self.disableCS)
176192

177193
def doOAuthMagic(self):
178194
"""Magic method with tokens
@@ -252,8 +268,7 @@ def loginWithCertificate(self):
252268
return S_ERROR("Can't find user certificate and key")
253269
self.certLoc = self.certLoc or cakLoc[0]
254270
self.keyLoc = self.keyLoc or cakLoc[1]
255-
# Generate proxy
256-
self.outputFile = self.outputFile or Locations.getDefaultProxyLocation()
271+
257272
chain = X509Chain()
258273
# Load user cert and key
259274
result = chain.loadChainFromFile(self.certLoc)
@@ -272,26 +287,28 @@ def loginWithCertificate(self):
272287
proxy = copy.copy(chain)
273288

274289
# Create local proxy with group
290+
self.outputFile = self.outputFile or Locations.getDefaultProxyLocation()
275291
result = chain.generateProxyToFile(self.outputFile, int(self.lifetime or 12) * 3600, self.group)
276292
if not result["OK"]:
277293
return S_ERROR(f"Couldn't generate proxy: {result['Message']}")
278294

279-
# After creating the proxy, we can try to connect to the server
280-
result = Script.enableCS()
281-
if not result["OK"]:
282-
return S_ERROR(f"Cannot contact CS: {result['Message']}")
283-
gConfig.forceRefresh()
295+
if self.enableCS:
296+
# After creating the proxy, we can try to connect to the server
297+
result = Script.enableCS()
298+
if not result["OK"]:
299+
return S_ERROR(f"Cannot contact CS: {result['Message']}")
300+
gConfig.forceRefresh()
284301

285-
# Step 2: Upload proxy to DIRAC server
286-
result = gProxyManager.getUploadedProxyLifeTime(credentials["subject"])
287-
if not result["OK"]:
288-
return result
289-
uploadedProxyLifetime = result["Value"]
302+
# Step 2: Upload proxy to DIRAC server
303+
result = gProxyManager.getUploadedProxyLifeTime(credentials["subject"])
304+
if not result["OK"]:
305+
return result
306+
uploadedProxyLifetime = result["Value"]
290307

291-
# Upload proxy to the server if it longer that uploaded one
292-
if credentials["secondsLeft"] > uploadedProxyLifetime:
293-
gLogger.notice("Upload proxy to server.")
294-
return gProxyManager.uploadProxy(proxy)
308+
# Upload proxy to the server if it longer that uploaded one
309+
if credentials["secondsLeft"] > uploadedProxyLifetime:
310+
gLogger.notice("Upload proxy to server.")
311+
return gProxyManager.uploadProxy(proxy)
295312
return S_OK()
296313

297314
def howToSwitch(self) -> bool:
@@ -367,7 +384,7 @@ def main():
367384
result = p.doOAuthMagic()
368385

369386
# Print authorization status
370-
if result["OK"]:
387+
if result["OK"] and p.enableCS:
371388
result = p.getAuthStatus()
372389

373390
if not result["OK"]:

0 commit comments

Comments
 (0)