99EC2_HOST_NAME_URI = DEFAULT_EC2_METADATA_URI + "local-hostname"
1010EC2_HOST_INSTANCE_TYPE_URI = DEFAULT_EC2_METADATA_URI + "instance-type"
1111
12+ # Used for IMDSv2 to retrieve API token that will be used to call the EC2 METADATA service.
13+ # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
14+ # Bandit marks the following line as risky because it contains the word "token",
15+ # thought it doesn't contain any secret; ignoring with # nosec
16+ # https://bandit.readthedocs.io/en/latest/plugins/b105_hardcoded_password_string.html
17+ EC2_API_TOKEN_URI = "http://169.254.169.254/latest/api/token" # nosec
18+ EC2_METADATA_TOKEN_HEADER_KEY = 'X-aws-ec2-metadata-token' # nosec
19+ EC2_METADATA_TOKEN_TTL_HEADER_KEY = 'X-aws-ec2-metadata-token-ttl-seconds' # nosec
20+ EC2_METADATA_TOKEN_TTL_HEADER_VALUE = '21600' # nosec
21+
1222logger = logging .getLogger (__name__ )
1323
24+
1425class AWSEC2Instance (FleetInfo ):
1526 """
1627 This class will get and parse the EC2 metadata if available.
@@ -27,12 +38,29 @@ def get_fleet_instance_id(self):
2738
2839 @classmethod
2940 def __look_up_host_name (cls ):
30- # The id of the fleet element. Eg. host name in ec2.
31- return http_get (url = EC2_HOST_NAME_URI ).read ().decode ()
41+ """
42+ The id of the fleet element. Eg. host name in ec2.
43+ """
44+ return cls .__look_up_with_IMDSv2 (EC2_HOST_NAME_URI )
3245
3346 @classmethod
3447 def __look_up_instance_type (cls ):
35- return http_get (url = EC2_HOST_INSTANCE_TYPE_URI ).read ().decode ()
48+ """
49+ The type of the instance. Eg. m5.2xlarge
50+ """
51+ return cls .__look_up_with_IMDSv2 (EC2_HOST_INSTANCE_TYPE_URI )
52+
53+ @classmethod
54+ def __look_up_with_IMDSv2 (cls , url ):
55+ return http_get (url = url ,
56+ headers = {EC2_METADATA_TOKEN_HEADER_KEY : cls .__look_up_ec2_api_token ()}) \
57+ .read ().decode ()
58+
59+ @classmethod
60+ def __look_up_ec2_api_token (cls ):
61+ return http_get (url = EC2_API_TOKEN_URI ,
62+ headers = {EC2_METADATA_TOKEN_TTL_HEADER_KEY : EC2_METADATA_TOKEN_TTL_HEADER_VALUE }) \
63+ .read ().decode ()
3664
3765 @classmethod
3866 def look_up_metadata (cls ):
@@ -45,10 +73,10 @@ def look_up_metadata(cls):
4573 log_exception (logger , "Unable to get Ec2 instance metadata, this is normal when running in a different "
4674 "environment (e.g. Fargate), profiler will still work" )
4775 return None
48-
76+
4977 def serialize_to_map (self ):
5078 return {
51- "computeType" : "aws_ec2_instance" ,
52- "hostName" : self .host_name ,
79+ "computeType" : "aws_ec2_instance" ,
80+ "hostName" : self .host_name ,
5381 "hostType" : self .host_type
5482 }
0 commit comments