Skip to content

Commit 6c2c0c5

Browse files
authored
Merge pull request #8213 from ic-hep/rss_time
[8.0] Token expiry in RSS
2 parents 1d94303 + 9e92045 commit 6c2c0c5

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

src/DIRAC/Interfaces/API/DiracAdmin.py

Lines changed: 16 additions & 6 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 gLogger, gConfig, S_OK, S_ERROR
1012
from DIRAC.Core.Utilities.PromptUser import promptUser
@@ -175,7 +177,7 @@ def getSiteSection(self, site, printOutput=False):
175177
return result
176178

177179
#############################################################################
178-
def allowSite(self, site, comment, printOutput=False):
180+
def allowSite(self, site, comment, printOutput=False, days=1):
179181
"""Adds the site to the site mask.
180182
181183
Example usage:
@@ -200,7 +202,12 @@ def allowSite(self, site, comment, printOutput=False):
200202
return S_OK(f"Site {site} is already Active")
201203

202204
if self.rssFlag:
203-
result = self.sitestatus.setSiteStatus(site, "Active", comment)
205+
tokenLifetime = int(days)
206+
if tokenLifetime <= 0:
207+
tokenExpiration = datetime.max
208+
else:
209+
tokenExpiration = datetime.utcnow().replace(microsecond=0) + timedelta(days=tokenLifetime)
210+
result = self.sitestatus.setSiteStatus(site, "Active", comment, expiry=tokenExpiration)
204211
else:
205212
result = WMSAdministratorClient().allowSite(site, comment)
206213
if not result["OK"]:
@@ -258,7 +265,7 @@ def getSiteMaskLogging(self, site=None, printOutput=False):
258265
return S_OK()
259266

260267
#############################################################################
261-
def banSite(self, site, comment, printOutput=False):
268+
def banSite(self, site, comment, printOutput=False, days=1):
262269
"""Removes the site from the site mask.
263270
264271
Example usage:
@@ -272,7 +279,6 @@ def banSite(self, site, comment, printOutput=False):
272279
result = self._checkSiteIsValid(site)
273280
if not result["OK"]:
274281
return result
275-
276282
mask = self.getSiteMask(status="Banned")
277283
if not mask["OK"]:
278284
return mask
@@ -281,9 +287,13 @@ def banSite(self, site, comment, printOutput=False):
281287
if printOutput:
282288
gLogger.notice(f"Site {site} is already Banned")
283289
return S_OK(f"Site {site} is already Banned")
284-
285290
if self.rssFlag:
286-
result = self.sitestatus.setSiteStatus(site, "Banned", comment)
291+
tokenLifetime = int(days)
292+
if tokenLifetime <= 0:
293+
tokenExpiration = datetime.max
294+
else:
295+
tokenExpiration = datetime.utcnow().replace(microsecond=0) + timedelta(days=tokenLifetime)
296+
result = self.sitestatus.setSiteStatus(site, "Banned", comment, expiry=tokenExpiration)
287297
else:
288298
result = WMSAdministratorClient().banSite(site, comment)
289299
if not result["OK"]:

src/DIRAC/Interfaces/scripts/dirac_admin_allow_site.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
@Script()
1818
def main():
1919
Script.registerSwitch("E:", "email=", "Boolean True/False (True by default)")
20+
Script.registerSwitch(
21+
"", "days=", "Number of days the token is valid for. Default is 1 day. 0 or less days denotes forever."
22+
)
2023
# Registering arguments will automatically add their description to the help menu
2124
Script.registerArgument("Site: Name of the Site")
2225
Script.registerArgument("Comment: Reason of the action")
@@ -35,9 +38,12 @@ def getBoolean(value):
3538
Script.showHelp()
3639

3740
email = True
41+
days = 1
3842
for switch in Script.getUnprocessedSwitches():
3943
if switch[0] == "email":
4044
email = getBoolean(switch[1])
45+
if switch[0] == "days":
46+
days = int(switch[1])
4147

4248
diracAdmin = DiracAdmin()
4349
exitCode = 0
@@ -58,7 +64,7 @@ def getBoolean(value):
5864

