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 fullmatch as Match
from pathlib import Path, PurePath
from custtypes import ExecutedPath, IdlePath, VirtualPrivateServers, AnsibleScopes
from enum import Enum
from softman import Apps
from custtypes import ExecutedPath, IdlePath
from enum import StrEnum, auto
from random import choice as gamble
from collections.abc import Sequence
from typing import Never, Union, Self, Callable, Required, Literal
from typing import TypedDict as Dict
from glob import glob as globbify
from typing import Never, Self, Callable
from whereami import USER_PATH
from softman import Softs
# import os
class RootFate(Enum):
disposal = 0
retention = 1
class SSHKeyType(Enum):
pubkey = 0
privkey = 1
dual = 2
class SSHKeyType(StrEnum):
pubkey = auto()
privkey = auto()
dual = auto()
# @TODO create unit tests for below class
@@ -40,7 +31,7 @@ class SSHKey:
if len(path) < 2:
self.__value: ExecutedPath | tuple[ExecutedPath] = path[0]
else:
self.category = SSHKeyType.dual.name
self.category = SSHKeyType.dual.name.lower()
self.__value: ExecutedPath | tuple[ExecutedPath] = path
def __int__(self) -> int:
@@ -226,17 +217,17 @@ class SSHKey:
pubkey_present = True
if pubkey_present and privkey_present:
self.category = SSHKeyType.dual.name
self.category = SSHKeyType.dual.name.lower()
elif pubkey_present or privkey_present:
if pubkey_present:
self.category = SSHKeyType.pubkey.name
self.category = SSHKeyType.pubkey.name.lower()
if privkey_present:
self.category = SSHKeyType.privkey.name
self.category = SSHKeyType.privkey.name.lower()
elif isinstance(self.__value, ExecutedPath):
if "-----BEGIN OPENSSH PRIVATE KEY-----" in self.__value.read_text():
self.category = SSHKeyType.privkey.name
self.category = SSHKeyType.privkey.name.lower()
else:
self.category = SSHKeyType.pubkey.name
self.category = SSHKeyType.pubkey.name.lower()
@property
def status(self) -> str:
@@ -582,48 +573,56 @@ class SSHKeyCollection(Sequence):
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()
pubkey = list()
self.__current = self.__first
if datatype == list:
while self.__current is not None:
if self.__current.category == SSHKeyType.privkey.name:
privkey.append(str(self.__current._SSHKey__value))
elif self.__current.category == SSHKeyType.pubkey.name:
pubkey.append(self.__current._SSHKey__value.read_text())
elif self.__current.category == SSHKeyType.dual.name:
privkey.append(str(self.__current._SSHKey__value[0]))
pubkey.append(self.__current._SSHKey__value[1].read_text())
if self.__current.category == SSHKeyType.privkey.name.lower():
privkey.append(self.__current._SSHKey__value)
elif self.__current.category == SSHKeyType.pubkey.name.lower():
pubkey.append(self.__current._SSHKey__value)
elif self.__current.category == SSHKeyType.dual.name.lower():
privkey.append(self.__current._SSHKey__value[0])
pubkey.append(self.__current._SSHKey__value[1])
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:
result = dict()
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))
elif self.__current.category == SSHKeyType.pubkey.name:
elif self.__current.category == SSHKeyType.pubkey.name.lower():
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]))
pubkey.append(self.__current._SSHKey__value[1].read_text())
self.__current = next(self.__first)
if category == SSHKeyType.pubkey.name.lower():
result["ssh_authorized_keys"]: list[str] = pubkey
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
# @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