40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
"""Client code"""
|
|
|
|
import base64
|
|
|
|
from typing import TextIO
|
|
import click
|
|
import asyncio
|
|
import asyncssh
|
|
|
|
from sshecret.crypto import decode_string, load_private_key
|
|
|
|
|
|
# async def request_secret(host: str, port: str, username: str, client_key: str, secretname: str) -> str:
|
|
# """Request secret."""
|
|
# async with asyncssh.connect(host, port, client_username=username, client_keys=[client_key]) as conn:
|
|
# result = await conn.run(secretname, check=True)
|
|
|
|
# if encoded := result.stdout:
|
|
# if isinstance(encoded, str):
|
|
# return encoded
|
|
# return encoded.decode()
|
|
|
|
|
|
def decrypt_secret(encoded: str, client_key: str) -> str:
|
|
"""Decrypt secret."""
|
|
private_key = load_private_key(client_key)
|
|
return decode_string(encoded, private_key)
|
|
|
|
|
|
@click.command()
|
|
@click.argument("keyfile", type=click.Path(exists=True, readable=True, dir_okay=False))
|
|
@click.argument("encrypted_input", type=click.File("r"))
|
|
def cli_decrypt(keyfile: str, encrypted_input: TextIO) -> None:
|
|
"""Decrypt on command line."""
|
|
decrypted = decrypt_secret(encrypted_input.read(), keyfile)
|
|
click.echo(decrypted)
|
|
|
|
if __name__ == "__main__":
|
|
cli_decrypt()
|