refactor: renamed module with utilities for management of SSH keys

This commit is contained in:
2026-01-21 09:16:51 -05:00
parent 05a680eb7e
commit 4eab3bd787

View File

@@ -1,27 +1,18 @@
from re import Pattern as RegEx from re import Pattern as RegEx
from re import fullmatch as Match from re import fullmatch as Match
from pathlib import Path, PurePath from pathlib import Path, PurePath
from custtypes import ExecutedPath, IdlePath, VirtualPrivateServers, AnsibleScopes from custtypes import ExecutedPath, IdlePath
from enum import Enum from enum import StrEnum, auto
from softman import Apps
from random import choice as gamble from random import choice as gamble
from collections.abc import Sequence from collections.abc import Sequence
from typing import Never, Union, Self, Callable, Required, Literal from typing import Never, Self, Callable
from typing import TypedDict as Dict
from glob import glob as globbify
from whereami import USER_PATH from whereami import USER_PATH
from softman import Softs
# import os # import os
class RootFate(Enum): class SSHKeyType(StrEnum):
disposal = 0 pubkey = auto()
retention = 1 privkey = auto()
dual = auto()
class SSHKeyType(Enum):
pubkey = 0
privkey = 1
dual = 2
# @TODO create unit tests for below class # @TODO create unit tests for below class
@@ -40,7 +31,7 @@ class SSHKey:
if len(path) < 2: if len(path) < 2:
self.__value: ExecutedPath | tuple[ExecutedPath] = path[0] self.__value: ExecutedPath | tuple[ExecutedPath] = path[0]
else: else:
self.category = SSHKeyType.dual.name self.category = SSHKeyType.dual.name.lower()
self.__value: ExecutedPath | tuple[ExecutedPath] = path self.__value: ExecutedPath | tuple[ExecutedPath] = path
def __int__(self) -> int: def __int__(self) -> int:
@@ -226,17 +217,17 @@ class SSHKey:
pubkey_present = True pubkey_present = True
if pubkey_present and privkey_present: if pubkey_present and privkey_present:
self.category = SSHKeyType.dual.name self.category = SSHKeyType.dual.name.lower()
elif pubkey_present or privkey_present: elif pubkey_present or privkey_present:
if pubkey_present: if pubkey_present:
self.category = SSHKeyType.pubkey.name self.category = SSHKeyType.pubkey.name.lower()
if privkey_present: if privkey_present:
self.category = SSHKeyType.privkey.name self.category = SSHKeyType.privkey.name.lower()
elif isinstance(self.__value, ExecutedPath): elif isinstance(self.__value, ExecutedPath):
if "-----BEGIN OPENSSH PRIVATE KEY-----" in self.__value.read_text(): if "-----BEGIN OPENSSH PRIVATE KEY-----" in self.__value.read_text():
self.category = SSHKeyType.privkey.name self.category = SSHKeyType.privkey.name.lower()
else: else:
self.category = SSHKeyType.pubkey.name self.category = SSHKeyType.pubkey.name.lower()
@property @property
def status(self) -> str: def status(self) -> str:
@@ -582,48 +573,56 @@ class SSHKeyCollection(Sequence):
return prefix + content + postfix return prefix + content + postfix
def publish(self, pref: int | None = None, datatype = dict): def publish(self, category: SSHKeyType = SSHKeyType.pubkey.name.lower(), pref: int | None = None, datatype = dict):
privkey = list() privkey = list()
pubkey = list() pubkey = list()
self.__current = self.__first self.__current = self.__first
if datatype == list: if datatype == list:
while self.__current is not None: while self.__current is not None:
if self.__current.category == SSHKeyType.privkey.name: if self.__current.category == SSHKeyType.privkey.name.lower():
privkey.append(str(self.__current._SSHKey__value)) privkey.append(self.__current._SSHKey__value)
elif self.__current.category == SSHKeyType.pubkey.name: elif self.__current.category == SSHKeyType.pubkey.name.lower():
pubkey.append(self.__current._SSHKey__value.read_text()) pubkey.append(self.__current._SSHKey__value)
elif self.__current.category == SSHKeyType.dual.name: elif self.__current.category == SSHKeyType.dual.name.lower():
privkey.append(str(self.__current._SSHKey__value[0])) privkey.append(self.__current._SSHKey__value[0])
pubkey.append(self.__current._SSHKey__value[1].read_text()) pubkey.append(self.__current._SSHKey__value[1])
self.__current = next(self.__first) self.__current = next(self.__first)
return (privkey, pubkey, gamble(range(len(privkey)))) if pref is None:
preference = gamble(range(len(privkey)))
else:
preference = pref
if category == SSHKeyType.pubkey.name.lower():
return pubkey
elif category == SSHKeyType.privkey.name.lower():
return (privkey, preference)
else:
return (privkey, pubkey, preference)
elif datatype == dict: elif datatype == dict:
result = dict() result = dict()
while self.__current is not None: while self.__current is not None:
if self.__current.category == SSHKeyType.privkey.name: if self.__current.category == SSHKeyType.privkey.name.lower():
privkey.append(str(self.__current._SSHKey__value)) privkey.append(str(self.__current._SSHKey__value))
elif self.__current.category == SSHKeyType.pubkey.name: elif self.__current.category == SSHKeyType.pubkey.name.lower():
pubkey.append(self.__current._SSHKey__value.read_text()) pubkey.append(self.__current._SSHKey__value.read_text())
elif self.__current.category == SSHKeyType.dual.name: elif self.__current.category == SSHKeyType.dual.name.lower():
privkey.append(str(self.__current._SSHKey__value[0])) privkey.append(str(self.__current._SSHKey__value[0]))
pubkey.append(self.__current._SSHKey__value[1].read_text()) pubkey.append(self.__current._SSHKey__value[1].read_text())
self.__current = next(self.__first) self.__current = next(self.__first)
result["ssh_authorized_keys"]: list[str] = pubkey if category == SSHKeyType.pubkey.name.lower():
result["ssh_private_key_paths"]: list[str] = privkey result["ssh_authorized_keys"]: list[str] = pubkey
result["ssh_private_key_path_pref"]: int = pref if pref is not None else gamble(range(len(privkey)))
if category == SSHKeyType.privkey.name.lower():
result["ssh_private_key_paths"]: list[str] = privkey
result["ssh_private_key_path_pref"]: int = pref if pref is not None else gamble(range(len(privkey)))
if category == SSHKeyType.dual.name.lower():
result["ssh_authorized_keys"]: list[str] = pubkey
result["ssh_private_key_paths"]: list[str] = privkey
result["ssh_private_key_path_pref"]: int = pref if pref is not None else gamble(range(len(privkey)))
return result return result
# @TODO maybe move to separate module for classes for handling users and groups
class UserSSH:
def __init__(self, username: str = "root", paths: Apps | None = None, keys: dict = dict(), password: str = "password123", fate: RootFate = RootFate.disposal.name):
self.username = username
self.paths = paths
self.keys = keys
self.password = password
self.fate = fate