Пример #1
0
def set_(
    client_obj: client.VaultClientBase,
    format_yaml: bool,
    stdin: bool,
    name: str,
    value: Sequence[str],
):
    """
    Set a single secret to the given value(s).

    Value can be either passed as argument (several arguments will be
    interpreted as a list) or via stdin with the --stdin flag.
    """
    if stdin and value:
        raise click.UsageError("Can't set both --stdin and a value")

    final_value: types.JSONValue
    if stdin:
        final_value = click.get_text_stream("stdin").read().strip()

    elif len(value) == 1:
        final_value = value[0]

    else:
        final_value = list(value)

    if format_yaml:
        assert isinstance(final_value, str)
        final_value = yaml.safe_load(final_value)

    client_obj.set_secret(path=name, value=final_value)
    click.echo("Done")
Пример #2
0
def set_(
    client_obj: client.VaultClientBase,
    format_yaml: bool,
    prompt: bool,
    stdin: bool,
    strip: bool,
    name: str,
    value: Sequence[str],
    force: Optional[bool],
):
    """
    Set a single secret to the given value(s).

    Value can be either passed as argument (several arguments will be
    interpreted as a list) or via stdin with the --stdin flag.
    """
    if prompt + bool(value) + stdin > 1:
        raise click.UsageError(
            "Conflicting input methods: use only one of --prompt, --stdin and "
            "positional argument"
        )

    json_value: types.JSONValue
    if stdin:
        json_value = click.get_text_stream("stdin").read()

    elif prompt:
        json_value = click.prompt(f"Please enter value for `{name}`", hide_input=True)

    elif len(value) == 1:
        json_value = value[0]

    else:
        json_value = list(value)

    if format_yaml:
        if not isinstance(json_value, str):
            raise click.UsageError(
                "Cannot pass several values when using yaml format. "
                "Please use a yaml list instead."
            )
        json_value = yaml.safe_load(json_value)

    if strip and isinstance(json_value, str):
        json_value = fix_whitespaces(string=json_value)

    try:
        client_obj.set_secret(path=name, value=json_value, force=force)
    except exceptions.VaultOverwriteSecretError as exc:
        raise click.ClickException(
            f"Secret already exists at {exc.path}. Use -f to force overwriting."
        )
    except exceptions.VaultMixSecretAndFolder as exc:
        raise click.ClickException(str(exc))
    click.echo("Done")
Пример #3
0
def set_(
    client_obj: client.VaultClientBase,
    format_yaml: bool,
    prompt: bool,
    stdin: bool,
    name: str,
    value: Sequence[str],
    force: Optional[bool],
):
    """
    Set a single secret to the given value(s).

    Value can be either passed as argument (several arguments will be
    interpreted as a list) or via stdin with the --stdin flag.
    """
    if prompt and value:
        raise click.UsageError("Can't set both --prompt and a value")

    if stdin and value:
        raise click.UsageError("Can't set both --stdin and a value")

    if stdin and prompt:
        raise click.UsageError("Can't use both --stdin and --prompt")

    final_value: types.JSONValue
    if stdin:
        final_value = click.get_text_stream("stdin").read().strip()
    elif prompt:
        final_value = click.prompt(
            f"Please enter value for `{name}`", hide_input=True
        ).strip()

    elif len(value) == 1:
        final_value = value[0]

    else:
        final_value = list(value)

    if format_yaml:
        assert isinstance(final_value, str)
        final_value = yaml.safe_load(final_value)

    try:
        client_obj.set_secret(path=name, value=final_value, force=force)
    except exceptions.VaultOverwriteSecretError as exc:
        raise click.ClickException(
            f"Secret already exists at {exc.path}. Use -f to force overwriting."
        )
    except exceptions.VaultMixSecretAndFolder as exc:
        raise click.ClickException(str(exc))
    click.echo("Done")
Пример #4
0
def set_(
    client_obj: client.VaultClientBase,
    update: bool,
    prompt: bool,
    yaml_file: TextIO,
    path: str,
    attributes: Sequence[str],
    force: Optional[bool],
):
    """
    Set a secret.

    \b
    You can give secrets in 3 different ways:
    - Usage: vault-cli set [OPTIONS] PATH [key=value...]
      directly in the arguments. A value of "-" means that value will be read from the standard input
    - Usage: vault-cli set [OPTIONS] PATH --prompt [key...]
      prompt user for a values using hidden input
    - Usage: vault-cli set [OPTIONS] PATH --file=/path/to/file
      using a json/yaml file
    """
    if bool(attributes) + bool(yaml_file) > 1:
        raise click.UsageError(
            "Conflicting input methods: you can't mix --file and positional argument"
        )

    json_value: types.JSONValue
    if yaml_file:
        json_value = yaml.safe_load(yaml_file)
    elif prompt:
        json_value = {}
        for key in attributes:
            json_value[key] = click.prompt(
                f"Please enter a value for key `{key}` of `{path}`",
                hide_input=True)
    else:
        json_value = dict(build_kv(attributes))

    try:
        client_obj.set_secret(path=path,
                              value=json_value,
                              force=force,
                              update=update)
    except exceptions.VaultOverwriteSecretError as exc:
        raise click.ClickException(
            f"Secret already exists at {exc.path}. Use -f to force overwriting."
        )
    except exceptions.VaultMixSecretAndFolder as exc:
        raise click.ClickException(str(exc))
    click.echo("Done")