[libvirt] [PATCH 08/20] Secret manipulation step 8: Add virsh commands

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



---
 src/virsh.c |  304 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 304 insertions(+), 0 deletions(-)

diff --git a/src/virsh.c b/src/virsh.c
index 94c3c4e..ad49052 100644
--- a/src/virsh.c
+++ b/src/virsh.c
@@ -41,6 +41,7 @@
 #endif
 
 #include "internal.h"
+#include "base64.h"
 #include "buf.h"
 #include "console.h"
 #include "util.h"
@@ -5249,9 +5250,303 @@ cmdVolPath(vshControl *ctl, const vshCmd *cmd)
 }
 
 
+/*
+ * "secret-allocate-id" command
+ */
+static const vshCmdInfo info_secret_allocate_id[] = {
+    {"help", gettext_noop("allocate an ID for a secret")},
+    {"desc", gettext_noop("Allocate an ID for a secret")},
+    {NULL, NULL}
+};
+
+static int
+cmdSecretAllocateID(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
+{
+    char *secret_id;
+
+    if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+        return FALSE;
+
+    secret_id = virSecretAllocateID(ctl->conn);
+    if (secret_id == NULL) {
+        vshError(ctl, FALSE, "%s", _("failed to allocate a secret ID"));
+        return FALSE;
+    }
+
+    vshPrint(ctl, "%s\n", secret_id);
+    free(secret_id);
+
+    return TRUE;
+}
+
+/*
+ * "secret-set-xml" command
+ */
+static const vshCmdInfo info_secret_set_xml[] = {
+    {"help", gettext_noop("set attributes of a secret from an XML file")},
+    {"desc", gettext_noop("Set attributes of a secret from an XML file")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_secret_set_xml[] = {
+    {"secret-id", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("secret ID")},
+    {"file", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("file containing secret attributes in XML")},
+    {NULL, 0, 0, NULL}
+};
 
+static int
+cmdSecretSetXML(vshControl *ctl, const vshCmd *cmd)
+{
+    char *secret_id, *from;
+    int found, res;
+    char *buffer;
 
+    if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+        return FALSE;
 
+    secret_id = vshCommandOptString(cmd, "secret-id", &found);
+    if (!found)
+        return FALSE;
+
+    from = vshCommandOptString(cmd, "file", &found);
+    if (!found)
+        return FALSE;
+
+    if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0)
+        return FALSE;
+
+    res = virSecretSetXML(ctl->conn, secret_id, buffer);
+    free (buffer);
+
+    if (res != 0) {
+        vshError(ctl, FALSE, _("Failed to set attributes from %s"), from);
+        return FALSE;
+    }
+    vshPrint(ctl, _("Attributes set from %s\n"), from);
+    return TRUE;
+}
+
+/*
+ * "secret-get-xml" command
+ */
+static const vshCmdInfo info_secret_get_xml[] = {
+    {"help", gettext_noop("secret attributes in XML")},
+    {"desc", gettext_noop("Output attributes of a secret as an XML dump to stdout.")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_secret_get_xml[] = {
+    {"secret-id", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("secret ID")},
+    {NULL, 0, 0, NULL}
+};
+
+static int
+cmdSecretGetXML(vshControl *ctl, const vshCmd *cmd)
+{
+    char *secret_id;
+    int found;
+    char *xml;
+
+    if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+        return FALSE;
+
+    secret_id = vshCommandOptString(cmd, "secret-id", &found);
+    if (!found)
+        return FALSE;
+
+    xml = virSecretGetXML(ctl->conn, secret_id);
+    if (xml == NULL)
+        return FALSE;
+
+    printf("%s", xml);
+    free(xml);
+    return TRUE;
+}
+
+/*
+ * "secret-set-value" command
+ */
+static const vshCmdInfo info_secret_set_value[] = {
+    {"help", gettext_noop("set a secret value")},
+    {"desc", gettext_noop("Set a secret value")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_secret_set_value[] = {
+    {"secret-id", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("secret ID")},
+    {"base64", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("base64-encoded secret value")},
+    {NULL, 0, 0, NULL}
+};
+
+static int
+cmdSecretSetValue(vshControl *ctl, const vshCmd *cmd)
+{
+    size_t value_size;
+    char *secret_id, *base64, *value;
+    int found, res;
+
+    if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+        return FALSE;
+
+    secret_id = vshCommandOptString(cmd, "secret-id", &found);
+    if (!found)
+        return FALSE;
+
+    base64 = vshCommandOptString(cmd, "base64", &found);
+    if (!base64)
+        return FALSE;
+
+    if (!base64_decode_alloc(base64, strlen(base64), &value, &value_size)) {
+        vshError(ctl, FALSE, _("Invalid base64 data"));
+        return FALSE;
+    }
+    if (value == NULL) {
+        vshError(ctl, FALSE, "%s", _("Failed to allocate memory"));
+        return FALSE;
+    }
+
+    res = virSecretSetValue(ctl->conn, secret_id, value, value_size);
+    memset(value, 0, value_size);
+    free (value);
+
+    if (res != 0) {
+        vshError(ctl, FALSE, "%s", _("Failed to set secret value"));
+        return FALSE;
+    }
+    vshPrint(ctl, "%s", _("Secret value set\n"));
+    return TRUE;
+}
+
+/*
+ * "secret-get-value" command
+ */
+static const vshCmdInfo info_secret_get_value[] = {
+    {"help", gettext_noop("Output a secret value")},
+    {"desc", gettext_noop("Output a secret value to stdout.")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_secret_get_value[] = {
+    {"secret-id", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("secret ID")},
+    {NULL, 0, 0, NULL}
+};
+
+static int
+cmdSecretGetValue(vshControl *ctl, const vshCmd *cmd)
+{
+    char *secret_id, *base64;
+    int found;
+    size_t value_size;
+    void *value;
+
+    if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+        return FALSE;
+
+    secret_id = vshCommandOptString(cmd, "secret-id", &found);
+    if (!found)
+        return FALSE;
+
+    value = virSecretGetValue(ctl->conn, secret_id, &value_size);
+    if (value == NULL)
+        return FALSE;
+
+    base64_encode_alloc(value, value_size, &base64);
+    memset(value, 0, value_size);
+    free(value);
+
+    if (base64 == NULL) {
+        vshError(ctl, FALSE, "%s", _("Failed to allocate memory"));
+        return FALSE;
+    }
+    printf("%s", base64);
+    memset(base64, 0, strlen(base64));
+    free(base64);
+
+    return TRUE;
+}
+
+/*
+ * "secret-delete" command
+ */
+static const vshCmdInfo info_secret_delete[] = {
+    {"help", gettext_noop("delete a secret")},
+    {"desc", gettext_noop("Delete a secret.")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_secret_delete[] = {
+    {"secret-id", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("secret ID")},
+    {NULL, 0, 0, NULL}
+};
+
+static int
+cmdSecretDelete(vshControl *ctl, const vshCmd *cmd)
+{
+    char *secret_id;
+    int found, ret;
+
+    if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+        return FALSE;
+
+    secret_id = vshCommandOptString(cmd, "secret-id", &found);
+    if (!found)
+        return FALSE;
+
+    ret = virSecretDelete(ctl->conn, secret_id);
+    if (ret < 0) {
+        vshError(ctl, FALSE, _("Failed to delete secret %s"), secret_id);
+        return FALSE;
+    }
+
+    vshPrint(ctl, _("Secret %s deleted\n"), secret_id);
+
+    return TRUE;
+}
+
+/*
+ * "secret-list" command
+ */
+static const vshCmdInfo info_secret_list[] = {
+    {"help", gettext_noop("list secrets")},
+    {"desc", gettext_noop("Returns a list of secrets")},
+    {NULL, NULL}
+};
+
+static int
+cmdSecretList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
+{
+    int maxids = 0, i;
+    char **ids = NULL;
+
+    if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
+        return FALSE;
+
+    maxids = virSecretNumOfSecrets(ctl->conn);
+    if (maxids < 0) {
+        vshError(ctl, FALSE, "%s", _("Failed to list secrets"));
+        return FALSE;
+    }
+    ids = vshMalloc(ctl, sizeof(*ids) * maxids);
+
+    maxids = virSecretListSecrets(ctl->conn, ids, maxids);
+    if (maxids < 0) {
+        vshError(ctl, FALSE, "%s", _("Failed to list secrets"));
+        free(ids);
+        return FALSE;
+    }
+
+    qsort(ids, maxids, sizeof(char *), namesorter);
+
+    vshPrintExtra(ctl, "%s\n", _("Secret ID"));
+    vshPrintExtra(ctl, "-----------------------------------------\n");
+
+    for (i = 0; i < maxids; i++) {
+        vshPrint(ctl, "%-36s\n", ids[i]);
+        free(ids[i]);
+    }
+    free(ids);
+    return TRUE;
+}
 
 
 /*
@@ -6921,6 +7216,15 @@ static const vshCmdDef commands[] = {
     {"pool-undefine", cmdPoolUndefine, opts_pool_undefine, info_pool_undefine},
     {"pool-uuid", cmdPoolUuid, opts_pool_uuid, info_pool_uuid},
 
+    {"secret-allocate-id", cmdSecretAllocateID, NULL, info_secret_allocate_id},
+    {"secret-set-xml", cmdSecretSetXML, opts_secret_set_xml, info_secret_set_xml},
+    {"secret-get-xml", cmdSecretGetXML, opts_secret_get_xml, info_secret_get_xml},
+    {"secret-set-value", cmdSecretSetValue, opts_secret_set_value, info_secret_set_value},
+    {"secret-get-value", cmdSecretGetValue, opts_secret_get_value, info_secret_get_value},
+    {"secret-delete", cmdSecretDelete, opts_secret_delete, info_secret_delete},
+    {"secret-list", cmdSecretList, NULL, info_secret_list},
+
+
 #ifndef WIN32
     {"pwd", cmdPwd, NULL, info_pwd},
 #endif
-- 
1.6.2.5

--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]