100af1935SKevin Wolf#!/usr/bin/env python 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 2700af1935SKevin Wolfiotests.verify_image_format(supported_fmts=['raw']) 2800af1935SKevin Wolfiotests.verify_protocol(supported=['ssh']) 2956ea7450SKevin Wolf 309ac10f2eSMax Reitzdef filter_hash(qmsg): 319ac10f2eSMax Reitz def _filter(key, value): 329ac10f2eSMax Reitz if key == 'hash' and re.match('[0-9a-f]+', value): 339ac10f2eSMax Reitz return 'HASH' 349ac10f2eSMax Reitz return value 359ac10f2eSMax Reitz return iotests.filter_qmp(qmsg, _filter) 3656ea7450SKevin Wolf 3700af1935SKevin Wolfdef blockdev_create(vm, options): 38*6055cdf3SKevin Wolf vm.blockdev_create(options, filters=[iotests.filter_qmp_testfiles, filter_hash]) 3956ea7450SKevin Wolf 4000af1935SKevin Wolfwith iotests.FilePath('t.img') as disk_path, \ 4100af1935SKevin Wolf iotests.VM() as vm: 4256ea7450SKevin Wolf 4300af1935SKevin Wolf remote_path = iotests.remote_filename(disk_path) 4456ea7450SKevin Wolf 4500af1935SKevin Wolf # 4600af1935SKevin Wolf # Successful image creation (defaults) 4700af1935SKevin Wolf # 4800af1935SKevin Wolf iotests.log("=== Successful image creation (defaults) ===") 4900af1935SKevin Wolf iotests.log("") 5056ea7450SKevin Wolf 5100af1935SKevin Wolf vm.launch() 5200af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 5300af1935SKevin Wolf 'location': { 5400af1935SKevin Wolf 'path': disk_path, 5500af1935SKevin Wolf 'server': { 5600af1935SKevin Wolf 'host': '127.0.0.1', 5700af1935SKevin Wolf 'port': '22' 5856ea7450SKevin Wolf } 5956ea7450SKevin Wolf }, 6000af1935SKevin Wolf 'size': 4194304 }) 6100af1935SKevin Wolf vm.shutdown() 6256ea7450SKevin Wolf 63b8c1f901SMax Reitz iotests.img_info_log(remote_path) 6400af1935SKevin Wolf iotests.log("") 6500af1935SKevin Wolf iotests.img_info_log(disk_path) 6656ea7450SKevin Wolf 6700af1935SKevin Wolf # 6800af1935SKevin Wolf # Test host-key-check options 6900af1935SKevin Wolf # 7000af1935SKevin Wolf iotests.log("=== Test host-key-check options ===") 7100af1935SKevin Wolf iotests.log("") 7256ea7450SKevin Wolf 7300af1935SKevin Wolf vm.launch() 7400af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 7500af1935SKevin Wolf 'location': { 7600af1935SKevin Wolf 'path': disk_path, 7700af1935SKevin Wolf 'server': { 7800af1935SKevin Wolf 'host': '127.0.0.1', 7900af1935SKevin Wolf 'port': '22' 8056ea7450SKevin Wolf }, 8100af1935SKevin Wolf 'host-key-check': { 8200af1935SKevin Wolf 'mode': 'none' 8356ea7450SKevin Wolf } 8456ea7450SKevin Wolf }, 8500af1935SKevin Wolf 'size': 8388608 }) 8600af1935SKevin Wolf vm.shutdown() 8756ea7450SKevin Wolf 88b8c1f901SMax Reitz iotests.img_info_log(remote_path) 8956ea7450SKevin Wolf 9000af1935SKevin Wolf vm.launch() 9100af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 9200af1935SKevin Wolf 'location': { 9300af1935SKevin Wolf 'path': disk_path, 9400af1935SKevin Wolf 'server': { 9500af1935SKevin Wolf 'host': '127.0.0.1', 9600af1935SKevin Wolf 'port': '22' 9756ea7450SKevin Wolf }, 9800af1935SKevin Wolf 'host-key-check': { 9900af1935SKevin Wolf 'mode': 'known_hosts' 10056ea7450SKevin Wolf } 10156ea7450SKevin Wolf }, 10200af1935SKevin Wolf 'size': 4194304 }) 10300af1935SKevin Wolf vm.shutdown() 10456ea7450SKevin Wolf 105b8c1f901SMax Reitz iotests.img_info_log(remote_path) 10656ea7450SKevin Wolf 107b10d49d7SPino Toscano keys = subprocess.check_output( 108b10d49d7SPino Toscano 'ssh-keyscan 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' + 109b10d49d7SPino Toscano 'cut -d" " -f3', 110b10d49d7SPino Toscano shell=True).rstrip().decode('ascii').split('\n') 111b10d49d7SPino Toscano 112b10d49d7SPino Toscano # Mappings of base64 representations to digests 113b10d49d7SPino Toscano md5_keys = {} 114b10d49d7SPino Toscano sha1_keys = {} 115b10d49d7SPino Toscano 116b10d49d7SPino Toscano for key in keys: 117b10d49d7SPino Toscano md5_keys[key] = subprocess.check_output( 118b10d49d7SPino Toscano 'echo %s | base64 -d | md5sum -b | cut -d" " -f1' % key, 119b10d49d7SPino Toscano shell=True).rstrip().decode('ascii') 120b10d49d7SPino Toscano 121b10d49d7SPino Toscano sha1_keys[key] = subprocess.check_output( 122b10d49d7SPino Toscano 'echo %s | base64 -d | sha1sum -b | cut -d" " -f1' % key, 1238eb5e674SMax Reitz shell=True).rstrip().decode('ascii') 12456ea7450SKevin Wolf 12500af1935SKevin Wolf vm.launch() 126b10d49d7SPino Toscano 127b10d49d7SPino Toscano # Find correct key first 128b10d49d7SPino Toscano matching_key = None 129b10d49d7SPino Toscano for key in keys: 130b10d49d7SPino Toscano result = vm.qmp('blockdev-add', 131b10d49d7SPino Toscano driver='ssh', node_name='node0', path=disk_path, 132b10d49d7SPino Toscano server={ 133b10d49d7SPino Toscano 'host': '127.0.0.1', 134b10d49d7SPino Toscano 'port': '22', 135b10d49d7SPino Toscano }, host_key_check={ 136b10d49d7SPino Toscano 'mode': 'hash', 137b10d49d7SPino Toscano 'type': 'md5', 138b10d49d7SPino Toscano 'hash': md5_keys[key], 139b10d49d7SPino Toscano }) 140b10d49d7SPino Toscano 141b10d49d7SPino Toscano if 'error' not in result: 142b10d49d7SPino Toscano vm.qmp('blockdev-del', node_name='node0') 143b10d49d7SPino Toscano matching_key = key 144b10d49d7SPino Toscano break 145b10d49d7SPino Toscano 146b10d49d7SPino Toscano if matching_key is None: 147b10d49d7SPino Toscano vm.shutdown() 148b10d49d7SPino Toscano iotests.notrun('Did not find a key that fits 127.0.0.1') 149b10d49d7SPino Toscano 15000af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 15100af1935SKevin Wolf 'location': { 15200af1935SKevin Wolf 'path': disk_path, 15300af1935SKevin Wolf 'server': { 15400af1935SKevin Wolf 'host': '127.0.0.1', 15500af1935SKevin Wolf 'port': '22' 15656ea7450SKevin Wolf }, 15700af1935SKevin Wolf 'host-key-check': { 15800af1935SKevin Wolf 'mode': 'hash', 15900af1935SKevin Wolf 'type': 'md5', 16000af1935SKevin Wolf 'hash': 'wrong', 16156ea7450SKevin Wolf } 16256ea7450SKevin Wolf }, 16300af1935SKevin Wolf 'size': 2097152 }) 16400af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 16500af1935SKevin Wolf 'location': { 16600af1935SKevin Wolf 'path': disk_path, 16700af1935SKevin Wolf 'server': { 16800af1935SKevin Wolf 'host': '127.0.0.1', 16900af1935SKevin Wolf 'port': '22' 17056ea7450SKevin Wolf }, 17100af1935SKevin Wolf 'host-key-check': { 17200af1935SKevin Wolf 'mode': 'hash', 17300af1935SKevin Wolf 'type': 'md5', 174b10d49d7SPino Toscano 'hash': md5_keys[matching_key], 17556ea7450SKevin Wolf } 17656ea7450SKevin Wolf }, 17700af1935SKevin Wolf 'size': 8388608 }) 17800af1935SKevin Wolf vm.shutdown() 17956ea7450SKevin Wolf 180b8c1f901SMax Reitz iotests.img_info_log(remote_path) 18156ea7450SKevin Wolf 18200af1935SKevin Wolf vm.launch() 18300af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 18400af1935SKevin Wolf 'location': { 18500af1935SKevin Wolf 'path': disk_path, 18600af1935SKevin Wolf 'server': { 18700af1935SKevin Wolf 'host': '127.0.0.1', 18800af1935SKevin Wolf 'port': '22' 18956ea7450SKevin Wolf }, 19000af1935SKevin Wolf 'host-key-check': { 19100af1935SKevin Wolf 'mode': 'hash', 19200af1935SKevin Wolf 'type': 'sha1', 19300af1935SKevin Wolf 'hash': 'wrong', 19456ea7450SKevin Wolf } 19556ea7450SKevin Wolf }, 19600af1935SKevin Wolf 'size': 2097152 }) 19700af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 19800af1935SKevin Wolf 'location': { 19900af1935SKevin Wolf 'path': disk_path, 20000af1935SKevin Wolf 'server': { 20100af1935SKevin Wolf 'host': '127.0.0.1', 20200af1935SKevin Wolf 'port': '22' 20356ea7450SKevin Wolf }, 20400af1935SKevin Wolf 'host-key-check': { 20500af1935SKevin Wolf 'mode': 'hash', 20600af1935SKevin Wolf 'type': 'sha1', 207b10d49d7SPino Toscano 'hash': sha1_keys[matching_key], 20856ea7450SKevin Wolf } 20956ea7450SKevin Wolf }, 21000af1935SKevin Wolf 'size': 4194304 }) 21100af1935SKevin Wolf vm.shutdown() 21256ea7450SKevin Wolf 213b8c1f901SMax Reitz iotests.img_info_log(remote_path) 21456ea7450SKevin Wolf 21500af1935SKevin Wolf # 21600af1935SKevin Wolf # Invalid path and user 21700af1935SKevin Wolf # 21800af1935SKevin Wolf iotests.log("=== Invalid path and user ===") 21900af1935SKevin Wolf iotests.log("") 22056ea7450SKevin Wolf 22100af1935SKevin Wolf vm.launch() 22200af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 22300af1935SKevin Wolf 'location': { 22400af1935SKevin Wolf 'path': '/this/is/not/an/existing/path', 22500af1935SKevin Wolf 'server': { 22600af1935SKevin Wolf 'host': '127.0.0.1', 22700af1935SKevin Wolf 'port': '22' 22800af1935SKevin Wolf }, 22900af1935SKevin Wolf 'host-key-check': { 23000af1935SKevin Wolf 'mode': 'none' 23156ea7450SKevin Wolf } 23256ea7450SKevin Wolf }, 23300af1935SKevin Wolf 'size': 4194304 }) 23400af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 23500af1935SKevin Wolf 'location': { 23600af1935SKevin Wolf 'path': disk_path, 23700af1935SKevin Wolf 'user': 'invalid user', 23800af1935SKevin Wolf 'server': { 23900af1935SKevin Wolf 'host': '127.0.0.1', 24000af1935SKevin Wolf 'port': '22' 24100af1935SKevin Wolf }, 24200af1935SKevin Wolf 'host-key-check': { 24300af1935SKevin Wolf 'mode': 'none' 24456ea7450SKevin Wolf } 24556ea7450SKevin Wolf }, 24600af1935SKevin Wolf 'size': 4194304 }) 24700af1935SKevin Wolf vm.shutdown() 248