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

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