44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
"""Canonical Blueforce device identity."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import re
|
|
from pathlib import Path
|
|
|
|
DEALER_ID_PATTERN = re.compile(r"^[0-9]{8}$")
|
|
DEALER_ID_FILE = Path("/etc/blueforce/dealer-id")
|
|
|
|
|
|
def validate_dealer_id(value: str) -> bool:
|
|
return bool(DEALER_ID_PATTERN.fullmatch(str(value).strip()))
|
|
|
|
|
|
def dealer_id_file(path: str | os.PathLike[str] | None = None) -> Path:
|
|
return Path(path) if path is not None else Path(
|
|
os.environ.get("BF_DEALER_ID_FILE", str(DEALER_ID_FILE))
|
|
)
|
|
|
|
|
|
def read_dealer_id(path: str | os.PathLike[str] | None = None) -> str:
|
|
value = dealer_id_file(path).read_text(encoding="utf-8").strip()
|
|
if not validate_dealer_id(value):
|
|
raise ValueError("dealer-id must contain exactly 8 digits")
|
|
return value
|
|
|
|
|
|
def identity_for(dealer_id: str) -> dict[str, str]:
|
|
dealer_id = str(dealer_id).strip()
|
|
if not validate_dealer_id(dealer_id):
|
|
raise ValueError("dealer-id must contain exactly 8 digits")
|
|
return {
|
|
"dealer_id": dealer_id,
|
|
"username": f"bf-{dealer_id}",
|
|
"hostname": f"bf-{dealer_id}",
|
|
"device_id": f"BF-{dealer_id}",
|
|
"active_user": f"bf-{dealer_id}",
|
|
}
|
|
|
|
|
|
def current_identity(path: str | os.PathLike[str] | None = None) -> dict[str, str]:
|
|
return identity_for(read_dealer_id(path))
|