1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
# (c) 2018, Matthias Fuchs <matthias.s.fuchs@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import annotations
import warnings
import pytest
from ansible.errors import AnsibleError
from ansible.plugins.filter.core import get_encrypted_password
from ansible.utils import encrypt
def assert_hash(expected, secret, algorithm, **settings):
assert encrypt.do_encrypt(secret, algorithm, **settings) == expected
assert encrypt.PasslibHash(algorithm).hash(secret, **settings) == expected
@pytest.mark.parametrize(
("algorithm", "ident", "salt", "rounds", "expected"),
[
pytest.param(
"bcrypt",
None,
"1234567890123456789012",
None,
"$2b$12$123456789012345678901ugbM1PeTfRQ0t6dCJu5lQA8hwrZOYgDu",
id="bcrypt_default",
),
pytest.param(
"bcrypt",
"2",
"1234567890123456789012",
None,
"$2$12$123456789012345678901ufd3hZRrev.WXCbemqGIV/gmWaTGLImm",
id="bcrypt_ident_2",
),
pytest.param(
"bcrypt",
"2y",
"1234567890123456789012",
None,
"$2y$12$123456789012345678901ugbM1PeTfRQ0t6dCJu5lQA8hwrZOYgDu",
id="bcrypt_ident_2y",
),
pytest.param(
"bcrypt",
"2a",
"1234567890123456789012",
None,
"$2a$12$123456789012345678901ugbM1PeTfRQ0t6dCJu5lQA8hwrZOYgDu",
id="bcrypt_ident_2a",
),
pytest.param(
"bcrypt",
"2b",
"1234567890123456789012",
None,
"$2b$12$123456789012345678901ugbM1PeTfRQ0t6dCJu5lQA8hwrZOYgDu",
id="bcrypt_ident_2b",
),
pytest.param(
"sha256_crypt",
"invalid_ident",
"12345678",
5000,
"$5$12345678$uAZsE3BenI2G.nA8DpTl.9Dc8JiqacI53pEqRr5ppT7",
id="sha256_crypt_invalid_ident",
),
pytest.param(
"crypt16",
None,
"12",
None,
"12pELHK2ME3McUFlHxel6uMM",
id="crypt16_no_ident",
),
],
)
@pytest.mark.skipif(not encrypt.PASSLIB_AVAILABLE, reason='passlib must be installed to run this test')
def test_encrypt_with_ident(algorithm, ident, salt, rounds, expected):
assert_hash(expected, secret="123", algorithm=algorithm, salt=salt, rounds=rounds, ident=ident)
# If passlib is not installed. this is identical to the test_encrypt_with_rounds_no_passlib() test
@pytest.mark.parametrize(
("algorithm", "rounds", "expected"),
[
pytest.param(
"sha256_crypt",
None,
"$5$rounds=535000$12345678$uy3TurUPaY71aioJi58HvUY8jkbhSQU8HepbyaNngv.",
id="sha256_crypt_default_rounds",
),
pytest.param(
"sha256_crypt",
5000,
"$5$12345678$uAZsE3BenI2G.nA8DpTl.9Dc8JiqacI53pEqRr5ppT7",
id="sha256_crypt_rounds_5000",
),
pytest.param(
"sha256_crypt",
10000,
"$5$rounds=10000$12345678$JBinliYMFEcBeAXKZnLjenhgEhTmJBvZn3aR8l70Oy/",
id="sha256_crypt_rounds_10000",
),
pytest.param(
"sha512_crypt",
None,
"$6$rounds=656000$12345678$InMy49UwxyCh2pGJU1NpOhVSElDDzKeyuC6n6E9O34BCUGVNYADnI.rcA3m.Vro9BiZpYmjEoNhpREqQcbvQ80",
id="sha512_crypt_default_rounds",
),
pytest.param(
"sha512_crypt",
5000,
"$6$12345678$LcV9LQiaPekQxZ.OfkMADjFdSO2k9zfbDQrHPVcYjSLqSdjLYpsgqviYvTEP/R41yPmhH3CCeEDqVhW1VHr3L.",
id="sha512_crypt_rounds_5000",
),
pytest.param(
"md5_crypt",
None,
"$1$12345678$tRy4cXc3kmcfRZVj4iFXr/",
id="md5_crypt_default_rounds",
),
],
)
@pytest.mark.skipif(not encrypt.PASSLIB_AVAILABLE, reason='passlib must be installed to run this test')
def test_encrypt_with_rounds(algorithm, rounds, expected):
assert_hash(expected, secret="123", algorithm=algorithm, salt="12345678", rounds=rounds)
@pytest.mark.skipif(not encrypt.PASSLIB_AVAILABLE, reason='passlib must be installed to run this test')
def test_password_hash_filter_passlib_with_exception():
with pytest.raises(AnsibleError):
get_encrypted_password("123", "sha257", salt="12345678")
@pytest.mark.parametrize(
("algorithm", "rounds", "expected_hash"),
[
pytest.param(
"sha256",
None,
"$5$rounds=535000$12345678$uy3TurUPaY71aioJi58HvUY8jkbhSQU8HepbyaNngv.",
id="sha256_default_rounds",
),
pytest.param(
"sha256",
5000,
"$5$12345678$uAZsE3BenI2G.nA8DpTl.9Dc8JiqacI53pEqRr5ppT7",
id="sha256_rounds_5000",
),
pytest.param(
"sha256",
10000,
"$5$rounds=10000$12345678$JBinliYMFEcBeAXKZnLjenhgEhTmJBvZn3aR8l70Oy/",
id="sha256_rounds_10000",
),
pytest.param(
"sha512",
5000,
"$6$12345678$LcV9LQiaPekQxZ.OfkMADjFdSO2k9zfbDQrHPVcYjSLqSdjLYpsgqviYvTEP/R41yPmhH3CCeEDqVhW1VHr3L.",
id="sha512_rounds_5000",
),
pytest.param(
"sha512",
6000,
"$6$rounds=6000$12345678$l/fC67BdJwZrJ7qneKGP1b6PcatfBr0dI7W6JLBrsv8P1wnv/0pu4WJsWq5p6WiXgZ2gt9Aoir3MeORJxg4.Z/",
id="sha512_rounds_6000",
),
],
)
@pytest.mark.skipif(not encrypt.PASSLIB_AVAILABLE, reason='passlib must be installed to run this test')
def test_password_hash_filter_passlib(algorithm, rounds, expected_hash):
assert get_encrypted_password("123", algorithm, salt="12345678", rounds=rounds) == expected_hash
@pytest.mark.skipif(not encrypt.PASSLIB_AVAILABLE, reason='passlib must be installed to run this test')
def test_do_encrypt_passlib_with_exception():
with pytest.raises(AnsibleError):
encrypt.do_encrypt("123", "sha257_crypt", salt="12345678")
def test_random_salt():
res = encrypt.random_salt()
expected_salt_candidate_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./'
assert len(res) == 8
for res_char in res:
assert res_char in expected_salt_candidate_chars
def test_passlib_bcrypt_salt(recwarn):
# deprecated: description='warning suppression only required for Python 3.12 and earlier' python_version='3.12'
with warnings.catch_warnings():
warnings.filterwarnings('ignore', message="'crypt' is deprecated and slated for removal in Python 3.13", category=DeprecationWarning)
passlib_exc = pytest.importorskip("passlib.exc")
secret = 'foo'
salt = '1234567890123456789012'
repaired_salt = '123456789012345678901u'
expected = '$2b$12$123456789012345678901uMv44x.2qmQeefEGb3bcIRc1mLuO7bqa'
ident = '2b'
passlib_obj = encrypt.PasslibHash('bcrypt')
result = passlib_obj.hash(secret, salt=salt, ident=ident)
passlib_warnings = [w.message for w in recwarn if isinstance(w.message, passlib_exc.PasslibHashWarning)]
assert len(passlib_warnings) == 0
assert result == expected
recwarn.clear()
result = passlib_obj.hash(secret, salt=repaired_salt, ident=ident)
assert result == expected
|