1#!/usr/bin/env python3 2# 3# Test case for encryption key management versus image sharing 4# 5# Copyright (C) 2019 Red Hat, Inc. 6# 7# This program is free software; you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation; either version 2 of the License, or 10# (at your option) any later version. 11# 12# This program is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with this program. If not, see <http://www.gnu.org/licenses/>. 19# 20 21import iotests 22import os 23import time 24import json 25 26test_img = os.path.join(iotests.test_dir, 'test.img') 27 28class Secret: 29 def __init__(self, index): 30 self._id = "keysec" + str(index) 31 # you are not supposed to see the password... 32 self._secret = "hunter" + str(index) 33 34 def id(self): 35 return self._id 36 37 def secret(self): 38 return self._secret 39 40 def to_cmdline_object(self): 41 return [ "secret,id=" + self._id + ",data=" + self._secret] 42 43 def to_qmp_object(self): 44 return { "qom_type" : "secret", "id": self.id(), 45 "props": { "data": self.secret() } } 46 47################################################################################ 48 49class EncryptionSetupTestCase(iotests.QMPTestCase): 50 51 # test case startup 52 def setUp(self): 53 54 # start the VMs 55 self.vm1 = iotests.VM(path_suffix = 'VM1') 56 self.vm2 = iotests.VM(path_suffix = 'VM2') 57 self.vm1.launch() 58 self.vm2.launch() 59 60 # create the secrets and load 'em into the VMs 61 self.secrets = [ Secret(i) for i in range(0, 4) ] 62 for secret in self.secrets: 63 result = self.vm1.qmp("object-add", **secret.to_qmp_object()) 64 self.assert_qmp(result, 'return', {}) 65 result = self.vm2.qmp("object-add", **secret.to_qmp_object()) 66 self.assert_qmp(result, 'return', {}) 67 68 # test case shutdown 69 def tearDown(self): 70 # stop the VM 71 self.vm1.shutdown() 72 self.vm2.shutdown() 73 74 ########################################################################### 75 # create the encrypted block device using qemu-img 76 def createImg(self, file, secret): 77 78 output = iotests.qemu_img_pipe( 79 'create', 80 '--object', *secret.to_cmdline_object(), 81 '-f', iotests.imgfmt, 82 '-o', 'key-secret=' + secret.id(), 83 '-o', 'iter-time=10', 84 file, 85 '1M') 86 87 iotests.log(output, filters=[iotests.filter_test_dir]) 88 89 # attempts to add a key using qemu-img 90 def addKey(self, file, secret, new_secret): 91 92 image_options = { 93 'key-secret' : secret.id(), 94 'driver' : iotests.imgfmt, 95 'file' : { 96 'driver':'file', 97 'filename': file, 98 } 99 } 100 101 output = iotests.qemu_img_pipe( 102 'amend', 103 '--object', *secret.to_cmdline_object(), 104 '--object', *new_secret.to_cmdline_object(), 105 106 '-o', 'state=active', 107 '-o', 'new-secret=' + new_secret.id(), 108 '-o', 'iter-time=10', 109 110 "json:" + json.dumps(image_options) 111 ) 112 113 iotests.log(output, filters=[iotests.filter_test_dir]) 114 115 ########################################################################### 116 # open an encrypted block device 117 def openImageQmp(self, vm, id, file, secret, 118 readOnly = False, reOpen = False): 119 120 command = 'x-blockdev-reopen' if reOpen else 'blockdev-add' 121 122 result = vm.qmp(command, ** 123 { 124 'driver': iotests.imgfmt, 125 'node-name': id, 126 'read-only': readOnly, 127 'key-secret' : secret.id(), 128 'file': { 129 'driver': 'file', 130 'filename': test_img, 131 } 132 } 133 ) 134 self.assert_qmp(result, 'return', {}) 135 136 # close the encrypted block device 137 def closeImageQmp(self, vm, id): 138 result = vm.qmp('blockdev-del', **{ 'node-name': id }) 139 self.assert_qmp(result, 'return', {}) 140 141 ########################################################################### 142 143 # add a key to an encrypted block device 144 def addKeyQmp(self, vm, id, new_secret): 145 146 args = { 147 'node-name': id, 148 'job-id' : 'job0', 149 'options' : { 150 'state' : 'active', 151 'driver' : iotests.imgfmt, 152 'new-secret': new_secret.id(), 153 'iter-time' : 10 154 }, 155 } 156 157 result = vm.qmp('x-blockdev-amend', **args) 158 assert result['return'] == {} 159 vm.run_job('job0') 160 161 # test that when the image opened by two qemu processes, 162 # neither of them can update the image 163 def test1(self): 164 self.createImg(test_img, self.secrets[0]); 165 166 # VM1 opens the image and adds a key 167 self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0]) 168 self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[1]) 169 170 171 # VM2 opens the image 172 self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0]) 173 174 175 # neither VMs now should be able to add a key 176 self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2]) 177 self.addKeyQmp(self.vm2, "testdev", new_secret = self.secrets[2]) 178 179 180 # VM 1 closes the image 181 self.closeImageQmp(self.vm1, "testdev") 182 183 184 # now VM2 can add the key 185 self.addKeyQmp(self.vm2, "testdev", new_secret = self.secrets[2]) 186 187 188 # qemu-img should also not be able to add a key 189 self.addKey(test_img, self.secrets[0], self.secrets[2]) 190 191 # cleanup 192 self.closeImageQmp(self.vm2, "testdev") 193 os.remove(test_img) 194 195 196 def test2(self): 197 self.createImg(test_img, self.secrets[0]); 198 199 # VM1 opens the image readonly 200 self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0], 201 readOnly = True) 202 203 # VM2 opens the image 204 self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0]) 205 206 # VM1 can't add a key since image is readonly 207 self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2]) 208 209 # VM2 can't add a key since VM is has the image opened 210 self.addKeyQmp(self.vm2, "testdev", new_secret = self.secrets[2]) 211 212 213 #VM1 reopens the image read-write 214 self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0], 215 reOpen = True, readOnly = False) 216 217 # VM1 still can't add the key 218 self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2]) 219 220 # VM2 gets away 221 self.closeImageQmp(self.vm2, "testdev") 222 223 # VM1 now can add the key 224 self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2]) 225 226 self.closeImageQmp(self.vm1, "testdev") 227 os.remove(test_img) 228 229 230if __name__ == '__main__': 231 # support only raw luks since luks encrypted qcow2 is a proper 232 # format driver which doesn't allow any sharing 233 iotests.activate_logging() 234 iotests.main(supported_fmts = ['luks']) 235