diff options
author | Jenkins <jenkins@review.openstack.org> | 2013-04-01 21:09:30 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2013-04-01 21:09:30 +0000 |
commit | c8067d1b700267cea74cba4836232527a612c5d8 (patch) | |
tree | d76d5eaf2b745d6ae9cde1bfdd1cd95aeec7c257 | |
parent | 3d1660de5a1e9e5dbed5635e33cd83dd031dba4b (diff) | |
parent | 69e940c661731c8ac3cc0cf4e874929c86dcdeb2 (diff) | |
download | nova-c8067d1b700267cea74cba4836232527a612c5d8.tar.gz nova-c8067d1b700267cea74cba4836232527a612c5d8.tar.xz nova-c8067d1b700267cea74cba4836232527a612c5d8.zip |
Merge "Accepts aws-sdk-java timestamp format"
-rw-r--r-- | nova/api/ec2/ec2utils.py | 18 | ||||
-rw-r--r-- | nova/tests/api/ec2/test_ec2_validate.py | 19 |
2 files changed, 33 insertions, 4 deletions
diff --git a/nova/api/ec2/ec2utils.py b/nova/api/ec2/ec2utils.py index 1f4b8d6ee..311d0baf1 100644 --- a/nova/api/ec2/ec2utils.py +++ b/nova/api/ec2/ec2utils.py @@ -199,6 +199,9 @@ def ec2_vol_id_to_uuid(ec2_id): return get_volume_uuid_from_int_id(ctxt, int_id) +_ms_time_regex = re.compile('^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3,6}Z$') + + def is_ec2_timestamp_expired(request, expires=None): """Checks the timestamp or expiry time included in an EC2 request and returns true if the request is expired @@ -206,6 +209,15 @@ def is_ec2_timestamp_expired(request, expires=None): query_time = None timestamp = request.get('Timestamp') expiry_time = request.get('Expires') + + def parse_strtime(strtime): + if _ms_time_regex.match(strtime): + # NOTE(MotoKen): time format for aws-sdk-java contains millisecond + time_format = "%Y-%m-%dT%H:%M:%S.%fZ" + else: + time_format = "%Y-%m-%dT%H:%M:%SZ" + return timeutils.parse_strtime(strtime, time_format) + try: if timestamp and expiry_time: msg = _("Request must include either Timestamp or Expires," @@ -213,12 +225,10 @@ def is_ec2_timestamp_expired(request, expires=None): LOG.error(msg) raise exception.InvalidRequest(msg) elif expiry_time: - query_time = timeutils.parse_strtime(expiry_time, - "%Y-%m-%dT%H:%M:%SZ") + query_time = parse_strtime(expiry_time) return timeutils.is_older_than(query_time, -1) elif timestamp: - query_time = timeutils.parse_strtime(timestamp, - "%Y-%m-%dT%H:%M:%SZ") + query_time = parse_strtime(timestamp) # Check if the difference between the timestamp in the request # and the time on our servers is larger than 5 minutes, the diff --git a/nova/tests/api/ec2/test_ec2_validate.py b/nova/tests/api/ec2/test_ec2_validate.py index 24d226335..9b67d67a1 100644 --- a/nova/tests/api/ec2/test_ec2_validate.py +++ b/nova/tests/api/ec2/test_ec2_validate.py @@ -194,6 +194,25 @@ class EC2TimestampValidationTestCase(test.TestCase): expired = ec2utils.is_ec2_timestamp_expired(params) self.assertFalse(expired) + def test_validate_ec2_timestamp_ms_time_regex(self): + result = ec2utils._ms_time_regex.match('2011-04-22T11:29:49.123Z') + self.assertIsNotNone(result) + result = ec2utils._ms_time_regex.match('2011-04-22T11:29:49.123456Z') + self.assertIsNotNone(result) + result = ec2utils._ms_time_regex.match('2011-04-22T11:29:49.1234567Z') + self.assertIsNone(result) + result = ec2utils._ms_time_regex.match('2011-04-22T11:29:49.123') + self.assertIsNone(result) + result = ec2utils._ms_time_regex.match('2011-04-22T11:29:49Z') + self.assertIsNone(result) + + def test_validate_ec2_timestamp_aws_sdk_format(self): + params = {'Timestamp': '2011-04-22T11:29:49.123Z'} + expired = ec2utils.is_ec2_timestamp_expired(params) + self.assertFalse(expired) + expired = ec2utils.is_ec2_timestamp_expired(params, expires=300) + self.assertTrue(expired) + def test_validate_ec2_timestamp_invalid_format(self): params = {'Timestamp': '2011-04-22T11:29:49.000P'} expired = ec2utils.is_ec2_timestamp_expired(params) |