summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMotoKen <motokentsai@gmail.com>2013-03-20 13:46:42 +0800
committerMotoKen <motokentsai@gmail.com>2013-03-29 10:53:44 +0800
commit69e940c661731c8ac3cc0cf4e874929c86dcdeb2 (patch)
tree1984c5de665fe5bc18c8fb45ce47d2511bab9864
parentbefef9ab1d497c42de60afa70f07b67b28350c3d (diff)
downloadnova-69e940c661731c8ac3cc0cf4e874929c86dcdeb2.tar.gz
nova-69e940c661731c8ac3cc0cf4e874929c86dcdeb2.tar.xz
nova-69e940c661731c8ac3cc0cf4e874929c86dcdeb2.zip
Accepts aws-sdk-java timestamp format
The aws-sdk-java timestamp format is "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" which contains millisecond representation. However, the current implementation does not accept this kind of format. References: https://github.com/aws/aws-sdk-java/blob/master/src/main/java/com/amazonaws/auth/QueryStringSigner.java#L173 Adds ability to parse this format in ec2utils.is_ec2_timestamp_expired. Fixes bug 1156445. Change-Id: I389ff8b9c6c91b699538b889add264d66dbb8131
-rw-r--r--nova/api/ec2/ec2utils.py18
-rw-r--r--nova/tests/api/ec2/test_ec2_validate.py19
2 files changed, 33 insertions, 4 deletions
diff --git a/nova/api/ec2/ec2utils.py b/nova/api/ec2/ec2utils.py
index bb0f7245a..46f45f3f0 100644
--- a/nova/api/ec2/ec2utils.py
+++ b/nova/api/ec2/ec2utils.py
@@ -178,6 +178,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
@@ -185,6 +188,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,"
@@ -192,12 +204,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)