render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
d76c62
From 1fdf4e4f9180b74d59882c1a70ad99e1d5165a14 Mon Sep 17 00:00:00 2001
d76c62
Message-Id: <1fdf4e4f9180b74d59882c1a70ad99e1d5165a14@dist-git>
d76c62
From: Peter Krempa <pkrempa@redhat.com>
d76c62
Date: Mon, 16 Mar 2020 22:12:07 +0100
d76c62
Subject: [PATCH] virstoragefile: Add JSON parser for 'sslverify', 'readahead',
d76c62
 'cookies' and 'timeout'
d76c62
MIME-Version: 1.0
d76c62
Content-Type: text/plain; charset=UTF-8
d76c62
Content-Transfer-Encoding: 8bit
d76c62
d76c62
Add support for parsing the recently added fields from backing file
d76c62
pseudo-protocol strings.
d76c62
d76c62
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
d76c62
Reviewed-by: Ján Tomko <jtomko@redhat.com>
d76c62
(cherry picked from commit 77194db01c4ab785a4668257bc9409b164f059aa)
d76c62
https://bugzilla.redhat.com/show_bug.cgi?id=1804750
d76c62
Message-Id: <9c5ef44994a5e60b08ad421762acefa26cde5a28.1584391727.git.pkrempa@redhat.com>
d76c62
Reviewed-by: Ján Tomko <jtomko@redhat.com>
d76c62
---
d76c62
 src/util/virstoragefile.c | 91 ++++++++++++++++++++++++++++++++++++++-
d76c62
 tests/qemublocktest.c     |  6 +++
d76c62
 tests/virstoragetest.c    | 15 +++++++
d76c62
 3 files changed, 111 insertions(+), 1 deletion(-)
d76c62
d76c62
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
d76c62
index 7893e054c3..931f2db6e9 100644
d76c62
--- a/src/util/virstoragefile.c
d76c62
+++ b/src/util/virstoragefile.c
d76c62
@@ -3210,10 +3210,61 @@ virStorageSourceParseBackingJSONUriStr(virStorageSourcePtr src,
d76c62
 }
d76c62
 
d76c62
 
d76c62
+static int
d76c62
+virStorageSourceParseBackingJSONUriCookies(virStorageSourcePtr src,
d76c62
+                                           virJSONValuePtr json,
d76c62
+                                           const char *jsonstr)
d76c62
+{
d76c62
+    const char *cookiestr;
d76c62
+    VIR_AUTOSTRINGLIST cookies = NULL;
d76c62
+    size_t ncookies = 0;
d76c62
+    size_t i;
d76c62
+
d76c62
+    if (!virJSONValueObjectHasKey(json, "cookie"))
d76c62
+        return 0;
d76c62
+
d76c62
+    if (!(cookiestr = virJSONValueObjectGetString(json, "cookie"))) {
d76c62
+        virReportError(VIR_ERR_INVALID_ARG,
d76c62
+                       _("wrong format of 'cookie' field in backing store definition '%s'"),
d76c62
+                       jsonstr);
d76c62
+        return -1;
d76c62
+    }
d76c62
+
d76c62
+    if (!(cookies = virStringSplitCount(cookiestr, ";", 0, &ncookies)))
d76c62
+        return -1;
d76c62
+
d76c62
+    src->cookies = g_new0(virStorageNetCookieDefPtr, ncookies);
d76c62
+    src->ncookies = ncookies;
d76c62
+
d76c62
+    for (i = 0; i < ncookies; i++) {
d76c62
+        char *cookiename = cookies[i];
d76c62
+        char *cookievalue;
d76c62
+
d76c62
+        virSkipSpaces((const char **) &cookiename);
d76c62
+
d76c62
+        if (!(cookievalue = strchr(cookiename, '='))) {
d76c62
+            virReportError(VIR_ERR_INVALID_ARG,
d76c62
+                           _("malformed http cookie '%s' in backing store definition '%s'"),
d76c62
+                           cookies[i], jsonstr);
d76c62
+            return -1;
d76c62
+        }
d76c62
+
d76c62
+        *cookievalue = '\0';
d76c62
+        cookievalue++;
d76c62
+
d76c62
+        src->cookies[i] = g_new0(virStorageNetCookieDef, 1);
d76c62
+        src->cookies[i]->name = g_strdup(cookiename);
d76c62
+        src->cookies[i]->value = g_strdup(cookievalue);
d76c62
+    }
d76c62
+
d76c62
+    return 0;
d76c62
+}
d76c62
+
d76c62
+
d76c62
 static int
d76c62
 virStorageSourceParseBackingJSONUri(virStorageSourcePtr src,
d76c62
                                     virJSONValuePtr json,
d76c62
-                                    const char *jsonstr G_GNUC_UNUSED,
d76c62
+                                    const char *jsonstr,
d76c62
                                     int protocol)
