xref: /openbmc/qemu/tests/qemu-iotests/233 (revision ebc0141ba7e5d594ef9ea0efbf841e2e90edea7c)
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"
64afcd1c2fSDaniel P. Berrangé
65afcd1c2fSDaniel P. Berrangéecho
66afcd1c2fSDaniel P. Berrangéecho "== preparing image =="
67afcd1c2fSDaniel P. Berrangé_make_test_img 64M
68*ebc0141bSDaniel P. Berrangé$QEMU_IO -c 'w -P 0x11 1m 1m' "$TEST_IMG" 2>&1 | _filter_qemu_io
69afcd1c2fSDaniel P. Berrangé
70afcd1c2fSDaniel P. Berrangéecho
71afcd1c2fSDaniel P. Berrangéecho "== check TLS client to plain server fails =="
72d0898051SEric Blakenbd_server_start_tcp_socket -f $IMGFMT "$TEST_IMG" 2> "$TEST_DIR/server.log"
73afcd1c2fSDaniel P. Berrangé
74ddd09448SEric Blakeobj=tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0
75ddd09448SEric Blake$QEMU_IMG info --image-opts --object $obj \
76afcd1c2fSDaniel P. Berrangé    driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \
77*ebc0141bSDaniel P. Berrangé    2>&1 | _filter_nbd
78ddd09448SEric Blake$QEMU_NBD_PROG -L -b $nbd_tcp_addr -p $nbd_tcp_port --object $obj \
79*ebc0141bSDaniel P. Berrangé    --tls-creds=tls0 2>&1 | _filter_qemu_nbd_exports
80afcd1c2fSDaniel P. Berrangé
81afcd1c2fSDaniel P. Berrangénbd_server_stop
82afcd1c2fSDaniel P. Berrangé
83afcd1c2fSDaniel P. Berrangéecho
84afcd1c2fSDaniel P. Berrangéecho "== check plain client to TLS server fails =="
85afcd1c2fSDaniel P. Berrangé
86e4c8f292SDaniel P. Berrangénbd_server_start_tcp_socket \
874d7beeabSDaniel P. Berrangé    --object tls-creds-x509,dir=${tls_dir}/server1,endpoint=server,id=tls0,verify-peer=on \
88e4c8f292SDaniel P. Berrangé    --tls-creds tls0 \
89d0898051SEric Blake    -f $IMGFMT "$TEST_IMG" 2>> "$TEST_DIR/server.log"
90afcd1c2fSDaniel P. Berrangé
91*ebc0141bSDaniel P. Berrangé$QEMU_IMG info nbd://localhost:$nbd_tcp_port \
92*ebc0141bSDaniel P. Berrangé    2>&1 | _filter_nbd
93*ebc0141bSDaniel P. Berrangé$QEMU_NBD_PROG -L -b $nbd_tcp_addr -p $nbd_tcp_port \
94*ebc0141bSDaniel P. Berrangé    2>&1 | _filter_qemu_nbd_exports
95afcd1c2fSDaniel P. Berrangé
96afcd1c2fSDaniel P. Berrangéecho
97afcd1c2fSDaniel P. Berrangéecho "== check TLS works =="
98b25e12daSDaniel P. Berrangeobj1=tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0
99b25e12daSDaniel P. Berrangeobj2=tls-creds-x509,dir=${tls_dir}/client3,endpoint=client,id=tls0
100b25e12daSDaniel P. Berrange$QEMU_IMG info --image-opts --object $obj1 \
101afcd1c2fSDaniel P. Berrangé    driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \
102*ebc0141bSDaniel P. Berrangé    2>&1 | _filter_nbd
103b25e12daSDaniel P. Berrange$QEMU_IMG info --image-opts --object $obj2 \
104b25e12daSDaniel P. Berrange    driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \
105*ebc0141bSDaniel P. Berrangé    2>&1 | _filter_nbd
106b25e12daSDaniel P. Berrange$QEMU_NBD_PROG -L -b $nbd_tcp_addr -p $nbd_tcp_port --object $obj1 \
107*ebc0141bSDaniel P. Berrangé    --tls-creds=tls0 2>&1 | _filter_qemu_nbd_exports
108afcd1c2fSDaniel P. Berrangé
109afcd1c2fSDaniel P. Berrangéecho
110afcd1c2fSDaniel P. Berrangéecho "== check TLS with different CA fails =="
111ddd09448SEric Blakeobj=tls-creds-x509,dir=${tls_dir}/client2,endpoint=client,id=tls0
112ddd09448SEric Blake$QEMU_IMG info --image-opts --object $obj \
113afcd1c2fSDaniel P. Berrangé    driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \
114*ebc0141bSDaniel P. Berrangé    2>&1 | _filter_nbd
115ddd09448SEric Blake$QEMU_NBD_PROG -L -b $nbd_tcp_addr -p $nbd_tcp_port --object $obj \
116*ebc0141bSDaniel P. Berrangé    --tls-creds=tls0 2>&1 | _filter_qemu_nbd_exports
117afcd1c2fSDaniel P. Berrangé
118bb39c47dSEric Blakeecho
119bb39c47dSEric Blakeecho "== perform I/O over TLS =="
120bb39c47dSEric BlakeQEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT
121bb39c47dSEric Blake$QEMU_IO -c 'r -P 0x11 1m 1m' -c 'w -P 0x22 1m 1m' --image-opts \
122bb39c47dSEric Blake    --object tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0 \
123bb39c47dSEric Blake    driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \
124bb39c47dSEric Blake    2>&1 | _filter_qemu_io
125bb39c47dSEric Blake
126*ebc0141bSDaniel P. Berrangé$QEMU_IO -f $IMGFMT -r -U -c 'r -P 0x22 1m 1m' "$TEST_IMG" \
127*ebc0141bSDaniel P. Berrangé    2>&1 | _filter_qemu_io
128bb39c47dSEric Blake
129d0898051SEric Blakeecho
130b25e12daSDaniel P. Berrangeecho "== check TLS with authorization =="
131b25e12daSDaniel P. Berrange
132b25e12daSDaniel P. Berrangenbd_server_stop
133b25e12daSDaniel P. Berrange
134b25e12daSDaniel P. Berrangenbd_server_start_tcp_socket \
1354d7beeabSDaniel P. Berrangé    --object tls-creds-x509,dir=${tls_dir}/server1,endpoint=server,id=tls0,verify-peer=on \
136b25e12daSDaniel P. Berrange    --object "authz-simple,id=authz0,identity=CN=localhost,, \
137b25e12daSDaniel P. Berrange      O=Cthulu Dark Lord Enterprises client1,,L=R'lyeh,,C=South Pacific" \
138b25e12daSDaniel P. Berrange    --tls-authz authz0 \
139b25e12daSDaniel P. Berrange    --tls-creds tls0 \
140b25e12daSDaniel P. Berrange    -f $IMGFMT "$TEST_IMG" 2>> "$TEST_DIR/server.log"
141b25e12daSDaniel P. Berrange
142b25e12daSDaniel P. Berrange$QEMU_IMG info --image-opts \
143b25e12daSDaniel P. Berrange    --object tls-creds-x509,dir=${tls_dir}/client1,endpoint=client,id=tls0 \
144876df72dSMax Reitz    driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \
145*ebc0141bSDaniel P. Berrangé    2>&1 | _filter_nbd
146b25e12daSDaniel P. Berrange
147b25e12daSDaniel P. Berrange$QEMU_IMG info --image-opts \
148b25e12daSDaniel P. Berrange    --object tls-creds-x509,dir=${tls_dir}/client3,endpoint=client,id=tls0 \
149876df72dSMax Reitz    driver=nbd,host=$nbd_tcp_addr,port=$nbd_tcp_port,tls-creds=tls0 \
150*ebc0141bSDaniel P. Berrangé    2>&1 | _filter_nbd
151b25e12daSDaniel P. Berrange
152b25e12daSDaniel P. Berrangeecho
153d0898051SEric Blakeecho "== final server log =="
154a6d2bb25SDaniel P. Berrangécat "$TEST_DIR/server.log" | _filter_authz_check_tls
15584f8b840SDaniel P. Berrangérm -f "$TEST_DIR/server.log"
156d0898051SEric Blake
157afcd1c2fSDaniel P. Berrangé# success, all done
158afcd1c2fSDaniel P. Berrangéecho "*** done"
159afcd1c2fSDaniel P. Berrangérm -f $seq.full
160afcd1c2fSDaniel P. Berrangéstatus=0
161