2021-10-31 11:53:20 -06:00
|
|
|
from collections import defaultdict
|
|
|
|
from functools import wraps
|
|
|
|
from pathlib import Path
|
|
|
|
from shutil import copytree
|
|
|
|
from tempfile import TemporaryDirectory
|
2021-11-01 11:37:00 -06:00
|
|
|
from typing import Any
|
|
|
|
from typing import Callable
|
2021-10-31 11:53:20 -06:00
|
|
|
from typing import Dict
|
2021-11-01 11:37:00 -06:00
|
|
|
from typing import Generator
|
2021-10-31 11:53:20 -06:00
|
|
|
from typing import List
|
2021-11-01 11:37:00 -06:00
|
|
|
from typing import Optional
|
2021-10-31 11:53:20 -06:00
|
|
|
from typing import Set
|
|
|
|
|
|
|
|
from pytest import fixture
|
|
|
|
|
|
|
|
from libkeyringctl.keyring import convert_certificate
|
|
|
|
from libkeyringctl.keyring import simplify_user_id
|
|
|
|
from libkeyringctl.sequoia import certify
|
|
|
|
from libkeyringctl.sequoia import key_extract_certificate
|
|
|
|
from libkeyringctl.sequoia import key_generate
|
|
|
|
from libkeyringctl.types import Fingerprint
|
|
|
|
from libkeyringctl.types import Uid
|
|
|
|
from libkeyringctl.types import Username
|
|
|
|
from libkeyringctl.util import cwd
|
|
|
|
|
|
|
|
test_keys: Dict[Username, List[Path]] = defaultdict(list)
|
|
|
|
test_certificates: Dict[Username, List[Path]] = defaultdict(list)
|
|
|
|
test_keyring_certificates: Dict[Username, List[Path]] = defaultdict(list)
|
|
|
|
test_main_fingerprints: Set[Fingerprint] = set()
|
|
|
|
|
|
|
|
|
|
|
|
@fixture(autouse=True)
|
2021-11-01 11:37:00 -06:00
|
|
|
def reset_storage() -> None:
|
2021-10-31 11:53:20 -06:00
|
|
|
test_keys.clear()
|
|
|
|
test_certificates.clear()
|
|
|
|
test_keyring_certificates.clear()
|
|
|
|
test_main_fingerprints.clear()
|
|
|
|
|
|
|
|
|
2021-11-01 11:37:00 -06:00
|
|
|
def create_certificate(
|
|
|
|
username: Username,
|
|
|
|
uids: List[Uid],
|
|
|
|
keyring_type: str = "packager",
|
|
|
|
func: Optional[Callable[..., Any]] = None,
|
|
|
|
) -> Callable[..., Any]:
|
|
|
|
def decorator(decorated_func: Callable[..., None]) -> Callable[..., Any]:
|
2021-10-31 11:53:20 -06:00
|
|
|
@wraps(decorated_func)
|
2021-11-01 11:37:00 -06:00
|
|
|
def wrapper(working_dir: Path, *args: Any, **kwargs: Any) -> None:
|
2021-10-31 11:53:20 -06:00
|
|
|
print(username)
|
|
|
|
|
|
|
|
key_directory = working_dir / "secret" / f"{id}"
|
|
|
|
key_directory.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
key_file: Path = key_directory / f"{username}.asc"
|
|
|
|
key_generate(uids=uids, outfile=key_file)
|
|
|
|
test_keys[username].append(key_file)
|
|
|
|
|
|
|
|
certificate_directory = working_dir / "certificate" / f"{id}"
|
|
|
|
certificate_directory.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
keyring_root: Path = working_dir / "keyring"
|
|
|
|
keyring_root.mkdir(parents=True, exist_ok=True)
|
|
|
|
certificate_file: Path = certificate_directory / f"{username}.asc"
|
|
|
|
|
|
|
|
key_extract_certificate(key=key_file, output=certificate_file)
|
|
|
|
test_certificates[username].append(certificate_file)
|
|
|
|
|
|
|
|
target_dir = keyring_root / keyring_type
|
|
|
|
|
|
|
|
decomposed_path: Path = convert_certificate(
|
|
|
|
working_dir=working_dir,
|
|
|
|
certificate=certificate_file,
|
|
|
|
keyring_dir=keyring_root / keyring_type,
|
|
|
|
)
|
|
|
|
user_dir = decomposed_path.parent
|
|
|
|
(target_dir / user_dir.name).mkdir(parents=True, exist_ok=True)
|
|
|
|
copytree(src=user_dir, dst=(target_dir / user_dir.name), dirs_exist_ok=True)
|
|
|
|
test_keyring_certificates[username].append(target_dir / user_dir.name / decomposed_path.name)
|
|
|
|
|
|
|
|
if "main" == keyring_type:
|
|
|
|
test_main_fingerprints.add(Fingerprint(decomposed_path.name))
|
|
|
|
|
|
|
|
decorated_func(working_dir=working_dir, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
if not func:
|
|
|
|
return decorator
|
|
|
|
return decorator(func)
|
|
|
|
|
|
|
|
|
2021-11-01 11:37:00 -06:00
|
|
|
def create_uid_certification(
|
|
|
|
issuer: Username, certified: Username, uid: Uid, func: Optional[Callable[[Any], None]] = None
|
|
|
|
) -> Callable[..., Any]:
|
|
|
|
def decorator(decorated_func: Callable[..., None]) -> Callable[..., Any]:
|
2021-10-31 11:53:20 -06:00
|
|
|
@wraps(decorated_func)
|
2021-11-01 11:37:00 -06:00
|
|
|
def wrapper(working_dir: Path, *args: Any, **kwargs: Any) -> None:
|
2021-10-31 11:53:20 -06:00
|
|
|
key: Path = test_keys[issuer][0]
|
|
|
|
certificate: Path = test_certificates[certified][0]
|
|
|
|
fingerprint: Fingerprint = Fingerprint(test_keyring_certificates[certified][0].name)
|
|
|
|
issuer_fingerprint: Fingerprint = Fingerprint(test_keyring_certificates[issuer][0].name)
|
|
|
|
simplified_uid = simplify_user_id(uid)
|
|
|
|
|
|
|
|
output: Path = (
|
|
|
|
working_dir
|
|
|
|
/ "keyring"
|
|
|
|
/ "packager"
|
|
|
|
/ certified
|
|
|
|
/ fingerprint
|
|
|
|
/ "uid"
|
|
|
|
/ simplified_uid
|
|
|
|
/ "certification"
|
|
|
|
/ f"{issuer_fingerprint}.asc"
|
|
|
|
)
|
|
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
certify(key, certificate, uid, output)
|
|
|
|
|
|
|
|
decorated_func(working_dir=working_dir, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
if not func:
|
|
|
|
return decorator
|
|
|
|
return decorator(func)
|
|
|
|
|
|
|
|
|
|
|
|
@fixture(scope="function")
|
2021-11-01 11:37:00 -06:00
|
|
|
def working_dir() -> Generator[Path, None, None]:
|
2021-10-31 11:53:20 -06:00
|
|
|
with TemporaryDirectory(prefix="arch-keyringctl-test-") as tempdir:
|
2021-11-01 11:37:00 -06:00
|
|
|
path: Path = Path(tempdir)
|
|
|
|
with cwd(path):
|
|
|
|
yield path
|
2021-10-31 11:53:20 -06:00
|
|
|
|
|
|
|
|
|
|
|
@fixture(scope="function")
|
2021-11-01 11:37:00 -06:00
|
|
|
def keyring_dir(working_dir: Path) -> Generator[Path, None, None]:
|
2021-10-31 11:53:20 -06:00
|
|
|
yield working_dir / "keyring"
|