2023-08-08 13:48:19 +00:00
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
|
|
|
|
2024-06-04 13:17:24 +00:00
|
|
|
from ..cmd import run
|
2024-06-04 11:40:24 +00:00
|
|
|
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]
|
2023-12-08 14:00:11 +00:00
|
|
|
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():
|
2023-08-08 16:46:37 +00:00
|
|
|
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
|
2023-11-03 11:51:17 +00:00
|
|
|
if (sops_secrets_folder(Path(args.flake)) / k / "secret").exists():
|
2023-08-08 16:46:37 +00:00
|
|
|
print(
|
|
|
|
f"WARNING: {k} already exists, skipping",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
encrypt_secret(
|
2023-11-03 11:51:17 +00:00
|
|
|
Path(args.flake),
|
|
|
|
sops_secrets_folder(Path(args.flake)) / k,
|
2023-08-08 16:46:37 +00:00
|
|
|
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(
|
2023-08-08 16:46:37 +00:00
|
|
|
"--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",
|
|
|
|
)
|
2024-06-04 11:40:24 +00:00
|
|
|
group_action = parser.add_argument(
|
2023-08-08 16:46:37 +00:00
|
|
|
"--group",
|
|
|
|
type=str,
|
|
|
|
action="append",
|
|
|
|
default=[],
|
|
|
|
help="the group to import the secrets to",
|
|
|
|
)
|
2024-06-04 11:40:24 +00:00
|
|
|
add_dynamic_completer(group_action, complete_groups)
|
|
|
|
machine_action = parser.add_argument(
|
2023-08-08 16:46:37 +00:00
|
|
|
"--machine",
|
|
|
|
type=str,
|
|
|
|
action="append",
|
|
|
|
default=[],
|
|
|
|
help="the machine to import the secrets to",
|
|
|
|
)
|
2024-06-04 11:40:24 +00:00
|
|
|
add_dynamic_completer(machine_action, complete_machines)
|
|
|
|
user_action = parser.add_argument(
|
2023-08-08 16:46:37 +00:00
|
|
|
"--user",
|
|
|
|
type=str,
|
|
|
|
action="append",
|
|
|
|
default=[],
|
|
|
|
help="the user to import the secrets to",
|
|
|
|
)
|
2024-06-04 11:40:24 +00:00
|
|
|
add_dynamic_completer(user_action, complete_users)
|
2023-08-08 16:46:37 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--prefix",
|
2023-08-08 13:48:19 +00:00
|
|
|
type=str,
|
2023-08-08 16:46:37 +00:00
|
|
|
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)
|