]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: secret: Add --plain flag for secret-get-value
authorPeter Krempa <pkrempa@redhat.com>
Fri, 10 Jan 2020 14:12:16 +0000 (15:12 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 28 Jan 2020 17:09:57 +0000 (18:09 +0100)
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>
docs/manpages/virsh.rst
tools/virsh-secret.c

index ef15c10e02533b71e5811412f6edd6ad62436e68..0e6eb4cf35cb84f06d34a6359487e66324403cf4 100644 (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
 ---------------
index 7067d1335343f672767d3e03df01199b4f3bbaad..ead740dd8ffb02f2e01d89843ab4b8af447ff336 100644 (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;