render / rpms / libvirt

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