added ssh key support

This commit is contained in:
Johannes Erwerle 2025-07-21 14:24:25 +02:00
parent f43ac5ed7f
commit 87828e1a88

View file

@ -269,6 +269,30 @@ def password_hash(password: str) -> str:
return pw_hash.stdout
def get_ssh_keys(username: str) -> List[str]:
"""
Gets all SSH keys that are specified for the user in the 'ssh_key/$username/*.pub' directory
"""
keys = list()
dir = Path("ssh_keys") / username
# early abort if the directory does not exists
if not dir.is_dir():
logger.debug(
f"could not find user ssh key diretory: {dir}.\nAdding no SSH Keys"
)
return keys
for file in (Path("ssh_keys") / username).iterdir():
if file.is_file() and file.suffix == ".pub":
keys.append(file.read_text())
logger.debug(f"Found SSH Keys:\n{keys}")
return keys
def build_user_data(username: str, password: str, target_dir: Path) -> Path:
"""
Builds the user-data file in *target_dir* and returns a Path to it.
@ -281,6 +305,7 @@ def build_user_data(username: str, password: str, target_dir: Path) -> Path:
"lock_passwd": False,
"shell": "/bin/bash",
"sudo": "ALL=(ALL) ALL",
"ssh_authorized_keys": get_ssh_keys(username),
}
]