diff --git a/swagspace_cloud_init/__init__.py b/swagspace_cloud_init/__init__.py index adae01b..136a6ca 100644 --- a/swagspace_cloud_init/__init__.py +++ b/swagspace_cloud_init/__init__.py @@ -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), } ]