[libvirt] [PATCH 05/19] secret: Move virStorageSecretType and rename

John Ferlan jferlan at redhat.com
Tue Jun 14 00:27:44 UTC 2016


Move the enum into a new src/util/virsecret.h, rename it to be
virSecretLookupType. Add a src/util/virsecret.h in order to perform
a couple of simple operations on the secret XML and virSecretLookupTypeDef
for clearing and copying.

This includes quite a bit of collateral damage, but the goal is to remove
the "virStorage*" and replace with the virSecretLookupType so that it's
easier to to add new lookups that aren't necessarily storage pool related.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 src/Makefile.am                     |  1 +
 src/conf/secret_conf.h              |  2 +-
 src/libvirt_private.syms            |  5 ++++
 src/libxl/libxl_conf.c              |  2 +-
 src/qemu/qemu_domain.c              |  4 +--
 src/secret/secret_util.c            | 18 ++++++------
 src/secret/secret_util.h            | 10 +++----
 src/storage/storage_backend_iscsi.c |  7 +++--
 src/storage/storage_backend_rbd.c   |  3 +-
 src/util/virsecret.c                | 57 +++++++++++++++++++++++++++++++++++++
 src/util/virsecret.h                | 50 ++++++++++++++++++++++++++++++++
 src/util/virstoragefile.c           | 32 +++++++++------------
 src/util/virstoragefile.h           | 17 ++---------
 tests/qemuargv2xmltest.c            |  4 +--
 14 files changed, 156 insertions(+), 56 deletions(-)
 create mode 100644 src/util/virsecret.c
 create mode 100644 src/util/virsecret.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 4333c2b..ad80cc9 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -161,6 +161,7 @@ UTIL_SOURCES =							\
 		util/virrotatingfile.h util/virrotatingfile.c   \
 		util/virscsi.c util/virscsi.h			\
 		util/virseclabel.c util/virseclabel.h		\
+		util/virsecret.c util/virsecret.h		\
 		util/virsexpr.c util/virsexpr.h			\
 		util/virsocketaddr.h util/virsocketaddr.c	\
 		util/virstats.c util/virstats.h	\
