Blame SOURCES/kvm-tests-exercise-NBD-server-in-TLS-mode.patch

7711c0
From 74a1f98290aa6ec986bea9bcd87c4fb8b93afd4d Mon Sep 17 00:00:00 2001
7711c0
From: John Snow <jsnow@redhat.com>
7711c0
Date: Fri, 22 Mar 2019 03:22:35 +0100
7711c0
Subject: [PATCH 068/163] tests: exercise NBD server in TLS mode
7711c0
MIME-Version: 1.0
7711c0
Content-Type: text/plain; charset=UTF-8
7711c0
Content-Transfer-Encoding: 8bit
7711c0
7711c0
RH-Author: John Snow <jsnow@redhat.com>
7711c0
Message-id: <20190322032241.8111-23-jsnow@redhat.com>
7711c0
Patchwork-id: 85104
7711c0
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 22/28] tests: exercise NBD server in TLS mode
7711c0
Bugzilla: 1691563
7711c0
RH-Acked-by: Max Reitz <mreitz@redhat.com>
7711c0
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
7711c0
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
7711c0
From: Daniel P. Berrangé <berrange@redhat.com>
7711c0
7711c0
Add tests that validate it is possible to connect to an NBD server
7711c0
running TLS mode. Also test mis-matched TLS vs non-TLS connections
7711c0
correctly fail.
7711c0
7711c0
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
7711c0
Message-Id: <20181116155325.22428-7-berrange@redhat.com>
7711c0
Reviewed-by: Eric Blake <eblake@redhat.com>
7711c0
Tested-by: Eric Blake <eblake@redhat.com>
7711c0
[eblake: rebase to iotests shell cleanups, use ss instead of socat for
7711c0
port probing, sanitize port number in expected output]
7711c0
Signed-off-by: Eric Blake <eblake@redhat.com>
7711c0
(cherry picked from commit afcd1c2f2d438930a17eb87293c0ac2c377158fa)
7711c0
Signed-off-by: John Snow <jsnow@redhat.com>
7711c0
7711c0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
---
7711c0
 tests/qemu-iotests/233        | 102 ++++++++++++++++++++++++++++++++++++++++++
7711c0
 tests/qemu-iotests/233.out    |  30 +++++++++++++
7711c0
 tests/qemu-iotests/common.nbd |  45 +++++++++++++++++++
7711c0
 tests/qemu-iotests/group      |   1 +
7711c0
 4 files changed, 178 insertions(+)
7711c0
 create mode 100755 tests/qemu-iotests/233
7711c0
 create mode 100644 tests/qemu-iotests/233.out