5965
# parseCommandLine show help when mandatory arguments are not specified or incorrect argument
6066
site, comment = Script.getPositionalArgs(group=True)
61-
result = diracAdmin.allowSite(site, comment, printOutput=True)
67+
result = diracAdmin.allowSite(site, comment, printOutput=True, days=days)
6268
if not result["OK"]:
6369
errorList.append((site, result["Message"]))
6470
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
@@ -17,6 +17,9 @@
1717
@Script()
1818
def main():
1919
Script.registerSwitch("E:", "email=", "Boolean True/False (True by default)")
20+
Script.registerSwitch(
21+
"", "days=", "Number of days the token is valid for. Default is 1 day. 0 or less days denotes forever."
22+
)
2023
# Registering arguments will automatically add their description to the help menu
2124
Script.registerArgument("Site: Name of the Site")
2225
Script.registerArgument("Comment: Reason of the action")
@@ -36,9 +39,12 @@ def getBoolean(value):
3639
Script.showHelp()
3740

3841
email = True
42+
days = 1
3943
for switch in Script.getUnprocessedSwitches():
4044
if switch[0] == "email":
4145
email = getBoolean(switch[1])
46+
if switch[0] == "days":
47+
days = int(switch[1])
4248

4349
diracAdmin = DiracAdmin()
4450
exitCode = 0
@@ -59,7 +65,7 @@ def getBoolean(value):
5965

6066
# parseCommandLine show help when mandatory arguments are not specified or incorrect argument
6167
site, comment = Script.getPositionalArgs(group=True)
62-
result = diracAdmin.banSite(site, comment, printOutput=True)
68+
result = diracAdmin.banSite(site, comment, printOutput=True, days=days)
6369
if not result["OK"]:
6470
errorList.append((site, result["Message"]))
6571
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.
@@ -221,7 +221,7 @@ def getSites(self, siteState="Active"):
221221

222222
return S_OK(siteList)
223223

224-
def setSiteStatus(self, site, status, comment="No comment"):
224+
def setSiteStatus(self, site, status, comment="No comment", expiry=None):
225225
"""
226226
Set the status of a site in the 'SiteStatus' table of RSS
227227
@@ -258,6 +258,8 @@ def setSiteStatus(self, site, status, comment="No comment"):
258258
return S_ERROR(f"Unable to get user proxy info {result['Message']} ")
259259

260260
tokenExpiration = datetime.utcnow() + timedelta(days=1)
261+
if expiry:
262+
tokenExpiration = expiry
261263

262264
self.rssCache.acquireLock()
263265
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
("status=", "Status to be changed"),
3030
("reason=", "Reason to set the Status"),
3131
("VO=", "VO to change a status for. When omitted, status will be changed for all VOs"),
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:
@@ -180,7 +182,11 @@ def setStatus(switchDict, tokenOwner):
180182
)
181183
return S_OK()
182184

183-
tomorrow = datetime.utcnow().replace(microsecond=0) + timedelta(days=1)
185+
tokenLifetime = int(switchDict["days"])
186+
if tokenLifetime <= 0:
187+
tokenExpiration = datetime.max
188+
else:
189+
tokenExpiration = datetime.utcnow().replace(microsecond=0) + timedelta(days=tokenLifetime)
184190

185191
for status, statusType in elements:
186192
gLogger.debug(f"{status} {statusType}")
@@ -190,8 +196,16 @@ def setStatus(switchDict, tokenOwner):
190196
continue
191197

192198
gLogger.debug(
193-
"About to set status %s -> %s for %s, statusType: %s, VO: %s, reason: %s"
194-
% (status, switchDict["status"], switchDict["name"], statusType, switchDict["VO"], switchDict["reason"])
199+
"About to set status %s -> %s for %s, statusType: %s, VO: %s, reason: %s, days: %s"
200+
% (
201+
status,
202+
switchDict["status"],
203+
switchDict["name"],
204+
statusType,
205+
switchDict["VO"],
206+
switchDict["reason"],
207+
switchDict["days"],
208+
)
195209
)
196210
result = rssClient.modifyStatusElement(
197211
switchDict["element"],
@@ -202,7 +216,7 @@ def setStatus(switchDict, tokenOwner):
202216
reason=switchDict["reason"],
203217
vO=switchDict["VO"],
204218
tokenOwner=tokenOwner,
205-
tokenExpiration=tomorrow,
219+
tokenExpiration=tokenExpiration,
206220
)
207221
if not result["OK"]:
208222
return result

0 commit comments

Comments
 (0)