even safer handling of expiration types across
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 6 Jul 2011 10:47:22 +0000 (12:47 +0200)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 6 Jul 2011 10:47:22 +0000 (12:47 +0200)
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
sfa/trust/credential.py
sfa/util/sfatime.py

index 5fcd6ef..0dd236c 100644 (file)
@@ -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)
index 45c34f8..bd7e7f1 100644 (file)
@@ -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
index 1c5b6fd..11cc566 100644 (file)
@@ -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))
+