3 # Test case for encryption key management versus image sharing
5 # Copyright (C) 2019 Red Hat, Inc.
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.
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.
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/>.
26 test_img = os.path.join(iotests.test_dir, 'test.img')
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)
40 def to_cmdline_object(self):
41 return [ "secret,id=" + self._id + ",data=" + self._secret]
43 def to_qmp_object(self):
44 return { "qom_type" : "secret", "id": self.id(),
45 "props": { "data": self.secret() } }
47 ################################################################################
49 class EncryptionSetupTestCase(iotests.QMPTestCase):
55 self.vm1 = iotests.VM(path_suffix = 'VM1')
56 self.vm2 = iotests.VM(path_suffix = 'VM2')
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', {})
74 ###########################################################################
75 # create the encrypted block device using qemu-img
76 def createImg(self, file, secret):
78 output = iotests.qemu_img_pipe(
80 '--object', *secret.to_cmdline_object(),
82 '-o', 'key-secret=' + secret.id(),
87 iotests.log(output, filters=[iotests.filter_test_dir])
89 # attempts to add a key using qemu-img
90 def addKey(self, file, secret, new_secret):
93 'key-secret' : secret.id(),
94 'driver' : iotests.imgfmt,
101 output = iotests.qemu_img_pipe(
103 '--object', *secret.to_cmdline_object(),
104 '--object', *new_secret.to_cmdline_object(),
106 '-o', 'state=active',
107 '-o', 'new-secret=' + new_secret.id(),
108 '-o', 'iter-time=10',
110 "json:" + json.dumps(image_options)
113 iotests.log(output, filters=[iotests.filter_test_dir])
115 ###########################################################################
116 # open an encrypted block device
117 def openImageQmp(self, vm, id, file, secret,
118 readOnly = False, reOpen = False):
120 command = 'x-blockdev-reopen' if reOpen else 'blockdev-add'
122 result = vm.qmp(command, **
124 'driver': iotests.imgfmt,
126 'read-only': readOnly,
127 'key-secret' : secret.id(),
130 'filename': test_img,
134 self.assert_qmp(result, 'return', {})
137 ###########################################################################
138 # add virtio-blk consumer for a block device
139 def addImageUser(self, vm, id, disk_id, share_rw=False):
140 result = vm.qmp('device_add', **
142 'driver': 'virtio-blk',
145 'share-rw' : share_rw
151 # close the encrypted block device
152 def closeImageQmp(self, vm, id):
153 result = vm.qmp('blockdev-del', **{ 'node-name': id })
154 self.assert_qmp(result, 'return', {})
156 ###########################################################################
158 # add a key to an encrypted block device
159 def addKeyQmp(self, vm, id, new_secret):
166 'driver' : iotests.imgfmt,
167 'new-secret': new_secret.id(),
172 result = vm.qmp('x-blockdev-amend', **args)
173 assert result['return'] == {}
176 # test that when the image opened by two qemu processes,
177 # neither of them can update the encryption keys
179 self.createImg(test_img, self.secrets[0]);
181 # VM1 opens the image and adds a key
182 self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0])
183 self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[1])
186 # VM2 opens the image
187 self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0])
190 # neither VMs now should be able to add a key
191 self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2])
192 self.addKeyQmp(self.vm2, "testdev", new_secret = self.secrets[2])
195 # VM 1 closes the image
196 self.closeImageQmp(self.vm1, "testdev")
199 # now VM2 can add the key
200 self.addKeyQmp(self.vm2, "testdev", new_secret = self.secrets[2])
203 # qemu-img should also not be able to add a key
204 self.addKey(test_img, self.secrets[0], self.secrets[2])
207 self.closeImageQmp(self.vm2, "testdev")
211 # test that when the image opened by two qemu processes,
212 # even if first VM opens it read-only, the second can't update encryption
215 self.createImg(test_img, self.secrets[0]);
217 # VM1 opens the image readonly
218 self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0],
221 # VM2 opens the image
222 self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0])
224 # VM1 can't add a key since image is readonly
225 self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2])
227 # VM2 can't add a key since VM is has the image opened
228 self.addKeyQmp(self.vm2, "testdev", new_secret = self.secrets[2])
231 #VM1 reopens the image read-write
232 self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0],
233 reOpen = True, readOnly = False)
235 # VM1 still can't add the key
236 self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2])
239 self.closeImageQmp(self.vm2, "testdev")
241 # VM1 now can add the key
242 self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2])
244 self.closeImageQmp(self.vm1, "testdev")
247 # test that two VMs can't open the same luks image by default
248 # and attach it to a guest device
250 self.createImg(test_img, self.secrets[0]);
252 self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0])
253 self.addImageUser(self.vm1, "testctrl", "testdev")
255 self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0])
256 self.addImageUser(self.vm2, "testctrl", "testdev")
259 # test that two VMs can attach the same luks image to a guest device,
260 # if both use share-rw=on
262 self.createImg(test_img, self.secrets[0]);
264 self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0])
265 self.addImageUser(self.vm1, "testctrl", "testdev", share_rw=True)
267 self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0])
268 self.addImageUser(self.vm2, "testctrl", "testdev", share_rw=True)
272 if __name__ == '__main__':
273 # support only raw luks since luks encrypted qcow2 is a proper
274 # format driver which doesn't allow any sharing
275 iotests.activate_logging()
276 iotests.main(supported_fmts = ['luks'])