| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# (c) 2015, Toshio Kuratomi <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
try: |
| 22 |
import passlib |
| 23 |
from passlib.handlers import pbkdf2 |
| 24 |
except ImportError: # pragma: nocover |
| 25 |
passlib = None |
| 26 |
pbkdf2 = None |
| 27 |
|
| 28 |
import pytest |
| 29 |
|
| 30 |
from units.mock.loader import DictDataLoader |
| 31 |
|
| 32 |
import unittest |
| 33 |
from unittest.mock import mock_open, patch |
| 34 |
from ansible.errors import AnsibleError |
| 35 |
import builtins |
| 36 |
from ansible.module_utils.common.text.converters import to_bytes |
| 37 |
from ansible.plugins.loader import PluginLoader, lookup_loader |
| 38 |
from ansible.plugins.lookup import password |
| 39 |
|
| 40 |
|
| 41 |
DEFAULT_LENGTH = 20 |
| 42 |
DEFAULT_CHARS = sorted([u'ascii_letters', u'digits', u".,:-_"]) |
| 43 |
DEFAULT_CANDIDATE_CHARS = u'.,:-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' |
| 44 |
|
| 45 |
# Currently there isn't a new-style |
| 46 |
old_style_params_data = ( |
| 47 |
# Simple case |
| 48 |
dict( |
| 49 |
term=u'/path/to/file', |
| 50 |
filename=u'/path/to/file', |
| 51 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, chars=DEFAULT_CHARS, seed=None), |
| 52 |
candidate_chars=DEFAULT_CANDIDATE_CHARS, |
| 53 |
), |
| 54 |
|
| 55 |
# Special characters in path |
| 56 |
dict( |
| 57 |
term=u'/path/with/embedded spaces and/file', |
| 58 |
filename=u'/path/with/embedded spaces and/file', |
| 59 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, chars=DEFAULT_CHARS, seed=None), |
| 60 |
candidate_chars=DEFAULT_CANDIDATE_CHARS, |
| 61 |
), |
| 62 |
dict( |
| 63 |
term=u'/path/with/equals/cn=com.ansible', |
| 64 |
filename=u'/path/with/equals/cn=com.ansible', |
| 65 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, chars=DEFAULT_CHARS, seed=None), |
| 66 |
candidate_chars=DEFAULT_CANDIDATE_CHARS, |
| 67 |
), |
| 68 |
dict( |
| 69 |
term=u'/path/with/unicode/くらとみ/file', |
| 70 |
filename=u'/path/with/unicode/くらとみ/file', |
| 71 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, chars=DEFAULT_CHARS, seed=None), |
| 72 |
candidate_chars=DEFAULT_CANDIDATE_CHARS, |
| 73 |
), |
| 74 |
|
| 75 |
# Mix several special chars |
| 76 |
dict( |
| 77 |
term=u'/path/with/utf 8 and spaces/くらとみ/file', |
| 78 |
filename=u'/path/with/utf 8 and spaces/くらとみ/file', |
| 79 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, chars=DEFAULT_CHARS, seed=None), |
| 80 |
candidate_chars=DEFAULT_CANDIDATE_CHARS, |
| 81 |
), |
| 82 |
dict( |
| 83 |
term=u'/path/with/encoding=unicode/くらとみ/file', |
| 84 |
filename=u'/path/with/encoding=unicode/くらとみ/file', |
| 85 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, chars=DEFAULT_CHARS, seed=None), |
| 86 |
candidate_chars=DEFAULT_CANDIDATE_CHARS, |
| 87 |
), |
| 88 |
dict( |
| 89 |
term=u'/path/with/encoding=unicode/くらとみ/and spaces file', |
| 90 |
filename=u'/path/with/encoding=unicode/くらとみ/and spaces file', |
| 91 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, chars=DEFAULT_CHARS, seed=None), |
| 92 |
candidate_chars=DEFAULT_CANDIDATE_CHARS, |
| 93 |
), |
| 94 |
|
| 95 |
# Simple parameters |
| 96 |
dict( |
| 97 |
term=u'/path/to/file length=42', |
| 98 |
filename=u'/path/to/file', |
| 99 |
params=dict(length=42, encrypt=None, ident=None, chars=DEFAULT_CHARS, seed=None), |
| 100 |
candidate_chars=DEFAULT_CANDIDATE_CHARS, |
| 101 |
), |
| 102 |
dict( |
| 103 |
term=u'/path/to/file encrypt=pbkdf2_sha256', |
| 104 |
filename=u'/path/to/file', |
| 105 |
params=dict(length=DEFAULT_LENGTH, encrypt='pbkdf2_sha256', ident=None, chars=DEFAULT_CHARS, seed=None), |
| 106 |
candidate_chars=DEFAULT_CANDIDATE_CHARS, |
| 107 |
), |
| 108 |
dict( |
| 109 |
term=u'/path/to/file chars=abcdefghijklmnop', |
| 110 |
filename=u'/path/to/file', |
| 111 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, chars=[u'abcdefghijklmnop'], seed=None), |
| 112 |
candidate_chars=u'abcdefghijklmnop', |
| 113 |
), |
| 114 |
dict( |
| 115 |
term=u'/path/to/file chars=digits,abc,def', |
| 116 |
filename=u'/path/to/file', |
| 117 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, |
| 118 |
chars=sorted([u'digits', u'abc', u'def']), seed=None), |
| 119 |
candidate_chars=u'abcdef0123456789', |
| 120 |
), |
| 121 |
dict( |
| 122 |
term=u'/path/to/file seed=1', |
| 123 |
filename=u'/path/to/file', |
| 124 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, chars=DEFAULT_CHARS, seed='1'), |
| 125 |
candidate_chars=DEFAULT_CANDIDATE_CHARS, |
| 126 |
), |
| 127 |
|
| 128 |
# Including comma in chars |
| 129 |
dict( |
| 130 |
term=u'/path/to/file chars=abcdefghijklmnop,,digits', |
| 131 |
filename=u'/path/to/file', |
| 132 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, |
| 133 |
chars=sorted([u'abcdefghijklmnop', u',', u'digits']), seed=None), |
| 134 |
candidate_chars=u',abcdefghijklmnop0123456789', |
| 135 |
), |
| 136 |
dict( |
| 137 |
term=u'/path/to/file chars=,,', |
| 138 |
filename=u'/path/to/file', |
| 139 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, |
| 140 |
chars=[u','], seed=None), |
| 141 |
candidate_chars=u',', |
| 142 |
), |
| 143 |
|
| 144 |
# Including = in chars |
| 145 |
dict( |
| 146 |
term=u'/path/to/file chars=digits,=,,', |
| 147 |
filename=u'/path/to/file', |
| 148 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, |
| 149 |
chars=sorted([u'digits', u'=', u',']), seed=None), |
| 150 |
candidate_chars=u',=0123456789', |
| 151 |
), |
| 152 |
dict( |
| 153 |
term=u'/path/to/file chars=digits,abc=def', |
| 154 |
filename=u'/path/to/file', |
| 155 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, |
| 156 |
chars=sorted([u'digits', u'abc=def']), seed=None), |
| 157 |
candidate_chars=u'abc=def0123456789', |
| 158 |
), |
| 159 |
|
| 160 |
# Including unicode in chars |
| 161 |
dict( |
| 162 |
term=u'/path/to/file chars=digits,くらとみ,,', |
| 163 |
filename=u'/path/to/file', |
| 164 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, |
| 165 |
chars=sorted([u'digits', u'くらとみ', u',']), seed=None), |
| 166 |
candidate_chars=u',0123456789くらとみ', |
| 167 |
), |
| 168 |
# Including only unicode in chars |
| 169 |
dict( |
| 170 |
term=u'/path/to/file chars=くらとみ', |
| 171 |
filename=u'/path/to/file', |
| 172 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, |
| 173 |
chars=sorted([u'くらとみ']), seed=None), |
| 174 |
candidate_chars=u'くらとみ', |
| 175 |
), |
| 176 |
|
| 177 |
# Include ':' in path |
| 178 |
dict( |
| 179 |
term=u'/path/to/file_with:colon chars=ascii_letters,digits', |
| 180 |
filename=u'/path/to/file_with:colon', |
| 181 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, |
| 182 |
chars=sorted([u'ascii_letters', u'digits']), seed=None), |
| 183 |
candidate_chars=u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', |
| 184 |
), |
| 185 |
|
| 186 |
# Including special chars in both path and chars |
| 187 |
# Special characters in path |
| 188 |
dict( |
| 189 |
term=u'/path/with/embedded spaces and/file chars=abc=def', |
| 190 |
filename=u'/path/with/embedded spaces and/file', |
| 191 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, chars=[u'abc=def'], seed=None), |
| 192 |
candidate_chars=u'abc=def', |
| 193 |
), |
| 194 |
dict( |
| 195 |
term=u'/path/with/equals/cn=com.ansible chars=abc=def', |
| 196 |
filename=u'/path/with/equals/cn=com.ansible', |
| 197 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, chars=[u'abc=def'], seed=None), |
| 198 |
candidate_chars=u'abc=def', |
| 199 |
), |
| 200 |
dict( |
| 201 |
term=u'/path/with/unicode/くらとみ/file chars=くらとみ', |
| 202 |
filename=u'/path/with/unicode/くらとみ/file', |
| 203 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, ident=None, chars=[u'くらとみ'], seed=None), |
| 204 |
candidate_chars=u'くらとみ', |
| 205 |
), |
| 206 |
) |
| 207 |
|
| 208 |
|
| 209 |
class TestParseParameters(unittest.TestCase): |
| 210 |
|
| 211 |
def setUp(self): |
| 212 |
self.fake_loader = DictDataLoader({'/path/to/somewhere': 'sdfsdf'}) |
| 213 |
self.password_lookup = lookup_loader.get('password') |
| 214 |
self.password_lookup._loader = self.fake_loader |
| 215 |
|
| 216 |
def test(self): |
| 217 |
for testcase in old_style_params_data: |
| 218 |
filename, params = self.password_lookup._parse_parameters(testcase['term']) |
| 219 |
params['chars'].sort() |
| 220 |
self.assertEqual(filename, testcase['filename']) |
| 221 |
self.assertEqual(params, testcase['params']) |
| 222 |
|
| 223 |
def test_unrecognized_value(self): |
| 224 |
testcase = dict(term=u'/path/to/file chars=くらとみi sdfsdf', |
| 225 |
filename=u'/path/to/file', |
| 226 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, chars=[u'くらとみ']), |
| 227 |
candidate_chars=u'くらとみ') |
| 228 |
self.assertRaises(AnsibleError, self.password_lookup._parse_parameters, testcase['term']) |
| 229 |
|
| 230 |
def test_invalid_params(self): |
| 231 |
testcase = dict(term=u'/path/to/file chars=くらとみi somethign_invalid=123', |
| 232 |
filename=u'/path/to/file', |
| 233 |
params=dict(length=DEFAULT_LENGTH, encrypt=None, chars=[u'くらとみ']), |
| 234 |
candidate_chars=u'くらとみ') |
| 235 |
self.assertRaises(AnsibleError, self.password_lookup._parse_parameters, testcase['term']) |
| 236 |
|
| 237 |
|
| 238 |
class TestReadPasswordFile(unittest.TestCase): |
| 239 |
def setUp(self): |
| 240 |
self.os_path_exists = password.os.path.exists |
| 241 |
|
| 242 |
def tearDown(self): |
| 243 |
password.os.path.exists = self.os_path_exists |
| 244 |
|
| 245 |
def test_no_password_file(self): |
| 246 |
password.os.path.exists = lambda x: False |
| 247 |
self.assertEqual(password._read_password_file(b'/nonexistent'), None) |
| 248 |
|
| 249 |
def test_with_password_file(self): |
| 250 |
password.os.path.exists = lambda x: True |
| 251 |
with patch.object(builtins, 'open', mock_open(read_data=b'Testing\n')) as m: |
| 252 |
self.assertEqual(password._read_password_file(b'/etc/motd'), u'Testing') |
| 253 |
|
| 254 |
|
| 255 |
class TestGenCandidateChars(unittest.TestCase): |
| 256 |
def _assert_gen_candidate_chars(self, testcase): |
| 257 |
expected_candidate_chars = testcase['candidate_chars'] |
| 258 |
params = testcase['params'] |
| 259 |
chars_spec = params['chars'] |
| 260 |
res = password._gen_candidate_chars(chars_spec) |
| 261 |
self.assertEqual(res, expected_candidate_chars) |
| 262 |
|
| 263 |
def test_gen_candidate_chars(self): |
| 264 |
for testcase in old_style_params_data: |
| 265 |
self._assert_gen_candidate_chars(testcase) |
| 266 |
|
| 267 |
|
| 268 |
class TestRandomPassword(unittest.TestCase): |
| 269 |
def _assert_valid_chars(self, res, chars): |
| 270 |
for res_char in res: |
| 271 |
self.assertIn(res_char, chars) |
| 272 |
|
| 273 |
def test_default(self): |
| 274 |
res = password.random_password() |
| 275 |
self.assertEqual(len(res), DEFAULT_LENGTH) |
| 276 |
self.assertTrue(isinstance(res, str)) |
| 277 |
self._assert_valid_chars(res, DEFAULT_CANDIDATE_CHARS) |
| 278 |
|
| 279 |
def test_zero_length(self): |
| 280 |
res = password.random_password(length=0) |
| 281 |
self.assertEqual(len(res), 0) |
| 282 |
self.assertTrue(isinstance(res, str)) |
| 283 |
self._assert_valid_chars(res, u',') |
| 284 |
|
| 285 |
def test_just_a_common(self): |
| 286 |
res = password.random_password(length=1, chars=u',') |
| 287 |
self.assertEqual(len(res), 1) |
| 288 |
self.assertEqual(res, u',') |
| 289 |
|
| 290 |
def test_free_will(self): |
| 291 |
# A Rush and Spinal Tap reference twofer |
| 292 |
res = password.random_password(length=11, chars=u'a') |
| 293 |
self.assertEqual(len(res), 11) |
| 294 |
self.assertEqual(res, 'aaaaaaaaaaa') |
| 295 |
self._assert_valid_chars(res, u'a') |
| 296 |
|
| 297 |
def test_unicode(self): |
| 298 |
res = password.random_password(length=11, chars=u'くらとみ') |
| 299 |
self._assert_valid_chars(res, u'くらとみ') |
| 300 |
self.assertEqual(len(res), 11) |
| 301 |
|
| 302 |
def test_seed(self): |
| 303 |
pw1 = password.random_password(seed=1) |
| 304 |
pw2 = password.random_password(seed=1) |
| 305 |
pw3 = password.random_password(seed=2) |
| 306 |
self.assertEqual(pw1, pw2) |
| 307 |
self.assertNotEqual(pw1, pw3) |
| 308 |
|
| 309 |
def test_gen_password(self): |
| 310 |
for testcase in old_style_params_data: |
| 311 |
params = testcase['params'] |
| 312 |
candidate_chars = testcase['candidate_chars'] |
| 313 |
params_chars_spec = password._gen_candidate_chars(params['chars']) |
| 314 |
password_string = password.random_password(length=params['length'], |
| 315 |
chars=params_chars_spec) |
| 316 |
self.assertEqual(len(password_string), |
| 317 |
params['length'], |
| 318 |
msg='generated password=%s has length (%s) instead of expected length (%s)' % |
| 319 |
(password_string, len(password_string), params['length'])) |
| 320 |
|
| 321 |
for char in password_string: |
| 322 |
self.assertIn(char, candidate_chars, |
| 323 |
msg='%s not found in %s from chars spect %s' % |
| 324 |
(char, candidate_chars, params['chars'])) |
| 325 |
|
| 326 |
|
| 327 |
class TestParseContent(unittest.TestCase): |
| 328 |
|
| 329 |
def test_empty_password_file(self): |
| 330 |
plaintext_password, salt, ident = password._parse_content(u'') |
| 331 |
self.assertEqual(plaintext_password, u'') |
| 332 |
self.assertEqual(salt, None) |
| 333 |
self.assertEqual(ident, None) |
| 334 |
|
| 335 |
def test(self): |
| 336 |
expected_content = u'12345678' |
| 337 |
file_content = expected_content |
| 338 |
plaintext_password, salt, ident = password._parse_content(file_content) |
| 339 |
self.assertEqual(plaintext_password, expected_content) |
| 340 |
self.assertEqual(salt, None) |
| 341 |
self.assertEqual(ident, None) |
| 342 |
|
| 343 |
def test_with_salt(self): |
| 344 |
expected_content = u'12345678 salt=87654321' |
| 345 |
file_content = expected_content |
| 346 |
plaintext_password, salt, ident = password._parse_content(file_content) |
| 347 |
self.assertEqual(plaintext_password, u'12345678') |
| 348 |
self.assertEqual(salt, u'87654321') |
| 349 |
self.assertEqual(ident, None) |
| 350 |
|
| 351 |
def test_with_salt_and_ident(self): |
| 352 |
expected_content = u'12345678 salt=87654321 ident=2a' |
| 353 |
file_content = expected_content |
| 354 |
plaintext_password, salt, ident = password._parse_content(file_content) |
| 355 |
self.assertEqual(plaintext_password, u'12345678') |
| 356 |
self.assertEqual(salt, u'87654321') |
| 357 |
self.assertEqual(ident, u'2a') |
| 358 |
|
| 359 |
|
| 360 |
class TestFormatContent(unittest.TestCase): |
| 361 |
def test_no_encrypt(self): |
| 362 |
self.assertEqual( |
| 363 |
password._format_content(password=u'hunter42', |
| 364 |
salt=u'87654321', |
| 365 |
encrypt=False), |
| 366 |
u'hunter42 salt=87654321') |
| 367 |
|
| 368 |
def test_no_encrypt_no_salt(self): |
| 369 |
self.assertEqual( |
| 370 |
password._format_content(password=u'hunter42', |
| 371 |
salt=None, |
| 372 |
encrypt=None), |
| 373 |
u'hunter42') |
| 374 |
|
| 375 |
def test_encrypt(self): |
| 376 |
self.assertEqual( |
| 377 |
password._format_content(password=u'hunter42', |
| 378 |
salt=u'87654321', |
| 379 |
encrypt='pbkdf2_sha256'), |
| 380 |
u'hunter42 salt=87654321') |
| 381 |
|
| 382 |
def test_encrypt_no_salt(self): |
| 383 |
self.assertRaises(AssertionError, password._format_content, u'hunter42', None, 'pbkdf2_sha256') |
| 384 |
|
| 385 |
|
| 386 |
class TestWritePasswordFile(unittest.TestCase): |
| 387 |
def setUp(self): |
| 388 |
self.makedirs_safe = password.makedirs_safe |
| 389 |
self.os_chmod = password.os.chmod |
| 390 |
password.makedirs_safe = self.noop |
| 391 |
password.os.chmod = self.noop |
| 392 |
|
| 393 |
def noop(self, *args, **kwargs): |
| 394 |
pass |
| 395 |
|
| 396 |
def tearDown(self): |
| 397 |
password.makedirs_safe = self.makedirs_safe |
| 398 |
password.os.chmod = self.os_chmod |
| 399 |
|
| 400 |
def test_content_written(self): |
| 401 |
|
| 402 |
with patch.object(builtins, 'open', mock_open()) as m: |
| 403 |
password._write_password_file(b'/this/is/a/test/caf\xc3\xa9', u'Testing Café') |
| 404 |
|
| 405 |
m.assert_called_once_with(b'/this/is/a/test/caf\xc3\xa9', 'wb') |
| 406 |
m().write.assert_called_once_with(u'Testing Café\n'.encode('utf-8')) |
| 407 |
|
| 408 |
|
| 409 |
class BaseTestLookupModule(unittest.TestCase): |
| 410 |
def setUp(self): |
| 411 |
self.fake_loader = DictDataLoader({'/path/to/somewhere': 'sdfsdf'}) |
| 412 |
self.password_lookup = lookup_loader.get('password') |
| 413 |
self.password_lookup._loader = self.fake_loader |
| 414 |
self.os_path_exists = password.os.path.exists |
| 415 |
self.os_open = password.os.open |
| 416 |
password.os.open = self.noop |
| 417 |
self.os_close = password.os.close |
| 418 |
password.os.close = self.noop |
| 419 |
self.makedirs_safe = password.makedirs_safe |
| 420 |
password.makedirs_safe = self.noop |
| 421 |
|
| 422 |
def noop(self, *args, **kwargs): |
| 423 |
pass |
| 424 |
|
| 425 |
def tearDown(self): |
| 426 |
password.os.path.exists = self.os_path_exists |
| 427 |
password.os.open = self.os_open |
| 428 |
password.os.close = self.os_close |
| 429 |
password.makedirs_safe = self.makedirs_safe |
| 430 |
|
| 431 |
|
| 432 |
class TestLookupModuleWithoutPasslib(BaseTestLookupModule): |
| 433 |
@patch.object(PluginLoader, '_get_paths') |
| 434 |
@patch('ansible.plugins.lookup.password._write_password_file') |
| 435 |
def test_no_encrypt(self, mock_get_paths, mock_write_file): |
| 436 |
mock_get_paths.return_value = ['/path/one', '/path/two', '/path/three'] |
| 437 |
|
| 438 |
results = self.password_lookup.run([u'/path/to/somewhere'], None) |
| 439 |
|
| 440 |
# FIXME: assert something useful |
| 441 |
for result in results: |
| 442 |
assert len(result) == DEFAULT_LENGTH |
| 443 |
assert isinstance(result, str) |
| 444 |
|
| 445 |
@patch.object(PluginLoader, '_get_paths') |
| 446 |
@patch('ansible.plugins.lookup.password._write_password_file') |
| 447 |
def test_password_already_created_no_encrypt(self, mock_get_paths, mock_write_file): |
| 448 |
mock_get_paths.return_value = ['/path/one', '/path/two', '/path/three'] |
| 449 |
password.os.path.exists = lambda x: x == to_bytes('/path/to/somewhere') |
| 450 |
|
| 451 |
with patch.object(builtins, 'open', mock_open(read_data=b'hunter42 salt=87654321\n')) as m: |
| 452 |
results = self.password_lookup.run([u'/path/to/somewhere chars=anything'], None) |
| 453 |
|
| 454 |
for result in results: |
| 455 |
self.assertEqual(result, u'hunter42') |
| 456 |
|
| 457 |
@patch.object(PluginLoader, '_get_paths') |
| 458 |
@patch('ansible.plugins.lookup.password._write_password_file') |
| 459 |
def test_only_a(self, mock_get_paths, mock_write_file): |
| 460 |
mock_get_paths.return_value = ['/path/one', '/path/two', '/path/three'] |
| 461 |
|
| 462 |
results = self.password_lookup.run([u'/path/to/somewhere chars=a'], None) |
| 463 |
for result in results: |
| 464 |
self.assertEqual(result, u'a' * DEFAULT_LENGTH) |
| 465 |
|
| 466 |
@patch('time.sleep') |
| 467 |
def test_lock_been_held(self, mock_sleep): |
| 468 |
# pretend the lock file is here |
| 469 |
def _already_exists(*args, **kwargs): |
| 470 |
raise FileExistsError("The lock is busy, wait and try again.") |
| 471 |
|
| 472 |
with ( |
| 473 |
pytest.raises(AnsibleError, match='^Password lookup cannot get the lock in 7 seconds.*'), |
| 474 |
patch.object(password.os, 'open', _already_exists), |
| 475 |
patch.object(password.os.path, 'exists', lambda *args, **kwargs: True), |
| 476 |
): |
| 477 |
# should timeout here |
| 478 |
self.password_lookup.run([u'/path/to/somewhere chars=anything'], None) |
| 479 |
|
| 480 |
def test_lock_not_been_held(self): |
| 481 |
# pretend now there is password file but no lock |
| 482 |
password.os.path.exists = lambda x: x == to_bytes('/path/to/somewhere') |
| 483 |
with patch.object(builtins, 'open', mock_open(read_data=b'hunter42 salt=87654321\n')) as m: |
| 484 |
# should not timeout here |
| 485 |
results = self.password_lookup.run([u'/path/to/somewhere chars=anything'], None) |
| 486 |
|
| 487 |
for result in results: |
| 488 |
self.assertEqual(result, u'hunter42') |
| 489 |
|
| 490 |
|
| 491 |
@pytest.mark.skipif(passlib is None, reason='passlib must be installed to run these tests') |
| 492 |
class TestLookupModuleWithPasslib(BaseTestLookupModule): |
| 493 |
def setUp(self): |
| 494 |
super(TestLookupModuleWithPasslib, self).setUp() |
| 495 |
|
| 496 |
# Different releases of passlib default to a different number of rounds |
| 497 |
self.sha256 = passlib.registry.get_crypt_handler('pbkdf2_sha256') |
| 498 |
sha256_for_tests = pbkdf2.create_pbkdf2_hash("sha256", 32, 20000) |
| 499 |
passlib.registry.register_crypt_handler(sha256_for_tests, force=True) |
| 500 |
|
| 501 |
def tearDown(self): |
| 502 |
super(TestLookupModuleWithPasslib, self).tearDown() |
| 503 |
|
| 504 |
passlib.registry.register_crypt_handler(self.sha256, force=True) |
| 505 |
|
| 506 |
@patch.object(PluginLoader, '_get_paths') |
| 507 |
@patch('ansible.plugins.lookup.password._write_password_file') |
| 508 |
def test_encrypt(self, mock_get_paths, mock_write_file): |
| 509 |
mock_get_paths.return_value = ['/path/one', '/path/two', '/path/three'] |
| 510 |
|
| 511 |
results = self.password_lookup.run([u'/path/to/somewhere encrypt=pbkdf2_sha256'], None) |
| 512 |
|
| 513 |
# pbkdf2 format plus hash |
| 514 |
expected_password_length = 76 |
| 515 |
|
| 516 |
for result in results: |
| 517 |
self.assertEqual(len(result), expected_password_length) |
| 518 |
# result should have 5 parts split by '$' |
| 519 |
str_parts = result.split('$', 5) |
| 520 |
|
| 521 |
# verify the result is parseable by the passlib |
| 522 |
crypt_parts = passlib.hash.pbkdf2_sha256.parsehash(result) |
| 523 |
|
| 524 |
# verify it used the right algo type |
| 525 |
self.assertEqual(str_parts[1], 'pbkdf2-sha256') |
| 526 |
|
| 527 |
self.assertEqual(len(str_parts), 5) |
| 528 |
|
| 529 |
# verify the string and parsehash agree on the number of rounds |
| 530 |
self.assertEqual(int(str_parts[2]), crypt_parts['rounds']) |
| 531 |
self.assertIsInstance(result, str) |
| 532 |
|
| 533 |
@patch('ansible.plugins.lookup.password._write_password_file') |
| 534 |
def test_password_already_created_encrypt(self, mock_write_file): |
| 535 |
password.os.path.exists = lambda x: x == to_bytes('/path/to/somewhere') |
| 536 |
|
| 537 |
with patch.object(builtins, 'open', mock_open(read_data=b'hunter42 salt=87654321\n')) as m: |
| 538 |
results = self.password_lookup.run([u'/path/to/somewhere chars=anything encrypt=pbkdf2_sha256'], None) |
| 539 |
for result in results: |
| 540 |
self.assertEqual(result, u'$pbkdf2-sha256$20000$ODc2NTQzMjE$Uikde0cv0BKaRaAXMrUQB.zvG4GmnjClwjghwIRf2gU') |
| 541 |
|
| 542 |
# Assert the password file is not rewritten |
| 543 |
mock_write_file.assert_not_called() |
| 544 |
|
| 545 |
|
| 546 |
@pytest.mark.skipif(passlib is None, reason='passlib must be installed to run these tests') |
| 547 |
class TestLookupModuleWithPasslibWrappedAlgo(BaseTestLookupModule): |
| 548 |
def setUp(self): |
| 549 |
super(TestLookupModuleWithPasslibWrappedAlgo, self).setUp() |
| 550 |
self.os_path_exists = password.os.path.exists |
| 551 |
|
| 552 |
def tearDown(self): |
| 553 |
super(TestLookupModuleWithPasslibWrappedAlgo, self).tearDown() |
| 554 |
password.os.path.exists = self.os_path_exists |
| 555 |
|
| 556 |
@patch('ansible.plugins.lookup.password._write_password_file') |
| 557 |
def test_encrypt_wrapped_crypt_algo(self, mock_write_file): |
| 558 |
|
| 559 |
password.os.path.exists = self.password_lookup._loader.path_exists |
| 560 |
with patch.object(builtins, 'open', mock_open(read_data=self.password_lookup._loader.get_text_file_contents('/path/to/somewhere'))): |
| 561 |
results = self.password_lookup.run([u'/path/to/somewhere encrypt=ldap_sha256_crypt'], None) |
| 562 |
|
| 563 |
wrapper = getattr(passlib.hash, 'ldap_sha256_crypt') |
| 564 |
|
| 565 |
self.assertEqual(len(results), 1) |
| 566 |
result = results[0] |
| 567 |
self.assertIsInstance(result, str) |
| 568 |
|
| 569 |
expected_password_length = 76 |
| 570 |
self.assertEqual(len(result), expected_password_length) |
| 571 |
|
| 572 |
# result should have 5 parts split by '$' |
| 573 |
str_parts = result.split('$') |
| 574 |
self.assertEqual(len(str_parts), 5) |
| 575 |
|
| 576 |
# verify the string and passlib agree on the number of rounds |
| 577 |
self.assertEqual(str_parts[2], "rounds=%s" % wrapper.default_rounds) |
| 578 |
|
| 579 |
# verify it used the right algo type |
| 580 |
self.assertEqual(str_parts[0], '{CRYPT}') |
| 581 |
|
| 582 |
# verify it used the right algo type |
| 583 |
self.assertTrue(wrapper.verify(self.password_lookup._loader.get_text_file_contents('/path/to/somewhere'), result)) |
| 584 |
|
| 585 |
# verify a password with a non default rounds value |
| 586 |
# generated with: echo test | mkpasswd -s --rounds 660000 -m sha-256 --salt testansiblepass. |
| 587 |
hashpw = '{CRYPT}$5$rounds=660000$testansiblepass.$KlRSdA3iFXoPI.dEwh7AixiXW3EtCkLrlQvlYA2sluD' |
| 588 |
self.assertTrue(wrapper.verify('test', hashpw)) |