render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
Blob Blame History Raw
From 2c711c10712280bd4dae442bc68c8e38df3ab171 Mon Sep 17 00:00:00 2001
Message-Id: <2c711c10712280bd4dae442bc68c8e38df3ab171@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Mon, 16 Mar 2020 22:12:13 +0100
Subject: [PATCH] qemu: Pass through arguments of 'ssh' block driver used by
 libguestfs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

We currently don't model the 'ssh' protocol properties properly and
since it seems impossible for now (agent path passed via environment
variable). To allow libguestfs to work as it used in pre-blockdev era we
must carry the properties over to the command line. For this instance we
just store it internally and format it back.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
(cherry picked from commit d6db013c6e507fe45ebc07fa109e608cf7451b22)
https://bugzilla.redhat.com/show_bug.cgi?id=1804750
Message-Id: <521e8b33432bfa847007866c631d6d6454f08ea3.1584391727.git.pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/qemu/qemu_block.c                              | 10 ++++++++++
 src/util/virstoragefile.c                          | 13 +++++++++++++
 src/util/virstoragefile.h                          |  5 +++++
 tests/qemublocktest.c                              |  1 +
 .../jsontojson/ssh-passthrough-libguestfs-in.json  |  1 +
 .../jsontojson/ssh-passthrough-libguestfs-out.json | 14 ++++++++++++++
 6 files changed, 44 insertions(+)
 create mode 100644 tests/qemublocktestdata/jsontojson/ssh-passthrough-libguestfs-in.json
 create mode 100644 tests/qemublocktestdata/jsontojson/ssh-passthrough-libguestfs-out.json

diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
index b077e2e02f..141059ae81 100644
--- a/src/qemu/qemu_block.c
+++ b/src/qemu/qemu_block.c
@@ -911,6 +911,7 @@ qemuBlockStorageSourceGetSshProps(virStorageSourcePtr src)
     g_autoptr(virJSONValue) serverprops = NULL;
     virJSONValuePtr ret = NULL;
     const char *username = NULL;
+    g_autoptr(virJSONValue) host_key_check = NULL;
 
     if (src->nhosts != 1) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -924,11 +925,20 @@ qemuBlockStorageSourceGetSshProps(virStorageSourcePtr src)
 
     if (src->auth)
         username = src->auth->username;
+    else if (src->ssh_user)
+        username = src->ssh_user;
+
+    if (src->ssh_host_key_check_disabled &&
+        virJSONValueObjectCreate(&host_key_check,
+                                 "s:mode", "none",
+                                 NULL) < 0)
+        return NULL;
 
     if (virJSONValueObjectCreate(&ret,
                                  "s:path", src->path,
                                  "a:server", &serverprops,
                                  "S:user", username,
+                                 "A:host-key-check", &host_key_check,
                                  NULL) < 0)
         return NULL;
 
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 9eca186e99..ce126f5cba 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -2464,6 +2464,10 @@ virStorageSourceCopy(const virStorageSource *src,
             return NULL;
     }
 
+    /* ssh config passthrough for libguestfs */
+    def->ssh_host_key_check_disabled = src->ssh_host_key_check_disabled;
+    def->ssh_user = g_strdup(src->ssh_user);
+
     return g_steal_pointer(&def);
 }
 
@@ -2705,6 +2709,8 @@ virStorageSourceClear(virStorageSourcePtr def)
     VIR_FREE(def->tlsAlias);
     VIR_FREE(def->tlsCertdir);
 
+    VIR_FREE(def->ssh_user);
+
     virStorageSourceInitiatorClear(&def->initiator);
 
     /* clear everything except the class header as the object APIs
@@ -3635,6 +3641,8 @@ virStorageSourceParseBackingJSONSSH(virStorageSourcePtr src,
     const char *path = virJSONValueObjectGetString(json, "path");
     const char *host = virJSONValueObjectGetString(json, "host");
     const char *port = virJSONValueObjectGetString(json, "port");
+    const char *user = virJSONValueObjectGetString(json, "user");
+    const char *host_key_check = virJSONValueObjectGetString(json, "host_key_check");
     virJSONValuePtr server = virJSONValueObjectGetObject(json, "server");
 
     if (!(host || server) || !path) {
@@ -3665,6 +3673,11 @@ virStorageSourceParseBackingJSONSSH(virStorageSourcePtr src,
             return -1;
     }
 
+    /* these two are parsed just to be passed back as we don't model them yet */
+    src->ssh_user = g_strdup(user);
+    if (STREQ_NULLABLE(host_key_check, "no"))
+        src->ssh_host_key_check_disabled = true;
+
     return 0;
 }
 
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
index 1abdaf89ce..c1430cadd1 100644
--- a/src/util/virstoragefile.h
+++ b/src/util/virstoragefile.h
@@ -385,6 +385,11 @@ struct _virStorageSource {
                        as a source for floppy drive */
 
     bool hostcdrom; /* backing device is a cdrom */
+
+    /* passthrough variables for the ssh driver which we don't handle properly */
+    /* these must not be used apart from formatting the output JSON in the qemu driver */
+    char *ssh_user;
+    bool ssh_host_key_check_disabled;
 };
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStorageSource, virObjectUnref);
diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c
index d8bd811b4d..f48875e16d 100644
--- a/tests/qemublocktest.c
+++ b/tests/qemublocktest.c
@@ -1132,6 +1132,7 @@ mymain(void)
     jsontojsondata.schemaroot = qmp_schemaroot_x86_64_blockdev_add;
 
     TEST_JSON_TO_JSON("curl-libguestfs");
+    TEST_JSON_TO_JSON("ssh-passthrough-libguestfs");
 
 #define TEST_IMAGE_CREATE(testname, testbacking) \
     do { \
diff --git a/tests/qemublocktestdata/jsontojson/ssh-passthrough-libguestfs-in.json b/tests/qemublocktestdata/jsontojson/ssh-passthrough-libguestfs-in.json
new file mode 100644
index 0000000000..da8fedef07
--- /dev/null
+++ b/tests/qemublocktestdata/jsontojson/ssh-passthrough-libguestfs-in.json
@@ -0,0 +1 @@
+json:{"file.driver":"ssh","file.user":"testuser","file.host":"random.host","file.port":1234,"file.path":"somewhere/something","file.host_key_check":"no"}
diff --git a/tests/qemublocktestdata/jsontojson/ssh-passthrough-libguestfs-out.json b/tests/qemublocktestdata/jsontojson/ssh-passthrough-libguestfs-out.json
new file mode 100644
index 0000000000..1f6032deb4
--- /dev/null
+++ b/tests/qemublocktestdata/jsontojson/ssh-passthrough-libguestfs-out.json
@@ -0,0 +1,14 @@
+{
+  "driver": "ssh",
+  "path": "somewhere/something",
+  "server": {
+    "host": "random.host",
+    "port": "22"
+  },
+  "user": "testuser",
+  "host-key-check": {
+    "mode": "none"
+  },
+  "auto-read-only": true,
+  "discard": "unmap"
+}
-- 
2.25.1