[PATCH 24/30] virstoragefile: Add JSON parser for 'sslverify', 'readahead', 'cookies' and 'timeout'

Peter Krempa pkrempa at redhat.com
Mon Mar 9 16:23:04 UTC 2020


Add support for parsing the recently added fields from backing file
pseudo-protocol strings.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/virstoragefile.c | 91 ++++++++++++++++++++++++++++++++++++++-
 tests/qemublocktest.c     |  6 +++
 tests/virstoragetest.c    | 15 +++++++
 3 files changed, 111 insertions(+), 1 deletion(-)

diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 9e740419eb..efc4c60681 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -3210,10 +3210,61 @@ virStorageSourceParseBackingJSONUriStr(virStorageSourcePtr src,
 }


+static int
+virStorageSourceParseBackingJSONUriCookies(virStorageSourcePtr src,
+                                           virJSONValuePtr json,
+                                           const char *jsonstr)
+{
+    const char *cookiestr;
+    VIR_AUTOSTRINGLIST cookies = NULL;
+    size_t ncookies = 0;
+    size_t i;
+
+    if (!virJSONValueObjectHasKey(json, "cookie"))
+        return 0;
+
+    if (!(cookiestr = virJSONValueObjectGetString(json, "cookie"))) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       _("wrong format of 'cookie' field in backing store definition '%s'"),
+                       jsonstr);
+        return -1;
+    }
+
+    if (!(cookies = virStringSplitCount(cookiestr, ";", 0, &ncookies)))
+        return -1;
+
+    src->cookies = g_new0(virStorageNetCookieDefPtr, ncookies);
+    src->ncookies = ncookies;
+
+    for (i = 0; i < ncookies; i++) {
+        char *cookiename = cookies[i];
+        char *cookievalue;
+
+        virSkipSpaces((const char **) &cookiename);
+
+        if (!(cookievalue = strchr(cookiename, '='))) {
+            virReportError(VIR_ERR_INVALID_ARG,
+                           _("malformed http cookie '%s' in backing store definition '%s'"),
+                           cookies[i], jsonstr);
+            return -1;
+        }
+
+        *cookievalue = '\0';
+        cookievalue++;
+
+        src->cookies[i] = g_new0(virStorageNetCookieDef, 1);
+        src->cookies[i]->name = g_strdup(cookiename);
+        src->cookies[i]->value = g_strdup(cookievalue);
+    }
+
+    return 0;
+}
+
+
 static int
 virStorageSourceParseBackingJSONUri(virStorageSourcePtr src,
                                     virJSONValuePtr json,
-                                    const char *jsonstr G_GNUC_UNUSED,
+                                    const char *jsonstr,
                                     int protocol)
 {
     const char *uri;
@@ -3224,6 +3275,44 @@ virStorageSourceParseBackingJSONUri(virStorageSourcePtr src,
         return -1;
     }

+    if (protocol == VIR_STORAGE_NET_PROTOCOL_HTTPS ||
+        protocol == VIR_STORAGE_NET_PROTOCOL_FTPS) {
+        if (virJSONValueObjectHasKey(json, "sslverify")) {
+            bool tmp;
+
+            if (virJSONValueObjectGetBoolean(json, "sslverify", &tmp) < 0) {
+                virReportError(VIR_ERR_INVALID_ARG,
+                               _("malformed 'sslverify' field in backing store definition '%s'"),
+                               jsonstr);
+                return -1;
+            }
+
+            src->sslverify = virTristateBoolFromBool(tmp);
+        }
+    }
+
+    if (protocol == VIR_STORAGE_NET_PROTOCOL_HTTPS ||
+        protocol == VIR_STORAGE_NET_PROTOCOL_HTTP) {
+        if (virStorageSourceParseBackingJSONUriCookies(src, json, jsonstr) < 0)
+            return -1;
+    }
+
+    if (virJSONValueObjectHasKey(json, "readahead") &&
+        virJSONValueObjectGetNumberUlong(json, "readahead", &src->readahead) < 0) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       _("malformed 'readahead' field in backing store definition '%s'"),
+                       jsonstr);
+        return -1;
+    }
+
+    if (virJSONValueObjectHasKey(json, "timeout") &&
+        virJSONValueObjectGetNumberUlong(json, "timeout", &src->timeout) < 0) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       _("malformed 'timeout' field in backing store definition '%s'"),
+                       jsonstr);
+        return -1;
+    }
+
     return virStorageSourceParseBackingJSONUriStr(src, uri, protocol);
 }

diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c
index 7b7948d4c6..96a3c7fc41 100644
--- a/tests/qemublocktest.c
+++ b/tests/qemublocktest.c
@@ -917,6 +917,12 @@ mymain(void)
     TEST_JSON_FORMAT_NET("<source protocol='https' name='file'>\n"
                          "  <host name='example.com' port='432'/>\n"
                          "</source>\n");
+    TEST_JSON_FORMAT_NET("<source protocol='https' name='file'>\n"
+                         "  <host name='example.com' port='432'/>\n"
+                         "  <ssl verify='no'/>\n"
+                         "  <readahead size='1024'/>\n"
+                         "  <timeout seconds='1337'/>\n"
+                         "</source>\n");
     TEST_JSON_FORMAT_NET("<source protocol='gluster' name='vol/file'>\n"
                          "  <host name='example.com' port='24007'/>\n"
                          "</source>\n");
diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c
index 97c22d42af..b49dfd2598 100644
--- a/tests/virstoragetest.c
+++ b/tests/virstoragetest.c
@@ -1607,6 +1607,21 @@ mymain(void)
                             "  </slices>\n"
                             "</source>\n", 0);

+    TEST_BACKING_PARSE_FULL("json:{ \"file.cookie\": \"vmware_soap_session=\\\"0c8db85112873a79b7ef74f294cb70ef7f\\\"\","
+                                   "\"file.sslverify\": false,"
+                                   "\"file.driver\": \"https\","
+                                   "\"file.url\": \"https://host/folder/esx6.5-rhel7.7-x86%5f64/esx6.5-rhel7.7-x86%5f64-flat.vmdk?dcPath=data&dsName=esx6.5-matrix\","
+                                   "\"file.timeout\": 2000"
+                                 "}",
+                           "<source protocol='https' name='folder/esx6.5-rhel7.7-x86_64/esx6.5-rhel7.7-x86_64-flat.vmdk'>\n"
+                           "  <host name='host' port='443'/>\n"
+                           "  <ssl verify='no'/>\n"
+                           "  <cookies>\n"
+                           "    <cookie name='vmware_soap_session'>"0c8db85112873a79b7ef74f294cb70ef7f"</cookie>\n"
+                           "  </cookies>\n"
+                           "  <timeout seconds='2000'/>\n"
+                           "</source>\n", 0);
+
 #endif /* WITH_YAJL */

  cleanup:
-- 
2.24.1




More information about the libvir-list mailing list