ec2_utils.py: get_instance_metadata explicitly add trailing /

This change works around a bug in CloudStack's EC2 api implementation.
That is filed upstream at [1].

The fix is safe for openstack and EC2 use cases as well.
EC2 and OpenStacks' EC2 metadata service both return a list with
access to either of:
  <url_base>/latest/meta-data
  <url_base>/latest/meta-data/

Additionally, the responses explicitly contain a trailing '/' for
items that have a child.  The ec2_utils code then just re-uses the trailng
/ there.  Thus, only the top level request for 'meta-data/' needs
the explicit fix.

This also changes test cases.  Those test cases failed without fixing
them.  If ever this regressed, those would fail again.

--
[1] https://issues.apache.org/jira/browse/CLOUDSTACK-7405
This commit is contained in:
Scott Moser 2014-08-22 11:40:25 -04:00
parent efe103f1d9
commit e89bdd0c6a
3 changed files with 9 additions and 7 deletions

View File

@ -166,7 +166,9 @@ def get_instance_metadata(api_version='latest',
metadata_address='http://169.254.169.254',
ssl_details=None, timeout=5, retries=5):
md_url = url_helper.combine_url(metadata_address, api_version)
md_url = url_helper.combine_url(md_url, 'meta-data')
# Note, 'meta-data' explicitly has trailing /.
# this is required for CloudStack (LP: #1356855)
md_url = url_helper.combine_url(md_url, 'meta-data/')
caller = functools.partial(util.read_file_or_url,
ssl_details=ssl_details, timeout=timeout,
retries=retries)

View File

@ -88,7 +88,7 @@ def _register_uris(version, ec2_files, ec2_meta, os_files):
path = uri.path.lstrip("/")
if path in ec2_files:
return (200, headers, ec2_files.get(path))
if path == 'latest/meta-data':
if path == 'latest/meta-data/':
buf = StringIO()
for (k, v) in ec2_meta.items():
if isinstance(v, (list, tuple)):
@ -97,7 +97,7 @@ def _register_uris(version, ec2_files, ec2_meta, os_files):
buf.write("%s" % (k))
buf.write("\n")
return (200, headers, buf.getvalue())
if path.startswith('latest/meta-data'):
if path.startswith('latest/meta-data/'):
value = None
pieces = path.split("/")
if path.endswith("/"):

View File

@ -44,7 +44,7 @@ class TestEc2Util(helpers.HttprettyTestCase):
@hp.activate
def test_metadata_fetch_no_keys(self):
base_url = 'http://169.254.169.254/%s/meta-data' % (self.VERSION)
base_url = 'http://169.254.169.254/%s/meta-data/' % (self.VERSION)
hp.register_uri(hp.GET, base_url, status=200,
body="\n".join(['hostname',
'instance-id',
@ -62,7 +62,7 @@ class TestEc2Util(helpers.HttprettyTestCase):
@hp.activate
def test_metadata_fetch_key(self):
base_url = 'http://169.254.169.254/%s/meta-data' % (self.VERSION)
base_url = 'http://169.254.169.254/%s/meta-data/' % (self.VERSION)
hp.register_uri(hp.GET, base_url, status=200,
body="\n".join(['hostname',
'instance-id',
@ -83,7 +83,7 @@ class TestEc2Util(helpers.HttprettyTestCase):
@hp.activate
def test_metadata_fetch_with_2_keys(self):
base_url = 'http://169.254.169.254/%s/meta-data' % (self.VERSION)
base_url = 'http://169.254.169.254/%s/meta-data/' % (self.VERSION)
hp.register_uri(hp.GET, base_url, status=200,
body="\n".join(['hostname',
'instance-id',
@ -108,7 +108,7 @@ class TestEc2Util(helpers.HttprettyTestCase):
@hp.activate
def test_metadata_fetch_bdm(self):
base_url = 'http://169.254.169.254/%s/meta-data' % (self.VERSION)
base_url = 'http://169.254.169.254/%s/meta-data/' % (self.VERSION)
hp.register_uri(hp.GET, base_url, status=200,
body="\n".join(['hostname',
'instance-id',