4 # Test ssh image creation
6 # Copyright (C) 2018 Red Hat, Inc.
8 # Creator/Owner: Kevin Wolf <kwolf@redhat.com>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 iotests.script_initialize(
29 supported_fmts=['raw'],
30 supported_protocols=['ssh'],
33 def filter_hash(qmsg):
34 def _filter(key, value):
35 if key == 'hash' and re.match('[0-9a-f]+', value):
38 if isinstance(qmsg, str):
39 # Strip key type and fingerprint
40 p = r"\S+ (key fingerprint) '(md5|sha1|sha256):[0-9a-f]+'"
41 return re.sub(p, r"\1 '\2:HASH'", qmsg)
43 return iotests.filter_qmp(qmsg, _filter)
45 def blockdev_create(vm, options):
46 vm.blockdev_create(options, filters=[iotests.filter_qmp_testfiles, filter_hash])
48 with iotests.FilePath('t.img') as disk_path, \
51 remote_path = iotests.remote_filename(disk_path)
54 # Successful image creation (defaults)
56 iotests.log("=== Successful image creation (defaults) ===")
60 blockdev_create(vm, { 'driver': 'ssh',
71 iotests.img_info_log(remote_path)
73 iotests.img_info_log(disk_path)
76 # Test host-key-check options
78 iotests.log("=== Test host-key-check options ===")
81 iotests.log("--- no host key checking --")
85 blockdev_create(vm, { 'driver': 'ssh',
99 iotests.img_info_log(remote_path)
101 iotests.log("--- known_hosts key checking --")
105 blockdev_create(vm, { 'driver': 'ssh',
113 'mode': 'known_hosts'
119 iotests.img_info_log(remote_path)
121 keys = subprocess.check_output(
122 'ssh-keyscan 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' +
124 shell=True).rstrip().decode('ascii').split('\n')
126 # Mappings of base64 representations to digests
132 md5_keys[key] = subprocess.check_output(
133 'echo %s | base64 -d | md5sum -b | cut -d" " -f1' % key,
134 shell=True).rstrip().decode('ascii')
136 sha1_keys[key] = subprocess.check_output(
137 'echo %s | base64 -d | sha1sum -b | cut -d" " -f1' % key,
138 shell=True).rstrip().decode('ascii')
140 sha256_keys[key] = subprocess.check_output(
141 'echo %s | base64 -d | sha256sum -b | cut -d" " -f1' % key,
142 shell=True).rstrip().decode('ascii')
146 # Find correct key first
149 result = vm.qmp('blockdev-add',
150 driver='ssh', node_name='node0', path=disk_path,
157 'hash': md5_keys[key],
160 if 'error' not in result:
161 vm.qmp('blockdev-del', node_name='node0')
165 if matching_key is None:
167 iotests.notrun('Did not find a key that fits 127.0.0.1')
169 iotests.log("--- explicit md5 key checking --")
172 blockdev_create(vm, { 'driver': 'ssh',
187 blockdev_create(vm, { 'driver': 'ssh',
197 'hash': md5_keys[matching_key],
203 iotests.img_info_log(remote_path)
205 iotests.log("--- explicit sha1 key checking --")
209 blockdev_create(vm, { 'driver': 'ssh',
223 blockdev_create(vm, { 'driver': 'ssh',
233 'hash': sha1_keys[matching_key],
239 iotests.img_info_log(remote_path)
241 iotests.log("--- explicit sha256 key checking --")
245 blockdev_create(vm, { 'driver': 'ssh',
259 blockdev_create(vm, { 'driver': 'ssh',
269 'hash': sha256_keys[matching_key],
275 iotests.img_info_log(remote_path)
278 # Invalid path and user
280 iotests.log("=== Invalid path and user ===")
284 blockdev_create(vm, { 'driver': 'ssh',
286 'path': '/this/is/not/an/existing/path',
296 blockdev_create(vm, { 'driver': 'ssh',
299 'user': 'invalid user',