diff --git a/src/conf/secret_conf.h b/src/conf/secret_conf.h
index ca1afec..4584403 100644
--- a/src/conf/secret_conf.h
+++ b/src/conf/secret_conf.h
@@ -35,7 +35,7 @@ struct _virSecretDef {
     bool isprivate;
     unsigned char uuid[VIR_UUID_BUFLEN];
     char *description;          /* May be NULL */
-    int usage_type;
+    int usage_type;  /* virSecretUsageType */
     union {
         char *volume;               /* May be NULL */
         char *ceph;
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ec197a1..32d5179 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2208,6 +2208,11 @@ virSecurityLabelDefFree;
 virSecurityLabelDefNew;
 
 
+# util/virsecret.h
+virSecretLookupDefClear;
+virSecretLookupDefCopy;
+
+
 # util/virsexpr.h
 sexpr2string;
 sexpr_append;
diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c
index acb6594..062a0e4 100644
--- a/src/libxl/libxl_conf.c
+++ b/src/libxl/libxl_conf.c
@@ -656,7 +656,7 @@ libxlMakeNetworkDiskSrc(virStorageSourcePtr src, char **srcstr)
         if (!(conn = virConnectOpen("xen:///system")))
             goto cleanup;
 
-        if (virSecretGetSecretString(conn, src->auth,
+        if (virSecretGetSecretString(conn, &src->auth->secdef,
                                      VIR_SECRET_USAGE_TYPE_CEPH,
                                      &secret, &secretlen) < 0)
             goto cleanup;
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index d1f8175..34e3d95 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -829,7 +829,7 @@ qemuDomainSecretPlainSetup(virConnectPtr conn,
     if (protocol == VIR_STORAGE_NET_PROTOCOL_RBD)
         secretType = VIR_SECRET_USAGE_TYPE_CEPH;
 
-    return virSecretGetSecretString(conn, authdef, secretType,
+    return virSecretGetSecretString(conn, &authdef->secdef, secretType,
                                     &secinfo->s.plain.secret,
                                     &secinfo->s.plain.secretlen);
 }
@@ -902,7 +902,7 @@ qemuDomainSecretAESSetup(virConnectPtr conn,
         goto cleanup;
 
     /* Grab the unencoded secret */
-    if (virSecretGetSecretString(conn, authdef, secretType,
+    if (virSecretGetSecretString(conn, &authdef->secdef, secretType,
                                  &secret, &secretlen) < 0)
         goto cleanup;
 
diff --git a/src/secret/secret_util.c b/src/secret/secret_util.c
index 5602401..7bfe635 100644
--- a/src/secret/secret_util.c
+++ b/src/secret/secret_util.c
@@ -36,12 +36,12 @@ VIR_LOG_INIT("secret.secret_util");
 
 /* virSecretGetSecretString:
  * @conn: Pointer to the connection driver to make secret driver call
- * @authdef: Pointer to the disk storage authentication
- * @secretUsageType: Type of secret usage for authdef lookup
+ * @secdef: Pointer to a storage type def for uuid/usage lookup
+ * @secretUsageType: Type of secret usage for usage lookup
  * @secret: returned secret as a sized stream of unsigned chars
  * @secret_size: Return size of the secret - either raw text or base64
  *
- * Lookup the secret for the authdef usage type and return it as raw text.
+ * Lookup the secret for the usage type and return it as raw text.
  * It is up to the caller to encode the secret further.
  *
  * Returns 0 on success, -1 on failure.  On success the memory in secret
@@ -49,7 +49,7 @@ VIR_LOG_INIT("secret.secret_util");
  */
 int
 virSecretGetSecretString(virConnectPtr conn,
-                         virStorageAuthDefPtr authdef,
+                         virSecretLookupTypeDefPtr secdef,
                          virSecretUsageType secretUsageType,
                          uint8_t **secret,
                          size_t *secret_size)
@@ -57,14 +57,14 @@ virSecretGetSecretString(virConnectPtr conn,
     virSecretPtr sec = NULL;
     int ret = -1;
 
-    switch (authdef->secretType) {
-    case VIR_STORAGE_SECRET_TYPE_UUID:
-        sec = conn->secretDriver->secretLookupByUUID(conn, authdef->secret.uuid);
+    switch (secdef->type) {
+    case VIR_SECRET_LOOKUP_TYPE_UUID:
+        sec = conn->secretDriver->secretLookupByUUID(conn, secdef->u.uuid);
         break;
 
-    case VIR_STORAGE_SECRET_TYPE_USAGE:
+    case VIR_SECRET_LOOKUP_TYPE_USAGE:
         sec = conn->secretDriver->secretLookupByUsage(conn, secretUsageType,
-                                                      authdef->secret.usage);
+                                                      secdef->u.usage);
         break;
     }
 
diff --git a/src/secret/secret_util.h b/src/secret/secret_util.h
index a039662..f7dedfc 100644
--- a/src/secret/secret_util.h
+++ b/src/secret/secret_util.h
@@ -19,17 +19,17 @@
  *
  */
 
-#ifndef __VIR_SECRET_H__
-# define __VIR_SECRET_H__
+#ifndef __VIR_SECRET_UTIL_H__
+# define __VIR_SECRET_UTIL_H__
 
 # include "internal.h"
-# include "virstoragefile.h"
+# include "virsecret.h"
 
 int virSecretGetSecretString(virConnectPtr conn,
-                             virStorageAuthDefPtr authdef,
+                             virSecretLookupTypeDefPtr secdef,
                              virSecretUsageType secretUsageType,
                              uint8_t **ret_secret,
                              size_t *ret_secret_size)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4)
     ATTRIBUTE_NONNULL(5) ATTRIBUTE_RETURN_CHECK;
-#endif /* __VIR_SECRET_H__ */
+#endif /* __VIR_SECRET_UTIL_H__ */
diff --git a/src/storage/storage_backend_iscsi.c b/src/storage/storage_backend_iscsi.c
index 6cefd50..ff013c6 100644
--- a/src/storage/storage_backend_iscsi.c
+++ b/src/storage/storage_backend_iscsi.c
@@ -286,8 +286,8 @@ virStorageBackendISCSISetAuth(const char *portal,
     if (!authdef || authdef->authType == VIR_STORAGE_AUTH_TYPE_NONE)
         return 0;
 
-    VIR_DEBUG("username='%s' authType=%d secretType=%d",
-              authdef->username, authdef->authType, authdef->secretType);
+    VIR_DEBUG("username='%s' authType=%d secdef.type=%d",
+              authdef->username, authdef->authType, authdef->secdef.type);
     if (authdef->authType != VIR_STORAGE_AUTH_TYPE_CHAP) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("iscsi pool only supports 'chap' auth type"));
@@ -301,7 +301,8 @@ virStorageBackendISCSISetAuth(const char *portal,
         return -1;
     }
 
-    if (virSecretGetSecretString(conn, authdef, VIR_SECRET_USAGE_TYPE_ISCSI,
+    if (virSecretGetSecretString(conn, &authdef->secdef,
+                                 VIR_SECRET_USAGE_TYPE_ISCSI,
                                  &secret_value, &secret_size) < 0)
         goto cleanup;
 
diff --git a/src/storage/storage_backend_rbd.c b/src/storage/storage_backend_rbd.c
index 64ec545..bf77c54 100644
--- a/src/storage/storage_backend_rbd.c
+++ b/src/storage/storage_backend_rbd.c
@@ -85,7 +85,8 @@ virStorageBackendRBDOpenRADOSConn(virStorageBackendRBDStatePtr ptr,
             return -1;
         }
 
-        if (virSecretGetSecretString(conn, authdef, VIR_SECRET_USAGE_TYPE_CEPH,
+        if (virSecretGetSecretString(conn, &authdef->secdef,
+                                     VIR_SECRET_USAGE_TYPE_CEPH,
                                      &secret_value, &secret_value_size) < 0)
             goto cleanup;
 
diff --git a/src/util/virsecret.c b/src/util/virsecret.c
new file mode 100644
index 0000000..e7a03b7
--- /dev/null
+++ b/src/util/virsecret.c
@@ -0,0 +1,57 @@
+/*
+ * virsecret.c: secret utility functions
+ *
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <config.h>
+
+#include "viralloc.h"
+#include "virerror.h"
+#include "virlog.h"
+#include "virsecret.h"
+#include "virstring.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+VIR_LOG_INIT("util.secret");
+
+
+void
+virSecretLookupDefClear(virSecretLookupTypeDefPtr secdef)
+{
+    if (secdef->type == VIR_SECRET_LOOKUP_TYPE_USAGE)
+        VIR_FREE(secdef->u.usage);
+    else if (secdef->type == VIR_SECRET_LOOKUP_TYPE_UUID)
+        memset(&secdef->u.uuid, 0, VIR_UUID_BUFLEN);
+}
+
+
+int
+virSecretLookupDefCopy(virSecretLookupTypeDefPtr dst,
+                       const virSecretLookupTypeDef *src)
+{
+    dst->type = src->type;
+    if (dst->type == VIR_SECRET_LOOKUP_TYPE_UUID) {
+        memcpy(dst->u.uuid, src->u.uuid, VIR_UUID_BUFLEN);
+    } else if (dst->type == VIR_SECRET_LOOKUP_TYPE_USAGE) {
+        if (VIR_STRDUP(dst->u.usage, src->u.usage) < 0)
+            return -1;
+    }
+    return 0;
+}
diff --git a/src/util/virsecret.h b/src/util/virsecret.h
new file mode 100644
index 0000000..f2a0b63
--- /dev/null
+++ b/src/util/virsecret.h
@@ -0,0 +1,50 @@
+/*
+ * virsecret.h: secret utility functions
+ *
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef __VIR_SECRET_H__
+# define __VIR_SECRET_H__
+
+# include "internal.h"
+
+typedef enum {
+    VIR_SECRET_LOOKUP_TYPE_NONE,
+    VIR_SECRET_LOOKUP_TYPE_UUID,
+    VIR_SECRET_LOOKUP_TYPE_USAGE,
+
+    VIR_SECRET_LOOKUP_TYPE_LAST
+} virSecretLookupType;
+
+typedef struct _virSecretLookupTypeDef virSecretLookupTypeDef;
+typedef virSecretLookupTypeDef *virSecretLookupTypeDefPtr;
+struct _virSecretLookupTypeDef {
+    int type;   /* virSecretLookupType */
+    union {
+        unsigned char uuid[VIR_UUID_BUFLEN];
+        char *usage;
+    } u;
+
+};
+
+void virSecretLookupDefClear(virSecretLookupTypeDefPtr secdef);
+int virSecretLookupDefCopy(virSecretLookupTypeDefPtr dst,
+                           const virSecretLookupTypeDef *src);
+
+#endif /* __VIR_SECRET_H__ */
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index d2da9e7..7ed52ab 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -1506,8 +1506,7 @@ virStorageAuthDefFree(virStorageAuthDefPtr authdef)
 
     VIR_FREE(authdef->username);
     VIR_FREE(authdef->secrettype);
-    if (authdef->secretType == VIR_STORAGE_SECRET_TYPE_USAGE)
-        VIR_FREE(authdef->secret.usage);
+    virSecretLookupDefClear(&authdef->secdef);
     VIR_FREE(authdef);
 }
 
@@ -1526,13 +1525,10 @@ virStorageAuthDefCopy(const virStorageAuthDef *src)
     if (VIR_STRDUP(ret->secrettype, src->secrettype) < 0)
         goto error;
     ret->authType = src->authType;
-    ret->secretType = src->secretType;
-    if (ret->secretType == VIR_STORAGE_SECRET_TYPE_UUID) {
-        memcpy(ret->secret.uuid, src->secret.uuid, sizeof(ret->secret.uuid));
-    } else if (ret->secretType == VIR_STORAGE_SECRET_TYPE_USAGE) {
-        if (VIR_STRDUP(ret->secret.usage, src->secret.usage) < 0)
-            goto error;
-    }
+
+    if (virSecretLookupDefCopy(&ret->secdef, &src->secdef) < 0)
+        goto error;
+
     return ret;
 
  error:
@@ -1573,16 +1569,16 @@ virStorageAuthDefParseSecret(xmlXPathContextPtr ctxt,
     }
 
     if (uuid) {
-        if (virUUIDParse(uuid, authdef->secret.uuid) < 0) {
+        if (virUUIDParse(uuid, authdef->secdef.u.uuid) < 0) {
             virReportError(VIR_ERR_XML_ERROR, "%s",
                             _("invalid auth secret uuid"));
             goto cleanup;
         }
-        authdef->secretType = VIR_STORAGE_SECRET_TYPE_UUID;
+        authdef->secdef.type = VIR_SECRET_LOOKUP_TYPE_UUID;
     } else {
-        authdef->secret.usage = usage;
+        authdef->secdef.u.usage = usage;
         usage = NULL;
-        authdef->secretType = VIR_STORAGE_SECRET_TYPE_USAGE;
+        authdef->secdef.type = VIR_SECRET_LOOKUP_TYPE_USAGE;
     }
     ret = 0;
 
@@ -1625,7 +1621,7 @@ virStorageAuthDefParseXML(xmlXPathContextPtr ctxt)
         VIR_FREE(authtype);
     }
 
-    authdef->secretType = VIR_STORAGE_SECRET_TYPE_NONE;
+    authdef->secdef.type = VIR_SECRET_LOOKUP_TYPE_NONE;
     if (virStorageAuthDefParseSecret(ctxt, authdef) < 0)
         goto error;
 
@@ -1680,12 +1676,12 @@ virStorageAuthDefFormat(virBufferPtr buf,
     else
         virBufferAddLit(buf, "<secret");
 
-    if (authdef->secretType == VIR_STORAGE_SECRET_TYPE_UUID) {
-        virUUIDFormat(authdef->secret.uuid, uuidstr);
+    if (authdef->secdef.type == VIR_SECRET_LOOKUP_TYPE_UUID) {
+        virUUIDFormat(authdef->secdef.u.uuid, uuidstr);
         virBufferAsprintf(buf, " uuid='%s'/>\n", uuidstr);
-    } else if (authdef->secretType == VIR_STORAGE_SECRET_TYPE_USAGE) {
+    } else if (authdef->secdef.type == VIR_SECRET_LOOKUP_TYPE_USAGE) {
         virBufferEscapeString(buf, " usage='%s'/>\n",
-                              authdef->secret.usage);
+                              authdef->secdef.u.usage);
     } else {
         virBufferAddLit(buf, "/>\n");
     }
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
index b88e715..9424fed 100644
--- a/src/util/virstoragefile.h
+++ b/src/util/virstoragefile.h
@@ -1,7 +1,7 @@
 /*
  * virstoragefile.h: file utility functions for FS storage backend
  *
- * Copyright (C) 2007-2009, 2012-2014 Red Hat, Inc.
+ * Copyright (C) 2007-2009, 2012-2016 Red Hat, Inc.
  * Copyright (C) 2007-2008 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -28,6 +28,7 @@
 # include "virseclabel.h"
 # include "virstorageencryption.h"
 # include "virutil.h"
+# include "virsecret.h"
 
 /* Minimum header size required to probe all known formats with
  * virStorageFileProbeFormat, or obtain metadata from a known format.
@@ -201,25 +202,13 @@ typedef enum {
 } virStorageAuthType;
 VIR_ENUM_DECL(virStorageAuth)
 
-typedef enum {
-    VIR_STORAGE_SECRET_TYPE_NONE,
-    VIR_STORAGE_SECRET_TYPE_UUID,
-    VIR_STORAGE_SECRET_TYPE_USAGE,
-
-    VIR_STORAGE_SECRET_TYPE_LAST
-} virStorageSecretType;
-
 typedef struct _virStorageAuthDef virStorageAuthDef;
 typedef virStorageAuthDef *virStorageAuthDefPtr;
 struct _virStorageAuthDef {
     char *username;
     char *secrettype; /* <secret type='%s' for disk source */
     int authType;     /* virStorageAuthType */
-    int secretType;   /* virStorageSecretType */
-    union {
-        unsigned char uuid[VIR_UUID_BUFLEN];
-        char *usage;
-    } secret;
+    virSecretLookupTypeDef secdef;
 };
 
 typedef struct _virStorageDriverData virStorageDriverData;
diff --git a/tests/qemuargv2xmltest.c b/tests/qemuargv2xmltest.c
index c5fe776..2e79533 100644
--- a/tests/qemuargv2xmltest.c
+++ b/tests/qemuargv2xmltest.c
@@ -36,8 +36,8 @@ static int testSanitizeDef(virDomainDefPtr vmdef)
         virDomainDiskDefPtr disk = vmdef->disks[i];
 
         if (disk->src->auth) {
-            disk->src->auth->secretType = VIR_STORAGE_SECRET_TYPE_USAGE;
-            if (VIR_STRDUP(disk->src->auth->secret.usage,
+            disk->src->auth->secdef.type = VIR_SECRET_LOOKUP_TYPE_USAGE;
+            if (VIR_STRDUP(disk->src->auth->secdef.u.usage,
                           "qemuargv2xml_usage") < 0)
                 goto fail;
         }
-- 
2.5.5




More information about the libvir-list mailing list