예제 #1
0
def test_edit_command_loop(tmpdir, capsys):
    with EncryptedConfigFile(str(tmpdir / "asdf"), write_lock=True) as sf:
        editor = Editor("true", sf)
        editor.cleartext = "asdf"

        with pytest.raises(ValueError):
            editor.process_cmd('asdf')

        def broken_cmd():
            raise RuntimeError('gpg is broken')

        editor.edit = broken_cmd
        editor.encrypt = broken_cmd

        cmds = ['edit', 'asdf', 'encrypt', 'quit']

        def _input():
            return cmds.pop(0)

        editor._input = _input
        editor.interact()

    out, err = capsys.readouterr()
    assert err == ''
    assert out == """\
예제 #2
0
def test_edit(tmpdir):
    with EncryptedConfigFile(str(tmpdir / "asdf"), write_lock=True) as sf:
        editor = Editor("true",
                        environment='none',
                        edit_file=list(sf.files.keys())[0])
        editor.cleartext = "asdf"
        editor.edit()
        assert editor.cleartext == "asdf"
예제 #3
0
def test_open_nonexistent_file_for_write_should_create_template_file():
    tf = tempfile.NamedTemporaryFile(prefix="new_encrypted.")
    tf.close()  # deletes file
    encrypted = EncryptedConfigFile(tf.name, write_lock=True)
    with encrypted as secrets:
        assert secrets.main_file.cleartext == NEW_FILE_TEMPLATE
        # The file exists, because we set the write lock
        assert os.path.exists(secrets.main_file.encrypted_filename)
    # When saving a config without members, delete the file.
    assert not os.path.exists(secrets.main_file.encrypted_filename)
예제 #4
0
def test_secrets_override_without_interpolation(tmp_path):
    environment = tmp_path / "environments" / "env"
    environment.mkdir(parents=True)
    # `add_secrets_to_environment` assumes to be run in the batou root
    os.chdir(tmp_path)
    secret_file = environment / "secrets.cfg"
    encrypted = EncryptedConfigFile(secret_file, write_lock=True)

    with encrypted as secrets:
        secrets.main_file.cleartext = """\
[batou]
members = batou
[asdf]
x = asdf%asdf%
[host:localhost]
data-asdf = 2
data-bsdf = 1
data-csdf = 3
"""
        secrets.read()

        with encrypted.add_file(environment / 'secret-asdf.txt') as f:
            f.cleartext = 'hello to me!'

        secrets.write()

    env = mock.Mock()
    env.name = "env"
    env.components = ["asdf"]
    env.overrides = {}
    env.hosts = {}
    env.hosts["localhost"] = host = mock.Mock()
    env.secret_files = {}
    env.secret_data = set()
    host.data = {"asdf": 1, "csdf": 3}

    add_secrets_to_environment(env)

    assert env.overrides == {"asdf": {"x": "asdf%asdf%"}}
    assert host.data == {"asdf": "2", "bsdf": "1", "csdf": "3"}
    assert env.secret_files['asdf.txt'] == 'hello to me!'
    assert env.secret_data == {'asdf%asdf%', 'hello', 'me!'}
예제 #5
0
def test_write_should_fail_unless_write_locked(encrypted_file):
    with EncryptedConfigFile(encrypted_file) as secrets:
        secrets.main_file.cleartext = """\
[batou]
members = batou
[asdf]
x = 1
"""
        secrets.read()

        with pytest.raises(RuntimeError):
            secrets.write()
예제 #6
0
def test_write_fails_if_recipient_key_is_missing_keeps_old_file(
        encrypted_file):
    encrypted = EncryptedConfigFile(encrypted_file, write_lock=True)
    with encrypted as secrets:
        secrets.main_file.cleartext = """\
[batou]
members = [email protected]
[asdf]
x = 1
"""
        secrets.read()
        with pytest.raises(RuntimeError):
            secrets.write()
    assert encrypted_file.read_bytes() == FIXTURE_ENCRYPTED_CONFIG.read_bytes()
예제 #7
0
def test_write_fails_without_recipients(encrypted_file):
    encrypted = EncryptedConfigFile(encrypted_file, write_lock=True)
    with encrypted as secrets:
        secrets.main_file.cleartext = """\
[batou]
members =
[asdf]
x = 1
"""
        secrets.read()
        with pytest.raises(ValueError) as exc:
            secrets.write()

        assert str(exc.value).startswith('Need at least one recipient.')
예제 #8
0
def test_write(encrypted_file):
    encrypted = EncryptedConfigFile(encrypted_file, write_lock=True)
    with encrypted as secrets:
        secrets.main_file.cleartext = """\
[batou]
members = batou
[asdf]
x = 1
"""
        secrets.read()
        secrets.write()

    assert FIXTURE_ENCRYPTED_CONFIG.read_bytes() != encrypted_file.read_bytes()
    assert 0 != encrypted_file.stat().st_size
예제 #9
0
def test_open_nonexistent_file_for_read_should_fail():
    with pytest.raises(IOError):
        EncryptedConfigFile("/no/such/file").__enter__()
예제 #10
0
def test_decrypt_missing_key(monkeypatch, encrypted_file):
    monkeypatch.setitem(os.environ, "GNUPGHOME", '/tmp')

    with pytest.raises(batou.GPGCallError):
        EncryptedConfigFile(encrypted_file)
예제 #11
0
def test_caches_cleartext(encrypted_file):
    with EncryptedConfigFile(encrypted_file) as secrets:
        secrets.main_file.cleartext = "[foo]bar=2"
        secrets.read()
        assert secrets.main_file.cleartext == "[foo]bar=2"
예제 #12
0
def test_decrypt(encrypted_file):
    with EncryptedConfigFile(encrypted_file) as secrets:
        with open(cleartext_file) as cleartext:
            assert (cleartext.read().strip() ==
                    secrets.main_file.cleartext.strip())
예제 #13
0
def test_write_unparsable_raises_error(encrypted_file):
    encrypted = EncryptedConfigFile(encrypted_file, write_lock=True)
    with encrypted as secrets:
        secrets.main_file.cleartext = "some new file contents\n"
        with pytest.raises(configparser.Error):
            secrets.read()
예제 #14
0
파일: test_editor.py 프로젝트: wosc/batou
def test_edit(tmpdir):
    with EncryptedConfigFile(str(tmpdir / "asdf"), write_lock=True) as sf:
        editor = Editor("true", sf)
        editor.cleartext = "asdf"
        editor.edit()
        assert editor.cleartext == "asdf"
예제 #15
0
def test_edit(tmpdir):
    with EncryptedConfigFile(str(tmpdir / 'asdf'), write_lock=True) as sf:
        editor = Editor('true', sf)
        editor.cleartext = 'asdf'
        editor.edit()
        assert editor.cleartext == 'asdf'