1a2cd85f6SMaxim Levitsky#!/usr/bin/env python3 29dd003a9SVladimir Sementsov-Ogievskiy# group: rw 3a2cd85f6SMaxim Levitsky# 4a2cd85f6SMaxim Levitsky# Test case for encryption key management versus image sharing 5a2cd85f6SMaxim Levitsky# 6a2cd85f6SMaxim Levitsky# Copyright (C) 2019 Red Hat, Inc. 7a2cd85f6SMaxim Levitsky# 8a2cd85f6SMaxim Levitsky# This program is free software; you can redistribute it and/or modify 9a2cd85f6SMaxim Levitsky# it under the terms of the GNU General Public License as published by 10a2cd85f6SMaxim Levitsky# the Free Software Foundation; either version 2 of the License, or 11a2cd85f6SMaxim Levitsky# (at your option) any later version. 12a2cd85f6SMaxim Levitsky# 13a2cd85f6SMaxim Levitsky# This program is distributed in the hope that it will be useful, 14a2cd85f6SMaxim Levitsky# but WITHOUT ANY WARRANTY; without even the implied warranty of 15a2cd85f6SMaxim Levitsky# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16a2cd85f6SMaxim Levitsky# GNU General Public License for more details. 17a2cd85f6SMaxim Levitsky# 18a2cd85f6SMaxim Levitsky# You should have received a copy of the GNU General Public License 19a2cd85f6SMaxim Levitsky# along with this program. If not, see <http://www.gnu.org/licenses/>. 20a2cd85f6SMaxim Levitsky# 21a2cd85f6SMaxim Levitsky 22a2cd85f6SMaxim Levitskyimport iotests 23a2cd85f6SMaxim Levitskyimport os 24a2cd85f6SMaxim Levitskyimport time 25a2cd85f6SMaxim Levitskyimport json 26a2cd85f6SMaxim Levitsky 27a2cd85f6SMaxim Levitskytest_img = os.path.join(iotests.test_dir, 'test.img') 28a2cd85f6SMaxim Levitsky 29a2cd85f6SMaxim Levitskyclass Secret: 30a2cd85f6SMaxim Levitsky def __init__(self, index): 31a2cd85f6SMaxim Levitsky self._id = "keysec" + str(index) 32a2cd85f6SMaxim Levitsky # you are not supposed to see the password... 33a2cd85f6SMaxim Levitsky self._secret = "hunter" + str(index) 34a2cd85f6SMaxim Levitsky 35a2cd85f6SMaxim Levitsky def id(self): 36a2cd85f6SMaxim Levitsky return self._id 37a2cd85f6SMaxim Levitsky 38a2cd85f6SMaxim Levitsky def secret(self): 39a2cd85f6SMaxim Levitsky return self._secret 40a2cd85f6SMaxim Levitsky 41a2cd85f6SMaxim Levitsky def to_cmdline_object(self): 42a2cd85f6SMaxim Levitsky return [ "secret,id=" + self._id + ",data=" + self._secret] 43a2cd85f6SMaxim Levitsky 44a2cd85f6SMaxim Levitsky def to_qmp_object(self): 45c5339030SVladimir Sementsov-Ogievskiy return { "qom-type" : "secret", "id": self.id(), 46fa818b2fSAlberto Garcia "data": self.secret() } 47a2cd85f6SMaxim Levitsky 48a2cd85f6SMaxim Levitsky################################################################################ 49a2cd85f6SMaxim Levitsky 50a2cd85f6SMaxim Levitskyclass EncryptionSetupTestCase(iotests.QMPTestCase): 51a2cd85f6SMaxim Levitsky 52a2cd85f6SMaxim Levitsky # test case startup 53a2cd85f6SMaxim Levitsky def setUp(self): 54a2cd85f6SMaxim Levitsky 55a2cd85f6SMaxim Levitsky # start the VMs 56a2cd85f6SMaxim Levitsky self.vm1 = iotests.VM(path_suffix = 'VM1') 57a2cd85f6SMaxim Levitsky self.vm2 = iotests.VM(path_suffix = 'VM2') 58a2cd85f6SMaxim Levitsky self.vm1.launch() 59a2cd85f6SMaxim Levitsky self.vm2.launch() 60a2cd85f6SMaxim Levitsky 61a2cd85f6SMaxim Levitsky # create the secrets and load 'em into the VMs 62a2cd85f6SMaxim Levitsky self.secrets = [ Secret(i) for i in range(0, 4) ] 63a2cd85f6SMaxim Levitsky for secret in self.secrets: 64*b6aed193SVladimir Sementsov-Ogievskiy self.vm1.cmd("object-add", secret.to_qmp_object()) 65*b6aed193SVladimir Sementsov-Ogievskiy self.vm2.cmd("object-add", secret.to_qmp_object()) 66a2cd85f6SMaxim Levitsky 67a2cd85f6SMaxim Levitsky # test case shutdown 68a2cd85f6SMaxim Levitsky def tearDown(self): 69a2cd85f6SMaxim Levitsky # stop the VM 70a2cd85f6SMaxim Levitsky self.vm1.shutdown() 71a2cd85f6SMaxim Levitsky self.vm2.shutdown() 72a2cd85f6SMaxim Levitsky 73a2cd85f6SMaxim Levitsky ########################################################################### 74a2cd85f6SMaxim Levitsky # create the encrypted block device using qemu-img 75a2cd85f6SMaxim Levitsky def createImg(self, file, secret): 76a2cd85f6SMaxim Levitsky 774cf661f2SJohn Snow iotests.qemu_img( 78a2cd85f6SMaxim Levitsky 'create', 79a2cd85f6SMaxim Levitsky '--object', *secret.to_cmdline_object(), 80a2cd85f6SMaxim Levitsky '-f', iotests.imgfmt, 81a2cd85f6SMaxim Levitsky '-o', 'key-secret=' + secret.id(), 82a2cd85f6SMaxim Levitsky '-o', 'iter-time=10', 83a2cd85f6SMaxim Levitsky file, 84a2cd85f6SMaxim Levitsky '1M') 854cf661f2SJohn Snow iotests.log('') 86a2cd85f6SMaxim Levitsky 87a2cd85f6SMaxim Levitsky # attempts to add a key using qemu-img 88a2cd85f6SMaxim Levitsky def addKey(self, file, secret, new_secret): 89a2cd85f6SMaxim Levitsky 90a2cd85f6SMaxim Levitsky image_options = { 91a2cd85f6SMaxim Levitsky 'key-secret' : secret.id(), 92a2cd85f6SMaxim Levitsky 'driver' : iotests.imgfmt, 93a2cd85f6SMaxim Levitsky 'file' : { 94a2cd85f6SMaxim Levitsky 'driver':'file', 95a2cd85f6SMaxim Levitsky 'filename': file, 96a2cd85f6SMaxim Levitsky } 97a2cd85f6SMaxim Levitsky } 98a2cd85f6SMaxim Levitsky 994cf661f2SJohn Snow output = iotests.qemu_img( 100a2cd85f6SMaxim Levitsky 'amend', 101a2cd85f6SMaxim Levitsky '--object', *secret.to_cmdline_object(), 102a2cd85f6SMaxim Levitsky '--object', *new_secret.to_cmdline_object(), 103a2cd85f6SMaxim Levitsky 104a2cd85f6SMaxim Levitsky '-o', 'state=active', 105a2cd85f6SMaxim Levitsky '-o', 'new-secret=' + new_secret.id(), 106a2cd85f6SMaxim Levitsky '-o', 'iter-time=10', 107a2cd85f6SMaxim Levitsky 1084cf661f2SJohn Snow "json:" + json.dumps(image_options), 1094cf661f2SJohn Snow check=False # Expected to fail. Log output. 1104cf661f2SJohn Snow ).stdout 111a2cd85f6SMaxim Levitsky 112a2cd85f6SMaxim Levitsky iotests.log(output, filters=[iotests.filter_test_dir]) 113a2cd85f6SMaxim Levitsky 114a2cd85f6SMaxim Levitsky ########################################################################### 115a2cd85f6SMaxim Levitsky # open an encrypted block device 116a2cd85f6SMaxim Levitsky def openImageQmp(self, vm, id, file, secret, 117a2cd85f6SMaxim Levitsky readOnly = False, reOpen = False): 118a2cd85f6SMaxim Levitsky 119e60edf69SAlberto Garcia command = 'blockdev-reopen' if reOpen else 'blockdev-add' 120a2cd85f6SMaxim Levitsky 1213908b7a8SAlberto Garcia opts = { 122a2cd85f6SMaxim Levitsky 'driver': iotests.imgfmt, 123a2cd85f6SMaxim Levitsky 'node-name': id, 124a2cd85f6SMaxim Levitsky 'read-only': readOnly, 125a2cd85f6SMaxim Levitsky 'key-secret' : secret.id(), 126a2cd85f6SMaxim Levitsky 'file': { 127a2cd85f6SMaxim Levitsky 'driver': 'file', 128a2cd85f6SMaxim Levitsky 'filename': test_img, 129a2cd85f6SMaxim Levitsky } 130a2cd85f6SMaxim Levitsky } 1313908b7a8SAlberto Garcia 1323908b7a8SAlberto Garcia if reOpen: 133*b6aed193SVladimir Sementsov-Ogievskiy vm.cmd(command, options=[opts]) 1343908b7a8SAlberto Garcia else: 135*b6aed193SVladimir Sementsov-Ogievskiy vm.cmd(command, opts) 136a2cd85f6SMaxim Levitsky 1370fca43deSMaxim Levitsky 1380fca43deSMaxim Levitsky ########################################################################### 1390fca43deSMaxim Levitsky # add virtio-blk consumer for a block device 1400fca43deSMaxim Levitsky def addImageUser(self, vm, id, disk_id, share_rw=False): 141c5339030SVladimir Sementsov-Ogievskiy result = vm.qmp('device_add', { 1420fca43deSMaxim Levitsky 'driver': 'virtio-blk', 1430fca43deSMaxim Levitsky 'id': id, 1440fca43deSMaxim Levitsky 'drive': disk_id, 1450fca43deSMaxim Levitsky 'share-rw' : share_rw 1460fca43deSMaxim Levitsky } 1470fca43deSMaxim Levitsky ) 1480fca43deSMaxim Levitsky 1490fca43deSMaxim Levitsky iotests.log(result) 1500fca43deSMaxim Levitsky 151a2cd85f6SMaxim Levitsky # close the encrypted block device 152a2cd85f6SMaxim Levitsky def closeImageQmp(self, vm, id): 153*b6aed193SVladimir Sementsov-Ogievskiy vm.cmd('blockdev-del', {'node-name': id}) 154a2cd85f6SMaxim Levitsky 155a2cd85f6SMaxim Levitsky ########################################################################### 156a2cd85f6SMaxim Levitsky 157a2cd85f6SMaxim Levitsky # add a key to an encrypted block device 158a2cd85f6SMaxim Levitsky def addKeyQmp(self, vm, id, new_secret): 159a2cd85f6SMaxim Levitsky 160a2cd85f6SMaxim Levitsky args = { 161a2cd85f6SMaxim Levitsky 'node-name': id, 162a2cd85f6SMaxim Levitsky 'job-id' : 'job0', 163a2cd85f6SMaxim Levitsky 'options' : { 164a2cd85f6SMaxim Levitsky 'state' : 'active', 165a2cd85f6SMaxim Levitsky 'driver' : iotests.imgfmt, 166a2cd85f6SMaxim Levitsky 'new-secret': new_secret.id(), 167a2cd85f6SMaxim Levitsky 'iter-time' : 10 168a2cd85f6SMaxim Levitsky }, 169a2cd85f6SMaxim Levitsky } 170a2cd85f6SMaxim Levitsky 171c5339030SVladimir Sementsov-Ogievskiy result = vm.qmp('x-blockdev-amend', args) 172c1019d16SEmanuele Giuseppe Esposito iotests.log(result) 173c1019d16SEmanuele Giuseppe Esposito # Run the job only if it was created 174c1019d16SEmanuele Giuseppe Esposito event = ('JOB_STATUS_CHANGE', 175c1019d16SEmanuele Giuseppe Esposito {'data': {'id': 'job0', 'status': 'created'}}) 176c1019d16SEmanuele Giuseppe Esposito if vm.events_wait([event], timeout=0.0) is not None: 177a2cd85f6SMaxim Levitsky vm.run_job('job0') 178a2cd85f6SMaxim Levitsky 179a2cd85f6SMaxim Levitsky # test that when the image opened by two qemu processes, 1800fca43deSMaxim Levitsky # neither of them can update the encryption keys 181a2cd85f6SMaxim Levitsky def test1(self): 182a2cd85f6SMaxim Levitsky self.createImg(test_img, self.secrets[0]); 183a2cd85f6SMaxim Levitsky 184a2cd85f6SMaxim Levitsky # VM1 opens the image and adds a key 185a2cd85f6SMaxim Levitsky self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0]) 186a2cd85f6SMaxim Levitsky self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[1]) 187a2cd85f6SMaxim Levitsky 188a2cd85f6SMaxim Levitsky 189a2cd85f6SMaxim Levitsky # VM2 opens the image 190a2cd85f6SMaxim Levitsky self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0]) 191a2cd85f6SMaxim Levitsky 192a2cd85f6SMaxim Levitsky 193a2cd85f6SMaxim Levitsky # neither VMs now should be able to add a key 194a2cd85f6SMaxim Levitsky self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2]) 195a2cd85f6SMaxim Levitsky self.addKeyQmp(self.vm2, "testdev", new_secret = self.secrets[2]) 196a2cd85f6SMaxim Levitsky 197a2cd85f6SMaxim Levitsky 198a2cd85f6SMaxim Levitsky # VM 1 closes the image 199a2cd85f6SMaxim Levitsky self.closeImageQmp(self.vm1, "testdev") 200a2cd85f6SMaxim Levitsky 201a2cd85f6SMaxim Levitsky 202a2cd85f6SMaxim Levitsky # now VM2 can add the key 203a2cd85f6SMaxim Levitsky self.addKeyQmp(self.vm2, "testdev", new_secret = self.secrets[2]) 204a2cd85f6SMaxim Levitsky 205a2cd85f6SMaxim Levitsky 206a2cd85f6SMaxim Levitsky # qemu-img should also not be able to add a key 207a2cd85f6SMaxim Levitsky self.addKey(test_img, self.secrets[0], self.secrets[2]) 208a2cd85f6SMaxim Levitsky 209a2cd85f6SMaxim Levitsky # cleanup 210a2cd85f6SMaxim Levitsky self.closeImageQmp(self.vm2, "testdev") 211a2cd85f6SMaxim Levitsky os.remove(test_img) 212a2cd85f6SMaxim Levitsky 213a2cd85f6SMaxim Levitsky 2140fca43deSMaxim Levitsky # test that when the image opened by two qemu processes, 2150fca43deSMaxim Levitsky # even if first VM opens it read-only, the second can't update encryption 2160fca43deSMaxim Levitsky # keys 217a2cd85f6SMaxim Levitsky def test2(self): 218a2cd85f6SMaxim Levitsky self.createImg(test_img, self.secrets[0]); 219a2cd85f6SMaxim Levitsky 220a2cd85f6SMaxim Levitsky # VM1 opens the image readonly 221a2cd85f6SMaxim Levitsky self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0], 222a2cd85f6SMaxim Levitsky readOnly = True) 223a2cd85f6SMaxim Levitsky 224a2cd85f6SMaxim Levitsky # VM2 opens the image 225a2cd85f6SMaxim Levitsky self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0]) 226a2cd85f6SMaxim Levitsky 227a2cd85f6SMaxim Levitsky # VM1 can't add a key since image is readonly 228a2cd85f6SMaxim Levitsky self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2]) 229a2cd85f6SMaxim Levitsky 230a2cd85f6SMaxim Levitsky # VM2 can't add a key since VM is has the image opened 231a2cd85f6SMaxim Levitsky self.addKeyQmp(self.vm2, "testdev", new_secret = self.secrets[2]) 232a2cd85f6SMaxim Levitsky 233a2cd85f6SMaxim Levitsky 234a2cd85f6SMaxim Levitsky #VM1 reopens the image read-write 235a2cd85f6SMaxim Levitsky self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0], 236a2cd85f6SMaxim Levitsky reOpen = True, readOnly = False) 237a2cd85f6SMaxim Levitsky 238a2cd85f6SMaxim Levitsky # VM1 still can't add the key 239a2cd85f6SMaxim Levitsky self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2]) 240a2cd85f6SMaxim Levitsky 241a2cd85f6SMaxim Levitsky # VM2 gets away 242a2cd85f6SMaxim Levitsky self.closeImageQmp(self.vm2, "testdev") 243a2cd85f6SMaxim Levitsky 244a2cd85f6SMaxim Levitsky # VM1 now can add the key 245a2cd85f6SMaxim Levitsky self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2]) 246a2cd85f6SMaxim Levitsky 247a2cd85f6SMaxim Levitsky self.closeImageQmp(self.vm1, "testdev") 248a2cd85f6SMaxim Levitsky os.remove(test_img) 249a2cd85f6SMaxim Levitsky 2500fca43deSMaxim Levitsky # test that two VMs can't open the same luks image by default 2510fca43deSMaxim Levitsky # and attach it to a guest device 2520fca43deSMaxim Levitsky def test3(self): 2530fca43deSMaxim Levitsky self.createImg(test_img, self.secrets[0]); 2540fca43deSMaxim Levitsky 2550fca43deSMaxim Levitsky self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0]) 2560fca43deSMaxim Levitsky self.addImageUser(self.vm1, "testctrl", "testdev") 2570fca43deSMaxim Levitsky 2580fca43deSMaxim Levitsky self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0]) 2590fca43deSMaxim Levitsky self.addImageUser(self.vm2, "testctrl", "testdev") 2600fca43deSMaxim Levitsky 2610fca43deSMaxim Levitsky 2620fca43deSMaxim Levitsky # test that two VMs can attach the same luks image to a guest device, 2630fca43deSMaxim Levitsky # if both use share-rw=on 2640fca43deSMaxim Levitsky def test4(self): 2650fca43deSMaxim Levitsky self.createImg(test_img, self.secrets[0]); 2660fca43deSMaxim Levitsky 2670fca43deSMaxim Levitsky self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0]) 2680fca43deSMaxim Levitsky self.addImageUser(self.vm1, "testctrl", "testdev", share_rw=True) 2690fca43deSMaxim Levitsky 2700fca43deSMaxim Levitsky self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0]) 2710fca43deSMaxim Levitsky self.addImageUser(self.vm2, "testctrl", "testdev", share_rw=True) 2720fca43deSMaxim Levitsky 2730fca43deSMaxim Levitsky 274a2cd85f6SMaxim Levitsky 275a2cd85f6SMaxim Levitskyif __name__ == '__main__': 276a2cd85f6SMaxim Levitsky # support only raw luks since luks encrypted qcow2 is a proper 277a2cd85f6SMaxim Levitsky # format driver which doesn't allow any sharing 278a2cd85f6SMaxim Levitsky iotests.activate_logging() 279a2cd85f6SMaxim Levitsky iotests.main(supported_fmts = ['luks']) 280