From 95d46d6cc87866e2bf2462d8d418a102fbe80402 Mon Sep 17 00:00:00 2001 From: Jorge Schrauwen Date: Sun, 12 Jan 2020 12:47:15 +0100 Subject: [PATCH] #55185 pdbedit module should check for version 4.8.x or newer --- salt/modules/pdbedit.py | 25 ++++++++++++------ tests/unit/modules/test_pdbedit.py | 42 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/salt/modules/pdbedit.py b/salt/modules/pdbedit.py index b25d79e0e14f..5d3b3ff699ba 100644 --- a/salt/modules/pdbedit.py +++ b/salt/modules/pdbedit.py @@ -11,6 +11,7 @@ from __future__ import absolute_import, print_function, unicode_literals # Import Python libs +import re import logging import hashlib import binascii @@ -22,6 +23,7 @@ # Import Salt libs from salt.ext import six import salt.utils.path +import salt.modules.cmdmod log = logging.getLogger(__name__) @@ -39,14 +41,21 @@ def __virtual__(): ''' Provides pdbedit if available ''' - if salt.utils.path.which('pdbedit'): - return __virtualname__ - return ( - False, - '{0} module can only be loaded when pdbedit is available'.format( - __virtualname__ - ) - ) + # NOTE: check for pdbedit command + if not salt.utils.path.which('pdbedit'): + return (False, 'pdbedit command is not available') + + # NOTE: check version is >= 4.8.x + ver = salt.modules.cmdmod.run('pdbedit -V') + ver_regex = re.compile(r'^Version\s(\d+)\.(\d+)\.(\d+)$') + ver_match = ver_regex.match(ver) + if not ver_match: + return (False, 'pdbedit -V returned an unknown version format') + + if not (int(ver_match.group(1)) >= 4 and int(ver_match.group(2)) >= 8): + return (False, 'pdbedit is to old, 4.8.0 or newer is required') + + return __virtualname__ def generate_nt_hash(password): diff --git a/tests/unit/modules/test_pdbedit.py b/tests/unit/modules/test_pdbedit.py index 117239a0739c..07dedc2f5dd8 100644 --- a/tests/unit/modules/test_pdbedit.py +++ b/tests/unit/modules/test_pdbedit.py @@ -9,6 +9,10 @@ # Import Salt Testing Libs from tests.support.mixins import LoaderModuleMockMixin from tests.support.unit import TestCase +from tests.support.mock import ( + MagicMock, + patch, +) class PdbeditTestCase(TestCase, LoaderModuleMockMixin): @@ -19,6 +23,44 @@ class PdbeditTestCase(TestCase, LoaderModuleMockMixin): def setup_loader_modules(self): return {pdbedit: {}} + def test_version(self): + ''' + Test salt.modules.__virtual__'s handling of pdbedit versions + ''' + mock_bad_ver = MagicMock(return_value='Ver 1.1a') + mock_old_ver = MagicMock(return_value='Version 1.0.0') + mock_exa_ver = MagicMock(return_value='Version 4.8.0') + mock_new_ver = MagicMock(return_value='Version 4.9.2') + + # NOTE: no pdbedit installed + with patch('salt.utils.path.which', MagicMock(return_value=None)): + ret = pdbedit.__virtual__() + self.assertEqual(ret, (False, 'pdbedit command is not available')) + + # NOTE: pdbedit is not returning a valid version + with patch('salt.utils.path.which', MagicMock(return_value='/opt/local/bin/pdbedit')), \ + patch('salt.modules.cmdmod.run', mock_bad_ver): + ret = pdbedit.__virtual__() + self.assertEqual(ret, (False, 'pdbedit -V returned an unknown version format')) + + # NOTE: pdbedit is too old + with patch('salt.utils.path.which', MagicMock(return_value='/opt/local/bin/pdbedit')), \ + patch('salt.modules.cmdmod.run', mock_old_ver): + ret = pdbedit.__virtual__() + self.assertEqual(ret, (False, 'pdbedit is to old, 4.8.0 or newer is required')) + + # NOTE: pdbedit is exactly 4.8.0 + with patch('salt.utils.path.which', MagicMock(return_value='/opt/local/bin/pdbedit')), \ + patch('salt.modules.cmdmod.run', mock_exa_ver): + ret = pdbedit.__virtual__() + self.assertEqual(ret, 'pdbedit') + + # NOTE: pdbedit is newer than 4.8.0 + with patch('salt.utils.path.which', MagicMock(return_value='/opt/local/bin/pdbedit')), \ + patch('salt.modules.cmdmod.run', mock_new_ver): + ret = pdbedit.__virtual__() + self.assertEqual(ret, 'pdbedit') + def test_generate_nt_hash(self): ''' Test salt.modules.pdbedit.generate_nt_hash