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): 383fb588a0SKevin Wolf result = vm.qmp_log('blockdev-create', job_id='job0', options=options, 399ac10f2eSMax Reitz filters=[iotests.filter_qmp_testfiles, filter_hash]) 4056ea7450SKevin Wolf 4100af1935SKevin Wolf if 'return' in result: 4200af1935SKevin Wolf assert result['return'] == {} 4300af1935SKevin Wolf vm.run_job('job0') 4400af1935SKevin Wolf iotests.log("") 4556ea7450SKevin Wolf 4600af1935SKevin Wolfwith iotests.FilePath('t.img') as disk_path, \ 4700af1935SKevin Wolf iotests.VM() as vm: 4856ea7450SKevin Wolf 4900af1935SKevin Wolf remote_path = iotests.remote_filename(disk_path) 5056ea7450SKevin Wolf 5100af1935SKevin Wolf # 5200af1935SKevin Wolf # Successful image creation (defaults) 5300af1935SKevin Wolf # 5400af1935SKevin Wolf iotests.log("=== Successful image creation (defaults) ===") 5500af1935SKevin Wolf iotests.log("") 5656ea7450SKevin Wolf 5700af1935SKevin Wolf vm.launch() 5800af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 5900af1935SKevin Wolf 'location': { 6000af1935SKevin Wolf 'path': disk_path, 6100af1935SKevin Wolf 'server': { 6200af1935SKevin Wolf 'host': '127.0.0.1', 6300af1935SKevin Wolf 'port': '22' 6456ea7450SKevin Wolf } 6556ea7450SKevin Wolf }, 6600af1935SKevin Wolf 'size': 4194304 }) 6700af1935SKevin Wolf vm.shutdown() 6856ea7450SKevin Wolf 69b8c1f901SMax Reitz iotests.img_info_log(remote_path) 7000af1935SKevin Wolf iotests.log("") 7100af1935SKevin Wolf iotests.img_info_log(disk_path) 7256ea7450SKevin Wolf 7300af1935SKevin Wolf # 7400af1935SKevin Wolf # Test host-key-check options 7500af1935SKevin Wolf # 7600af1935SKevin Wolf iotests.log("=== Test host-key-check options ===") 7700af1935SKevin Wolf iotests.log("") 7856ea7450SKevin Wolf 7900af1935SKevin Wolf vm.launch() 8000af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 8100af1935SKevin Wolf 'location': { 8200af1935SKevin Wolf 'path': disk_path, 8300af1935SKevin Wolf 'server': { 8400af1935SKevin Wolf 'host': '127.0.0.1', 8500af1935SKevin Wolf 'port': '22' 8656ea7450SKevin Wolf }, 8700af1935SKevin Wolf 'host-key-check': { 8800af1935SKevin Wolf 'mode': 'none' 8956ea7450SKevin Wolf } 9056ea7450SKevin Wolf }, 9100af1935SKevin Wolf 'size': 8388608 }) 9200af1935SKevin Wolf vm.shutdown() 9356ea7450SKevin Wolf 94b8c1f901SMax Reitz iotests.img_info_log(remote_path) 9556ea7450SKevin Wolf 9600af1935SKevin Wolf vm.launch() 9700af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 9800af1935SKevin Wolf 'location': { 9900af1935SKevin Wolf 'path': disk_path, 10000af1935SKevin Wolf 'server': { 10100af1935SKevin Wolf 'host': '127.0.0.1', 10200af1935SKevin Wolf 'port': '22' 10356ea7450SKevin Wolf }, 10400af1935SKevin Wolf 'host-key-check': { 10500af1935SKevin Wolf 'mode': 'known_hosts' 10656ea7450SKevin Wolf } 10756ea7450SKevin Wolf }, 10800af1935SKevin Wolf 'size': 4194304 }) 10900af1935SKevin Wolf vm.shutdown() 11056ea7450SKevin Wolf 111b8c1f901SMax Reitz iotests.img_info_log(remote_path) 11256ea7450SKevin Wolf 113*b10d49d7SPino Toscano keys = subprocess.check_output( 114*b10d49d7SPino Toscano 'ssh-keyscan 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' + 115*b10d49d7SPino Toscano 'cut -d" " -f3', 116*b10d49d7SPino Toscano shell=True).rstrip().decode('ascii').split('\n') 117*b10d49d7SPino Toscano 118*b10d49d7SPino Toscano # Mappings of base64 representations to digests 119*b10d49d7SPino Toscano md5_keys = {} 120*b10d49d7SPino Toscano sha1_keys = {} 121*b10d49d7SPino Toscano 122*b10d49d7SPino Toscano for key in keys: 123*b10d49d7SPino Toscano md5_keys[key] = subprocess.check_output( 124*b10d49d7SPino Toscano 'echo %s | base64 -d | md5sum -b | cut -d" " -f1' % key, 125*b10d49d7SPino Toscano shell=True).rstrip().decode('ascii') 126*b10d49d7SPino Toscano 127*b10d49d7SPino Toscano sha1_keys[key] = subprocess.check_output( 128*b10d49d7SPino Toscano 'echo %s | base64 -d | sha1sum -b | cut -d" " -f1' % key, 1298eb5e674SMax Reitz shell=True).rstrip().decode('ascii') 13056ea7450SKevin Wolf 13100af1935SKevin Wolf vm.launch() 132*b10d49d7SPino Toscano 133*b10d49d7SPino Toscano # Find correct key first 134*b10d49d7SPino Toscano matching_key = None 135*b10d49d7SPino Toscano for key in keys: 136*b10d49d7SPino Toscano result = vm.qmp('blockdev-add', 137*b10d49d7SPino Toscano driver='ssh', node_name='node0', path=disk_path, 138*b10d49d7SPino Toscano server={ 139*b10d49d7SPino Toscano 'host': '127.0.0.1', 140*b10d49d7SPino Toscano 'port': '22', 141*b10d49d7SPino Toscano }, host_key_check={ 142*b10d49d7SPino Toscano 'mode': 'hash', 143*b10d49d7SPino Toscano 'type': 'md5', 144*b10d49d7SPino Toscano 'hash': md5_keys[key], 145*b10d49d7SPino Toscano }) 146*b10d49d7SPino Toscano 147*b10d49d7SPino Toscano if 'error' not in result: 148*b10d49d7SPino Toscano vm.qmp('blockdev-del', node_name='node0') 149*b10d49d7SPino Toscano matching_key = key 150*b10d49d7SPino Toscano break 151*b10d49d7SPino Toscano 152*b10d49d7SPino Toscano if matching_key is None: 153*b10d49d7SPino Toscano vm.shutdown() 154*b10d49d7SPino Toscano iotests.notrun('Did not find a key that fits 127.0.0.1') 155*b10d49d7SPino Toscano 15600af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 15700af1935SKevin Wolf 'location': { 15800af1935SKevin Wolf 'path': disk_path, 15900af1935SKevin Wolf 'server': { 16000af1935SKevin Wolf 'host': '127.0.0.1', 16100af1935SKevin Wolf 'port': '22' 16256ea7450SKevin Wolf }, 16300af1935SKevin Wolf 'host-key-check': { 16400af1935SKevin Wolf 'mode': 'hash', 16500af1935SKevin Wolf 'type': 'md5', 16600af1935SKevin Wolf 'hash': 'wrong', 16756ea7450SKevin Wolf } 16856ea7450SKevin Wolf }, 16900af1935SKevin Wolf 'size': 2097152 }) 17000af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 17100af1935SKevin Wolf 'location': { 17200af1935SKevin Wolf 'path': disk_path, 17300af1935SKevin Wolf 'server': { 17400af1935SKevin Wolf 'host': '127.0.0.1', 17500af1935SKevin Wolf 'port': '22' 17656ea7450SKevin Wolf }, 17700af1935SKevin Wolf 'host-key-check': { 17800af1935SKevin Wolf 'mode': 'hash', 17900af1935SKevin Wolf 'type': 'md5', 180*b10d49d7SPino Toscano 'hash': md5_keys[matching_key], 18156ea7450SKevin Wolf } 18256ea7450SKevin Wolf }, 18300af1935SKevin Wolf 'size': 8388608 }) 18400af1935SKevin Wolf vm.shutdown() 18556ea7450SKevin Wolf 186b8c1f901SMax Reitz iotests.img_info_log(remote_path) 18756ea7450SKevin Wolf 18800af1935SKevin Wolf vm.launch() 18900af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 19000af1935SKevin Wolf 'location': { 19100af1935SKevin Wolf 'path': disk_path, 19200af1935SKevin Wolf 'server': { 19300af1935SKevin Wolf 'host': '127.0.0.1', 19400af1935SKevin Wolf 'port': '22' 19556ea7450SKevin Wolf }, 19600af1935SKevin Wolf 'host-key-check': { 19700af1935SKevin Wolf 'mode': 'hash', 19800af1935SKevin Wolf 'type': 'sha1', 19900af1935SKevin Wolf 'hash': 'wrong', 20056ea7450SKevin Wolf } 20156ea7450SKevin Wolf }, 20200af1935SKevin Wolf 'size': 2097152 }) 20300af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 20400af1935SKevin Wolf 'location': { 20500af1935SKevin Wolf 'path': disk_path, 20600af1935SKevin Wolf 'server': { 20700af1935SKevin Wolf 'host': '127.0.0.1', 20800af1935SKevin Wolf 'port': '22' 20956ea7450SKevin Wolf }, 21000af1935SKevin Wolf 'host-key-check': { 21100af1935SKevin Wolf 'mode': 'hash', 21200af1935SKevin Wolf 'type': 'sha1', 213*b10d49d7SPino Toscano 'hash': sha1_keys[matching_key], 21456ea7450SKevin Wolf } 21556ea7450SKevin Wolf }, 21600af1935SKevin Wolf 'size': 4194304 }) 21700af1935SKevin Wolf vm.shutdown() 21856ea7450SKevin Wolf 219b8c1f901SMax Reitz iotests.img_info_log(remote_path) 22056ea7450SKevin Wolf 22100af1935SKevin Wolf # 22200af1935SKevin Wolf # Invalid path and user 22300af1935SKevin Wolf # 22400af1935SKevin Wolf iotests.log("=== Invalid path and user ===") 22500af1935SKevin Wolf iotests.log("") 22656ea7450SKevin Wolf 22700af1935SKevin Wolf vm.launch() 22800af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 22900af1935SKevin Wolf 'location': { 23000af1935SKevin Wolf 'path': '/this/is/not/an/existing/path', 23100af1935SKevin Wolf 'server': { 23200af1935SKevin Wolf 'host': '127.0.0.1', 23300af1935SKevin Wolf 'port': '22' 23400af1935SKevin Wolf }, 23500af1935SKevin Wolf 'host-key-check': { 23600af1935SKevin Wolf 'mode': 'none' 23756ea7450SKevin Wolf } 23856ea7450SKevin Wolf }, 23900af1935SKevin Wolf 'size': 4194304 }) 24000af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 24100af1935SKevin Wolf 'location': { 24200af1935SKevin Wolf 'path': disk_path, 24300af1935SKevin Wolf 'user': 'invalid user', 24400af1935SKevin Wolf 'server': { 24500af1935SKevin Wolf 'host': '127.0.0.1', 24600af1935SKevin Wolf 'port': '22' 24700af1935SKevin Wolf }, 24800af1935SKevin Wolf 'host-key-check': { 24900af1935SKevin Wolf 'mode': 'none' 25056ea7450SKevin Wolf } 25156ea7450SKevin Wolf }, 25200af1935SKevin Wolf 'size': 4194304 }) 25300af1935SKevin Wolf vm.shutdown() 254