From 29c198661fe905ba7f31554a950c690f3588fda4 Mon Sep 17 00:00:00 2001 From: Dan Spangenberger Date: Mon, 29 Jun 2015 14:42:41 -0400 Subject: [PATCH 1/2] [ec2] Fix issue requesting current EC2 role EC2 appears to have changed how you access the collection members of instance metadata--you now need a `/` after each collection to list them: ``` curl http://169.254.169.254/latest/meta-data/iam/security-credentials # nothing curl http://169.254.169.254/latest/meta-data/iam/security-credentials/ ec2-staging-es ``` Rebased: [olivier.vielpeau@datadoghq.com] Merge the trailing slash to `/iam/security-credentials` in iam_params request --- util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util.py b/util.py index 6ee1282cee..da0987624e 100644 --- a/util.py +++ b/util.py @@ -332,8 +332,8 @@ def get_tags(agentConfig): pass try: - iam_role = urllib2.urlopen(EC2.METADATA_URL_BASE + "/iam/security-credentials").read().strip() - iam_params = json.loads(urllib2.urlopen(EC2.METADATA_URL_BASE + "/iam/security-credentials" + "/" + unicode(iam_role)).read().strip()) + iam_role = urllib2.urlopen(EC2.METADATA_URL_BASE + "/iam/security-credentials/").read().strip() + iam_params = json.loads(urllib2.urlopen(EC2.METADATA_URL_BASE + "/iam/security-credentials/" + unicode(iam_role)).read().strip()) instance_identity = json.loads(urllib2.urlopen(EC2.INSTANCE_IDENTITY_URL).read().strip()) region = instance_identity['region'] From ed3a3eab7c8e981670b3494bfcbfc79089881714 Mon Sep 17 00:00:00 2001 From: Olivier Vielpeau Date: Mon, 6 Jul 2015 15:55:28 -0400 Subject: [PATCH 2/2] [ec2] Add trailing slash to EC2 metadata `public-keys` endpoint Without the trailing slash we get a 301 permanent redirection to the URL with the trailing slash. Add the trailing slash in case the redirection is removed in the future. --- util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util.py b/util.py index da0987624e..c76697f7aa 100644 --- a/util.py +++ b/util.py @@ -384,11 +384,11 @@ def get_metadata(agentConfig): except Exception: pass - for k in ('instance-id', 'hostname', 'local-hostname', 'public-hostname', 'ami-id', 'local-ipv4', 'public-keys', 'public-ipv4', 'reservation-id', 'security-groups'): + for k in ('instance-id', 'hostname', 'local-hostname', 'public-hostname', 'ami-id', 'local-ipv4', 'public-keys/', 'public-ipv4', 'reservation-id', 'security-groups'): try: v = urllib2.urlopen(EC2.METADATA_URL_BASE + "/" + unicode(k)).read().strip() assert type(v) in (types.StringType, types.UnicodeType) and len(v) > 0, "%s is not a string" % v - EC2.metadata[k] = v + EC2.metadata[k.rstrip('/')] = v except Exception: pass