clan-core/pkgs/clan-cli/clan_cli/secrets/import_sops.py

103 lines
3.0 KiB
Python
Raw Normal View History

2023-08-08 13:48:19 +00:00
import argparse
import json
import sys
from pathlib import Path
from ..cmd import run
from ..completions import (
add_dynamic_completer,
complete_groups,
complete_machines,
complete_users,
)
2023-08-08 13:48:19 +00:00
from ..errors import ClanError
from ..nix import nix_shell
2023-08-08 14:28:38 +00:00
from .secrets import encrypt_secret, sops_secrets_folder
2023-08-08 13:48:19 +00:00
def import_sops(args: argparse.Namespace) -> None:
file = Path(args.sops_file)
file_type = file.suffix
try:
file.read_text()
except OSError as e:
raise ClanError(f"Could not read file {file}: {e}") from e
if file_type == ".yaml":
cmd = ["sops"]
if args.input_type:
cmd += ["--input-type", args.input_type]
cmd += ["--output-type", "json", "--decrypt", args.sops_file]
cmd = nix_shell(["nixpkgs#sops"], cmd)
2024-01-12 15:52:29 +00:00
res = run(cmd, error_msg=f"Could not import sops file {file}")
2023-08-08 13:48:19 +00:00
secrets = json.loads(res.stdout)
for k, v in secrets.items():
k = args.prefix + k
2023-08-08 13:48:19 +00:00
if not isinstance(v, str):
print(
f"WARNING: {k} is not a string but {type(v)}, skipping",
file=sys.stderr,
)
2023-08-08 14:28:38 +00:00
continue
if (sops_secrets_folder(Path(args.flake)) / k / "secret").exists():
print(
f"WARNING: {k} already exists, skipping",
file=sys.stderr,
)
continue
encrypt_secret(
Path(args.flake),
sops_secrets_folder(Path(args.flake)) / k,
v,
add_groups=args.group,
add_machines=args.machine,
add_users=args.user,
)
2023-08-08 13:48:19 +00:00
def register_import_sops_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--input-type",
type=str,
default=None,
help="the input type of the sops file (yaml, json, ...). If not specified, it will be guessed from the file extension",
)
group_action = parser.add_argument(
"--group",
type=str,
action="append",
default=[],
help="the group to import the secrets to",
)
add_dynamic_completer(group_action, complete_groups)
machine_action = parser.add_argument(
"--machine",
type=str,
action="append",
default=[],
help="the machine to import the secrets to",
)
add_dynamic_completer(machine_action, complete_machines)
user_action = parser.add_argument(
"--user",
type=str,
action="append",
default=[],
help="the user to import the secrets to",
)
add_dynamic_completer(user_action, complete_users)
parser.add_argument(
"--prefix",
2023-08-08 13:48:19 +00:00
type=str,
default="",
help="the prefix to use for the secret names",
2023-08-08 13:48:19 +00:00
)
parser.add_argument(
2023-08-08 14:28:38 +00:00
"sops_file",
2023-08-08 13:48:19 +00:00
type=str,
2023-08-08 14:28:38 +00:00
help="the sops file to import (- for stdin)",
2023-08-08 13:48:19 +00:00
)
2023-10-23 20:31:12 +00:00
2023-08-08 13:48:19 +00:00
parser.set_defaults(func=import_sops)