render / rpms / libvirt

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