111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 29dd003a9SVladimir Sementsov-Ogievskiy# group: quick 3afcd1c2fSDaniel P. Berrangé# 4afcd1c2fSDaniel P. Berrangé# Test NBD TLS certificate / authorization integration 5afcd1c2fSDaniel P. Berrangé# 6d0898051SEric Blake# Copyright (C) 2018-2019 Red Hat, Inc. 7afcd1c2fSDaniel P. Berrangé# 8afcd1c2fSDaniel P. Berrangé# This program is free software; you can redistribute it and/or modify 9afcd1c2fSDaniel P. Berrangé# it under the terms of the GNU General Public License as published by 10afcd1c2fSDaniel P. Berrangé# the Free Software Foundation; either version 2 of the License, or 11afcd1c2fSDaniel P. Berrangé# (at your option) any later version. 12afcd1c2fSDaniel P. Berrangé# 13afcd1c2fSDaniel P. Berrangé# This program is distributed in the hope that it will be useful, 14afcd1c2fSDaniel P. Berrangé# but WITHOUT ANY WARRANTY; without even the implied warranty of 15afcd1c2fSDaniel P. Berrangé# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16afcd1c2fSDaniel P. Berrangé# GNU General Public License for more details. 17afcd1c2fSDaniel P. Berrangé# 18afcd1c2fSDaniel P. Berrangé# You should have received a copy of the GNU General Public License 19afcd1c2fSDaniel P. Berrangé# along with this program. If not, see <http://www.gnu.org/licenses/>. 20afcd1c2fSDaniel P. Berrangé# 21afcd1c2fSDaniel P. Berrangé 22afcd1c2fSDaniel P. Berrangé# creator 23afcd1c2fSDaniel P. Berrangéowner=berrange@redhat.com 24afcd1c2fSDaniel P. Berrangé 25afcd1c2fSDaniel P. Berrangéseq=$(basename $0) 26afcd1c2fSDaniel P. Berrangéecho "QA output created by $seq" 27afcd1c2fSDaniel P. Berrangé 28afcd1c2fSDaniel P. Berrangéstatus=1 # failure is the default! 29afcd1c2fSDaniel P. Berrangé 30afcd1c2fSDaniel P. Berrangé_cleanup() 31afcd1c2fSDaniel P. Berrangé{ 32afcd1c2fSDaniel P. Berrangé nbd_server_stop 33afcd1c2fSDaniel P. Berrangé _cleanup_test_img 3484f8b840SDaniel P. Berrangé # If we aborted early we want to see this log for diagnosis 3584f8b840SDaniel P. Berrangé test -f "$TEST_DIR/server.log" && cat "$TEST_DIR/server.log" 36d0898051SEric Blake rm -f "$TEST_DIR/server.log" 37afcd1c2fSDaniel P. Berrangé tls_x509_cleanup 38afcd1c2fSDaniel P. Berrangé} 39afcd1c2fSDaniel P. Berrangétrap "_cleanup; exit \$status" 0 1 2 3 15 40afcd1c2fSDaniel P. Berrangé 41afcd1c2fSDaniel P. Berrangé# get standard environment, filters and checks 42afcd1c2fSDaniel P. Berrangé. ./common.rc 43afcd1c2fSDaniel P. Berrangé. ./common.filter 44afcd1c2fSDaniel P. Berrangé. ./common.pattern 45afcd1c2fSDaniel P. Berrangé. ./common.tls 46afcd1c2fSDaniel P. Berrangé. ./common.nbd 47afcd1c2fSDaniel P. Berrangé 48afcd1c2fSDaniel P. Berrangé_supported_fmt raw qcow2 49afcd1c2fSDaniel P. Berrangé_supported_proto file 50afcd1c2fSDaniel P. Berrangé# If porting to non-Linux, consider using socat instead of ss in common.nbd 51afcd1c2fSDaniel P. Berrangé_require_command QEMU_NBD 52afcd1c2fSDaniel P. Berrangé 53afcd1c2fSDaniel P. Berrangétls_x509_init 54afcd1c2fSDaniel P. Berrangé 55afcd1c2fSDaniel P. Berrangéecho 56afcd1c2fSDaniel P. Berrangéecho "== preparing TLS creds ==" 57afcd1c2fSDaniel P. Berrangé 58afcd1c2fSDaniel P. Berrangétls_x509_create_root_ca "ca1" 59afcd1c2fSDaniel P. Berrangétls_x509_create_root_ca "ca2" 60afcd1c2fSDaniel P. Berrangétls_x509_create_server "ca1" "server1" 61afcd1c2fSDaniel P. Berrangétls_x509_create_client "ca1" "client1" 62afcd1c2fSDaniel P. Berrangétls_x509_create_client "ca2" "client2" 63b25e12daSDaniel P. Berrangetls_x509_create_client "ca1" "client3" 64*10cc95c3SDaniel P. Berrangétls_psk_create_creds "psk1" 65*10cc95c3SDaniel P. Berrangétls_psk_create_creds "psk2" 66afcd1c2fSDaniel P. Berrangé 67afcd1c2fSDaniel P. Berrangéecho 68afcd1c2fSDaniel P. Berrangéecho "== preparing image ==" 69afcd1c2fSDaniel P. Berrangé_make_test_img 64M 70ebc0141bSDaniel P. Berrangé$QEMU_IO -c 'w -P 0x11 1m 1m' "$TEST_IMG" 2>&1 | _filter_qemu_io 71afcd1c2fSDaniel P. Berrangé 72afcd1c2fSDaniel P. Berrangéecho 73afcd1c2fSDaniel P. Berrangéecho "== check TLS client to plain server fails ==" 74d0898051SEric Blakenbd_server_start_tcp_socket -f $IMGFMT "$TEST_IMG" 2> "$TEST_DIR/server.log" 75afcd1c2fSDaniel P. Berrangé 76ddd09448SEric Blakeobj=tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0 77ddd09448SEric Blake$QEMU_IMG info --image-opts --object $obj \ 78afcd1c2fSDaniel P. Berrangé driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \ 79ebc0141bSDaniel P. Berrangé 2>&1 | _filter_nbd 80ddd09448SEric Blake$QEMU_NBD_PROG -L -b $nbd_tcp_addr -p $nbd_tcp_port --object $obj \ 81ebc0141bSDaniel P. Berrangé --tls-creds=tls0 2>&1 | _filter_qemu_nbd_exports 82afcd1c2fSDaniel P. Berrangé 83afcd1c2fSDaniel P. Berrangénbd_server_stop 84afcd1c2fSDaniel P. Berrangé 85afcd1c2fSDaniel P. Berrangéecho 86afcd1c2fSDaniel P. Berrangéecho "== check plain client to TLS server fails ==" 87afcd1c2fSDaniel P. Berrangé 88e4c8f292SDaniel P. Berrangénbd_server_start_tcp_socket \ 894d7beeabSDaniel P. Berrangé --object tls-creds-x509,dir=${tls_dir}/server1,endpoint=server,id=tls0,verify-peer=on \ 90e4c8f292SDaniel P. Berrangé --tls-creds tls0 \ 91d0898051SEric Blake -f $IMGFMT "$TEST_IMG" 2>> "$TEST_DIR/server.log" 92afcd1c2fSDaniel P. Berrangé 93ebc0141bSDaniel P. Berrangé$QEMU_IMG info nbd://localhost:$nbd_tcp_port \ 94ebc0141bSDaniel P. Berrangé 2>&1 | _filter_nbd 95ebc0141bSDaniel P. Berrangé$QEMU_NBD_PROG -L -b $nbd_tcp_addr -p $nbd_tcp_port \ 96ebc0141bSDaniel P. Berrangé 2>&1 | _filter_qemu_nbd_exports 97afcd1c2fSDaniel P. Berrangé 98afcd1c2fSDaniel P. Berrangéecho 99afcd1c2fSDaniel P. Berrangéecho "== check TLS works ==" 100b25e12daSDaniel P. Berrangeobj1=tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0 101b25e12daSDaniel P. Berrangeobj2=tls-creds-x509,dir=${tls_dir}/client3,endpoint=client,id=tls0 102b25e12daSDaniel P. Berrange$QEMU_IMG info --image-opts --object $obj1 \ 103afcd1c2fSDaniel P. Berrangé driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \ 104ebc0141bSDaniel P. Berrangé 2>&1 | _filter_nbd 105b25e12daSDaniel P. Berrange$QEMU_IMG info --image-opts --object $obj2 \ 106b25e12daSDaniel P. Berrange driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \ 107ebc0141bSDaniel P. Berrangé 2>&1 | _filter_nbd 108b25e12daSDaniel P. Berrange$QEMU_NBD_PROG -L -b $nbd_tcp_addr -p $nbd_tcp_port --object $obj1 \ 109ebc0141bSDaniel P. Berrangé --tls-creds=tls0 2>&1 | _filter_qemu_nbd_exports 110afcd1c2fSDaniel P. Berrangé 111afcd1c2fSDaniel P. Berrangéecho 1123da93d4bSDaniel P. Berrangéecho "== check TLS fail over TCP with mismatched hostname ==" 1133da93d4bSDaniel P. Berrangéobj1=tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0 1143da93d4bSDaniel P. Berrangé$QEMU_IMG info --image-opts --object $obj1 \ 1153da93d4bSDaniel P. Berrangé driver=nbd,host=localhost,port=$nbd_tcp_port,tls-creds=tls0 \ 1163da93d4bSDaniel P. Berrangé 2>&1 | _filter_nbd 1173da93d4bSDaniel P. Berrangé$QEMU_NBD_PROG -L -b localhost -p $nbd_tcp_port --object $obj1 \ 1183da93d4bSDaniel P. Berrangé --tls-creds=tls0 | _filter_qemu_nbd_exports 1193da93d4bSDaniel P. Berrangé 1203da93d4bSDaniel P. Berrangéecho 1213da93d4bSDaniel P. Berrangéecho "== check TLS works over TCP with mismatched hostname and override ==" 1223da93d4bSDaniel P. Berrangéobj1=tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0 1233da93d4bSDaniel P. Berrangé$QEMU_IMG info --image-opts --object $obj1 \ 1243da93d4bSDaniel P. Berrangé driver=nbd,host=localhost,port=$nbd_tcp_port,tls-creds=tls0,tls-hostname=127.0.0.1 \ 1253da93d4bSDaniel P. Berrangé 2>&1 | _filter_nbd 1263da93d4bSDaniel P. Berrangé$QEMU_NBD_PROG -L -b localhost -p $nbd_tcp_port --object $obj1 \ 1273da93d4bSDaniel P. Berrangé --tls-creds=tls0 --tls-hostname=127.0.0.1 | _filter_qemu_nbd_exports 1283da93d4bSDaniel P. Berrangé 1293da93d4bSDaniel P. Berrangéecho 130afcd1c2fSDaniel P. Berrangéecho "== check TLS with different CA fails ==" 131ddd09448SEric Blakeobj=tls-creds-x509,dir=${tls_dir}/client2,endpoint=client,id=tls0 132ddd09448SEric Blake$QEMU_IMG info --image-opts --object $obj \ 133afcd1c2fSDaniel P. Berrangé driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \ 134ebc0141bSDaniel P. Berrangé 2>&1 | _filter_nbd 135ddd09448SEric Blake$QEMU_NBD_PROG -L -b $nbd_tcp_addr -p $nbd_tcp_port --object $obj \ 136ebc0141bSDaniel P. Berrangé --tls-creds=tls0 2>&1 | _filter_qemu_nbd_exports 137afcd1c2fSDaniel P. Berrangé 138bb39c47dSEric Blakeecho 139bb39c47dSEric Blakeecho "== perform I/O over TLS ==" 140bb39c47dSEric BlakeQEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT 141bb39c47dSEric Blake$QEMU_IO -c 'r -P 0x11 1m 1m' -c 'w -P 0x22 1m 1m' --image-opts \ 142bb39c47dSEric Blake --object tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0 \ 143bb39c47dSEric Blake driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \ 144bb39c47dSEric Blake 2>&1 | _filter_qemu_io 145bb39c47dSEric Blake 146ebc0141bSDaniel P. Berrangé$QEMU_IO -f $IMGFMT -r -U -c 'r -P 0x22 1m 1m' "$TEST_IMG" \ 147ebc0141bSDaniel P. Berrangé 2>&1 | _filter_qemu_io 148bb39c47dSEric Blake 149d0898051SEric Blakeecho 150b25e12daSDaniel P. Berrangeecho "== check TLS with authorization ==" 151b25e12daSDaniel P. Berrange 152b25e12daSDaniel P. Berrangenbd_server_stop 153b25e12daSDaniel P. Berrange 154b25e12daSDaniel P. Berrangenbd_server_start_tcp_socket \ 1554d7beeabSDaniel P. Berrangé --object tls-creds-x509,dir=${tls_dir}/server1,endpoint=server,id=tls0,verify-peer=on \ 156b25e12daSDaniel P. Berrange --object "authz-simple,id=authz0,identity=CN=localhost,, \ 157b25e12daSDaniel P. Berrange O=Cthulu Dark Lord Enterprises client1,,L=R'lyeh,,C=South Pacific" \ 158b25e12daSDaniel P. Berrange --tls-authz authz0 \ 159b25e12daSDaniel P. Berrange --tls-creds tls0 \ 160b25e12daSDaniel P. Berrange -f $IMGFMT "$TEST_IMG" 2>> "$TEST_DIR/server.log" 161b25e12daSDaniel P. Berrange 162b25e12daSDaniel P. Berrange$QEMU_IMG info --image-opts \ 163b25e12daSDaniel P. Berrange --object tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0 \ 164876df72dSMax Reitz driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \ 165ebc0141bSDaniel P. Berrangé 2>&1 | _filter_nbd 166b25e12daSDaniel P. Berrange 167b25e12daSDaniel P. Berrange$QEMU_IMG info --image-opts \ 168b25e12daSDaniel P. Berrange --object tls-creds-x509,dir=${tls_dir}/client3,endpoint=client,id=tls0 \ 169876df72dSMax Reitz driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \ 170ebc0141bSDaniel P. Berrangé 2>&1 | _filter_nbd 171b25e12daSDaniel P. Berrange 172f0620835SDaniel P. Berrangénbd_server_stop 173f0620835SDaniel P. Berrangé 174f0620835SDaniel P. Berrangénbd_server_start_unix_socket \ 175f0620835SDaniel P. Berrangé --object tls-creds-x509,dir=${tls_dir}/server1,endpoint=server,id=tls0,verify-peer=on \ 176f0620835SDaniel P. Berrangé --tls-creds tls0 \ 177f0620835SDaniel P. Berrangé -f $IMGFMT "$TEST_IMG" 2>> "$TEST_DIR/server.log" 178f0620835SDaniel P. Berrangé 179f0620835SDaniel P. Berrangéecho 180f0620835SDaniel P. Berrangéecho "== check TLS fail over UNIX with no hostname ==" 181f0620835SDaniel P. Berrangéobj1=tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0 182f0620835SDaniel P. Berrangé$QEMU_IMG info --image-opts --object $obj1 \ 183f0620835SDaniel P. Berrangé driver=nbd,path=$nbd_unix_socket,tls-creds=tls0 2>&1 | _filter_nbd 184f0620835SDaniel P. Berrangé$QEMU_NBD_PROG -L -k $nbd_unix_socket --object $obj1 --tls-creds=tls0 \ 185f0620835SDaniel P. Berrangé 2>&1 | _filter_qemu_nbd_exports 186f0620835SDaniel P. Berrangé 187f0620835SDaniel P. Berrangéecho 188f0620835SDaniel P. Berrangéecho "== check TLS works over UNIX with hostname override ==" 189f0620835SDaniel P. Berrangéobj1=tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0 190f0620835SDaniel P. Berrangé$QEMU_IMG info --image-opts --object $obj1 \ 191f0620835SDaniel P. Berrangé driver=nbd,path=$nbd_unix_socket,tls-creds=tls0,tls-hostname=127.0.0.1 \ 192f0620835SDaniel P. Berrangé 2>&1 | _filter_nbd 193f0620835SDaniel P. Berrangé$QEMU_NBD_PROG -L -k $nbd_unix_socket --object $obj1 \ 194f0620835SDaniel P. Berrangé --tls-creds=tls0 --tls-hostname=127.0.0.1 2>&1 | _filter_qemu_nbd_exports 195f0620835SDaniel P. Berrangé 196*10cc95c3SDaniel P. Berrangé 197*10cc95c3SDaniel P. Berrangéecho 198*10cc95c3SDaniel P. Berrangéecho "== check TLS works over UNIX with PSK ==" 199*10cc95c3SDaniel P. Berrangénbd_server_stop 200*10cc95c3SDaniel P. Berrangé 201*10cc95c3SDaniel P. Berrangénbd_server_start_unix_socket \ 202*10cc95c3SDaniel P. Berrangé --object tls-creds-psk,dir=${tls_dir}/psk1,endpoint=server,id=tls0,verify-peer=on \ 203*10cc95c3SDaniel P. Berrangé --tls-creds tls0 \ 204*10cc95c3SDaniel P. Berrangé -f $IMGFMT "$TEST_IMG" 2>> "$TEST_DIR/server.log" 205*10cc95c3SDaniel P. Berrangé 206*10cc95c3SDaniel P. Berrangéobj1=tls-creds-psk,dir=${tls_dir}/psk1,username=psk1,endpoint=client,id=tls0 207*10cc95c3SDaniel P. Berrangé$QEMU_IMG info --image-opts --object $obj1 \ 208*10cc95c3SDaniel P. Berrangé driver=nbd,path=$nbd_unix_socket,tls-creds=tls0 \ 209*10cc95c3SDaniel P. Berrangé 2>&1 | _filter_nbd 210*10cc95c3SDaniel P. Berrangé$QEMU_NBD_PROG -L -k $nbd_unix_socket --object $obj1 \ 211*10cc95c3SDaniel P. Berrangé --tls-creds=tls0 2>&1 | _filter_qemu_nbd_exports 212*10cc95c3SDaniel P. Berrangé 213*10cc95c3SDaniel P. Berrangéecho 214*10cc95c3SDaniel P. Berrangéecho "== check TLS fails over UNIX with mismatch PSK ==" 215*10cc95c3SDaniel P. Berrangéobj1=tls-creds-psk,dir=${tls_dir}/psk2,username=psk2,endpoint=client,id=tls0 216*10cc95c3SDaniel P. Berrangé$QEMU_IMG info --image-opts --object $obj1 \ 217*10cc95c3SDaniel P. Berrangé driver=nbd,path=$nbd_unix_socket,tls-creds=tls0 \ 218*10cc95c3SDaniel P. Berrangé 2>&1 | _filter_nbd 219*10cc95c3SDaniel P. Berrangé$QEMU_NBD_PROG -L -k $nbd_unix_socket --object $obj1 \ 220*10cc95c3SDaniel P. Berrangé --tls-creds=tls0 2>&1 | _filter_qemu_nbd_exports 221*10cc95c3SDaniel P. Berrangé 222b25e12daSDaniel P. Berrangeecho 223d0898051SEric Blakeecho "== final server log ==" 224a6d2bb25SDaniel P. Berrangécat "$TEST_DIR/server.log" | _filter_authz_check_tls 22584f8b840SDaniel P. Berrangérm -f "$TEST_DIR/server.log" 226d0898051SEric Blake 227afcd1c2fSDaniel P. Berrangé# success, all done 228afcd1c2fSDaniel P. Berrangéecho "*** done" 229afcd1c2fSDaniel P. Berrangérm -f $seq.full 230afcd1c2fSDaniel P. Berrangéstatus=0 231