7711c0
7711c0
diff --git a/tests/qemu-iotests/233 b/tests/qemu-iotests/233
7711c0
new file mode 100755
7711c0
index 0000000..46013ce
7711c0
--- /dev/null
7711c0
+++ b/tests/qemu-iotests/233
7711c0
@@ -0,0 +1,102 @@
7711c0
+#!/bin/bash
7711c0
+#
7711c0
+# Test NBD TLS certificate / authorization integration
7711c0
+#
7711c0
+# Copyright (C) 2018 Red Hat, Inc.
7711c0
+#
7711c0
+# This program is free software; you can redistribute it and/or modify
7711c0
+# it under the terms of the GNU General Public License as published by
7711c0
+# the Free Software Foundation; either version 2 of the License, or
7711c0
+# (at your option) any later version.
7711c0
+#
7711c0
+# This program is distributed in the hope that it will be useful,
7711c0
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
7711c0
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7711c0
+# GNU General Public License for more details.
7711c0
+#
7711c0
+# You should have received a copy of the GNU General Public License
7711c0
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
7711c0
+#
7711c0
+
7711c0
+# creator
7711c0
+owner=berrange@redhat.com
7711c0
+
7711c0
+seq=$(basename $0)
7711c0
+echo "QA output created by $seq"
7711c0
+
7711c0
+status=1	# failure is the default!
7711c0
+
7711c0
+_cleanup()
7711c0
+{
7711c0
+    nbd_server_stop
7711c0
+    _cleanup_test_img
7711c0
+    tls_x509_cleanup
7711c0
+}
7711c0
+trap "_cleanup; exit \$status" 0 1 2 3 15
7711c0
+
7711c0
+# get standard environment, filters and checks
7711c0
+. ./common.rc
7711c0
+. ./common.filter
7711c0
+. ./common.pattern
7711c0
+. ./common.tls
7711c0
+. ./common.nbd
7711c0
+
7711c0
+_supported_fmt raw qcow2
7711c0
+_supported_proto file
7711c0
+# If porting to non-Linux, consider using socat instead of ss in common.nbd
7711c0
+_supported_os Linux
7711c0
+_require_command QEMU_NBD
7711c0
+
7711c0
+nbd_server_set_tcp_port
7711c0
+tls_x509_init
7711c0
+
7711c0
+echo
7711c0
+echo "== preparing TLS creds =="
7711c0
+
7711c0
+tls_x509_create_root_ca "ca1"
7711c0
+tls_x509_create_root_ca "ca2"
7711c0
+tls_x509_create_server "ca1" "server1"
7711c0
+tls_x509_create_client "ca1" "client1"
7711c0
+tls_x509_create_client "ca2" "client2"
7711c0
+
7711c0
+echo
7711c0
+echo "== preparing image =="
7711c0
+_make_test_img 64M
7711c0
+
7711c0
+
7711c0
+echo
7711c0
+echo "== check TLS client to plain server fails =="
7711c0
+nbd_server_start_tcp_socket "$TEST_IMG"
7711c0
+
7711c0
+$QEMU_IMG info --image-opts \
7711c0
+    --object tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0 \
7711c0
+    driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \
7711c0
+    2>&1 | sed "s/$nbd_tcp_port/PORT/g"
7711c0
+
7711c0
+nbd_server_stop
7711c0
+
7711c0
+echo
7711c0
+echo "== check plain client to TLS server fails =="
7711c0
+
7711c0
+nbd_server_start_tcp_socket --object tls-creds-x509,dir=${tls_dir}/server1,endpoint=server,id=tls0,verify-peer=yes --tls-creds tls0 "$TEST_IMG"
7711c0
+
7711c0
+$QEMU_IMG info nbd://localhost:$nbd_tcp_port 2>&1 | sed "s/$nbd_tcp_port/PORT/g"
7711c0
+
7711c0
+echo
7711c0
+echo "== check TLS works =="
7711c0
+$QEMU_IMG info --image-opts \
7711c0
+    --object tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0 \
7711c0
+    driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \
7711c0
+    2>&1 | sed "s/$nbd_tcp_port/PORT/g"
7711c0
+
7711c0
+echo
7711c0
+echo "== check TLS with different CA fails =="
7711c0
+$QEMU_IMG info --image-opts \
7711c0
+    --object tls-creds-x509,dir=${tls_dir}/client2,endpoint=client,id=tls0 \
7711c0
+    driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \
7711c0
+    2>&1 | sed "s/$nbd_tcp_port/PORT/g"
7711c0
+
7711c0
+# success, all done
7711c0
+echo "*** done"
7711c0
+rm -f $seq.full
7711c0
+status=0
7711c0
diff --git a/tests/qemu-iotests/233.out b/tests/qemu-iotests/233.out
7711c0
new file mode 100644
7711c0
index 0000000..616e923
7711c0
--- /dev/null
7711c0
+++ b/tests/qemu-iotests/233.out
7711c0
@@ -0,0 +1,30 @@
7711c0
+QA output created by 233
7711c0
+
7711c0
+== preparing TLS creds ==
7711c0
+Generating a self signed certificate...
7711c0
+Generating a self signed certificate...
7711c0
+Generating a signed certificate...
7711c0
+Generating a signed certificate...
7711c0
+Generating a signed certificate...
7711c0
+
7711c0
+== preparing image ==
7711c0
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
7711c0
+
7711c0
+== check TLS client to plain server fails ==
7711c0
+qemu-img: Could not open 'driver=nbd,host=127.0.0.1,port=PORT,tls-creds=tls0': Denied by server for option 5 (starttls)
7711c0
+server reported: TLS not configured
7711c0
+
7711c0
+== check plain client to TLS server fails ==
7711c0
+qemu-img: Could not open 'nbd://localhost:PORT': TLS negotiation required before option 8 (structured reply)
7711c0
+server reported: Option 0x8 not permitted before TLS
7711c0
+
7711c0
+== check TLS works ==
7711c0
+image: nbd://127.0.0.1:PORT
7711c0
+file format: nbd
7711c0
+virtual size: 64M (67108864 bytes)
7711c0
+disk size: unavailable
7711c0
+
7711c0
+== check TLS with different CA fails ==
7711c0
+option negotiation failed: Verify failed: No certificate was found.
7711c0
+qemu-img: Could not open 'driver=nbd,host=127.0.0.1,port=PORT,tls-creds=tls0': The certificate hasn't got a known issuer
7711c0
+*** done
7711c0
diff --git a/tests/qemu-iotests/common.nbd b/tests/qemu-iotests/common.nbd
7711c0
index 9f841ab..0f4497a 100644
7711c0
--- a/tests/qemu-iotests/common.nbd
7711c0
+++ b/tests/qemu-iotests/common.nbd
7711c0
@@ -20,6 +20,7 @@
7711c0
 #
