Skip to content

Commit db28585

Browse files
authored
Merge pull request #8250 from ic-hep/cherry-pick-2-6c2c0c52b-integration
[sweep:integration] Token expiry in RSS
2 parents c53246f + 968d9a2 commit db28585

File tree

5 files changed

+53
-13
lines changed

5 files changed

+53
-13
lines changed

src/DIRAC/Interfaces/API/DiracAdmin.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
site banning and unbanning, WMS proxy uploading etc.
55
66
"""
7+
78
import os
9+
from datetime import datetime, timedelta
810

911
from DIRAC import S_ERROR, S_OK, gConfig, gLogger
1012
from DIRAC.ConfigurationSystem.Client.CSAPI import CSAPI
@@ -150,7 +152,7 @@ def getSiteSection(self, site, printOutput=False):
150152
return result
151153

152154
#############################################################################
153-
def allowSite(self, site, comment, printOutput=False):
155+
def allowSite(self, site, comment, printOutput=False, days=1):
154156
"""Adds the site to the site mask. The site must be a valid DIRAC site name
155157
156158
Example usage:
@@ -174,7 +176,13 @@ def allowSite(self, site, comment, printOutput=False):
174176
gLogger.notice(f"Site {site} is already Active")
175177
return S_OK(f"Site {site} is already Active")
176178

177-
if not (result := self.sitestatus.setSiteStatus(site, "Active", comment))["OK"]:
179+
tokenLifetime = int(days)
180+
if tokenLifetime <= 0:
181+
tokenExpiration = datetime.max
182+
else:
183+
tokenExpiration = datetime.utcnow().replace(microsecond=0) + timedelta(days=tokenLifetime)
184+
185+
if not (result := self.sitestatus.setSiteStatus(site, "Active", comment, expiry=tokenExpiration))["OK"]:
178186
return result
179187

180188
if printOutput:
@@ -223,7 +231,7 @@ def getSiteMaskLogging(self, site=None, printOutput=False):
223231
return S_OK()
224232

225233
#############################################################################
226-
def banSite(self, site, comment, printOutput=False):
234+
def banSite(self, site, comment, printOutput=False, days=1):
227235
"""Removes the site from the site mask.
228236
229237
Example usage:
@@ -236,7 +244,6 @@ def banSite(self, site, comment, printOutput=False):
236244
"""
237245
if not (result := self._checkSiteIsValid(site))["OK"]:
238246
return result
239-
240247
mask = self.getSiteMask(status="Banned")
241248
if not mask["OK"]:
242249
return mask
@@ -246,7 +253,12 @@ def banSite(self, site, comment, printOutput=False):
246253
gLogger.notice(f"Site {site} is already Banned")
247254
return S_OK(f"Site {site} is already Banned")
248255

249-
if not (result := self.sitestatus.setSiteStatus(site, "Banned", comment))["OK"]:
256+
tokenLifetime = int(days)
257+
if tokenLifetime <= 0:
258+
tokenExpiration = datetime.max
259+
else:
260+
tokenExpiration = datetime.utcnow().replace(microsecond=0) + timedelta(days=tokenLifetime)
261+
if not (result := self.sitestatus.setSiteStatus(site, "Banned", comment, expiry=tokenExpiration))["OK"]:
250262
return result
251263

252264
if printOutput:

src/DIRAC/Interfaces/scripts/dirac_admin_allow_site.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
@Script()
1414
def main():
1515
Script.registerSwitch("E:", "email=", "Boolean True/False (True by default)")
16+
Script.registerSwitch(
17+
"", "days=", "Number of days the token is valid for. Default is 1 day. 0 or less days denotes forever."
18+
)
1619
# Registering arguments will automatically add their description to the help menu
1720
Script.registerArgument("Site: Name of the Site")
1821
Script.registerArgument("Comment: Reason of the action")
@@ -32,17 +35,20 @@ def getBoolean(value):
3235
Script.showHelp()
3336

3437
email = True
38+
days = 1
3539
for switch in Script.getUnprocessedSwitches():
3640
if switch[0] == "email":
3741
email = getBoolean(switch[1])
42+
if switch[0] == "days":
43+
days = int(switch[1])
3844

3945
diracAdmin = DiracAdmin()
4046
exitCode = 0
4147
errorList = []
4248

4349
# parseCommandLine show help when mandatory arguments are not specified or incorrect argument
4450
site, comment = Script.getPositionalArgs(group=True)
45-
result = diracAdmin.allowSite(site, comment, printOutput=True)
51+
result = diracAdmin.allowSite(site, comment, printOutput=True, days=days)
4652
if not result["OK"]:
4753
errorList.append((site, result["Message"]))
4854
exitCode = 2

