Ejemplo n.º 1
0
def test_get_secret_with_prefix(vault_prefix, transform):
    """Trailing and leading slashes don't matter."""
    config = Konfig(vault_backend=VaultBackend(transform(vault_prefix),
                                               try_env_first=False))
    assert config.get_secret("/path/to") == {
        "SECRET": "value",
        "IS_SECRET": True,
        "DECIMAL": "1.3"
    }
Ejemplo n.º 2
0
def test_retry_object(vault_prefix, mocker):
    config = Konfig(
        vault_backend=VaultBackend(
            vault_prefix,
            retry=Retrying(retry=retry_if_exception_type(KonfettiError), reraise=True, stop=stop_after_attempt(2)),
        )
    )
    mocker.patch("requests.adapters.HTTPAdapter.send", side_effect=KonfettiError)
    m = mocker.patch.object(config.vault_backend, "_call", wraps=config.vault_backend._call)
    with pytest.raises(KonfettiError):
        config.SECRET
    assert m.called is True
    assert m.call_count == 2
Ejemplo n.º 3
0
def test_from_object(vault_prefix, vault_addr, vault_token):
    class Test:
        VALUE = 42
        VAULT_ADDR = env("VAULT_ADDR")
        VAULT_TOKEN = env("VAULT_TOKEN")
        SECRET = vault("path/to")["SECRET"]

    config = Konfig.from_object(Test, vault_backend=VaultBackend(vault_prefix))
    assert config.asdict() == {
        "VALUE": 42,
        "SECRET": "value",
        "VAULT_ADDR": vault_addr,
        "VAULT_TOKEN": vault_token
    }
Ejemplo n.º 4
0
def test_vault_override_variables(monkeypatch, vault_prefix):
    monkeypatch.setenv("KONFETTI_SETTINGS", "test_app.settings.subset")
    config = Konfig(vault_backend=VaultBackend(vault_prefix))
    assert config.vault.get_override_examples() == {
        "NESTED_SECRET": {
            "PATH__TO__NESTED":
            '{"NESTED_SECRET": {"nested": "example_value"}}'
        },
        "SECRET": {
            "PATH__TO": '{"SECRET": "example_value"}'
        },
        "WHOLE_SECRET": {
            "PATH__TO": "{}"
        },
    }
Ejemplo n.º 5
0
def test_vault_var_reusage(vault_prefix, vault_addr, vault_token):
    variable = vault("path/to")

    class Test:
        VAULT_ADDR = vault_addr
        VAULT_TOKEN = vault_token
        SECRET = variable["SECRET"]
        IS_SECRET = variable["IS_SECRET"]

    config = Konfig.from_object(Test, vault_backend=VaultBackend(vault_prefix))
    assert config.asdict() == {
        "SECRET": "value",
        "IS_SECRET": True,
        "VAULT_ADDR": vault_addr,
        "VAULT_TOKEN": vault_token,
    }
Ejemplo n.º 6
0
def test_from_string(vault_prefix, vault_addr, vault_token):
    config = Konfig.from_object("test_app.settings.subset",
                                vault_backend=VaultBackend(vault_prefix))
    assert config.asdict() == {
        "DEBUG": True,
        "SECRET": "value",
        "KEY": "value",
        "VAULT_ADDR": vault_addr,
        "VAULT_TOKEN": vault_token,
        "NESTED_SECRET": "what?",
        "WHOLE_SECRET": {
            "DECIMAL": "1.3",
            "IS_SECRET": True,
            "SECRET": "value"
        },
        "DICTIONARY": {
            "static": 1,
            "env": True,
            "vault": "value"
        },
    }
Ejemplo n.º 7
0
def test_asdict(monkeypatch, vault_prefix, vault_addr, vault_token):
    monkeypatch.setenv("KONFETTI_SETTINGS", "test_app.settings.subset")
    config = Konfig(vault_backend=VaultBackend(vault_prefix))
    assert config.asdict() == {
        "DEBUG": True,
        "SECRET": "value",
        "KEY": "value",
        "VAULT_ADDR": vault_addr,
        "VAULT_TOKEN": vault_token,
        "NESTED_SECRET": "what?",
        "WHOLE_SECRET": {
            "DECIMAL": "1.3",
            "IS_SECRET": True,
            "SECRET": "value"
        },
        "DICTIONARY": {
            "static": 1,
            "env": True,
            "vault": "value"
        },
    }
Ejemplo n.º 8
0
def test_ttl(ttl, exc_type, message):
    with pytest.raises(exc_type, match=message):
        VaultBackend("path/to", cache_ttl=ttl)
Ejemplo n.º 9
0
def config_with_cached_vault(vault_prefix):
    return Konfig(vault_backend=VaultBackend(vault_prefix, cache_ttl=1))
Ejemplo n.º 10
0
def test_prefixes(prefix, path, expected):
    backend = VaultBackend(prefix)
    assert backend._get_full_path(path) == expected
Ejemplo n.º 11
0
def test_vault_override_variables_cache(monkeypatch, vault_prefix):
    monkeypatch.setenv("KONFETTI_SETTINGS", "test_app.settings.subset")
    config = Konfig(vault_backend=VaultBackend(vault_prefix))
    assert config.vault is config.vault
    assert config.vault.get_override_examples(
    ) is config.vault.get_override_examples()
Ejemplo n.º 12
0
def test_from_json(vault_prefix):
    path = os.path.join(HERE, "test_app/settings/config.json")
    config = Konfig.from_json(path, vault_backend=VaultBackend(vault_prefix))
    assert config.asdict() == {"VALUE": "from json", "SECRET": 42}
Ejemplo n.º 13
0
def test_dictionary_access(monkeypatch, vault_prefix):
    monkeypatch.setenv("KONFETTI_SETTINGS", "test_app.settings.subset")
    config = Konfig(vault_backend=VaultBackend(vault_prefix))
    assert config.DICTIONARY == {"static": 1, "env": True, "vault": "value"}