Skip to content

Commit

Permalink
aws_instance: fix broken user_data()
Browse files Browse the repository at this point in the history
user_data() is mistakenly unsupported IMDSv2, since it's not using
__instance_metadata().

Fixed the code to use __instance_metadata().

Fixes scylladb#498
  • Loading branch information
syuu1228 committed Feb 5, 2024
1 parent a14d9f0 commit 96f8c01
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/scylla_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,15 +696,15 @@ def __instance_metadata(self, path):
time_diff_sec = int(time_diff.total_seconds())
if time_diff_sec >= self.METADATA_TOKEN_TTL - 120:
self.__refresh_metadata_token()
return curl(self.META_DATA_BASE_URL + "meta-data/" + path, headers={"X-aws-ec2-metadata-token": self._metadata_token})
return curl(self.META_DATA_BASE_URL + path, headers={"X-aws-ec2-metadata-token": self._metadata_token})

def __device_exists(self, dev):
if dev[0:4] != "/dev":
dev = "/dev/%s" % dev
return os.path.exists(dev)

def __xenify(self, devname):
dev = self.__instance_metadata('block-device-mapping/' + devname)
dev = self.__instance_metadata('meta-data/block-device-mapping/' + devname)
return dev.replace("sd", "xvd")

def __filter_nvmes(self, dev, dev_type):
Expand Down Expand Up @@ -735,7 +735,7 @@ def _non_root_nvmes(self):
return {"root": [ root_dev ], "ephemeral": ephemeral_present, "ebs": [ x for x in ebs_present if not root_dev.startswith(os.path.join("/dev/", x))] }

def __populate_disks(self):
devmap = self.__instance_metadata("block-device-mapping")
devmap = self.__instance_metadata("meta-data/block-device-mapping")
self._disks = {}
devname = re.compile("^\D+")
nvmes_present = self._non_root_nvmes()
Expand All @@ -761,7 +761,7 @@ def __mac_address(self, nic='eth0'):
def __init__(self):
self._metadata_token = None
self._metadata_token_time = None
self._type = self.__instance_metadata("instance-type")
self._type = self.__instance_metadata("meta-data/instance-type")
self.__populate_disks()

@property
Expand Down Expand Up @@ -864,15 +864,15 @@ def get_remote_disks(self):

def public_ipv4(self):
"""Returns the public IPv4 address of this instance"""
return self.__instance_metadata("public-ipv4")
return self.__instance_metadata("meta-data/public-ipv4")

def private_ipv4(self):
"""Returns the private IPv4 address of this instance"""
return self.__instance_metadata("local-ipv4")
return self.__instance_metadata("meta-data/local-ipv4")

def is_vpc_enabled(self, nic='eth0'):
mac = self.__mac_address(nic)
mac_stat = self.__instance_metadata('network/interfaces/macs/{}'.format(mac))
mac_stat = self.__instance_metadata('meta-data/network/interfaces/macs/{}'.format(mac))
return True if re.search(r'^vpc-id$', mac_stat, flags=re.MULTILINE) else False

@staticmethod
Expand All @@ -884,9 +884,9 @@ def io_setup(self):

@property
def user_data(self):
base_contents = curl(self.META_DATA_BASE_URL).splitlines()
base_contents = self.__instance_metadata('').splitlines()
if 'user-data' in base_contents:
return curl(self.META_DATA_BASE_URL + 'user-data')
return self.__instance_metadata('user-data')
else:
return ''

Expand Down

0 comments on commit 96f8c01

Please sign in to comment.