src/DIRAC/Interfaces/scripts/dirac_admin_ban_site.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
@Script()
1414
def main():
1515
Script.registerSwitch("E:", "email=", "Boolean True/False (True by default)")
16+
Script.registerSwitch(
17+
"", "days=", "Number of days the token is valid for. Default is 1 day. 0 or less days denotes forever."
18+
)
1619
# Registering arguments will automatically add their description to the help menu
1720
Script.registerArgument("Site: Name of the Site")
1821
Script.registerArgument("Comment: Reason of the action")
@@ -32,9 +35,12 @@ def getBoolean(value):
3235
Script.showHelp()
3336

3437
email = True
38+
days = 1
3539
for switch in Script.getUnprocessedSwitches():
3640
if switch[0] == "email":
3741
email = getBoolean(switch[1])
42+
if switch[0] == "days":
43+
days = int(switch[1])
3844

3945
diracAdmin = DiracAdmin()
4046
exitCode = 0
@@ -50,7 +56,7 @@ def getBoolean(value):
5056

5157
# parseCommandLine show help when mandatory arguments are not specified or incorrect argument
5258
site, comment = Script.getPositionalArgs(group=True)
53-
result = diracAdmin.banSite(site, comment, printOutput=True)
59+
result = diracAdmin.banSite(site, comment, printOutput=True, days=days)
5460
if not result["OK"]:
5561
errorList.append((site, result["Message"]))
5662
exitCode = 2

src/DIRAC/ResourceStatusSystem/Client/SiteStatus.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" SiteStatus helper
1+
"""SiteStatus helper
22
33
Module that acts as a helper for knowing the status of a site.
44
It takes care of switching between the CS and the RSS.
@@ -195,7 +195,7 @@ def getSites(self, siteState="Active"):
195195

196196
return S_OK(siteList)
197197

198-
def setSiteStatus(self, site, status, comment="No comment"):
198+
def setSiteStatus(self, site, status, comment="No comment", expiry=None):
199199
"""
200200
Set the status of a site in the 'SiteStatus' table of RSS
201201
@@ -231,6 +231,8 @@ def setSiteStatus(self, site, status, comment="No comment"):
231231
return S_ERROR(f"Unable to get user proxy info {result['Message']} ")
232232

233233
tokenExpiration = datetime.utcnow() + timedelta(days=1)
234+
if expiry:
235+
tokenExpiration = expiry
234236

235237
self.rssCache.acquireLock()
236238
try:

src/DIRAC/ResourceStatusSystem/scripts/dirac_rss_set_status.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def registerSwitches():
2929
("reason=", "Reason to set the Status"),
3030
("VO=", "VO to change a status for. When omitted, status will be changed for all VOs"),
3131
("tokenOwner=", "Owner of the token"),
32+
("days=", "Number of days the token is valid for. Default is 1 day. 0 or less days denotes forever."),
3233
)
3334

3435
for switch in switches:
@@ -50,6 +51,7 @@ def parseSwitches():
5051
switches = dict(Script.getUnprocessedSwitches())
5152
switches.setdefault("statusType", None)
5253
switches.setdefault("VO", None)
54+
switches.setdefault("days", 1)
5355

5456
for key in ("element", "name", "status", "reason"):
5557
if key not in switches:
@@ -183,7 +185,11 @@ def setStatus(switchDict, tokenOwner):
183185
)
184186
return S_OK()
185187

186-
tomorrow = datetime.utcnow().replace(microsecond=0) + timedelta(days=1)
188+
tokenLifetime = int(switchDict["days"])
189+
if tokenLifetime <= 0:
190+
tokenExpiration = datetime.max
191+
else:
192+
tokenExpiration = datetime.utcnow().replace(microsecond=0) + timedelta(days=tokenLifetime)
187193

188194
for status, statusType in elements:
189195
gLogger.debug(f"{status} {statusType}")
@@ -193,8 +199,16 @@ def setStatus(switchDict, tokenOwner):
193199
continue
194200

195201
gLogger.debug(
196-
"About to set status %s -> %s for %s, statusType: %s, VO: %s, reason: %s"
197-
% (status, switchDict["status"], switchDict["name"], statusType, switchDict["VO"], switchDict["reason"])
202+
"About to set status %s -> %s for %s, statusType: %s, VO: %s, reason: %s, days: %s"
203+
% (
204+
status,
205+
switchDict["status"],
206+
switchDict["name"],
207+
statusType,
208+
switchDict["VO"],
209+
switchDict["reason"],
210+
switchDict["days"],
211+
)
198212
)
199213
result = rssClient.modifyStatusElement(
200214
switchDict["element"],
@@ -205,7 +219,7 @@ def setStatus(switchDict, tokenOwner):
205219
reason=switchDict["reason"],
206220
vO=switchDict["VO"],
207221
tokenOwner=tokenOwner,
208-
tokenExpiration=tomorrow,
222+
tokenExpiration=tokenExpiration,
209223
)
210224
if not result["OK"]:
211225
return result

0 commit comments

Comments
 (0)