Blame SOURCES/bz1896827-fence_aws-add-imdsv2-support.patch

156419
From c9f8890264e0257197b31124dbb26c1046475314 Mon Sep 17 00:00:00 2001
156419
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
156419
Date: Fri, 13 Nov 2020 14:30:43 +0100
156419
Subject: [PATCH] fence_aws: add support for IMDSv2
156419
156419
---
156419
 agents/aws/fence_aws.py | 8 +++++---
156419
 1 file changed, 5 insertions(+), 3 deletions(-)
156419
156419
diff --git a/agents/aws/fence_aws.py b/agents/aws/fence_aws.py
156419
index 483a2991..e2a2391f 100644
156419
--- a/agents/aws/fence_aws.py
156419
+++ b/agents/aws/fence_aws.py
156419
@@ -3,12 +3,13 @@
156419
 import sys, re
156419
 import logging
156419
 import atexit
156419
-import requests
156419
 sys.path.append("@FENCEAGENTSLIBDIR@")
156419
 from fencing import *
156419
 from fencing import fail, fail_usage, run_delay, EC_STATUS, SyslogLibHandler
156419
 
156419
+import requests
156419
 import boto3
156419
+from requests import HTTPError
156419
 from botocore.exceptions import ConnectionError, ClientError, EndpointConnectionError, NoRegionError
156419
 
156419
 logger = logging.getLogger("fence_aws")
156419
@@ -19,8 +20,9 @@
156419
 
156419
 def get_instance_id():
156419
 	try:
156419
-		r = requests.get('http://169.254.169.254/latest/meta-data/instance-id')
156419
-		return r.content.decode("UTF-8")
156419
+		token = requests.put('http://169.254.169.254/latest/api/token', headers={"X-aws-ec2-metadata-token-ttl-seconds" : "21600"}).content.decode("UTF-8")
156419
+		r = requests.get('http://169.254.169.254/latest/meta-data/instance-id', headers={"X-aws-ec2-metadata-token" : token}).content.decode("UTF-8")
156419
+		return r
156419
 	except HTTPError as http_err:
156419
 		logger.error('HTTP error occurred while trying to access EC2 metadata server: %s', http_err)
156419
 	except Exception as err: