]> xenbits.xensource.com Git - libvirt.git/commitdiff
secret: Move and rename secretLoadAllConfigs
authorJohn Ferlan <jferlan@redhat.com>
Wed, 2 Mar 2016 18:10:54 +0000 (13:10 -0500)
committerJohn Ferlan <jferlan@redhat.com>
Mon, 25 Apr 2016 19:45:29 +0000 (15:45 -0400)
Move to secret_conf.c and rename to virSecretLoadAllConfigs. Also includes
moving/renaming the supporting virSecretLoad, virSecretLoadValue, and
virSecretLoadValidateUUID.

src/conf/secret_conf.c
src/conf/secret_conf.h
src/conf/virsecretobj.c
src/conf/virsecretobj.h
src/libvirt_private.syms
src/secret/secret_driver.c

index 8373051b122124786bc331e2fbf253d13d64ae0b..5c39f24d1cca0bd4e6de34f9e27534c94de68d80 100644 (file)
@@ -28,6 +28,7 @@
 #include "virlog.h"
 #include "viralloc.h"
 #include "secret_conf.h"
+#include "virsecretobj.h"
 #include "virerror.h"
 #include "virxml.h"
 #include "viruuid.h"
index c87efe4bc9194e68f037c86cf84b1cab42c268b0..5ca4ecd4c649e30f11594ed09a84d67cf5a6d285 100644 (file)
@@ -62,4 +62,5 @@ char *virSecretDefFormat(const virSecretDef *def);
                 (VIR_CONNECT_LIST_SECRETS_FILTERS_EPHEMERAL  | \
                  VIR_CONNECT_LIST_SECRETS_FILTERS_PRIVATE)
 
+
 #endif
index eab4e30a15e459983db6df7ec82c561215816e05..e5dafa47a1ae6128fd6415f9ae6c91dc63bf244f 100644 (file)
@@ -19,6 +19,9 @@
  */
 
 #include <config.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <sys/stat.h>
 
 #include "datatypes.h"
 #include "virsecretobj.h"
@@ -27,6 +30,7 @@
 #include "virfile.h"
 #include "virhash.h"
 #include "virlog.h"
+#include "base64.h"
 
 #define VIR_FROM_THIS VIR_FROM_SECRET
 
@@ -642,3 +646,174 @@ virSecretObjListGetUUIDs(virSecretObjListPtr secrets,
     }
     return ret;
 }
