xref: /openbmc/qemu/tests/qemu-iotests/207 (revision 7d8140595f1e131935ba1c98a55af7d066660707)
17c477526SPhilippe Mathieu-Daudé#!/usr/bin/env python3
256ea7450SKevin Wolf#
356ea7450SKevin Wolf# Test ssh image creation
456ea7450SKevin Wolf#
556ea7450SKevin Wolf# Copyright (C) 2018 Red Hat, Inc.
656ea7450SKevin Wolf#
700af1935SKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
800af1935SKevin Wolf#
956ea7450SKevin Wolf# This program is free software; you can redistribute it and/or modify
1056ea7450SKevin Wolf# it under the terms of the GNU General Public License as published by
1156ea7450SKevin Wolf# the Free Software Foundation; either version 2 of the License, or
1256ea7450SKevin Wolf# (at your option) any later version.
1356ea7450SKevin Wolf#
1456ea7450SKevin Wolf# This program is distributed in the hope that it will be useful,
1556ea7450SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of
1656ea7450SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1756ea7450SKevin Wolf# GNU General Public License for more details.
1856ea7450SKevin Wolf#
1956ea7450SKevin Wolf# You should have received a copy of the GNU General Public License
2056ea7450SKevin Wolf# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2156ea7450SKevin Wolf#
2256ea7450SKevin Wolf
2300af1935SKevin Wolfimport iotests
2400af1935SKevin Wolfimport subprocess
2500af1935SKevin Wolfimport re
2656ea7450SKevin Wolf
27*7d814059SJohn Snowiotests.script_initialize(
28*7d814059SJohn Snow    supported_fmts=['raw'],
29*7d814059SJohn Snow    supported_protocols=['ssh'],
30*7d814059SJohn Snow)
3156ea7450SKevin Wolf
329ac10f2eSMax Reitzdef filter_hash(qmsg):
339ac10f2eSMax Reitz    def _filter(key, value):
349ac10f2eSMax Reitz        if key == 'hash' and re.match('[0-9a-f]+', value):
359ac10f2eSMax Reitz            return 'HASH'
369ac10f2eSMax Reitz        return value
379ac10f2eSMax Reitz    return iotests.filter_qmp(qmsg, _filter)
3856ea7450SKevin Wolf
3900af1935SKevin Wolfdef blockdev_create(vm, options):
406055cdf3SKevin Wolf    vm.blockdev_create(options, filters=[iotests.filter_qmp_testfiles, filter_hash])
4156ea7450SKevin Wolf
4200af1935SKevin Wolfwith iotests.FilePath('t.img') as disk_path, \
4300af1935SKevin Wolf     iotests.VM() as vm:
4456ea7450SKevin Wolf
4500af1935SKevin Wolf    remote_path = iotests.remote_filename(disk_path)
4656ea7450SKevin Wolf
4700af1935SKevin Wolf    #
4800af1935SKevin Wolf    # Successful image creation (defaults)
4900af1935SKevin Wolf    #
5000af1935SKevin Wolf    iotests.log("=== Successful image creation (defaults) ===")
5100af1935SKevin Wolf    iotests.log("")
5256ea7450SKevin Wolf
5300af1935SKevin Wolf    vm.launch()
5400af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
5500af1935SKevin Wolf                          'location': {
5600af1935SKevin Wolf                              'path': disk_path,
5700af1935SKevin Wolf                              'server': {
5800af1935SKevin Wolf                                  'host': '127.0.0.1',
5900af1935SKevin Wolf                                  'port': '22'
6056ea7450SKevin Wolf                              }
6156ea7450SKevin Wolf                          },
6200af1935SKevin Wolf                          'size': 4194304 })
6300af1935SKevin Wolf    vm.shutdown()
6456ea7450SKevin Wolf
65b8c1f901SMax Reitz    iotests.img_info_log(remote_path)
6600af1935SKevin Wolf    iotests.log("")
6700af1935SKevin Wolf    iotests.img_info_log(disk_path)
6856ea7450SKevin Wolf
6900af1935SKevin Wolf    #
7000af1935SKevin Wolf    # Test host-key-check options
7100af1935SKevin Wolf    #
7200af1935SKevin Wolf    iotests.log("=== Test host-key-check options ===")
7300af1935SKevin Wolf    iotests.log("")
7456ea7450SKevin Wolf
7500af1935SKevin Wolf    vm.launch()
7600af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
7700af1935SKevin Wolf                          'location': {
7800af1935SKevin Wolf                              'path': disk_path,
7900af1935SKevin Wolf                              'server': {
8000af1935SKevin Wolf                                  'host': '127.0.0.1',
8100af1935SKevin Wolf                                  'port': '22'
8256ea7450SKevin Wolf                              },
8300af1935SKevin Wolf                              'host-key-check': {
8400af1935SKevin Wolf                                  'mode': 'none'
8556ea7450SKevin Wolf                              }
8656ea7450SKevin Wolf                          },
8700af1935SKevin Wolf                          'size': 8388608 })
8800af1935SKevin Wolf    vm.shutdown()
8956ea7450SKevin Wolf
90b8c1f901SMax Reitz    iotests.img_info_log(remote_path)
9156ea7450SKevin Wolf
9200af1935SKevin Wolf    vm.launch()
9300af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
9400af1935SKevin Wolf                          'location': {
9500af1935SKevin Wolf                              'path': disk_path,
9600af1935SKevin Wolf                              'server': {
9700af1935SKevin Wolf                                  'host': '127.0.0.1',
9800af1935SKevin Wolf                                  'port': '22'
9956ea7450SKevin Wolf                              },
10000af1935SKevin Wolf                              'host-key-check': {
10100af1935SKevin Wolf                                  'mode': 'known_hosts'
10256ea7450SKevin Wolf                              }
10356ea7450SKevin Wolf                          },
10400af1935SKevin Wolf                          'size': 4194304 })
10500af1935SKevin Wolf    vm.shutdown()
10656ea7450SKevin Wolf
107b8c1f901SMax Reitz    iotests.img_info_log(remote_path)
10856ea7450SKevin Wolf
109b10d49d7SPino Toscano    keys = subprocess.check_output(
110b10d49d7SPino Toscano        'ssh-keyscan 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' +
111b10d49d7SPino Toscano        'cut -d" " -f3',
112b10d49d7SPino Toscano        shell=True).rstrip().decode('ascii').split('\n')
113b10d49d7SPino Toscano
114b10d49d7SPino Toscano    # Mappings of base64 representations to digests
115b10d49d7SPino Toscano    md5_keys = {}
116b10d49d7SPino Toscano    sha1_keys = {}
117b10d49d7SPino Toscano
118b10d49d7SPino Toscano    for key in keys:
119b10d49d7SPino Toscano        md5_keys[key] = subprocess.check_output(
120b10d49d7SPino Toscano            'echo %s | base64 -d | md5sum -b | cut -d" " -f1' % key,
121b10d49d7SPino Toscano            shell=True).rstrip().decode('ascii')
122b10d49d7SPino Toscano
123b10d49d7SPino Toscano        sha1_keys[key] = subprocess.check_output(
124b10d49d7SPino Toscano            'echo %s | base64 -d | sha1sum -b | cut -d" " -f1' % key,
1258eb5e674SMax Reitz            shell=True).rstrip().decode('ascii')
12656ea7450SKevin Wolf
12700af1935SKevin Wolf    vm.launch()
128b10d49d7SPino Toscano
129b10d49d7SPino Toscano    # Find correct key first
130b10d49d7SPino Toscano    matching_key = None
131b10d49d7SPino Toscano    for key in keys:
132b10d49d7SPino Toscano        result = vm.qmp('blockdev-add',
133b10d49d7SPino Toscano                        driver='ssh', node_name='node0', path=disk_path,
134b10d49d7SPino Toscano                        server={
135b10d49d7SPino Toscano                             'host': '127.0.0.1',
136b10d49d7SPino Toscano                             'port': '22',
137b10d49d7SPino Toscano                        }, host_key_check={
138b10d49d7SPino Toscano                             'mode': 'hash',
139b10d49d7SPino Toscano                             'type': 'md5',
140b10d49d7SPino Toscano                             'hash': md5_keys[key],
141b10d49d7SPino Toscano                        })
142b10d49d7SPino Toscano
143b10d49d7SPino Toscano        if 'error' not in result:
144b10d49d7SPino Toscano            vm.qmp('blockdev-del', node_name='node0')
145b10d49d7SPino Toscano            matching_key = key
146b10d49d7SPino Toscano            break
147b10d49d7SPino Toscano
148b10d49d7SPino Toscano    if matching_key is None:
149b10d49d7SPino Toscano        vm.shutdown()
150b10d49d7SPino Toscano        iotests.notrun('Did not find a key that fits 127.0.0.1')
151b10d49d7SPino Toscano
15200af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
15300af1935SKevin Wolf                          'location': {
15400af1935SKevin Wolf                              'path': disk_path,
15500af1935SKevin Wolf                              'server': {
15600af1935SKevin Wolf                                  'host': '127.0.0.1',
15700af1935SKevin Wolf                                  'port': '22'
15856ea7450SKevin Wolf                              },
15900af1935SKevin Wolf                              'host-key-check': {
16000af1935SKevin Wolf                                  'mode': 'hash',
16100af1935SKevin Wolf                                  'type': 'md5',
16200af1935SKevin Wolf                                  'hash': 'wrong',
16356ea7450SKevin Wolf                              }
16456ea7450SKevin Wolf                          },
16500af1935SKevin Wolf                          'size': 2097152 })
16600af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
16700af1935SKevin Wolf                          'location': {
16800af1935SKevin Wolf                              'path': disk_path,
16900af1935SKevin Wolf                              'server': {
17000af1935SKevin Wolf                                  'host': '127.0.0.1',
17100af1935SKevin Wolf                                  'port': '22'
17256ea7450SKevin Wolf                              },
17300af1935SKevin Wolf                              'host-key-check': {
17400af1935SKevin Wolf                                  'mode': 'hash',
17500af1935SKevin Wolf                                  'type': 'md5',
176b10d49d7SPino Toscano                                  'hash': md5_keys[matching_key],
17756ea7450SKevin Wolf                              }
17856ea7450SKevin Wolf                          },
17900af1935SKevin Wolf                          'size': 8388608 })
18000af1935SKevin Wolf    vm.shutdown()
18156ea7450SKevin Wolf
182b8c1f901SMax Reitz    iotests.img_info_log(remote_path)
18356ea7450SKevin Wolf
18400af1935SKevin Wolf    vm.launch()
18500af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
18600af1935SKevin Wolf                          'location': {
18700af1935SKevin Wolf                              'path': disk_path,
18800af1935SKevin Wolf                              'server': {
18900af1935SKevin Wolf                                  'host': '127.0.0.1',
19000af1935SKevin Wolf                                  'port': '22'
19156ea7450SKevin Wolf                              },
19200af1935SKevin Wolf                              'host-key-check': {
19300af1935SKevin Wolf                                  'mode': 'hash',
19400af1935SKevin Wolf                                  'type': 'sha1',
19500af1935SKevin Wolf                                  'hash': 'wrong',
19656ea7450SKevin Wolf                              }
19756ea7450SKevin Wolf                          },
19800af1935SKevin Wolf                          'size': 2097152 })
19900af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
20000af1935SKevin Wolf                          'location': {
20100af1935SKevin Wolf                              'path': disk_path,
20200af1935SKevin Wolf                              'server': {
20300af1935SKevin Wolf                                  'host': '127.0.0.1',
20400af1935SKevin Wolf                                  'port': '22'
20556ea7450SKevin Wolf                              },
20600af1935SKevin Wolf                              'host-key-check': {
20700af1935SKevin Wolf                                  'mode': 'hash',
20800af1935SKevin Wolf                                  'type': 'sha1',
209b10d49d7SPino Toscano                                  'hash': sha1_keys[matching_key],
21056ea7450SKevin Wolf                              }
21156ea7450SKevin Wolf                          },
21200af1935SKevin Wolf                          'size': 4194304 })
21300af1935SKevin Wolf    vm.shutdown()
21456ea7450SKevin Wolf
215b8c1f901SMax Reitz    iotests.img_info_log(remote_path)
21656ea7450SKevin Wolf
21700af1935SKevin Wolf    #
21800af1935SKevin Wolf    # Invalid path and user
21900af1935SKevin Wolf    #
22000af1935SKevin Wolf    iotests.log("=== Invalid path and user ===")
22100af1935SKevin Wolf    iotests.log("")
22256ea7450SKevin Wolf
22300af1935SKevin Wolf    vm.launch()
22400af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
22500af1935SKevin Wolf                          'location': {
22600af1935SKevin Wolf                              'path': '/this/is/not/an/existing/path',
22700af1935SKevin Wolf                              'server': {
22800af1935SKevin Wolf                                  'host': '127.0.0.1',
22900af1935SKevin Wolf                                  'port': '22'
23000af1935SKevin Wolf                              },
23100af1935SKevin Wolf                              'host-key-check': {
23200af1935SKevin Wolf                                  'mode': 'none'
23356ea7450SKevin Wolf                              }
23456ea7450SKevin Wolf                          },
23500af1935SKevin Wolf                          'size': 4194304 })
23600af1935SKevin Wolf    blockdev_create(vm, { 'driver': 'ssh',
23700af1935SKevin Wolf                          'location': {
23800af1935SKevin Wolf                              'path': disk_path,
23900af1935SKevin Wolf                              'user': 'invalid user',
24000af1935SKevin Wolf                              'server': {
24100af1935SKevin Wolf                                  'host': '127.0.0.1',
24200af1935SKevin Wolf                                  'port': '22'
24300af1935SKevin Wolf                              },
24400af1935SKevin Wolf                              'host-key-check': {
24500af1935SKevin Wolf                                  'mode': 'none'
24656ea7450SKevin Wolf                              }
24756ea7450SKevin Wolf                          },
24800af1935SKevin Wolf                          'size': 4194304 })
24900af1935SKevin Wolf    vm.shutdown()
250