From 59222622c76eb2831522080ed859855762f2ecf4 Mon Sep 17 00:00:00 2001 From: Lech Gudalewicz Date: Tue, 17 Mar 2020 15:40:29 -0700 Subject: [PATCH] 202003.5 --- .flake8 | 2 +- README.md | 6 +++++- hush/console.py | 50 ++++++++++++++++++++++++++++------------------- pyproject.toml | 4 ++-- tests/test_cmd.py | 44 ++++++++++++++++++++++++++++++++++++++++- 5 files changed, 81 insertions(+), 25 deletions(-) diff --git a/.flake8 b/.flake8 index becb475..b7fd779 100644 --- a/.flake8 +++ b/.flake8 @@ -1,5 +1,5 @@ [flake8] exclude = .venv -ignore = E203 +ignore = E203, E231 [isort] diff --git a/README.md b/README.md index c5189d8..e655d3e 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,13 @@ Hush does not require a master password as other password managers do, instead i #### What's new? +- Version 202003.5 (Mar 17, 2020) + + - Fixed configuration initialization issue + - Version 202003.4 (Mar 6, 2020) - - introduced GCM mode for the AES encryption. Choose between GCM and EAX. For the secrets encrypted with the previous version explicitly pass `--mode eax` to decrypt. + - Introduced GCM mode for the AES encryption. Choose between GCM and EAX. For the secrets encrypted with the previous version explicitly pass `--mode eax` to decrypt. - Version 202001.4 (Jan 23, 2020) diff --git a/hush/console.py b/hush/console.py index acd7d1b..6893c75 100644 --- a/hush/console.py +++ b/hush/console.py @@ -66,7 +66,7 @@ def config_callback(ctx, param, value): @click.group() -@click.version_option("202003.4") +@click.version_option("202003.5") @click.option( "-c", "--config-file", @@ -344,23 +344,33 @@ def config(ctx, list, set, val): "-p", "--public-key-file", type=str, required=True, help="Public key file", ) @click.option( - "--yes", type=bool, help="Overwrite the existing config file if exists.", + "-f", + "--overwrite", + is_flag=True, + help="Overwrite the existing config file if exists.", ) -def init(ctx, private_key_file, public_key_file, yes): - if not yes and os.path.exists(ctx.config_file): - yn = click.prompt(f"{ctx.config_file} exists, overwrite? [y/n]") - yes = yn.strip() and yn[0].lower() == "y" - if yes: - with configuration(ctx.config_file) as config: - values = [ - ("generate", "length", str(DEFAULT_PASSWORD_LENGTH)), - ("generate", "character_clsses", DEFAULT_CHARACTER_CLASSES), - ("decrypt", "private_key_file", private_key_file), - ("encrypt", "public_key_file", public_key_file), - ] - for (section, key, v) in values: - if section not in config.sections(): - config.add_section(section) - config[section][key] = v - - pass +def init(ctx, private_key_file, public_key_file, overwrite): + if os.path.exists(ctx.config_file): + if overwrite: + os.remove(ctx.config_file) + else: + yn = click.prompt(f"{ctx.config_file} exists, overwrite? [y/n]") + yes = yn.strip() and yn[0].lower() == "y" + if yes: + os.remove(ctx.config_file) + else: + return 1 + + with configuration(ctx.config_file) as config: + values = [ + ("generate", "length", str(DEFAULT_PASSWORD_LENGTH)), + ("generate", "character_clsses", DEFAULT_CHARACTER_CLASSES), + ("encrypt", "public_key_file", public_key_file), + ("encrypt", "mode", "eax"), + ("decrypt", "private_key_file", private_key_file), + ("decrypt", "mode", "eax"), + ] + for (section, key, v) in values: + if section not in config.sections(): + config.add_section(section) + config[section][key] = v diff --git a/pyproject.toml b/pyproject.toml index a3e20f3..154c259 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ multi_line_output=3 include_trailing_comma = true [pycalver] -current_version = "v202001.0003" +current_version = "v202003.0005" version_pattern = "{pycalver}" commit = false tag = false @@ -30,7 +30,7 @@ push = false [tool.poetry] name = "hush" -version = "202003.4" +version = "202003.5" description = "Minimalistic command line secret management" license = "BSD-3-Clause" authors = ["Lech Gudalewicz "] diff --git a/tests/test_cmd.py b/tests/test_cmd.py index 19fa939..0d47f27 100644 --- a/tests/test_cmd.py +++ b/tests/test_cmd.py @@ -32,11 +32,18 @@ def config_file(lines): os.remove(file_name) +@contextmanager +def temp_file(): + file_name = mktemp() + yield file_name + os.remove(file_name) + + def test_version(): runner = CliRunner() output = runner.invoke(cli, "--version").output.strip() - assert "202003.4" in output + assert "202003.5" in output def test_generate_default(): @@ -278,6 +285,41 @@ def test_change_passphrase_empty(): assert result.output.strip() == "secret" +def test_config_init(): + runner = CliRunner() + with temp_file() as t: + with keypair(): + result = runner.invoke( + cli, ["-c", t, "init", "-r", "rsa.pri", "-p", "rsa.pub"], + ) + assert result.exit_code == 0 + result = runner.invoke(cli, ["-c", t, "config", "encrypt.mode"]) + assert result.exit_code == 0 + assert result.output.strip() == "eax" + + +def test_config_init_override(): + runner = CliRunner() + with temp_file() as t: + with keypair(): + result = runner.invoke( + cli, ["-c", t, "init", "-r", "rsa.pri", "-p", "rsa.pub"], + ) + assert result.exit_code == 0 + result = runner.invoke( + cli, ["-c", t, "config", "-s", "encrypt.mode", "gcm"] + ) + assert result.exit_code == 0 + result = runner.invoke(cli, ["-c", t, "config", "encrypt.mode"]) + assert result.exit_code == 0 + assert result.output.strip() == "gcm" + result = runner.invoke( + cli, + ["-c", t, "init", "-f", "-r", "rsa.pri", "-p", "rsa.pub",], + ) + assert result.exit_code == 0 + + def test_alterantive_config(): lines = """ [generate]