d76c62
 {
d76c62
     const char *uri;
d76c62
@@ -3224,6 +3275,44 @@ virStorageSourceParseBackingJSONUri(virStorageSourcePtr src,
d76c62
         return -1;
d76c62
     }
d76c62
 
d76c62
+    if (protocol == VIR_STORAGE_NET_PROTOCOL_HTTPS ||
d76c62
+        protocol == VIR_STORAGE_NET_PROTOCOL_FTPS) {
d76c62
+        if (virJSONValueObjectHasKey(json, "sslverify")) {
d76c62
+            bool tmp;
d76c62
+
d76c62
+            if (virJSONValueObjectGetBoolean(json, "sslverify", &tmp) < 0) {
d76c62
+                virReportError(VIR_ERR_INVALID_ARG,
d76c62
+                               _("malformed 'sslverify' field in backing store definition '%s'"),
d76c62
+                               jsonstr);
d76c62
+                return -1;
d76c62
+            }
d76c62
+
d76c62
+            src->sslverify = virTristateBoolFromBool(tmp);
d76c62
+        }
d76c62
+    }
d76c62
+
d76c62
+    if (protocol == VIR_STORAGE_NET_PROTOCOL_HTTPS ||
d76c62
+        protocol == VIR_STORAGE_NET_PROTOCOL_HTTP) {
d76c62
+        if (virStorageSourceParseBackingJSONUriCookies(src, json, jsonstr) < 0)
d76c62
+            return -1;
d76c62
+    }
d76c62
+
d76c62
+    if (virJSONValueObjectHasKey(json, "readahead") &&
d76c62
+        virJSONValueObjectGetNumberUlong(json, "readahead", &src->readahead) < 0) {
d76c62
+        virReportError(VIR_ERR_INVALID_ARG,
d76c62
+                       _("malformed 'readahead' field in backing store definition '%s'"),
d76c62
+                       jsonstr);
d76c62
+        return -1;
d76c62
+    }
d76c62
+
d76c62
+    if (virJSONValueObjectHasKey(json, "timeout") &&
d76c62
+        virJSONValueObjectGetNumberUlong(json, "timeout", &src->timeout) < 0) {
d76c62
+        virReportError(VIR_ERR_INVALID_ARG,
d76c62
+                       _("malformed 'timeout' field in backing store definition '%s'"),
d76c62
+                       jsonstr);
d76c62
+        return -1;
d76c62
+    }
d76c62
+
d76c62
     return virStorageSourceParseBackingJSONUriStr(src, uri, protocol);
d76c62
 }
d76c62
 
d76c62
diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c
d76c62
index 29af0781fc..3057db6930 100644
d76c62
--- a/tests/qemublocktest.c
d76c62
+++ b/tests/qemublocktest.c
d76c62
@@ -913,6 +913,12 @@ mymain(void)
d76c62
     TEST_JSON_FORMAT_NET("<source protocol='https' name='file'>\n"
d76c62
                          "  <host name='example.com' port='432'/>\n"
d76c62
                          "</source>\n");
d76c62
+    TEST_JSON_FORMAT_NET("<source protocol='https' name='file'>\n"
d76c62
+                         "  <host name='example.com' port='432'/>\n"
d76c62
+                         "  <ssl verify='no'/>\n"
d76c62
+                         "  <readahead size='1024'/>\n"
d76c62
+                         "  <timeout seconds='1337'/>\n"
d76c62
+                         "</source>\n");
d76c62
     TEST_JSON_FORMAT_NET("<source protocol='gluster' name='vol/file'>\n"
d76c62
                          "  <host name='example.com' port='24007'/>\n"
d76c62
                          "</source>\n");
d76c62
diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c
d76c62
index e7794d6168..63b991eb71 100644
d76c62
--- a/tests/virstoragetest.c
d76c62
+++ b/tests/virstoragetest.c
d76c62
@@ -1606,6 +1606,21 @@ mymain(void)
d76c62
                             "  </slices>\n"
d76c62
                             "</source>\n", 0);
d76c62
 
d76c62
+    TEST_BACKING_PARSE_FULL("json:{ \"file.cookie\": \"vmware_soap_session=\\\"0c8db85112873a79b7ef74f294cb70ef7f\\\"\","
d76c62
+                                   "\"file.sslverify\": false,"
d76c62
+                                   "\"file.driver\": \"https\","
d76c62
+                                   "\"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\","
d76c62
+                                   "\"file.timeout\": 2000"
d76c62
+                                 "}",
d76c62
+                           "<source protocol='https' name='folder/esx6.5-rhel7.7-x86_64/esx6.5-rhel7.7-x86_64-flat.vmdk'>\n"
d76c62
+                           "  <host name='host' port='443'/>\n"
d76c62
+                           "  <ssl verify='no'/>\n"
d76c62
+                           "  <cookies>\n"
d76c62
+                           "    <cookie name='vmware_soap_session'>"0c8db85112873a79b7ef74f294cb70ef7f"</cookie>\n"
d76c62
+                           "  </cookies>\n"
d76c62
+                           "  <timeout seconds='2000'/>\n"
d76c62
+                           "</source>\n", 0);
d76c62
+
d76c62
 #endif /* WITH_YAJL */
d76c62
 
d76c62
  cleanup:
d76c62
-- 
d76c62
2.25.1
d76c62