mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 06:17:25 +00:00
Work-in-progress commit on the collective code-scanning sidetree, so main can be merged in; the single quality-high gate runs on the integration branch before the pull request. Fixes the CodeQL notes py/import-and-import-from, py/unused-import, py/unused-global-variable, py/empty-except, py/mixed-tuple-returns and py/unused-local-variable across 194 files, and adds one lint per rule under tests/lint/repository/python/ so none of them can come back: test_import_and_import_from, test_unused_imports_hidden_from_ruff, test_unused_global_variables, test_empty_except, test_mixed_tuple_returns, test_unused_local_variables, plus test_modules_import. Accepted security finding: MEDUSA-MP-SCAN-2056 (low) on utils/i18n/client.py:21, an import line matched by a keyword heuristic; operator-confirmed false positive. 🎯 Fix: 85 % 💥 Break: 8 % 💸 Cost: – ⏱️ Time: – 🛡️ Security: 100 % 🏆 Score: 78 🚦 Gate: ⛔ fix 85 % < 90, break 8 % > 5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
141 lines
4.5 KiB
Python
141 lines
4.5 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import sys
|
|
import types
|
|
import unittest
|
|
|
|
from . import PROJECT_ROOT
|
|
|
|
sys.path.insert(0, str(PROJECT_ROOT))
|
|
|
|
MODULE_PATH = PROJECT_ROOT / "library" / "ldap_passwords.py"
|
|
URI = "ldap://ldap:389"
|
|
ALICE = "uid=alice,ou=users,dc=example,dc=org"
|
|
BOB = "uid=bob,ou=users,dc=example,dc=org"
|
|
|
|
|
|
class _LDAPError(Exception):
|
|
pass
|
|
|
|
|
|
class _InvalidCredentialsError(_LDAPError):
|
|
pass
|
|
|
|
|
|
class _Server:
|
|
def __init__(self, passwords, unreachable=(), refuse_change=()):
|
|
self.passwords = dict(passwords)
|
|
self.unreachable = set(unreachable)
|
|
self.refuse_change = set(refuse_change)
|
|
self.changes = []
|
|
self.unbinds = 0
|
|
|
|
def initialize(self, uri):
|
|
if uri != URI:
|
|
raise _LDAPError(f"unexpected uri {uri}")
|
|
return _Connection(self)
|
|
|
|
|
|
class _Connection:
|
|
def __init__(self, server):
|
|
self.server = server
|
|
|
|
def simple_bind_s(self, dn, passwd):
|
|
if dn in self.server.unreachable:
|
|
raise _LDAPError("server down")
|
|
if self.server.passwords.get(dn) != passwd:
|
|
raise _InvalidCredentialsError(dn)
|
|
|
|
def unbind(self):
|
|
self.server.unbinds += 1
|
|
|
|
def passwd_s(self, dn, oldpw, newpw):
|
|
if dn in self.server.refuse_change:
|
|
raise _LDAPError("insufficient access")
|
|
self.server.changes.append((dn, oldpw, newpw))
|
|
self.server.passwords[dn] = newpw
|
|
|
|
|
|
def _load(server) -> types.ModuleType:
|
|
fake = types.ModuleType("ldap")
|
|
fake.LDAPError = _LDAPError
|
|
fake.INVALID_CREDENTIALS = _InvalidCredentialsError
|
|
fake.OPT_REFERRALS = 8
|
|
fake.initialize = server.initialize
|
|
sys.modules["ldap"] = fake
|
|
spec = importlib.util.spec_from_file_location(
|
|
"library_ldap_passwords", str(MODULE_PATH)
|
|
)
|
|
if spec is None or spec.loader is None:
|
|
raise ImportError(f"Could not load: {MODULE_PATH}")
|
|
mod = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(mod) # type: ignore[attr-defined]
|
|
return mod
|
|
|
|
|
|
def _converge(server, entries, check_mode=False):
|
|
mod = _load(server)
|
|
admin = server.initialize(URI)
|
|
return mod.converge(admin, URI, entries, check_mode)
|
|
|
|
|
|
class ConvergeTests(unittest.TestCase):
|
|
def test_a_password_that_already_binds_is_left_alone(self):
|
|
server = _Server({ALICE: "current"})
|
|
changed, errors = _converge(server, [{"dn": ALICE, "passwd": "current"}])
|
|
self.assertEqual((changed, errors), ([], {}))
|
|
self.assertEqual(server.changes, [])
|
|
|
|
def test_a_password_that_does_not_bind_is_set(self):
|
|
server = _Server({ALICE: "stale", BOB: "current"})
|
|
changed, errors = _converge(
|
|
server,
|
|
[{"dn": ALICE, "passwd": "fresh"}, {"dn": BOB, "passwd": "current"}],
|
|
)
|
|
self.assertEqual((changed, errors), ([ALICE], {}))
|
|
self.assertEqual(server.changes, [(ALICE, None, "fresh")])
|
|
|
|
def test_an_entry_without_a_password_yet_is_set(self):
|
|
server = _Server({})
|
|
changed, _ = _converge(server, [{"dn": ALICE, "passwd": "fresh"}])
|
|
self.assertEqual(changed, [ALICE])
|
|
|
|
def test_every_probe_connection_is_unbound(self):
|
|
server = _Server({ALICE: "stale", BOB: "current"})
|
|
_converge(
|
|
server,
|
|
[{"dn": ALICE, "passwd": "fresh"}, {"dn": BOB, "passwd": "current"}],
|
|
)
|
|
self.assertEqual(server.unbinds, 2)
|
|
|
|
def test_check_mode_reports_without_writing(self):
|
|
server = _Server({ALICE: "stale"})
|
|
changed, errors = _converge(
|
|
server, [{"dn": ALICE, "passwd": "fresh"}], check_mode=True
|
|
)
|
|
self.assertEqual((changed, errors), ([ALICE], {}))
|
|
self.assertEqual(server.changes, [])
|
|
|
|
|
|
class FailureTests(unittest.TestCase):
|
|
def test_a_bind_error_names_the_entry_and_the_rest_still_converge(self):
|
|
server = _Server({BOB: "stale"}, unreachable={ALICE})
|
|
changed, errors = _converge(
|
|
server,
|
|
[{"dn": ALICE, "passwd": "fresh"}, {"dn": BOB, "passwd": "fresh"}],
|
|
)
|
|
self.assertEqual(changed, [BOB])
|
|
self.assertEqual(list(errors), [ALICE])
|
|
self.assertIn(ALICE, errors[ALICE])
|
|
|
|
def test_a_refused_change_names_the_entry_and_is_not_reported_changed(self):
|
|
server = _Server({ALICE: "stale"}, refuse_change={ALICE})
|
|
changed, errors = _converge(server, [{"dn": ALICE, "passwd": "fresh"}])
|
|
self.assertEqual(changed, [])
|
|
self.assertIn("Unable to set the password of " + ALICE, errors[ALICE])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|