+
+
+static int
+virSecretLoadValidateUUID(virSecretDefPtr def,
+                          const char *file)
+{
+    char uuidstr[VIR_UUID_STRING_BUFLEN];
+
+    virUUIDFormat(def->uuid, uuidstr);
+
+    if (!virFileMatchesNameSuffix(file, uuidstr, ".xml")) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("<uuid> does not match secret file name '%s'"),
+                       file);
+        return -1;
+    }
+
+    return 0;
+}
+
+
+static int
+virSecretLoadValue(virSecretObjPtr secret)
+{
+    int ret = -1, fd = -1;
+    struct stat st;
+    char *contents = NULL, *value = NULL;
+    size_t value_size;
+
+    if ((fd = open(secret->base64File, O_RDONLY)) == -1) {
+        if (errno == ENOENT) {
+            ret = 0;
+            goto cleanup;
+        }
+        virReportSystemError(errno, _("cannot open '%s'"),
+                             secret->base64File);
+        goto cleanup;
+    }
+
+    if (fstat(fd, &st) < 0) {
+        virReportSystemError(errno, _("cannot stat '%s'"),
+                             secret->base64File);
+        goto cleanup;
+    }
+
+    if ((size_t)st.st_size != st.st_size) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("'%s' file does not fit in memory"),
+                       secret->base64File);
+        goto cleanup;
+    }
+
+    if (VIR_ALLOC_N(contents, st.st_size) < 0)
+        goto cleanup;
+
+    if (saferead(fd, contents, st.st_size) != st.st_size) {
+        virReportSystemError(errno, _("cannot read '%s'"),
+                             secret->base64File);
+        goto cleanup;
+    }
+
+    VIR_FORCE_CLOSE(fd);
+
+    if (!base64_decode_alloc(contents, st.st_size, &value, &value_size)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("invalid base64 in '%s'"),
+                       secret->base64File);
+        goto cleanup;
+    }
+    if (value == NULL)
+        goto cleanup;
+
+    secret->value = (unsigned char *)value;
+    value = NULL;
+    secret->value_size = value_size;
+
+    ret = 0;
+
+ cleanup:
+    if (value != NULL) {
+        memset(value, 0, value_size);
+        VIR_FREE(value);
+    }
+    if (contents != NULL) {
+        memset(contents, 0, st.st_size);
+        VIR_FREE(contents);
+    }
+    VIR_FORCE_CLOSE(fd);
+    return ret;
+}
+
+
+static virSecretObjPtr
+virSecretLoad(virSecretObjListPtr secrets,
+              const char *file,
+              const char *path,
+              const char *configDir)
+{
+    virSecretDefPtr def = NULL;
+    virSecretObjPtr secret = NULL, ret = NULL;
+
+    if (!(def = virSecretDefParseFile(path)))
+        goto cleanup;
+
+    if (virSecretLoadValidateUUID(def, file) < 0)
+        goto cleanup;
+
+    if (!(secret = virSecretObjListAdd(secrets, def, configDir, NULL)))
+        goto cleanup;
+    def = NULL;
+
+    if (virSecretLoadValue(secret) < 0)
+        goto cleanup;
+
+    ret = secret;
+    secret = NULL;
+
+ cleanup:
+    if (secret)
+        virSecretObjListRemove(secrets, secret);
+    virSecretDefFree(def);
+    return ret;
+}
+
+
+int
+virSecretLoadAllConfigs(virSecretObjListPtr secrets,
+                        const char *configDir)
+{
+    DIR *dir = NULL;
+    struct dirent *de;
+
+    if (!(dir = opendir(configDir))) {
+        if (errno == ENOENT)
+            return 0;
+        virReportSystemError(errno, _("cannot open '%s'"), configDir);
+        return -1;
+    }
+
+    /* Ignore errors reported by readdir or other calls within the
+     * loop (if any).  It's better to keep the secrets we managed to find. */
+    while (virDirRead(dir, &de, NULL) > 0) {
+        char *path;
+        virSecretObjPtr secret;
+
+        if (STREQ(de->d_name, ".") || STREQ(de->d_name, ".."))
+            continue;
+
+        if (!virFileHasSuffix(de->d_name, ".xml"))
+            continue;
+
+        if (!(path = virFileBuildPath(configDir, de->d_name, NULL)))
+            continue;
+
+        if (!(secret = virSecretLoad(secrets, de->d_name, path, configDir))) {
+            virErrorPtr err = virGetLastError();
+
+            VIR_ERROR(_("Error reading secret: %s"),
+                      err != NULL ? err->message: _("unknown error"));
+            virResetError(err);
+            VIR_FREE(path);
+            continue;
+        }
+
+        VIR_FREE(path);
+        virSecretObjEndAPI(&secret);
+    }
+
+    closedir(dir);
+    return 0;
+}
index 514db2fc88b037eb42666ed58f906a7636623ce7..2e8dcf69ebca21d6bfcabb9ee3ed38a4bfc1bc28 100644 (file)
@@ -93,4 +93,6 @@ int virSecretObjListGetUUIDs(virSecretObjListPtr secrets,
                              virSecretObjListACLFilter filter,
                              virConnectPtr conn);
 
+int virSecretLoadAllConfigs(virSecretObjListPtr secrets,
+                            const char *configDir);
 #endif /* __VIRSECRETOBJ_H__ */
index 603eba532d751b56cf1a4b449bd09bf8c375d937..5a6265fe9638f66425b230330e23c2902c59f362 100644 (file)
@@ -895,6 +895,7 @@ virDomainObjListRename;
 
 
 # conf/virsecretobj.h
+virSecretLoadAllConfigs;
 virSecretObjEndAPI;
 virSecretObjListAdd;
 virSecretObjListExport;
index 90ec4bac601414f1e8fe750bd49c270b84b4539c..c8b4163f9abceb5c1ffc207be0820f0cef253605 100644 (file)
@@ -22,7 +22,6 @@
 
 #include <config.h>
 
-#include <dirent.h>
 #include <fcntl.h>
 #include <string.h>
 #include <sys/stat.h>
@@ -190,175 +189,6 @@ secretDeleteSaved(const virSecretObj *secret)
     return 0;
 }
 
