hint and test dump keyring (#15355)

This commit is contained in:
Kyle Altendorf 2023-05-22 15:54:46 -04:00 committed by GitHub
parent 9938eee808
commit fab36d5b4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 21 deletions

View File

@ -2,9 +2,10 @@
from __future__ import annotations
import os
from io import TextIOWrapper
from pathlib import Path
from typing import Optional
from typing import Optional, cast
import click
import colorama
@ -20,33 +21,37 @@ DEFAULT_KEYRING_YAML = DEFAULT_KEYS_ROOT_PATH / "keyring.yaml"
def get_passphrase_prompt(keyring_file: str) -> str:
prompt = (
colorama.Fore.YELLOW
+ colorama.Style.BRIGHT
+ "(Unlock Keyring: "
+ colorama.Fore.MAGENTA
+ keyring_file
+ colorama.Style.RESET_ALL
+ colorama.Fore.YELLOW
+ colorama.Style.BRIGHT
+ ")"
+ colorama.Style.RESET_ALL
+ " Passphrase: "
) # noqa: E501
# casting since the colors are Any
prompt = cast(
str,
(
colorama.Fore.YELLOW
+ colorama.Style.BRIGHT
+ "(Unlock Keyring: "
+ colorama.Fore.MAGENTA
+ keyring_file
+ colorama.Style.RESET_ALL
+ colorama.Fore.YELLOW
+ colorama.Style.BRIGHT
+ ")"
+ colorama.Style.RESET_ALL
+ " Passphrase: "
), # noqa: E501
)
return prompt
@click.command()
@click.argument("keyring_file", nargs=1, default=DEFAULT_KEYRING_YAML)
@click.argument("keyring_file", nargs=1, default=os.fspath(DEFAULT_KEYRING_YAML))
@click.option(
"--full-payload", is_flag=True, default=False, help="Print the full keyring contents, including plaintext"
)
@click.option("--passphrase-file", type=click.File("r"), help="File or descriptor to read the passphrase from")
@click.option("--pretty-print", is_flag=True, default=False)
def dump(keyring_file, full_payload: bool, passphrase_file: Optional[TextIOWrapper], pretty_print: bool):
def dump(keyring_file: str, full_payload: bool, passphrase_file: Optional[TextIOWrapper], pretty_print: bool) -> None:
saved_passphrase: Optional[str] = KeyringWrapper.get_shared_instance().get_master_passphrase_from_credential_store()
passphrase: str = saved_passphrase or DEFAULT_PASSPHRASE_IF_NO_MASTER_PASSPHRASE
prompt: str = get_passphrase_prompt(str(keyring_file))
prompt: str = get_passphrase_prompt(keyring_file)
print(f"Attempting to dump contents of keyring file: {keyring_file}\n")
@ -67,9 +72,10 @@ def dump(keyring_file, full_payload: bool, passphrase_file: Optional[TextIOWrapp
dump_content = data_dict
if pretty_print:
dump_content = yaml.dump(dump_content)
print(yaml.dump(dump_content))
else:
print(dump_content)
print(dump_content)
break
except (ValueError, InvalidTag):
passphrase = prompt_for_passphrase(prompt)
@ -78,7 +84,7 @@ def dump(keyring_file, full_payload: bool, passphrase_file: Optional[TextIOWrapp
break
def main():
def main() -> None:
colorama.init()
dump() # pylint: disable=no-value-for-parameter

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,60 @@
from __future__ import annotations
import os
import re
from dataclasses import dataclass
from typing import Sequence
from click.testing import CliRunner
from chia.util.dump_keyring import dump
from chia.util.keychain import Keychain
from tests.util.misc import Marks, datacases
output_prefix = """Attempting to dump contents of keyring file: {path}
"""
@dataclass
class KeyringCase:
args: Sequence[str]
regex: str
id: str
marks: Marks = ()
@datacases(
KeyringCase(
id="empty",
args=[],
regex="\\{\\}\n",
),
KeyringCase(
id="empty, pretty",
args=["--pretty-print"],
regex="\\{\\}\n\n",
),
KeyringCase(
id="empty, full",
args=["--full-payload"],
regex=(
"\\{'version': 1, 'salt': '[0-9a-f]{32}', 'nonce': '[0-9a-f]{24}', 'data': \\{\\},"
" 'passphrase_hint': None\\}\n"
),
),
KeyringCase(
id="empty, full, pretty",
args=["--full-payload", "--pretty-print"],
regex="data: \\{\\}\nnonce: [0-9a-f]{24}\npassphrase_hint: null\nsalt: [0-9a-f]{32}\nversion: 1\n\n",
),
)
def test_keyring_dump_empty(empty_keyring: Keychain, case: KeyringCase) -> None:
keyring_path = empty_keyring.keyring_wrapper.keyring.keyring_path
runner = CliRunner()
result = runner.invoke(dump, [*case.args, os.fspath(keyring_path)])
regex = re.escape(output_prefix.format(path=keyring_path)) + case.regex
assert re.fullmatch(regex, result.output) is not None
assert result.exit_code == 0