[PATCH v2 4/9] virsh: secret: Add --plain flag for secret-get-value

Peter Krempa pkrempa at redhat.com
Fri Jan 24 16:08:36 UTC 2020


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 at redhat.com>
---
 docs/manpages/virsh.rst |  6 +++++-
 tools/virsh-secret.c    | 17 +++++++++++++++--
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst
index ef15c10e02..0e6eb4cf35 100644
--- a/docs/manpages/virsh.rst
+++ b/docs/manpages/virsh.rst
@@ -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
 ---------------
diff --git a/tools/virsh-secret.c b/tools/virsh-secret.c
index 7067d13353..ead740dd8f 100644
--- a/tools/virsh-secret.c
+++ b/tools/virsh-secret.c
@@ -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;
-- 
2.24.1




More information about the libvir-list mailing list