-static int
-secretLoadValidateUUID(virSecretDefPtr def,
-                       const char *file)
-{
-    char uuidstr[VIR_UUID_STRING_BUFLEN];
-
-    virUUIDFormat(def->uuid, uuidstr);
-
-    if (!virFileMatchesNameSuffix(file, uuidstr, ".xml")) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("<uuid> does not match secret file name '%s'"),
-                       file);
-        return -1;
-    }
-
-    return 0;
-}
-
-static int
-secretLoadValue(virSecretObjPtr secret)
-{
-    int ret = -1, fd = -1;
-    struct stat st;
-    char *contents = NULL, *value = NULL;
-    size_t value_size;
-
-    if ((fd = open(secret->base64File, O_RDONLY)) == -1) {
-        if (errno == ENOENT) {
-            ret = 0;
-            goto cleanup;
-        }
-        virReportSystemError(errno, _("cannot open '%s'"),
-                             secret->base64File);
-        goto cleanup;
-    }
-
-    if (fstat(fd, &st) < 0) {
-        virReportSystemError(errno, _("cannot stat '%s'"),
-                             secret->base64File);
-        goto cleanup;
-    }
-
-    if ((size_t)st.st_size != st.st_size) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("'%s' file does not fit in memory"),
-                       secret->base64File);
-        goto cleanup;
-    }
-
-    if (VIR_ALLOC_N(contents, st.st_size) < 0)
-        goto cleanup;
-
-    if (saferead(fd, contents, st.st_size) != st.st_size) {
-        virReportSystemError(errno, _("cannot read '%s'"),
-                             secret->base64File);
-        goto cleanup;
-    }
-
-    VIR_FORCE_CLOSE(fd);
-
-    if (!base64_decode_alloc(contents, st.st_size, &value, &value_size)) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("invalid base64 in '%s'"),
-                       secret->base64File);
-        goto cleanup;
-    }
-    if (value == NULL)
-        goto cleanup;
-
-    secret->value = (unsigned char *)value;
-    value = NULL;
-    secret->value_size = value_size;
-
-    ret = 0;
-
- cleanup:
-    if (value != NULL) {
-        memset(value, 0, value_size);
-        VIR_FREE(value);
-    }
-    if (contents != NULL) {
-        memset(contents, 0, st.st_size);
-        VIR_FREE(contents);
-    }
-    VIR_FORCE_CLOSE(fd);
-    return ret;
-}
-
-
-static virSecretObjPtr
-secretLoad(virSecretObjListPtr secrets,
-           const char *file,
-           const char *path,
-           const char *configDir)
-{
-    virSecretDefPtr def = NULL;
-    virSecretObjPtr secret = NULL, ret = NULL;
-
-    if (!(def = virSecretDefParseFile(path)))
-        goto cleanup;
-
-    if (secretLoadValidateUUID(def, file) < 0)
-        goto cleanup;
-
-    if (!(secret = virSecretObjListAdd(secrets, def, configDir, NULL)))
-        goto cleanup;
-    def = NULL;
-
-    if (secretLoadValue(secret) < 0)
-        goto cleanup;
-
-    ret = secret;
-    secret = NULL;
-
- cleanup:
-    if (secret)
-        virSecretObjListRemove(secrets, secret);
-    virSecretDefFree(def);
-    return ret;
-}
-
-
-static int
-secretLoadAllConfigs(virSecretObjListPtr secrets,
-                     const char *configDir)
-{
-    DIR *dir = NULL;
-    struct dirent *de;
-
-    if (!(dir = opendir(configDir))) {
-        if (errno == ENOENT)
-            return 0;
-        virReportSystemError(errno, _("cannot open '%s'"), configDir);
-        return -1;
-    }
-
-    /* Ignore errors reported by readdir or other calls within the
-     * loop (if any).  It's better to keep the secrets we managed to find. */
-    while (virDirRead(dir, &de, NULL) > 0) {
-        char *path;
-        virSecretObjPtr secret;
-
-        if (STREQ(de->d_name, ".") || STREQ(de->d_name, ".."))
-            continue;
-
-        if (!virFileHasSuffix(de->d_name, ".xml"))
-            continue;
-
-        if (!(path = virFileBuildPath(configDir, de->d_name, NULL)))
-            continue;
-
-        if (!(secret = secretLoad(secrets, de->d_name, path, configDir))) {
-            virErrorPtr err = virGetLastError();
-
-            VIR_ERROR(_("Error reading secret: %s"),
-                      err != NULL ? err->message: _("unknown error"));
-            virResetError(err);
-            VIR_FREE(path);
-            continue;
-        }
-
-        VIR_FREE(path);
-        virSecretObjEndAPI(&secret);
-    }
-
-    closedir(dir);
-    return 0;
-}
-
 /* Driver functions */
 
 static int
@@ -722,7 +552,7 @@ secretStateInitialize(bool privileged,
     if (!(driver->secrets = virSecretObjListNew()))
         goto error;
 
-    if (secretLoadAllConfigs(driver->secrets, driver->configDir) < 0)
+    if (virSecretLoadAllConfigs(driver->secrets, driver->configDir) < 0)
         goto error;
 
     secretDriverUnlock();
@@ -743,7 +573,7 @@ secretStateReload(void)
 
     secretDriverLock();
 
-    ignore_value(secretLoadAllConfigs(driver->secrets, driver->configDir));
+    ignore_value(virSecretLoadAllConfigs(driver->secrets, driver->configDir));
 
     secretDriverUnlock();
     return 0;