Skip osd-devices not absolute paths

This change skips over any devices which does not start with a leading
folder separator ('/'). Allowing such entries causes an OSD to be
created out of the charm directory. This can be caused by something as
innocuous as 2 spaces between devices. The result is that the root
device is also running an OSD, which is undesirable.

Change-Id: I5b52096da0b6f100ae9835c339905585425b27ae
Closes-Bug: 1652175
This commit is contained in:
Billy Olsen 2016-12-22 17:13:07 -07:00
parent 9e020aac19
commit a631169edd
3 changed files with 36 additions and 8 deletions

View File

@ -59,9 +59,13 @@ def add_device(request, device_path, bucket=None):
def get_devices():
return [
os.path.realpath(path)
for path in action_get('osd-devices').split(' ')]
devices = []
for path in action_get('osd-devices').split(' '):
path = path.strip()
if os.path.isabs(path):
devices.append(path)
return devices
if __name__ == "__main__":

View File

@ -365,12 +365,17 @@ def reformat_osd():
def get_devices():
devices = []
if config('osd-devices'):
devices = [
os.path.realpath(path)
for path in config('osd-devices').split(' ')]
else:
devices = []
for path in config('osd-devices').split(' '):
path = path.strip()
# Make sure its a device which is specified using an
# absolute path so that the current working directory
# or any relative path under this directory is not used
if os.path.isabs(path):
devices.append(os.path.realpath(path))
# List storage instances for the 'osd-devices'
# store declared for this charm too, and add
# their block device paths to the list.

View File

@ -72,6 +72,25 @@ class GetDevicesTestCase(test_utils.CharmTestCase):
self.test_config.set("osd-devices", "{} {}".format(device1, device2))
self.assertEqual([device1, device2], hooks.get_devices())
def test_get_devices_extra_spaces(self):
"""
Multiple spaces do not result in additional devices.
"""
device1 = os.path.join(self.tmp_dir, "device1")
device2 = os.path.join(self.tmp_dir, "device2")
self.test_config.set("osd-devices", "{} {}".format(device1, device2))
self.assertEqual([device1, device2], hooks.get_devices())
def test_get_devices_non_absolute_path(self):
"""
Charm does not allow relative paths as this may result in a path
on the root device/within the charm directory.
"""
device1 = os.path.join(self.tmp_dir, "device1")
device2 = "foo"
self.test_config.set("osd-devices", "{} {}".format(device1, device2))
self.assertEqual([device1], hooks.get_devices())
def test_get_devices_symlink(self):
"""
If a symlink is specified in osd-devices, get_devices() resolves