Skip to content

Commit 435faac

Browse files
committed
Use passlib in place of legacycrypt
legacycrypt requires to install libcrypt.so.1 manually Keep legacycrypt as a dependency for now, to ease the transition to those who already have a custom data.py. Move hash_password() to lib.common, to make easier to change the implementation in the future. Signed-off-by: Gaëtan Lehmann <gaetan.lehmann@vates.tech>
1 parent 39f990c commit 435faac

6 files changed

Lines changed: 34 additions & 6 deletions

File tree

data.py-dist

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ from __future__ import annotations
44

55
import os
66

7-
import legacycrypt as crypt # type: ignore[import-untyped]
7+
from lib.common import hash_password
88

99
from typing import TYPE_CHECKING, Any
1010

@@ -17,11 +17,6 @@ if TYPE_CHECKING:
1717
HOST_DEFAULT_USER = "root"
1818
HOST_DEFAULT_PASSWORD = ""
1919

20-
def hash_password(password):
21-
"""Hash password for /etc/password."""
22-
salt = crypt.mksalt(crypt.METHOD_SHA512) # type: ignore
23-
return crypt.crypt(password, salt)
24-
2520
HOST_DEFAULT_PASSWORD_HASH = hash_password(HOST_DEFAULT_PASSWORD)
2621

2722
# Public keys for a private keys available to the test runner

lib/common.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from uuid import UUID
1919

2020
import requests
21+
from passlib.hash import sha512_crypt
2122
from pydantic import TypeAdapter
2223

2324
from typing import (
@@ -386,3 +387,8 @@ def _param_clear(host: Host, xe_prefix: str, uuid: str, param_name: str) -> None
386387
""" Common implementation for param_clear. """
387388
args: dict[str, str | bool | dict[str, str]] = {'uuid': uuid, 'param-name': param_name}
388389
host.xe(f'{xe_prefix}-param-clear', args)
390+
391+
def hash_password(password: str) -> str:
392+
"""Hash password for /etc/shadow."""
393+
# XCP-ng uses sha512 with 5000 rounds by default
394+
return sha512_crypt.using(rounds=5000).hash(password)

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ requires-python = ">=3.11"
77
dependencies = [
88
"cryptography>=3.3.1",
99
"gitpython",
10+
# TODO: keep legacycrypt to let the user transition to lib.common.hash_password in their data.py
1011
"legacycrypt",
1112
"packaging>=20.7",
13+
"passlib",
1214
"pluggy>=1.1.0",
1315
"pydantic",
1416
"pytest>=8.0.0",
@@ -34,6 +36,7 @@ dev = [
3436
"zizmor",
3537
"prek>=0.4.0",
3638
"autopep8",
39+
"types-passlib",
3740
]
3841

3942
[tool.pyright]

requirements/base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cryptography>=3.3.1
33
gitpython
44
legacycrypt
55
packaging>=20.7
6+
passlib
67
pluggy>=1.1.0
78
pydantic
89
pytest>=8.0.0

requirements/dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ types-pexpect
1515
zizmor
1616
prek>=0.4.0
1717
autopep8
18+
types-passlib
1819
-r base.txt

uv.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)