Merge pull request 'add api for secret groups and listing secrets' (#112) from Mic92-mic92 into main
All checks were successful
build / test (push) Successful in 15s
All checks were successful
build / test (push) Successful in 15s
This commit is contained in:
commit
e1265b2e04
@ -23,29 +23,51 @@ def users_folder(group: str) -> Path:
|
|||||||
return sops_groups_folder() / group / "users"
|
return sops_groups_folder() / group / "users"
|
||||||
|
|
||||||
|
|
||||||
# TODO: make this a tree
|
class Group:
|
||||||
def list_command(args: argparse.Namespace) -> None:
|
def __init__(self, name: str, machines: list[str], users: list[str]) -> None:
|
||||||
|
self.name = name
|
||||||
|
self.machines = machines
|
||||||
|
self.users = users
|
||||||
|
|
||||||
|
|
||||||
|
def list_groups() -> list[Group]:
|
||||||
|
groups: list[Group] = []
|
||||||
folder = sops_groups_folder()
|
folder = sops_groups_folder()
|
||||||
if not folder.exists():
|
if not folder.exists():
|
||||||
return
|
return groups
|
||||||
|
|
||||||
for group in os.listdir(folder):
|
for name in os.listdir(folder):
|
||||||
group_folder = folder / group
|
group_folder = folder / name
|
||||||
if not group_folder.is_dir():
|
if not group_folder.is_dir():
|
||||||
continue
|
continue
|
||||||
print(group)
|
machines_path = machines_folder(name)
|
||||||
machines = machines_folder(group)
|
machines = []
|
||||||
if machines.is_dir():
|
if machines_path.is_dir():
|
||||||
print("machines:")
|
for f in machines_path.iterdir():
|
||||||
for f in machines.iterdir():
|
|
||||||
if validate_hostname(f.name):
|
if validate_hostname(f.name):
|
||||||
print(f.name)
|
machines.append(f.name)
|
||||||
users = users_folder(group)
|
users_path = users_folder(name)
|
||||||
if users.is_dir():
|
users = []
|
||||||
print("users:")
|
if users_path.is_dir():
|
||||||
for f in users.iterdir():
|
for f in users_path.iterdir():
|
||||||
if VALID_USER_NAME.match(f.name):
|
if VALID_USER_NAME.match(f.name):
|
||||||
print(f)
|
users.append(f.name)
|
||||||
|
groups.append(Group(name, machines, users))
|
||||||
|
return groups
|
||||||
|
|
||||||
|
|
||||||
|
def list_command(args: argparse.Namespace) -> None:
|
||||||
|
for group in list_groups():
|
||||||
|
print(group.name)
|
||||||
|
if group.machines:
|
||||||
|
print("machines:")
|
||||||
|
for machine in group.machines:
|
||||||
|
print(f" {machine}")
|
||||||
|
if group.users:
|
||||||
|
print("users:")
|
||||||
|
for user in group.users:
|
||||||
|
print(f" {user}")
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
def list_directory(directory: Path) -> str:
|
def list_directory(directory: Path) -> str:
|
||||||
@ -89,38 +111,56 @@ def remove_member(group_folder: Path, name: str) -> None:
|
|||||||
os.rmdir(group_folder.parent)
|
os.rmdir(group_folder.parent)
|
||||||
|
|
||||||
|
|
||||||
|
def add_user(group: str, name: str) -> None:
|
||||||
|
add_member(users_folder(group), sops_users_folder(), name)
|
||||||
|
|
||||||
|
|
||||||
def add_user_command(args: argparse.Namespace) -> None:
|
def add_user_command(args: argparse.Namespace) -> None:
|
||||||
add_member(users_folder(args.group), sops_users_folder(), args.user)
|
add_user(args.group, args.user)
|
||||||
|
|
||||||
|
|
||||||
|
def remove_user(group: str, name: str) -> None:
|
||||||
|
remove_member(users_folder(group), name)
|
||||||
|
|
||||||
|
|
||||||
def remove_user_command(args: argparse.Namespace) -> None:
|
def remove_user_command(args: argparse.Namespace) -> None:
|
||||||
remove_member(users_folder(args.group), args.user)
|
remove_user(args.group, args.user)
|
||||||
|
|
||||||
|
|
||||||
|
def add_machine(group: str, name: str) -> None:
|
||||||
|
add_member(machines_folder(group), sops_machines_folder(), name)
|
||||||
|
|
||||||
|
|
||||||
def add_machine_command(args: argparse.Namespace) -> None:
|
def add_machine_command(args: argparse.Namespace) -> None:
|
||||||
add_member(
|
add_machine(args.group, args.machine)
|
||||||
machines_folder(args.group),
|
|
||||||
sops_machines_folder(),
|
|
||||||
args.machine,
|
def remove_machine(group: str, name: str) -> None:
|
||||||
)
|
remove_member(machines_folder(group), name)
|
||||||
|
|
||||||
|
|
||||||
def remove_machine_command(args: argparse.Namespace) -> None:
|
def remove_machine_command(args: argparse.Namespace) -> None:
|
||||||
remove_member(machines_folder(args.group), args.machine)
|
remove_machine(args.group, args.machine)
|
||||||
|
|
||||||
|
|
||||||
def add_group_argument(parser: argparse.ArgumentParser) -> None:
|
def add_group_argument(parser: argparse.ArgumentParser) -> None:
|
||||||
parser.add_argument("group", help="the name of the secret", type=group_name_type)
|
parser.add_argument("group", help="the name of the secret", type=group_name_type)
|
||||||
|
|
||||||
|
|
||||||
|
def add_secret(group: str, name: str) -> None:
|
||||||
|
secrets.allow_member(secrets.groups_folder(name), sops_groups_folder(), group)
|
||||||
|
|
||||||
|
|
||||||
def add_secret_command(args: argparse.Namespace) -> None:
|
def add_secret_command(args: argparse.Namespace) -> None:
|
||||||
secrets.allow_member(
|
add_secret(args.group, args.secret)
|
||||||
secrets.groups_folder(args.secret), sops_groups_folder(), args.group
|
|
||||||
)
|
|
||||||
|
def remove_secret(group: str, name: str) -> None:
|
||||||
|
secrets.disallow_member(secrets.groups_folder(name), group)
|
||||||
|
|
||||||
|
|
||||||
def remove_secret_command(args: argparse.Namespace) -> None:
|
def remove_secret_command(args: argparse.Namespace) -> None:
|
||||||
secrets.disallow_member(secrets.groups_folder(args.secret), args.group)
|
remove_secret(args.group, args.secret)
|
||||||
|
|
||||||
|
|
||||||
def register_groups_parser(parser: argparse.ArgumentParser) -> None:
|
def register_groups_parser(parser: argparse.ArgumentParser) -> None:
|
||||||
|
@ -183,13 +183,16 @@ def list_command(args: argparse.Namespace) -> None:
|
|||||||
print("\n".join(lst))
|
print("\n".join(lst))
|
||||||
|
|
||||||
|
|
||||||
def get_command(args: argparse.Namespace) -> None:
|
def decrypt_secret(secret: str) -> str:
|
||||||
secret: str = args.secret
|
|
||||||
ensure_sops_key()
|
ensure_sops_key()
|
||||||
secret_path = sops_secrets_folder() / secret / "secret"
|
secret_path = sops_secrets_folder() / secret / "secret"
|
||||||
if not secret_path.exists():
|
if not secret_path.exists():
|
||||||
raise ClanError(f"Secret '{secret}' does not exist")
|
raise ClanError(f"Secret '{secret}' does not exist")
|
||||||
print(decrypt_file(secret_path), end="")
|
return decrypt_file(secret_path)
|
||||||
|
|
||||||
|
|
||||||
|
def get_command(args: argparse.Namespace) -> None:
|
||||||
|
print(decrypt_secret(args.secret), end="")
|
||||||
|
|
||||||
|
|
||||||
def set_command(args: argparse.Namespace) -> None:
|
def set_command(args: argparse.Namespace) -> None:
|
||||||
|
@ -18,7 +18,7 @@ log() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# If the commit has no files, skip everything as there is nothing to format
|
# If the commit has no files, skip everything as there is nothing to format
|
||||||
if [[ ${#commit_files} = 0 ]]; then
|
if [[ -z ${commit_files+x} ]] || [[ ${#commit_files} = 0 ]]; then
|
||||||
log "no files to format"
|
log "no files to format"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
Loading…
Reference in New Issue
Block a user