7711c0
 
7711c0
 nbd_unix_socket="${TEST_DIR}/qemu-nbd.sock"
7711c0
+nbd_tcp_addr="127.0.0.1"
7711c0
 nbd_pid_file="${TEST_DIR}/qemu-nbd.pid"
7711c0
 
7711c0
 function nbd_server_stop()
7711c0
@@ -62,3 +63,47 @@ function nbd_server_start_unix_socket()
7711c0
     $QEMU_NBD -v -t -k "$nbd_unix_socket" "$@" &
7711c0
     nbd_server_wait_for_unix_socket $!
7711c0
 }
7711c0
+
7711c0
+function nbd_server_set_tcp_port()
7711c0
+{
7711c0
+    (ss --help) >/dev/null 2>&1 || _notrun "ss utility not found, skipping test"
7711c0
+
7711c0
+    for ((port = 10809; port <= 10909; port++))
7711c0
+    do
7711c0
+        if ! ss -tln | grep -sqE ":$port\b"; then
7711c0
+            nbd_tcp_port=$port
7711c0
+            return
7711c0
+        fi
7711c0
+    done
7711c0
+
7711c0
+    echo "Cannot find free TCP port for nbd in range 10809-10909"
7711c0
+    exit 1
7711c0
+}
7711c0
+
7711c0
+function nbd_server_wait_for_tcp_socket()
7711c0
+{
7711c0
+    pid=$1
7711c0
+
7711c0
+    for ((i = 0; i < 300; i++))
7711c0
+    do
7711c0
+        if ss -tln | grep -sqE ":$nbd_tcp_port\b"; then
7711c0
+            return
7711c0
+        fi
7711c0
+        kill -s 0 $pid 2>/dev/null
7711c0
+        if test $? != 0
7711c0
+        then
7711c0
+            echo "qemu-nbd unexpectedly quit"
7711c0
+            exit 1
7711c0
+        fi
7711c0
+        sleep 0.1
7711c0
+    done
7711c0
+    echo "Failed in check of TCP socket created by qemu-nbd"
7711c0
+    exit 1
7711c0
+}
7711c0
+
7711c0
+function nbd_server_start_tcp_socket()
7711c0
+{
7711c0
+    nbd_server_stop
7711c0
+    $QEMU_NBD -v -t -b $nbd_tcp_addr -p $nbd_tcp_port "$@" &
7711c0
+    nbd_server_wait_for_tcp_socket $!
7711c0
+}
7711c0
diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group
7711c0
index bee2855..b3aeb6b 100644
7711c0
--- a/tests/qemu-iotests/group
7711c0
+++ b/tests/qemu-iotests/group
7711c0
@@ -226,6 +226,7 @@
7711c0
 229 auto quick
7711c0
 231 auto quick
7711c0
 232 auto quick
7711c0
+233 auto quick
7711c0
 234 auto quick migration
7711c0
 236 auto quick
7711c0
 242 rw auto quick
7711c0
-- 
7711c0
1.8.3.1
7711c0