virsh: secret: Add --plain flag for secret-get-value

Users might want to get the raw value instead of dealing with base64
encoding. This might be useful for redirection to file and also for
simple human-readable secrets.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Peter Krempa 2020-01-10 15:12:16 +01:00
parent 1a552eccf1
commit 5611795b2b
2 changed files with 20 additions and 3 deletions

View File

@ -6576,11 +6576,15 @@ secret-get-value
.. code-block::
secret-get-value secret
secret-get-value [--plain] secret
Output the value associated with *secret* (specified by its UUID) to stdout,
encoded using Base64.
If the *--plain* flag is used the value is not base64 encoded, but rather
printed raw. Note that unless virsh is started in quiet mode (*virsh -q*) it
prints a newline at the end of the command. This newline is not part of the
secret.
secret-undefine
---------------

View File

@ -234,6 +234,10 @@ static const vshCmdOptDef opts_secret_get_value[] = {
.help = N_("secret UUID"),
.completer = virshSecretUUIDCompleter,
},
{.name = "plain",
.type = VSH_OT_BOOL,
.help = N_("get value without converting to base64")
},
{.name = NULL}
};
@ -244,6 +248,7 @@ cmdSecretGetValue(vshControl *ctl, const vshCmd *cmd)
VIR_AUTODISPOSE_STR base64 = NULL;
unsigned char *value;
size_t value_size;
bool plain = vshCommandOptBool(cmd, "plain");
if (!(secret = virshCommandOptSecret(ctl, cmd, NULL)))
return false;
@ -251,9 +256,17 @@ cmdSecretGetValue(vshControl *ctl, const vshCmd *cmd)
if (!(value = virSecretGetValue(secret, &value_size, 0)))
return false;
base64 = g_base64_encode(value, value_size);
if (plain) {
if (fwrite(value, 1, value_size, stdout) != value_size) {
VIR_DISPOSE_N(value, value_size);
vshError(ctl, "failed to write secret");
return false;
}
} else {
base64 = g_base64_encode(value, value_size);
vshPrint(ctl, "%s", base64);
vshPrint(ctl, "%s", base64);
}
VIR_DISPOSE_N(value, value_size);
return true;