mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Fix several shadow tests for PY3
The recent unicode_literals changes broke PY3 when generating a password using random bytes from pycryto/Cryptodome, this catches the failure to decode and skips until we get a decodable byte.
This commit is contained in:
parent
79aa909d7a
commit
fa37509d1f
2 changed files with 29 additions and 13 deletions
|
@ -5,6 +5,7 @@ Use pycrypto to generate random passwords on the fly.
|
|||
|
||||
# Import python libraries
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
import logging
|
||||
import re
|
||||
import string
|
||||
import random
|
||||
|
@ -31,25 +32,38 @@ except ImportError:
|
|||
|
||||
# Import salt libs
|
||||
import salt.utils.stringutils
|
||||
from salt.exceptions import SaltInvocationError
|
||||
from salt.exceptions import CommandExecutionError, SaltInvocationError
|
||||
from salt.ext import six
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def secure_password(length=20, use_random=True):
|
||||
'''
|
||||
Generate a secure password.
|
||||
'''
|
||||
length = int(length)
|
||||
pw = ''
|
||||
while len(pw) < length:
|
||||
if HAS_RANDOM and use_random:
|
||||
pw += re.sub(
|
||||
r'\W',
|
||||
'',
|
||||
salt.utils.stringutils.to_str(get_random_bytes(1))
|
||||
)
|
||||
else:
|
||||
pw += random.SystemRandom().choice(string.ascii_letters + string.digits)
|
||||
return pw
|
||||
try:
|
||||
length = int(length)
|
||||
pw = ''
|
||||
while len(pw) < length:
|
||||
if HAS_RANDOM and use_random:
|
||||
while True:
|
||||
try:
|
||||
char = salt.utils.stringutils.to_str(get_random_bytes(1))
|
||||
break
|
||||
except UnicodeDecodeError:
|
||||
continue
|
||||
pw += re.sub(
|
||||
salt.utils.stringutils.to_str(r'\W'),
|
||||
str(), # future lint: disable=blacklisted-function
|
||||
char
|
||||
)
|
||||
else:
|
||||
pw += random.SystemRandom().choice(string.ascii_letters + string.digits)
|
||||
return pw
|
||||
except Exception as exc:
|
||||
log.exception('Failed to generate secure passsword')
|
||||
raise CommandExecutionError(six.text_type(exc))
|
||||
|
||||
|
||||
def gen_hash(crypt_salt=None, password=None, algorithm='sha512'):
|
||||
|
|
|
@ -37,6 +37,8 @@ class ShadowModuleTest(ModuleCase):
|
|||
'''
|
||||
Get current settings
|
||||
'''
|
||||
if 'ERROR' in self._password:
|
||||
self.fail('Failed to generate password: {0}'.format(self._password))
|
||||
super(ShadowModuleTest, self).setUp()
|
||||
os_grain = self.run_function('grains.item', ['kernel'])
|
||||
if os_grain['kernel'] not in 'Linux':
|
||||
|
|
Loading…
Add table
Reference in a new issue