| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# (c) 2019, Jordan Borean <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
# Make coding more python3-ish |
| 6 |
from __future__ import (absolute_import, division, print_function) |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
import os |
| 10 |
import platform |
| 11 |
import pytest |
| 12 |
import sys |
| 13 |
from unittest.mock import MagicMock |
| 14 |
|
| 15 |
|
| 16 |
from ansible.errors import AnsibleLookupError |
| 17 |
from ansible.plugins.loader import lookup_loader |
| 18 |
|
| 19 |
|
| 20 |
class FakeLDAPError(Exception): |
| 21 |
pass |
| 22 |
|
| 23 |
|
| 24 |
class FakeLDAPAuthUnknownError(Exception): |
| 25 |
pass |
| 26 |
|
| 27 |
|
| 28 |
class FakeLdap(object): |
| 29 |
SASL_AVAIL = 1 |
| 30 |
TLS_AVAIL = 1 |
| 31 |
|
| 32 |
SCOPE_SUBTREE = 2 |
| 33 |
|
| 34 |
OPT_PROTOCOL_VERSION = 17 |
| 35 |
OPT_REFERRALS = 8 |
| 36 |
|
| 37 |
OPT_X_TLS_NEVER = 0 |
| 38 |
OPT_X_TLS_DEMAND = 2 |
| 39 |
OPT_X_TLS_ALLOW = 3 |
| 40 |
OPT_X_TLS_TRY = 4 |
| 41 |
|
| 42 |
OPT_X_TLS_CACERTFILE = 24578 |
| 43 |
OPT_X_TLS_REQUIRE_CERT = 24582 |
| 44 |
|
| 45 |
LDAPError = FakeLDAPError |
| 46 |
AUTH_UNKNOWN = FakeLDAPAuthUnknownError |
| 47 |
|
| 48 |
@staticmethod |
| 49 |
def initialize(uri, bytes_mode=None, **kwargs): |
| 50 |
return MagicMock() |
| 51 |
|
| 52 |
@staticmethod |
| 53 |
def set_option(option, invalue): |
| 54 |
pass |
| 55 |
|
| 56 |
|
| 57 |
class FakeLdapUrl(object): |
| 58 |
|
| 59 |
def __init__(self, ldapUrl=None, urlscheme='ldap', hostport='', **kwargs): |
| 60 |
url = ldapUrl if ldapUrl else "%s://%s" % (urlscheme, hostport) |
| 61 |
self.urlscheme = url.split('://', 2)[0].lower() |
| 62 |
self._url = url |
| 63 |
|
| 64 |
def initializeUrl(self): |
| 65 |
return self._url |
| 66 |
|
| 67 |
|
| 68 |
def fake_is_ldap_url(s): |
| 69 |
s_lower = s.lower() |
| 70 |
return s_lower.startswith("ldap://") or s_lower.startswith("ldaps://") or s_lower.startswith("ldapi://") |
| 71 |
|
| 72 |
|
| 73 |
@pytest.fixture(autouse=True) |
| 74 |
def laps_password(): |
| 75 |
"""Imports and the laps_password lookup with a mocks laps module for testing""" |
| 76 |
|
| 77 |
# Build the fake ldap and ldapurl Python modules |
| 78 |
fake_ldap_obj = FakeLdap() |
| 79 |
fake_ldap_url_obj = MagicMock() |
| 80 |
fake_ldap_url_obj.isLDAPUrl.side_effect = fake_is_ldap_url |
| 81 |
fake_ldap_url_obj.LDAPUrl.side_effect = FakeLdapUrl |
| 82 |
|
| 83 |
# Take a snapshot of sys.modules before we manipulate it |
| 84 |
orig_modules = sys.modules.copy() |
| 85 |
try: |
| 86 |
sys.modules["ldap"] = fake_ldap_obj |
| 87 |
sys.modules["ldapurl"] = fake_ldap_url_obj |
| 88 |
|
| 89 |
from ansible_collections.community.windows.plugins.lookup import laps_password as lookup_obj |
| 90 |
|
| 91 |
# ensure the changes to these globals aren't persisted after each test |
| 92 |
orig_has_ldap = lookup_obj.HAS_LDAP |
| 93 |
orig_ldap_imp_err = lookup_obj.LDAP_IMP_ERR |
| 94 |
|
| 95 |
yield lookup_obj |
| 96 |
|
| 97 |
lookup_obj.HAS_LDAP = orig_has_ldap |
| 98 |
lookup_obj.LDAP_IMP_ERR = orig_ldap_imp_err |
| 99 |
finally: |
| 100 |
# Restore sys.modules back to our pre-shenanigans |
| 101 |
sys.modules = orig_modules |
| 102 |
|
| 103 |
|
| 104 |
def test_missing_ldap(laps_password): |
| 105 |
laps_password.HAS_LDAP = False |
| 106 |
laps_password.LDAP_IMP_ERR = "no import for you!" |
| 107 |
|
| 108 |
with pytest.raises(AnsibleLookupError) as err: |
| 109 |
lookup_loader.get('community.windows.laps_password').run(["host"], domain="test") |
| 110 |
|
| 111 |
assert str(err.value).startswith( |
| 112 |
"Failed to import the required Python library (python-ldap) on %s's Python %s. See " |
| 113 |
"https://pypi.org/project/python-ldap/ for more info. Please " |
| 114 |
% (platform.node(), sys.executable) |
| 115 |
) |
| 116 |
assert str(err.value).endswith(". Import Error: no import for you!") |
| 117 |
|
| 118 |
|
| 119 |
def test_gssapi_without_sasl(monkeypatch, ): |
| 120 |
monkeypatch.setattr("ldap.SASL_AVAIL", 0) |
| 121 |
|
| 122 |
with pytest.raises(AnsibleLookupError) as err: |
| 123 |
lookup_loader.get('community.windows.laps_password').run(["host"], domain="test") |
| 124 |
|
| 125 |
assert str(err.value) == "Cannot use auth=gssapi when SASL is not configured with the local LDAP install" |
| 126 |
|
| 127 |
|
| 128 |
def test_simple_auth_without_credentials(): |
| 129 |
with pytest.raises(AnsibleLookupError) as err: |
| 130 |
lookup_loader.get('community.windows.laps_password').run(["host"], domain="test", auth="simple") |
| 131 |
|
| 132 |
assert str(err.value) == "The username and password values are required when auth=simple" |
| 133 |
|
| 134 |
|
| 135 |
def test_gssapi_auth_with_credentials(): |
| 136 |
with pytest.raises(AnsibleLookupError) as err: |
| 137 |
lookup_loader.get('community.windows.laps_password').run(["host"], domain="test", auth="gssapi", username="u", |
| 138 |
password="p") |
| 139 |
|
| 140 |
assert str(err.value) == "Explicit credentials are not supported when auth='gssapi'. Call kinit outside of Ansible" |
| 141 |
|
| 142 |
|
| 143 |
def test_not_encrypted_without_override(): |
| 144 |
with pytest.raises(AnsibleLookupError) as err: |
| 145 |
lookup_loader.get('community.windows.laps_password').run(["host"], domain="dc01", auth="simple", |
| 146 |
username="test", password="test") |
| 147 |
|
| 148 |
assert str(err.value) == "Current configuration will result in plaintext traffic exposing credentials. Set " \ |
| 149 |
"auth=gssapi, scheme=ldaps, start_tls=True, or allow_plaintext=True to continue" |
| 150 |
|
| 151 |
|
| 152 |
def test_ldaps_without_tls(monkeypatch, ): |
| 153 |
monkeypatch.setattr("ldap.TLS_AVAIL", 0) |
| 154 |
|
| 155 |
with pytest.raises(AnsibleLookupError) as err: |
| 156 |
lookup_loader.get('community.windows.laps_password').run(["host"], domain="dc01", scheme="ldaps") |
| 157 |
|
| 158 |
assert str(err.value) == "Cannot use TLS as the local LDAP installed has not been configured to support it" |
| 159 |
|
| 160 |
|
| 161 |
def test_start_tls_without_tls(monkeypatch, ): |
| 162 |
monkeypatch.setattr("ldap.TLS_AVAIL", 0) |
| 163 |
|
| 164 |
with pytest.raises(AnsibleLookupError) as err: |
| 165 |
lookup_loader.get('community.windows.laps_password').run(["host"], domain="dc01", start_tls=True) |
| 166 |
|
| 167 |
assert str(err.value) == "Cannot use TLS as the local LDAP installed has not been configured to support it" |
| 168 |
|
| 169 |
|
| 170 |
def test_normal_run(monkeypatch, laps_password): |
| 171 |
def get_laps_password(conn, cn, search_base): |
| 172 |
return "CN=%s,%s" % (cn, search_base) |
| 173 |
|
| 174 |
mock_ldap = MagicMock() |
| 175 |
mock_ldap.return_value.read_rootdse_s.return_value = {"defaultNamingContext": ["DC=domain,DC=com"]} |
| 176 |
monkeypatch.setattr("ldap.initialize", mock_ldap) |
| 177 |
|
| 178 |
mock_get_laps_password = MagicMock(side_effect=get_laps_password) |
| 179 |
monkeypatch.setattr(laps_password, "get_laps_password", mock_get_laps_password) |
| 180 |
|
| 181 |
actual = lookup_loader.get('community.windows.laps_password').run(["host1", "host2"], domain="dc01") |
| 182 |
assert actual == ["CN=host1,DC=domain,DC=com", "CN=host2,DC=domain,DC=com"] |
| 183 |
|
| 184 |
# Verify the call count to get_laps_password |
| 185 |
assert mock_get_laps_password.call_count == 2 |
| 186 |
|
| 187 |
# Verify the initialize() method call |
| 188 |
assert mock_ldap.call_count == 1 |
| 189 |
assert mock_ldap.call_args[0] == ("ldap://dc01:389",) |
| 190 |
assert mock_ldap.call_args[1] == {"bytes_mode": False} |
| 191 |
|
| 192 |
# Verify the number of calls made to the mocked LDAP object |
| 193 |
assert mock_ldap.mock_calls[1][0] == "().set_option" |
| 194 |
assert mock_ldap.mock_calls[1][1] == (FakeLdap.OPT_PROTOCOL_VERSION, 3) |
| 195 |
|
| 196 |
assert mock_ldap.mock_calls[2][0] == "().set_option" |
| 197 |
assert mock_ldap.mock_calls[2][1] == (FakeLdap.OPT_REFERRALS, 0) |
| 198 |
|
| 199 |
assert mock_ldap.mock_calls[3][0] == '().sasl_gssapi_bind_s' |
| 200 |
assert mock_ldap.mock_calls[3][1] == () |
| 201 |
|
| 202 |
assert mock_ldap.mock_calls[4][0] == "().read_rootdse_s" |
| 203 |
assert mock_ldap.mock_calls[4][1] == () |
| 204 |
|
| 205 |
assert mock_ldap.mock_calls[5][0] == "().unbind_s" |
| 206 |
assert mock_ldap.mock_calls[5][1] == () |
| 207 |
|
| 208 |
|
| 209 |
def test_run_with_simple_auth_and_search_base(monkeypatch, laps_password): |
| 210 |
def get_laps_password(conn, cn, search_base): |
| 211 |
return "CN=%s,%s" % (cn, search_base) |
| 212 |
|
| 213 |
mock_ldap = MagicMock() |
| 214 |
monkeypatch.setattr("ldap.initialize", mock_ldap) |
| 215 |
|
| 216 |
mock_get_laps_password = MagicMock(side_effect=get_laps_password) |
| 217 |
monkeypatch.setattr(laps_password, "get_laps_password", mock_get_laps_password) |
| 218 |
|
| 219 |
actual = lookup_loader.get('community.windows.laps_password').run(["host1", "host2"], domain="dc01", auth="simple", |
| 220 |
username="user", password="pass", |
| 221 |
allow_plaintext=True, |
| 222 |
search_base="OU=Workstations,DC=domain,DC=com") |
| 223 |
assert actual == ["CN=host1,OU=Workstations,DC=domain,DC=com", "CN=host2,OU=Workstations,DC=domain,DC=com"] |
| 224 |
|
| 225 |
# Verify the call count to get_laps_password |
| 226 |
assert mock_get_laps_password.call_count == 2 |
| 227 |
|
| 228 |
# Verify the initialize() method call |
| 229 |
assert mock_ldap.call_count == 1 |
| 230 |
assert mock_ldap.call_args[0] == ("ldap://dc01:389",) |
| 231 |
assert mock_ldap.call_args[1] == {"bytes_mode": False} |
| 232 |
|
| 233 |
# Verify the number of calls made to the mocked LDAP object |
| 234 |
assert mock_ldap.mock_calls[1][0] == "().set_option" |
| 235 |
assert mock_ldap.mock_calls[1][1] == (FakeLdap.OPT_PROTOCOL_VERSION, 3) |
| 236 |
|
| 237 |
assert mock_ldap.mock_calls[2][0] == "().set_option" |
| 238 |
assert mock_ldap.mock_calls[2][1] == (FakeLdap.OPT_REFERRALS, 0) |
| 239 |
|
| 240 |
assert mock_ldap.mock_calls[3][0] == '().bind_s' |
| 241 |
assert mock_ldap.mock_calls[3][1] == (u"user", u"pass") |
| 242 |
|
| 243 |
assert mock_ldap.mock_calls[4][0] == "().unbind_s" |
| 244 |
assert mock_ldap.mock_calls[4][1] == () |
| 245 |
|
| 246 |
|
| 247 |
@pytest.mark.parametrize("kwargs, expected", [ |
| 248 |
[{"domain": "dc01"}, "ldap://dc01:389"], |
| 249 |
[{"domain": "dc02", "port": 1234}, "ldap://dc02:1234"], |
| 250 |
[{"domain": "dc03", "scheme": "ldaps"}, "ldaps://dc03:636"], |
| 251 |
# Verifies that an explicit URI ignores port and scheme |
| 252 |
[{"domain": "ldap://dc04", "port": 1234, "scheme": "ldaps"}, "ldap://dc04"], |
| 253 |
]) |
| 254 |
def test_uri_options(monkeypatch, kwargs, expected): |
| 255 |
mock_ldap = MagicMock() |
| 256 |
monkeypatch.setattr("ldap.initialize", mock_ldap) |
| 257 |
|
| 258 |
lookup_loader.get('community.windows.laps_password').run([], **kwargs) |
| 259 |
|
| 260 |
assert mock_ldap.call_count == 1 |
| 261 |
assert mock_ldap.call_args[0] == (expected,) |
| 262 |
assert mock_ldap.call_args[1] == {"bytes_mode": False} |
| 263 |
|
| 264 |
|
| 265 |
@pytest.mark.parametrize("validate, expected", [ |
| 266 |
["never", FakeLdap.OPT_X_TLS_NEVER], |
| 267 |
["allow", FakeLdap.OPT_X_TLS_ALLOW], |
| 268 |
["try", FakeLdap.OPT_X_TLS_TRY], |
| 269 |
["demand", FakeLdap.OPT_X_TLS_DEMAND], |
| 270 |
]) |
| 271 |
def test_certificate_validation(monkeypatch, validate, expected): |
| 272 |
mock_ldap_option = MagicMock() |
| 273 |
monkeypatch.setattr(FakeLdap, "set_option", mock_ldap_option) |
| 274 |
|
| 275 |
mock_ldap = MagicMock() |
| 276 |
monkeypatch.setattr("ldap.initialize", mock_ldap) |
| 277 |
|
| 278 |
lookup_loader.get('community.windows.laps_password').run([], domain="dc01", start_tls=True, |
| 279 |
validate_certs=validate) |
| 280 |
|
| 281 |
assert mock_ldap_option.mock_calls[0][1] == (FakeLdap.OPT_X_TLS_REQUIRE_CERT, expected) |
| 282 |
|
| 283 |
assert mock_ldap.mock_calls[3][0] == "().start_tls_s" |
| 284 |
assert mock_ldap.mock_calls[3][1] == () |
| 285 |
|
| 286 |
assert mock_ldap.mock_calls[4][0] == "().sasl_gssapi_bind_s" |
| 287 |
assert mock_ldap.mock_calls[4][1] == () |
| 288 |
|
| 289 |
|
| 290 |
def test_certificate_validate_with_custom_cacert(monkeypatch): |
| 291 |
mock_ldap_option = MagicMock() |
| 292 |
monkeypatch.setattr(FakeLdap, "set_option", mock_ldap_option) |
| 293 |
|
| 294 |
mock_ldap = MagicMock() |
| 295 |
monkeypatch.setattr("ldap.initialize", mock_ldap) |
| 296 |
monkeypatch.setattr(os.path, 'exists', lambda x: True) |
| 297 |
|
| 298 |
lookup_loader.get('community.windows.laps_password').run([], domain="dc01", scheme="ldaps", |
| 299 |
cacert_file="cacert.pem") |
| 300 |
|
| 301 |
assert mock_ldap_option.mock_calls[0][1] == (FakeLdap.OPT_X_TLS_REQUIRE_CERT, FakeLdap.OPT_X_TLS_DEMAND) |
| 302 |
assert mock_ldap_option.mock_calls[1][1] == (FakeLdap.OPT_X_TLS_CACERTFILE, u"cacert.pem") |
| 303 |
|
| 304 |
assert mock_ldap.mock_calls[3][0] == "().sasl_gssapi_bind_s" |
| 305 |
assert mock_ldap.mock_calls[3][1] == () |
| 306 |
|
| 307 |
|
| 308 |
def test_certificate_validate_with_custom_cacert_fail(monkeypatch): |
| 309 |
def set_option(self, key, value): |
| 310 |
if key == FakeLdap.OPT_X_TLS_CACERTFILE: |
| 311 |
raise ValueError("set_option() failed") |
| 312 |
|
| 313 |
monkeypatch.setattr(FakeLdap, "set_option", set_option) |
| 314 |
monkeypatch.setattr(os.path, 'exists', lambda x: True) |
| 315 |
|
| 316 |
with pytest.raises(AnsibleLookupError) as err: |
| 317 |
lookup_loader.get('community.windows.laps_password').run([], domain="dc01", scheme="ldaps", |
| 318 |
cacert_file="cacert.pem") |
| 319 |
|
| 320 |
assert str(err.value) == "Failed to set path to cacert file, this is a known issue with older OpenLDAP " \ |
| 321 |
"libraries on the host. Update OpenLDAP and reinstall python-ldap to continue" |
| 322 |
|
| 323 |
|
| 324 |
@pytest.mark.parametrize("path", [ |
| 325 |
"cacert.pem", |
| 326 |
"~/.certs/cacert.pem", |
| 327 |
"~/.certs/$USER/cacert.pem", |
| 328 |
]) |
| 329 |
def test_certificate_invalid_path(monkeypatch, path): |
| 330 |
lookup = lookup_loader.get('community.windows.laps_password') |
| 331 |
monkeypatch.setattr(os.path, 'exists', lambda x: False) |
| 332 |
expected_path = os.path.expanduser(os.path.expandvars(path)) |
| 333 |
|
| 334 |
with pytest.raises(AnsibleLookupError) as err: |
| 335 |
lookup.run([], domain="dc01", scheme="ldaps", cacert_file=path) |
| 336 |
|
| 337 |
assert str(err.value) == "The cacert_file specified '%s' does not exist" % expected_path |
| 338 |
|
| 339 |
|
| 340 |
def test_simple_auth_with_ldaps(monkeypatch): |
| 341 |
mock_ldap_option = MagicMock() |
| 342 |
monkeypatch.setattr(FakeLdap, "set_option", mock_ldap_option) |
| 343 |
|
| 344 |
mock_ldap = MagicMock() |
| 345 |
monkeypatch.setattr("ldap.initialize", mock_ldap) |
| 346 |
|
| 347 |
lookup_loader.get('community.windows.laps_password').run([], domain="dc01", scheme="ldaps", auth="simple", |
| 348 |
username="user", password="pass") |
| 349 |
|
| 350 |
assert mock_ldap_option.mock_calls[0][1] == (FakeLdap.OPT_X_TLS_REQUIRE_CERT, FakeLdap.OPT_X_TLS_DEMAND) |
| 351 |
|
| 352 |
assert mock_ldap.mock_calls[3][0] == '().bind_s' |
| 353 |
assert mock_ldap.mock_calls[3][1] == (u"user", u"pass") |
| 354 |
|
| 355 |
assert mock_ldap.mock_calls[4][0] == "().read_rootdse_s" |
| 356 |
assert mock_ldap.mock_calls[4][1] == () |
| 357 |
|
| 358 |
|
| 359 |
def test_simple_auth_with_start_tls(monkeypatch): |
| 360 |
mock_ldap_option = MagicMock() |
| 361 |
monkeypatch.setattr(FakeLdap, "set_option", mock_ldap_option) |
| 362 |
|
| 363 |
mock_ldap = MagicMock() |
| 364 |
monkeypatch.setattr("ldap.initialize", mock_ldap) |
| 365 |
|
| 366 |
lookup_loader.get('community.windows.laps_password').run([], domain="dc01", start_tls=True, auth="simple", |
| 367 |
username="user", password="pass") |
| 368 |
|
| 369 |
assert mock_ldap_option.mock_calls[0][1] == (FakeLdap.OPT_X_TLS_REQUIRE_CERT, FakeLdap.OPT_X_TLS_DEMAND) |
| 370 |
|
| 371 |
assert mock_ldap.mock_calls[3][0] == "().start_tls_s" |
| 372 |
assert mock_ldap.mock_calls[3][1] == () |
| 373 |
|
| 374 |
assert mock_ldap.mock_calls[4][0] == '().bind_s' |
| 375 |
assert mock_ldap.mock_calls[4][1] == (u"user", u"pass") |
| 376 |
|
| 377 |
assert mock_ldap.mock_calls[5][0] == "().read_rootdse_s" |
| 378 |
assert mock_ldap.mock_calls[5][1] == () |
| 379 |
|
| 380 |
|
| 381 |
def test_start_tls_ldap_error(monkeypatch): |
| 382 |
mock_ldap = MagicMock() |
| 383 |
mock_ldap.return_value.start_tls_s.side_effect = FakeLDAPError("fake error") |
| 384 |
monkeypatch.setattr("ldap.initialize", mock_ldap) |
| 385 |
|
| 386 |
with pytest.raises(AnsibleLookupError) as err: |
| 387 |
lookup_loader.get('community.windows.laps_password').run([], domain="dc01", start_tls=True) |
| 388 |
|
| 389 |
assert str(err.value) == "Failed to send StartTLS to LDAP host 'ldap://dc01:389': fake error" |
| 390 |
|
| 391 |
|
| 392 |
def test_simple_bind_ldap_error(monkeypatch): |
| 393 |
mock_ldap = MagicMock() |
| 394 |
mock_ldap.return_value.bind_s.side_effect = FakeLDAPError("fake error") |
| 395 |
monkeypatch.setattr("ldap.initialize", mock_ldap) |
| 396 |
|
| 397 |
with pytest.raises(AnsibleLookupError) as err: |
| 398 |
lookup_loader.get('community.windows.laps_password').run([], domain="dc01", auth="simple", username="user", |
| 399 |
password="pass", allow_plaintext=True) |
| 400 |
|
| 401 |
assert str(err.value) == "Failed to simple bind against LDAP host 'ldap://dc01:389': fake error" |
| 402 |
|
| 403 |
|
| 404 |
def test_sasl_bind_ldap_error(monkeypatch): |
| 405 |
mock_ldap = MagicMock() |
| 406 |
mock_ldap.return_value.sasl_gssapi_bind_s.side_effect = FakeLDAPError("fake error") |
| 407 |
monkeypatch.setattr("ldap.initialize", mock_ldap) |
| 408 |
|
| 409 |
with pytest.raises(AnsibleLookupError) as err: |
| 410 |
lookup_loader.get('community.windows.laps_password').run([], domain="dc01") |
| 411 |
|
| 412 |
assert str(err.value) == "Failed to do a sasl bind against LDAP host 'ldap://dc01:389': fake error" |
| 413 |
|
| 414 |
|
| 415 |
def test_sasl_bind_ldap_no_mechs_error(monkeypatch): |
| 416 |
mock_ldap = MagicMock() |
| 417 |
mock_ldap.return_value.sasl_gssapi_bind_s.side_effect = FakeLDAPAuthUnknownError("no mechs") |
| 418 |
monkeypatch.setattr("ldap.initialize", mock_ldap) |
| 419 |
|
| 420 |
with pytest.raises(AnsibleLookupError) as err: |
| 421 |
lookup_loader.get('community.windows.laps_password').run([], domain="dc01") |
| 422 |
|
| 423 |
assert str(err.value) == "Failed to do a sasl bind against LDAP host 'ldap://dc01:389', the GSSAPI mech is " \ |
| 424 |
"not installed: no mechs" |
| 425 |
|
| 426 |
|
| 427 |
def test_get_password_valid(laps_password): |
| 428 |
mock_conn = MagicMock() |
| 429 |
mock_conn.search_s.return_value = [ |
| 430 |
("CN=server,DC=domain,DC=local", |
| 431 |
{"ms-Mcs-AdmPwd": ["pass"], "distinguishedName": ["CN=server,DC=domain,DC=local"]}), |
| 432 |
# Replicates the 3 extra entries AD returns that aren't server objects |
| 433 |
(None, ["ldap://ForestDnsZones.domain.com/DC=ForestDnsZones,DC=domain,DC=com"]), |
| 434 |
(None, ["ldap://DomainDnsZones.domain.com/DC=DomainDnsZones,DC=domain,DC=com"]), |
| 435 |
(None, ["ldap://domain.com/CN=Configuration,DC=domain,DC=com"]), |
| 436 |
] |
| 437 |
|
| 438 |
actual = laps_password.get_laps_password(mock_conn, "server", "DC=domain,DC=local") |
| 439 |
assert actual == "pass" |
| 440 |
|
| 441 |
assert len(mock_conn.method_calls) == 1 |
| 442 |
assert mock_conn.method_calls[0][0] == "search_s" |
| 443 |
assert mock_conn.method_calls[0][1] == ("DC=domain,DC=local", FakeLdap.SCOPE_SUBTREE, |
| 444 |
"(&(objectClass=computer)(CN=server))") |
| 445 |
assert mock_conn.method_calls[0][2] == {"attrlist": ["distinguishedName", "ms-Mcs-AdmPwd"]} |
| 446 |
|
| 447 |
|
| 448 |
def test_get_password_laps_not_configured(laps_password): |
| 449 |
mock_conn = MagicMock() |
| 450 |
mock_conn.search_s.return_value = [ |
| 451 |
("CN=server,DC=domain,DC=local", {"distinguishedName": ["CN=server,DC=domain,DC=local"]}), |
| 452 |
(None, ["ldap://ForestDnsZones.domain.com/DC=ForestDnsZones,DC=domain,DC=com"]), |
| 453 |
(None, ["ldap://DomainDnsZones.domain.com/DC=DomainDnsZones,DC=domain,DC=com"]), |
| 454 |
(None, ["ldap://domain.com/CN=Configuration,DC=domain,DC=com"]), |
| 455 |
] |
| 456 |
|
| 457 |
with pytest.raises(AnsibleLookupError) as err: |
| 458 |
laps_password.get_laps_password(mock_conn, "server2", "DC=test,DC=local") |
| 459 |
assert str(err.value) == \ |
| 460 |
"The server 'CN=server,DC=domain,DC=local' did not have the LAPS attribute 'ms-Mcs-AdmPwd'" |
| 461 |
|
| 462 |
assert len(mock_conn.method_calls) == 1 |
| 463 |
assert mock_conn.method_calls[0][0] == "search_s" |
| 464 |
assert mock_conn.method_calls[0][1] == ("DC=test,DC=local", FakeLdap.SCOPE_SUBTREE, |
| 465 |
"(&(objectClass=computer)(CN=server2))") |
| 466 |
assert mock_conn.method_calls[0][2] == {"attrlist": ["distinguishedName", "ms-Mcs-AdmPwd"]} |
| 467 |
|
| 468 |
|
| 469 |
def test_get_password_no_results(laps_password): |
| 470 |
mock_conn = MagicMock() |
| 471 |
mock_conn.search_s.return_value = [ |
| 472 |
(None, ["ldap://ForestDnsZones.domain.com/DC=ForestDnsZones,DC=domain,DC=com"]), |
| 473 |
(None, ["ldap://DomainDnsZones.domain.com/DC=DomainDnsZones,DC=domain,DC=com"]), |
| 474 |
(None, ["ldap://domain.com/CN=Configuration,DC=domain,DC=com"]), |
| 475 |
] |
| 476 |
|
| 477 |
with pytest.raises(AnsibleLookupError) as err: |
| 478 |
laps_password.get_laps_password(mock_conn, "server", "DC=domain,DC=local") |
| 479 |
assert str(err.value) == "Failed to find the server 'server' in the base 'DC=domain,DC=local'" |
| 480 |
|
| 481 |
assert len(mock_conn.method_calls) == 1 |
| 482 |
assert mock_conn.method_calls[0][0] == "search_s" |
| 483 |
assert mock_conn.method_calls[0][1] == ("DC=domain,DC=local", FakeLdap.SCOPE_SUBTREE, |
| 484 |
"(&(objectClass=computer)(CN=server))") |
| 485 |
assert mock_conn.method_calls[0][2] == {"attrlist": ["distinguishedName", "ms-Mcs-AdmPwd"]} |
| 486 |
|
| 487 |
|
| 488 |
def test_get_password_multiple_results(laps_password): |
| 489 |
mock_conn = MagicMock() |
| 490 |
mock_conn.search_s.return_value = [ |
| 491 |
("CN=server,OU=Workstations,DC=domain,DC=local", |
| 492 |
{"ms-Mcs-AdmPwd": ["pass"], "distinguishedName": ["CN=server,OU=Workstations,DC=domain,DC=local"]}), |
| 493 |
("CN=server,OU=Servers,DC=domain,DC=local", |
| 494 |
{"ms-Mcs-AdmPwd": ["pass"], "distinguishedName": ["CN=server,OU=Servers,DC=domain,DC=local"]}), |
| 495 |
(None, ["ldap://ForestDnsZones.domain.com/DC=ForestDnsZones,DC=domain,DC=com"]), |
| 496 |
(None, ["ldap://DomainDnsZones.domain.com/DC=DomainDnsZones,DC=domain,DC=com"]), |
| 497 |
(None, ["ldap://domain.com/CN=Configuration,DC=domain,DC=com"]), |
| 498 |
] |
| 499 |
|
| 500 |
with pytest.raises(AnsibleLookupError) as err: |
| 501 |
laps_password.get_laps_password(mock_conn, "server", "DC=domain,DC=local") |
| 502 |
assert str(err.value) == \ |
| 503 |
"Found too many results for the server 'server' in the base 'DC=domain,DC=local'. Specify a more explicit " \ |
| 504 |
"search base for the server required. Found servers 'CN=server,OU=Workstations,DC=domain,DC=local', " \ |
| 505 |
"'CN=server,OU=Servers,DC=domain,DC=local'" |
| 506 |
|
| 507 |
assert len(mock_conn.method_calls) == 1 |
| 508 |
assert mock_conn.method_calls[0][0] == "search_s" |
| 509 |
assert mock_conn.method_calls[0][1] == ("DC=domain,DC=local", FakeLdap.SCOPE_SUBTREE, |
| 510 |
"(&(objectClass=computer)(CN=server))") |
| 511 |
assert mock_conn.method_calls[0][2] == {"attrlist": ["distinguishedName", "ms-Mcs-AdmPwd"]} |