aboutsummaryrefslogtreecommitdiff
path: root/tests/qemu-iotests/296
blob: 0bc3c6c7d72a809ad3bcf99163a0f8f2553f2155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python3
# group: rw
#
# Test case for encryption key management versus image sharing
#
# Copyright (C) 2019 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import iotests
import os
import time
import json

test_img = os.path.join(iotests.test_dir, 'test.img')

class Secret:
    def __init__(self, index):
        self._id = "keysec" + str(index)
        # you are not supposed to see the password...
        self._secret = "hunter" + str(index)

    def id(self):
        return self._id

    def secret(self):
        return self._secret

    def to_cmdline_object(self):
        return  [ "secret,id=" + self._id + ",data=" + self._secret]

    def to_qmp_object(self):
        return { "qom_type" : "secret", "id": self.id(),
                 "props": { "data": self.secret() } }

################################################################################

class EncryptionSetupTestCase(iotests.QMPTestCase):

    # test case startup
    def setUp(self):

        # start the VMs
        self.vm1 = iotests.VM(path_suffix = 'VM1')
        self.vm2 = iotests.VM(path_suffix = 'VM2')
        self.vm1.launch()
        self.vm2.launch()

        # create the secrets and load 'em into the VMs
        self.secrets = [ Secret(i) for i in range(0, 4) ]
        for secret in self.secrets:
            result = self.vm1.qmp("object-add", **secret.to_qmp_object())
            self.assert_qmp(result, 'return', {})
            result = self.vm2.qmp("object-add", **secret.to_qmp_object())
            self.assert_qmp(result, 'return', {})

    # test case shutdown
    def tearDown(self):
        # stop the VM
        self.vm1.shutdown()
        self.vm2.shutdown()

    ###########################################################################
    # create the encrypted block device using qemu-img
    def createImg(self, file, secret):

        output = iotests.qemu_img_pipe(
            'create',
            '--object', *secret.to_cmdline_object(),
            '-f', iotests.imgfmt,
            '-o', 'key-secret=' + secret.id(),
            '-o', 'iter-time=10',
            file,
            '1M')

        iotests.log(output, filters=[iotests.filter_test_dir])

    # attempts to add a key using qemu-img
    def addKey(self, file, secret, new_secret):

        image_options = {
            'key-secret' : secret.id(),
            'driver' : iotests.imgfmt,
            'file' : {
                'driver':'file',
                'filename': file,
                }
            }

        output = iotests.qemu_img_pipe(
            'amend',
            '--object', *secret.to_cmdline_object(),
            '--object', *new_secret.to_cmdline_object(),

            '-o', 'state=active',
            '-o', 'new-secret=' + new_secret.id(),
            '-o', 'iter-time=10',

            "json:" + json.dumps(image_options)
            )

        iotests.log(output, filters=[iotests.filter_test_dir])

    ###########################################################################
    # open an encrypted block device
    def openImageQmp(self, vm, id, file, secret,
                     readOnly = False, reOpen = False):

        command = 'x-blockdev-reopen' if reOpen else 'blockdev-add'

        result = vm.qmp(command, **
            {
                'driver': iotests.imgfmt,
                'node-name': id,
                'read-only': readOnly,
                'key-secret' : secret.id(),
                'file': {
                    'driver': 'file',
                    'filename': test_img,
                }
            }
        )
        self.assert_qmp(result, 'return', {})


    ###########################################################################
    # add virtio-blk consumer for a block device
    def addImageUser(self, vm, id, disk_id, share_rw=False):
        result = vm.qmp('device_add', **
            {
                'driver': 'virtio-blk',
                'id': id,
                'drive': disk_id,
                'share-rw' : share_rw
            }
        )

        iotests.log(result)

    # close the encrypted block device
    def closeImageQmp(self, vm, id):
        result = vm.qmp('blockdev-del', **{ 'node-name': id })
        self.assert_qmp(result, 'return', {})

    ###########################################################################

    # add a key to an encrypted block device
    def addKeyQmp(self, vm, id, new_secret):

        args = {
            'node-name': id,
            'job-id' : 'job0',
            'options' : {
                'state'     : 'active',
                'driver'    : iotests.imgfmt,
                'new-secret': new_secret.id(),
                'iter-time' : 10
            },
        }

        result = vm.qmp('x-blockdev-amend', **args)
        assert result['return'] == {}
        vm.run_job('job0')

    # test that when the image opened by two qemu processes,
    # neither of them can update the encryption keys
    def test1(self):
        self.createImg(test_img, self.secrets[0]);

        # VM1 opens the image and adds a key
        self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0])
        self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[1])


        # VM2 opens the image
        self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0])


        # neither VMs now should be able to add a key
        self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2])
        self.addKeyQmp(self.vm2, "testdev", new_secret = self.secrets[2])


        # VM 1 closes the image
        self.closeImageQmp(self.vm1, "testdev")


        # now VM2 can add the key
        self.addKeyQmp(self.vm2, "testdev", new_secret = self.secrets[2])


        # qemu-img should also not be able to add a key
        self.addKey(test_img, self.secrets[0], self.secrets[2])

        # cleanup
        self.closeImageQmp(self.vm2, "testdev")
        os.remove(test_img)


    # test that when the image opened by two qemu processes,
    # even if first VM opens it read-only, the second can't update encryption
    # keys
    def test2(self):
        self.createImg(test_img, self.secrets[0]);

        # VM1 opens the image readonly
        self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0],
                          readOnly = True)

        # VM2 opens the image
        self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0])

        # VM1 can't add a key since image is readonly
        self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2])

        # VM2 can't add a key since VM is has the image opened
        self.addKeyQmp(self.vm2, "testdev", new_secret = self.secrets[2])


        #VM1 reopens the image read-write
        self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0],
                          reOpen = True, readOnly = False)

        # VM1 still can't add the key
        self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2])

        # VM2 gets away
        self.closeImageQmp(self.vm2, "testdev")

        # VM1 now can add the key
        self.addKeyQmp(self.vm1, "testdev", new_secret = self.secrets[2])

        self.closeImageQmp(self.vm1, "testdev")
        os.remove(test_img)

    # test that two VMs can't open the same luks image by default
    # and attach it to a guest device
    def test3(self):
        self.createImg(test_img, self.secrets[0]);

        self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0])
        self.addImageUser(self.vm1, "testctrl", "testdev")

        self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0])
        self.addImageUser(self.vm2, "testctrl", "testdev")


    # test that two VMs can attach the same luks image to a guest device,
    # if both use share-rw=on
    def test4(self):
        self.createImg(test_img, self.secrets[0]);

        self.openImageQmp(self.vm1, "testdev", test_img, self.secrets[0])
        self.addImageUser(self.vm1, "testctrl", "testdev", share_rw=True)

        self.openImageQmp(self.vm2, "testdev", test_img, self.secrets[0])
        self.addImageUser(self.vm2, "testctrl", "testdev", share_rw=True)



if __name__ == '__main__':
    # support only raw luks since luks encrypted qcow2 is a proper
    # format driver which doesn't allow any sharing
    iotests.activate_logging()
    iotests.main(supported_fmts = ['luks'])