1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-21 03:09:04 +00:00

udm_user, homectl - replace crypt/legacycrypt with passlib (#11860)

* udm_user - replace crypt/legacycrypt with passlib

The stdlib crypt module was removed in Python 3.13. Replace the
crypt/legacycrypt import chain with passlib (already used elsewhere
in the collection) and use CryptContext.verify() for password
comparison.

Fixes #4690

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Add changelog fragment for PR 11860

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* remove redundant ignore file entries

* udm_user, homectl - replace crypt/legacycrypt with _crypt module utils

Add a new _crypt module_utils that abstracts password hashing and
verification. It uses passlib when available, falling back to the
stdlib crypt or legacycrypt, and raises ImportError if none of them
can be imported. Both udm_user and homectl now use this shared
utility, fixing compatibility with Python 3.13+.

Fixes #4690

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Add BOTMETA entry for _crypt module utils

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* _crypt - fix mypy errors and handle complete unavailability

Replace CryptContext = object fallback (rejected by mypy) with a
proper dummy class definition. Add has_crypt_context flag so modules
can detect when no backend is available. Update both modules to
import and check has_crypt_context instead of testing for None.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* adjsutments from review

* Update plugins/modules/homectl.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/udm_user.py

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Alexei Znamensky 2026-04-20 07:53:44 +12:00 committed by GitHub
parent 39f4cda6b5
commit 25b21183bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 100 additions and 96 deletions

View file

@ -16,10 +16,11 @@ description:
- This module allows to manage posix users on a univention corporate server (UCS). It uses the Python API of the UCS to
create a new object or edit it.
notes:
- This module requires the deprecated L(crypt Python module, https://docs.python.org/3.12/library/crypt.html) library which
was removed from Python 3.13. For Python 3.13 or newer, you need to install L(legacycrypt, https://pypi.org/project/legacycrypt/).
- This module uses L(passlib, https://pypi.org/project/passlib/) for password hashing when available,
falling back to the Python C(crypt) module or L(legacycrypt, https://pypi.org/project/legacycrypt/).
requirements:
- legacycrypt (on Python 3.13 or newer)
- passlib (Python library, recommended), or legacycrypt on Python 3.13 or newer
- It requires no dependency on Python 3.12 and earlier, but then it relies on the deprecated standard library C(crypt).
extends_documentation_fragment:
- community.general.attributes
attributes:
@ -316,10 +317,18 @@ EXAMPLES = r"""
RETURN = """#"""
import traceback
from datetime import date, timedelta
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.plugins.module_utils import deps
with deps.declare("crypt_context"):
from ansible_collections.community.general.plugins.module_utils._crypt import CryptContext, has_crypt_context
if not has_crypt_context:
raise ImportError("Failed to import any of: passlib, crypt, legacycrypt")
from ansible_collections.community.general.plugins.module_utils.univention_umc import (
base_dn,
@ -328,29 +337,6 @@ from ansible_collections.community.general.plugins.module_utils.univention_umc i
umc_module_for_edit,
)
CRYPT_IMPORT_ERROR: str | None
try:
import crypt
except ImportError:
HAS_CRYPT = False
CRYPT_IMPORT_ERROR = traceback.format_exc()
else:
HAS_CRYPT = True
CRYPT_IMPORT_ERROR = None
LEGACYCRYPT_IMPORT_ERROR: str | None
try:
import legacycrypt
if not HAS_CRYPT:
crypt = legacycrypt
except ImportError:
HAS_LEGACYCRYPT = False
LEGACYCRYPT_IMPORT_ERROR = traceback.format_exc()
else:
HAS_LEGACYCRYPT = True
LEGACYCRYPT_IMPORT_ERROR = None
def main():
expiry = date.strftime(date.today() + timedelta(days=365), "%Y-%m-%d")
@ -410,11 +396,9 @@ def main():
required_if=([("state", "present", ["firstname", "lastname", "password"])]),
)
if not HAS_CRYPT and not HAS_LEGACYCRYPT:
module.fail_json(
msg=missing_required_lib("crypt (part of standard library up to Python 3.12) or legacycrypt (PyPI)"),
exception=LEGACYCRYPT_IMPORT_ERROR,
)
deps.validate(module)
crypt_context = CryptContext(schemes=["sha512_crypt", "sha256_crypt", "md5_crypt", "des_crypt"])
username = module.params["username"]
position = module.params["position"]
@ -474,7 +458,7 @@ def main():
obj["password"] = password
if module.params["update_password"] == "always":
old_password = obj["password"].split("}", 2)[1]
if crypt.crypt(password, old_password) != old_password:
if not crypt_context.verify(password, old_password):
obj["overridePWHistory"] = module.params["overridePWHistory"]
obj["overridePWLength"] = module.params["overridePWLength"]
obj["password"] = password