Skip to content

Commit f9706d0

Browse files
authored
Merge pull request #396 from xcp-ng/gln/passlib-integration-kuyq
Use passlib in place of legacycrypt
2 parents ae37481 + 4004073 commit f9706d0

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
@@ -20,6 +20,7 @@
2020
from uuid import UUID
2121

2222
import requests
23+
from passlib.hash import sha512_crypt
2324
from pydantic import TypeAdapter
2425

2526
from typing import (
@@ -419,3 +420,8 @@ def _param_clear(host: Host, xe_prefix: str, uuid: str, param_name: str) -> None
419420
""" Common implementation for param_clear. """
420421
args: dict[str, str | bool | dict[str, str]] = {'uuid': uuid, 'param-name': param_name}
421422
host.xe(f'{xe_prefix}-param-clear', args)
423+
424+
def hash_password(password: str) -> str:
425+
"""Hash password for /etc/shadow."""
426+
# XCP-ng uses sha512 with 5000 rounds by default
427+
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.5",
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.5
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)