Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#55185 pdbedit module should check for version 4.8.x or newer #55894

Merged
merged 3 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions salt/modules/pdbedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from __future__ import absolute_import, print_function, unicode_literals

# Import Python libs
import re
import logging
import hashlib
import binascii
Expand All @@ -22,6 +23,7 @@
# Import Salt libs
from salt.ext import six
import salt.utils.path
import salt.modules.cmdmod

log = logging.getLogger(__name__)

Expand All @@ -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):
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/modules/test_pdbedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down