Ejemplo n.º 1
0
def env(
    client_obj: client.VaultClientBase, path: Sequence[str], command: Sequence[str]
) -> NoReturn:
    """
    Launches a command, loading secrets in environment.

    Strings are exported as-is, other types (including booleans, nulls, dicts, lists)
    are exported as yaml (more specifically as json).
    """
    paths = list(path) or [""]

    env_secrets = {}

    for path in paths:
        secrets = client_obj.get_secrets(path)
        env_secrets.update(
            {
                environment.make_env_key(
                    path=path, key=key
                ): environment.make_env_value(value)
                for key, value in secrets.items()
            }
        )

    environ = os.environ.copy()
    environ.update(env_secrets)

    environment.exec_command(command=command, environ=environ)
Ejemplo n.º 2
0
def _test_make_env_key(base_path, path, prefix, expected):
    assert (
        environment.make_env_key(
            base_path=base_path, path=path, name="myattr", prefix=prefix
        )
        == expected
    )
Ejemplo n.º 3
0
def env(
    client_obj: client.VaultClientBase, path: Sequence[str], command: Sequence[str]
) -> NoReturn:
    """
    Launch a command, loading secrets in environment.

    Strings are exported as-is, other types (including booleans, nulls, dicts, lists)
    are exported as yaml (more specifically as json).

    VARIABLE NAMES

    If the path is not a folder the prefix value is used as the name of the
    variable.
    e.g.: for a/b/key, `--path a/b/c=foo` gives `FOO=...`

    If the path is a folder, then the variable names will be generated as:
    PREFIX + _ + relative path
    e.g.: for a/b/key, `--path a/b=my` gives `MY_KEY=...`

    The standard behavior when no prefix is set will use the relative path to
    the parent of the given path as variable name
    e.g.: for a/b/key, --path a/b gives `B_KEY=...`
    e.g.: for a/b/key, --path a/b/key gives `KEY=...`
    """
    paths = list(path) or [""]

    env_secrets = {}

    for path in paths:
        path, _, prefix = path.partition("=")
        secrets = client_obj.get_secrets(path)
        env_secrets.update(
            {
                environment.make_env_key(
                    path=path, prefix=prefix, key=key
                ): environment.make_env_value(value)
                for key, value in secrets.items()
            }
        )

    environ = os.environ.copy()
    environ.update(env_secrets)

    environment.exec_command(command=command, environ=environ)
Ejemplo n.º 4
0
def test_make_env_key(path, key, expected):
    assert environment.make_env_key(path=path, key=key) == expected