xref: /openbmc/qemu/tests/qemu-iotests/207 (revision 9dd003a99842d1d82c336e45c5cce656149de382)
17c477526SPhilippe Mathieu-Daudé#!/usr/bin/env python3
2*9dd003a9SVladimir Sementsov-Ogievskiy# group: rw
356ea7450SKevin Wolf#
456ea7450SKevin Wolf# Test ssh image creation
556ea7450SKevin Wolf#
656ea7450SKevin Wolf# Copyright (C) 2018 Red Hat, Inc.
756ea7450SKevin Wolf#
800af1935SKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
900af1935SKevin Wolf#
1056ea7450SKevin Wolf# This program is free software; you can redistribute it and/or modify
1156ea7450SKevin Wolf# it under the terms of the GNU General Public License as published by
1256ea7450SKevin Wolf# the Free Software Foundation; either version 2 of the License, or
1356ea7450SKevin Wolf# (at your option) any later version.
1456ea7450SKevin Wolf#
1556ea7450SKevin Wolf# This program is distributed in the hope that it will be useful,
1656ea7450SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of
1756ea7450SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1856ea7450SKevin Wolf# GNU General Public License for more details.
1956ea7450SKevin Wolf#
2056ea7450SKevin Wolf# You should have received a copy of the GNU General Public License
2156ea7450SKevin Wolf# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2256ea7450SKevin Wolf#
2356ea7450SKevin Wolf
2400af1935SKevin Wolfimport iotests
2500af1935SKevin Wolfimport subprocess
2600af1935SKevin Wolfimport re
2756ea7450SKevin Wolf
287d814059SJohn Snowiotests.script_initialize(
297d814059SJohn Snow    supported_fmts=['raw'],
307d814059SJohn Snow    supported_protocols=['ssh'],
317d814059SJohn Snow)
3256ea7450SKevin Wolf
339ac10f2eSMax Reitzdef filter_hash(qmsg):
349ac10f2eSMax Reitz    def _filter(key, value):
359ac10f2eSMax Reitz        if key == 'hash' and re.match('[0-9a-f]+', value):
369ac10f2eSMax Reitz            return 'HASH'
379ac10f2eSMax Reitz        return value
389ac10f2eSMax Reitz    return iotests.filter_qmp(qmsg, _filter)
3956ea7450SKevin Wolf
4000af1935SKevin Wolfdef blockdev_create(vm, options):
416055cdf3SKevin Wolf    vm.blockdev_create(options, filters=[iotests.filter_qmp_testfiles, filter_hash])
4256ea7450SKevin Wolf
4300af1935SKevin Wolfwith iotests.FilePath('t.img') as disk_path, \
4400af1935SKevin Wolf     iotests.VM() as vm:
4556ea7450SKevin Wolf
4600af1935SKevin Wolf    remote_path = iotests.remote_filename(disk_path)
4756ea7450SKevin Wolf
4800af1935SKevin Wolf    #
4900af1935SKevin Wolf    # Successful image creation (defaults)
5000af1935SKevin Wolf    #
5100af1935SKevin Wolf    iotests.log("=== Successful image creation (defaults) ===")
5200af1935SKevin Wolf    iotests.log("")
5356ea7450SKevin Wolf
5400af1935SKevin Wolf    vm.launch()
5500af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
5600af1935SKevin Wolf                          'location': {
5700af1935SKevin Wolf                              'path': disk_path,
5800af1935SKevin Wolf                              'server': {
5900af1935SKevin Wolf                                  'host': '127.0.0.1',
6000af1935SKevin Wolf                                  'port': '22'
6156ea7450SKevin Wolf                              }
6256ea7450SKevin Wolf                          },
6300af1935SKevin Wolf                          'size': 4194304 })
6400af1935SKevin Wolf    vm.shutdown()
6556ea7450SKevin Wolf
66b8c1f901SMax Reitz    iotests.img_info_log(remote_path)
6700af1935SKevin Wolf    iotests.log("")
6800af1935SKevin Wolf    iotests.img_info_log(disk_path)
6956ea7450SKevin Wolf
7000af1935SKevin Wolf    #
7100af1935SKevin Wolf    # Test host-key-check options
7200af1935SKevin Wolf    #
7300af1935SKevin Wolf    iotests.log("=== Test host-key-check options ===")
7400af1935SKevin Wolf    iotests.log("")
7556ea7450SKevin Wolf
7600af1935SKevin Wolf    vm.launch()
7700af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
7800af1935SKevin Wolf                          'location': {
7900af1935SKevin Wolf                              'path': disk_path,
8000af1935SKevin Wolf                              'server': {
8100af1935SKevin Wolf                                  'host': '127.0.0.1',
8200af1935SKevin Wolf                                  'port': '22'
8356ea7450SKevin Wolf                              },
8400af1935SKevin Wolf                              'host-key-check': {
8500af1935SKevin Wolf                                  'mode': 'none'
8656ea7450SKevin Wolf                              }
8756ea7450SKevin Wolf                          },
8800af1935SKevin Wolf                          'size': 8388608 })
8900af1935SKevin Wolf    vm.shutdown()
9056ea7450SKevin Wolf
91b8c1f901SMax Reitz    iotests.img_info_log(remote_path)
9256ea7450SKevin Wolf
9300af1935SKevin Wolf    vm.launch()
9400af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
9500af1935SKevin Wolf                          'location': {
9600af1935SKevin Wolf                              'path': disk_path,
9700af1935SKevin Wolf                              'server': {
9800af1935SKevin Wolf                                  'host': '127.0.0.1',
9900af1935SKevin Wolf                                  'port': '22'
10056ea7450SKevin Wolf                              },
10100af1935SKevin Wolf                              'host-key-check': {
10200af1935SKevin Wolf                                  'mode': 'known_hosts'
10356ea7450SKevin Wolf                              }
10456ea7450SKevin Wolf                          },
10500af1935SKevin Wolf                          'size': 4194304 })
10600af1935SKevin Wolf    vm.shutdown()
10756ea7450SKevin Wolf
108b8c1f901SMax Reitz    iotests.img_info_log(remote_path)
10956ea7450SKevin Wolf
110b10d49d7SPino Toscano    keys = subprocess.check_output(
111b10d49d7SPino Toscano        'ssh-keyscan 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' +
112b10d49d7SPino Toscano        'cut -d" " -f3',
113b10d49d7SPino Toscano        shell=True).rstrip().decode('ascii').split('\n')
114b10d49d7SPino Toscano
115b10d49d7SPino Toscano    # Mappings of base64 representations to digests
116b10d49d7SPino Toscano    md5_keys = {}
117b10d49d7SPino Toscano    sha1_keys = {}
118b10d49d7SPino Toscano
119b10d49d7SPino Toscano    for key in keys:
120b10d49d7SPino Toscano        md5_keys[key] = subprocess.check_output(
121b10d49d7SPino Toscano            'echo %s | base64 -d | md5sum -b | cut -d" " -f1' % key,
122b10d49d7SPino Toscano            shell=True).rstrip().decode('ascii')
123b10d49d7SPino Toscano
124b10d49d7SPino Toscano        sha1_keys[key] = subprocess.check_output(
125b10d49d7SPino Toscano            'echo %s | base64 -d | sha1sum -b | cut -d" " -f1' % key,
1268eb5e674SMax Reitz            shell=True).rstrip().decode('ascii')
12756ea7450SKevin Wolf
12800af1935SKevin Wolf    vm.launch()
129b10d49d7SPino Toscano
130b10d49d7SPino Toscano    # Find correct key first
131b10d49d7SPino Toscano    matching_key = None
132b10d49d7SPino Toscano    for key in keys:
133b10d49d7SPino Toscano        result = vm.qmp('blockdev-add',
134b10d49d7SPino Toscano                        driver='ssh', node_name='node0', path=disk_path,
135b10d49d7SPino Toscano                        server={
136b10d49d7SPino Toscano                             'host': '127.0.0.1',
137b10d49d7SPino Toscano                             'port': '22',
138b10d49d7SPino Toscano                        }, host_key_check={
139b10d49d7SPino Toscano                             'mode': 'hash',
140b10d49d7SPino Toscano                             'type': 'md5',
141b10d49d7SPino Toscano                             'hash': md5_keys[key],
142b10d49d7SPino Toscano                        })
143b10d49d7SPino Toscano
144b10d49d7SPino Toscano        if 'error' not in result:
145b10d49d7SPino Toscano            vm.qmp('blockdev-del', node_name='node0')
146b10d49d7SPino Toscano            matching_key = key
147b10d49d7SPino Toscano            break
148b10d49d7SPino Toscano
149b10d49d7SPino Toscano    if matching_key is None:
150b10d49d7SPino Toscano        vm.shutdown()
151b10d49d7SPino Toscano        iotests.notrun('Did not find a key that fits 127.0.0.1')
152b10d49d7SPino Toscano
15300af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
15400af1935SKevin Wolf                          'location': {
15500af1935SKevin Wolf                              'path': disk_path,
15600af1935SKevin Wolf                              'server': {
15700af1935SKevin Wolf                                  'host': '127.0.0.1',
15800af1935SKevin Wolf                                  'port': '22'
15956ea7450SKevin Wolf                              },
16000af1935SKevin Wolf                              'host-key-check': {
16100af1935SKevin Wolf                                  'mode': 'hash',
16200af1935SKevin Wolf                                  'type': 'md5',
16300af1935SKevin Wolf                                  'hash': 'wrong',
16456ea7450SKevin Wolf                              }
16556ea7450SKevin Wolf                          },
16600af1935SKevin Wolf                          'size': 2097152 })
16700af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
16800af1935SKevin Wolf                          'location': {
16900af1935SKevin Wolf                              'path': disk_path,
17000af1935SKevin Wolf                              'server': {
17100af1935SKevin Wolf                                  'host': '127.0.0.1',
17200af1935SKevin Wolf                                  'port': '22'
17356ea7450SKevin Wolf                              },
17400af1935SKevin Wolf                              'host-key-check': {
17500af1935SKevin Wolf                                  'mode': 'hash',
17600af1935SKevin Wolf                                  'type': 'md5',
177b10d49d7SPino Toscano                                  'hash': md5_keys[matching_key],
17856ea7450SKevin Wolf                              }
17956ea7450SKevin Wolf                          },
18000af1935SKevin Wolf                          'size': 8388608 })
18100af1935SKevin Wolf    vm.shutdown()
18256ea7450SKevin Wolf
183b8c1f901SMax Reitz    iotests.img_info_log(remote_path)
18456ea7450SKevin Wolf
18500af1935SKevin Wolf    vm.launch()
18600af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
18700af1935SKevin Wolf                          'location': {
18800af1935SKevin Wolf                              'path': disk_path,
18900af1935SKevin Wolf                              'server': {
19000af1935SKevin Wolf                                  'host': '127.0.0.1',
19100af1935SKevin Wolf                                  'port': '22'
19256ea7450SKevin Wolf                              },
19300af1935SKevin Wolf                              'host-key-check': {
19400af1935SKevin Wolf                                  'mode': 'hash',
19500af1935SKevin Wolf                                  'type': 'sha1',
19600af1935SKevin Wolf                                  'hash': 'wrong',
19756ea7450SKevin Wolf                              }
19856ea7450SKevin Wolf                          },
19900af1935SKevin Wolf                          'size': 2097152 })
20000af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
20100af1935SKevin Wolf                          'location': {
20200af1935SKevin Wolf                              'path': disk_path,
20300af1935SKevin Wolf                              'server': {
20400af1935SKevin Wolf                                  'host': '127.0.0.1',
20500af1935SKevin Wolf                                  'port': '22'
20656ea7450SKevin Wolf                              },
20700af1935SKevin Wolf                              'host-key-check': {
20800af1935SKevin Wolf                                  'mode': 'hash',
20900af1935SKevin Wolf                                  'type': 'sha1',
210b10d49d7SPino Toscano                                  'hash': sha1_keys[matching_key],
21156ea7450SKevin Wolf                              }
21256ea7450SKevin Wolf                          },
21300af1935SKevin Wolf                          'size': 4194304 })
21400af1935SKevin Wolf    vm.shutdown()
21556ea7450SKevin Wolf
216b8c1f901SMax Reitz    iotests.img_info_log(remote_path)
21756ea7450SKevin Wolf
21800af1935SKevin Wolf    #
21900af1935SKevin Wolf    # Invalid path and user
22000af1935SKevin Wolf    #
22100af1935SKevin Wolf    iotests.log("=== Invalid path and user ===")
22200af1935SKevin Wolf    iotests.log("")
22356ea7450SKevin Wolf
22400af1935SKevin Wolf    vm.launch()
22500af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
22600af1935SKevin Wolf                          'location': {
22700af1935SKevin Wolf                              'path': '/this/is/not/an/existing/path',
22800af1935SKevin Wolf                              'server': {
22900af1935SKevin Wolf                                  'host': '127.0.0.1',
23000af1935SKevin Wolf                                  'port': '22'
23100af1935SKevin Wolf                              },
23200af1935SKevin Wolf                              'host-key-check': {
23300af1935SKevin Wolf                                  'mode': 'none'
23456ea7450SKevin Wolf                              }
23556ea7450SKevin Wolf                          },
23600af1935SKevin Wolf                          'size': 4194304 })
23700af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
23800af1935SKevin Wolf                          'location': {
23900af1935SKevin Wolf                              'path': disk_path,
24000af1935SKevin Wolf                              'user': 'invalid user',
24100af1935SKevin Wolf                              'server': {
24200af1935SKevin Wolf                                  'host': '127.0.0.1',
24300af1935SKevin Wolf                                  'port': '22'
24400af1935SKevin Wolf                              },
24500af1935SKevin Wolf                              'host-key-check': {
24600af1935SKevin Wolf                                  'mode': 'none'
24756ea7450SKevin Wolf                              }
24856ea7450SKevin Wolf                          },
24900af1935SKevin Wolf                          'size': 4194304 })
25000af1935SKevin Wolf    vm.shutdown()
251