From a13cf328d09aad13f97a4b0ae1eae864ba40ef6d Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Wed, 6 Jul 2011 12:47:22 +0200 Subject: [PATCH] even safer handling of expiration types across timestamp/datetime/strings, as this tends to be used outside of the trust area in the pl aggregate, and RenewSliver method --- sfa/managers/aggregate_manager_pl.py | 3 ++- sfa/trust/credential.py | 2 +- sfa/util/sfatime.py | 26 ++++++++++++++++++-------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/sfa/managers/aggregate_manager_pl.py b/sfa/managers/aggregate_manager_pl.py index 5fcd6ef7..0dd236ce 100644 --- a/sfa/managers/aggregate_manager_pl.py +++ b/sfa/managers/aggregate_manager_pl.py @@ -79,7 +79,8 @@ def __get_registry_objects(slice_xrn, creds, users): slice = {} - extime = utcparse(Credential(string=creds[0]).get_expiration()) + # get_expiration always returns a normalized datetime - no need to utcparse + extime = Credential(string=creds[0]).get_expiration() # If the expiration time is > 60 days from now, set the expiration time to 60 days from now if extime > datetime.datetime.utcnow() + datetime.timedelta(days=60): extime = datetime.datetime.utcnow() + datetime.timedelta(days=60) diff --git a/sfa/trust/credential.py b/sfa/trust/credential.py index 45c34f81..bd7e7f1d 100644 --- a/sfa/trust/credential.py +++ b/sfa/trust/credential.py @@ -348,7 +348,7 @@ class Credential(object): # Expiration: an absolute UTC time of expiration (as either an int or string or datetime) # def set_expiration(self, expiration): - if isinstance(expiration, int): + if isinstance(expiration, (int,float)): self.expiration = datetime.datetime.fromtimestamp(expiration) elif isinstance (expiration, datetime.datetime): self.expiration = expiration diff --git a/sfa/util/sfatime.py b/sfa/util/sfatime.py index 1c5b6fd3..11cc566b 100644 --- a/sfa/util/sfatime.py +++ b/sfa/util/sfatime.py @@ -1,16 +1,26 @@ +from types import StringTypes import dateutil.parser import datetime from sfa.util.sfalogging import logger -def utcparse(string): +def utcparse(input): """ Translate a string into a time using dateutil.parser.parse but make sure it's in UTC time and strip - the timezone, so that it's compatible with normal datetime.datetime objects""" +the timezone, so that it's compatible with normal datetime.datetime objects. + +For safety this can also handle inputs that are either timestamps, or datetimes +""" - if isinstance (string, datetime.datetime): + if isinstance (input, datetime.datetime): logger.warn ("argument to utcparse already a datetime - doing nothing") - return string - t = dateutil.parser.parse(string) - if t.utcoffset() is not None: - t = t.utcoffset() + t.replace(tzinfo=None) - return t + return input + elif isinstance (input, StringTypes): + t = dateutil.parser.parse(input) + if t.utcoffset() is not None: + t = t.utcoffset() + t.replace(tzinfo=None) + return t + elif isinstance (input, (int,float)): + return datetime.datetime.fromtimestamp(input) + else: + logger.error("Unexpected type in utcparse [%s]"%type(input)) + -- 2.43.0