add machine subcommand

This commit is contained in:
Jörg Thalheim
2023-08-24 16:58:22 +02:00
parent be78e65b11
commit c5b16124ef
9 changed files with 104 additions and 25 deletions

View File

@@ -3,15 +3,14 @@ import os
from pathlib import Path
from ..errors import ClanError
from ..machines.types import machine_name_type, validate_hostname
from . import secrets
from .folders import sops_groups_folder, sops_machines_folder, sops_users_folder
from .types import (
VALID_USER_NAME,
group_name_type,
machine_name_type,
secret_name_type,
user_name_type,
validate_hostname,
)

View File

@@ -1,13 +1,12 @@
import argparse
from ..machines.types import machine_name_type, validate_hostname
from . import secrets
from .folders import list_objects, remove_object, sops_machines_folder
from .sops import write_key
from .types import (
machine_name_type,
public_or_private_age_key_type,
secret_name_type,
validate_hostname,
)

View File

@@ -9,13 +9,6 @@ from .sops import get_public_key
VALID_SECRET_NAME = re.compile(r"^[a-zA-Z0-9._-]+$")
VALID_USER_NAME = re.compile(r"^[a-z_]([a-z0-9_-]{0,31})?$")
VALID_HOSTNAME = re.compile(r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$", re.IGNORECASE)
def validate_hostname(hostname: str) -> bool:
if len(hostname) > 63:
return False
return VALID_HOSTNAME.match(hostname) is not None
def secret_name_type(arg_value: str) -> str:
@@ -26,18 +19,6 @@ def secret_name_type(arg_value: str) -> str:
return arg_value
def machine_name_type(arg_value: str) -> str:
if len(arg_value) > 63:
raise argparse.ArgumentTypeError(
"Machine name must be less than 63 characters long"
)
if not VALID_SECRET_NAME.match(arg_value):
raise argparse.ArgumentTypeError(
"Invalid character in machine name. Allowed characters are a-z, 0-9, ., -, and _. Must not start with a number"
)
return arg_value
def public_or_private_age_key_type(arg_value: str) -> str:
if os.path.isfile(arg_value):
arg_value = Path(arg_value).read_text().strip()