age: generate private and public key in one go
This commit is contained in:
@@ -9,8 +9,8 @@ def generate_key() -> str:
|
|||||||
path = default_sops_key_path()
|
path = default_sops_key_path()
|
||||||
if path.exists():
|
if path.exists():
|
||||||
raise ClanError(f"Key already exists at {path}")
|
raise ClanError(f"Key already exists at {path}")
|
||||||
generate_private_key(path)
|
priv_key, pub_key = generate_private_key()
|
||||||
pub_key = get_public_key(path.read_text())
|
path.write_text(priv_key)
|
||||||
return pub_key
|
return pub_key
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -30,10 +30,25 @@ def get_public_key(privkey: str) -> str:
|
|||||||
return res.stdout.strip()
|
return res.stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
def generate_private_key(path: Path) -> None:
|
def generate_private_key() -> tuple[str, str]:
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
cmd = nix_shell(["age"], ["age-keygen"])
|
||||||
cmd = nix_shell(["age"], ["age-keygen", "-o", str(path)])
|
try:
|
||||||
subprocess.run(cmd, check=True)
|
proc = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, text=True)
|
||||||
|
res = proc.stdout.strip()
|
||||||
|
pubkey = None
|
||||||
|
private_key = None
|
||||||
|
for line in res.splitlines():
|
||||||
|
if line.startswith("# public key:"):
|
||||||
|
pubkey = line.split(":")[1].strip()
|
||||||
|
if not line.startswith("#"):
|
||||||
|
private_key = line
|
||||||
|
if not pubkey:
|
||||||
|
raise ClanError("Could not find public key in age-keygen output")
|
||||||
|
if not private_key:
|
||||||
|
raise ClanError("Could not find private key in age-keygen output")
|
||||||
|
return private_key, pubkey
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
raise ClanError("Failed to generate private sops key") from e
|
||||||
|
|
||||||
|
|
||||||
def get_user_name(user: str) -> str:
|
def get_user_name(user: str) -> str:
|
||||||
|
|||||||
Reference in New Issue
Block a user