Blob Blame History Raw
From 1fdf4e4f9180b74d59882c1a70ad99e1d5165a14 Mon Sep 17 00:00:00 2001
Message-Id: <1fdf4e4f9180b74d59882c1a70ad99e1d5165a14@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Mon, 16 Mar 2020 22:12:07 +0100
Subject: [PATCH] virstoragefile: Add JSON parser for 'sslverify', 'readahead',
 'cookies' and 'timeout'
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
(cherry picked from commit 77194db01c4ab785a4668257bc9409b164f059aa)
https://bugzilla.redhat.com/show_bug.cgi?id=1804750
Message-Id: <9c5ef44994a5e60b08ad421762acefa26cde5a28.1584391727.git.pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@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 7893e054c3..931f2db6e9 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 29af0781fc..3057db6930 100644
--- a/tests/qemublocktest.c
+++ b/tests/qemublocktest.c
@@ -913,6 +913,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 e7794d6168..63b991eb71 100644
--- a/tests/virstoragetest.c
+++ b/tests/virstoragetest.c
@@ -1606,6 +1606,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'>&quot;0c8db85112873a79b7ef74f294cb70ef7f&quot;</cookie>\n"
+                           "  </cookies>\n"
+                           "  <timeout seconds='2000'/>\n"
+                           "</source>\n", 0);
+
 #endif /* WITH_YAJL */
 
  cleanup:
-- 
2.25.1