Transform octal escape sequences in mtab fields (#60122)

pull/60297/head
Andrew Gaffney 5 years ago committed by GitHub
parent 9cb47832d1
commit b38cb37728
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
minor_changes:
- setup - octal escape sequences are now evaluated for mount facts pulled from /etc/mtab

@ -79,6 +79,9 @@ class LinuxHardware(Hardware):
# regex used against mtab content to find entries that are bind mounts # regex used against mtab content to find entries that are bind mounts
MTAB_BIND_MOUNT_RE = re.compile(r'.*bind.*"') MTAB_BIND_MOUNT_RE = re.compile(r'.*bind.*"')
# regex used for replacing octal escape sequences
OCTAL_ESCAPE_RE = re.compile(r'\\[0-9]{3}')
def populate(self, collected_facts=None): def populate(self, collected_facts=None):
hardware_facts = {} hardware_facts = {}
self.module.run_command_environ_update = {'LANG': 'C', 'LC_ALL': 'C', 'LC_NUMERIC': 'C'} self.module.run_command_environ_update = {'LANG': 'C', 'LC_ALL': 'C', 'LC_NUMERIC': 'C'}
@ -460,6 +463,14 @@ class LinuxHardware(Hardware):
mtab_entries.append(fields) mtab_entries.append(fields)
return mtab_entries return mtab_entries
@staticmethod
def _replace_octal_escapes_helper(match):
# Convert to integer using base8 and then convert to character
return chr(int(match.group()[1:], 8))
def _replace_octal_escapes(self, value):
return self.OCTAL_ESCAPE_RE.sub(self._replace_octal_escapes_helper, value)
def get_mount_info(self, mount, device, uuids): def get_mount_info(self, mount, device, uuids):
mount_size = get_mount_size(mount) mount_size = get_mount_size(mount)
@ -485,6 +496,8 @@ class LinuxHardware(Hardware):
pool = ThreadPool(processes=min(len(mtab_entries), cpu_count())) pool = ThreadPool(processes=min(len(mtab_entries), cpu_count()))
maxtime = globals().get('GATHER_TIMEOUT') or timeout.DEFAULT_GATHER_TIMEOUT maxtime = globals().get('GATHER_TIMEOUT') or timeout.DEFAULT_GATHER_TIMEOUT
for fields in mtab_entries: for fields in mtab_entries:
# Transform octal escape sequences
fields = [self._replace_octal_escapes(field) for field in fields]
device, mount, fstype, options = fields[0], fields[1], fields[2], fields[3] device, mount, fstype, options = fields[0], fields[1], fields[2], fields[3]

@ -514,7 +514,11 @@ MTAB_ENTRIES = [
'0', '0',
'0' '0'
], ],
['fusectl', '/sys/fs/fuse/connections', 'fusectl', 'rw,relatime', '0', '0']] ['fusectl', '/sys/fs/fuse/connections', 'fusectl', 'rw,relatime', '0', '0'],
# Mount path with space in the name
# The space is encoded as \040 since the fields in /etc/mtab are space-delimeted
['/dev/sdz9', r'/mnt/foo\040bar', 'ext4', 'rw,relatime', '0', '0'],
]
BIND_MOUNTS = ['/not/a/real/bind_mount'] BIND_MOUNTS = ['/not/a/real/bind_mount']
@ -555,6 +559,11 @@ class TestFactsLinuxHardwareGetMountFacts(unittest.TestCase):
self.assertIsInstance(mount_facts['mounts'], list) self.assertIsInstance(mount_facts['mounts'], list)
self.assertIsInstance(mount_facts['mounts'][0], dict) self.assertIsInstance(mount_facts['mounts'][0], dict)
# Find mounts with space in the mountpoint path
mounts_with_space = [x for x in mount_facts['mounts'] if ' ' in x['mount']]
self.assertEqual(len(mounts_with_space), 1)
self.assertEqual(mounts_with_space[0]['mount'], '/mnt/foo bar')
@patch('ansible.module_utils.facts.hardware.linux.get_file_content', return_value=MTAB) @patch('ansible.module_utils.facts.hardware.linux.get_file_content', return_value=MTAB)
def test_get_mtab_entries(self, mock_get_file_content): def test_get_mtab_entries(self, mock_get_file_content):

Loading…
Cancel
Save