summaryrefslogtreecommitdiff
path: root/tests/qemu-iotests/295
blob: 270ad3999f2c7272b719ad754918db016bfba4e9 (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
278
279
280
281
#!/usr/bin/env python3
# group: rw
#
# Test case QMP's encrypted key management
#
# 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(),
                 "data": self.secret() }

################################################################################
class EncryptionSetupTestCase(iotests.QMPTestCase):

    # test case startup
    def setUp(self):
        # start the VM
        self.vm = iotests.VM()
        self.vm.launch()

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

        if iotests.imgfmt == "qcow2":
            self.pfx = "encrypt."
            self.img_opts = [ '-o', "encrypt.format=luks" ]
        else:
            self.pfx = ""
            self.img_opts = []

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

    ###########################################################################
    # create the encrypted block device
    def createImg(self, file, secret):

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

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

        encrypt_options = {
            'key-secret' : secret.id()
        }

        if iotests.imgfmt == "qcow2":
            encrypt_options = {
                'encrypt': {
                    'format':'luks',
                    **encrypt_options
                }
            }

        result = self.vm.qmp('blockdev-add', **
            {
                'driver': iotests.imgfmt,
                'node-name': id,
                'read-only': read_only,

                **encrypt_options,

                'file': {
                    'driver': 'file',
                    'filename': test_img,
                }
            }
        )
        self.assert_qmp(result, 'return', {})

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

    ###########################################################################
    # add a key to an encrypted block device
    def addKeyQmp(self, id, new_secret, secret = None,
                  slot = None, force = False):

        crypt_options = {
            'state'      : 'active',
            'new-secret' : new_secret.id(),
            'iter-time' : 10
        }

        if slot != None:
            crypt_options['keyslot'] = slot


        if secret != None:
            crypt_options['secret'] = secret.id()

        if iotests.imgfmt == "qcow2":
            crypt_options['format'] = 'luks'
            crypt_options = {
                'encrypt': crypt_options
            }

        args = {
            'node-name': id,
            'job-id' : 'job_add_key',
            'options' : {
                    'driver' : iotests.imgfmt,
                    **crypt_options
                },
        }

        if force == True:
            args['force'] = True

        #TODO: check what jobs return
        result = self.vm.qmp('x-blockdev-amend', **args)
        assert result['return'] == {}
        self.vm.run_job('job_add_key')

    # erase a key from an encrypted block device
    def eraseKeyQmp(self, id, old_secret = None, slot = None, force = False):

        crypt_options = {
            'state'      : 'inactive',
        }

        if slot != None:
            crypt_options['keyslot'] = slot
        if old_secret != None:
            crypt_options['old-secret'] = old_secret.id()

        if iotests.imgfmt == "qcow2":
            crypt_options['format'] = 'luks'
            crypt_options = {
                'encrypt': crypt_options
            }

        args = {
            'node-name': id,
            'job-id' : 'job_erase_key',
            'options' : {
                    'driver' : iotests.imgfmt,
                    **crypt_options
                },
        }

        if force == True:
            args['force'] = True

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

    ###########################################################################
    # create image, and change its key
    def testChangeKey(self):

        # create the image with secret0 and open it
        self.createImg(test_img, self.secrets[0]);
        self.openImageQmp("testdev", test_img, self.secrets[0])

        # add key to slot 1
        self.addKeyQmp("testdev", new_secret = self.secrets[1])

        # add key to slot 5
        self.addKeyQmp("testdev", new_secret = self.secrets[2], slot=5)

        # erase key from slot 0
        self.eraseKeyQmp("testdev", old_secret = self.secrets[0])

        #reopen the image with secret1
        self.closeImageQmp("testdev")
        self.openImageQmp("testdev", test_img, self.secrets[1])

        # close and erase the image for good
        self.closeImageQmp("testdev")
        os.remove(test_img)

    # test that if we erase the old password,
    # we can still change the encryption keys using 'old-secret'
    def testOldPassword(self):

        # create the image with secret0 and open it
        self.createImg(test_img, self.secrets[0]);
        self.openImageQmp("testdev", test_img, self.secrets[0])

        # add key to slot 1
        self.addKeyQmp("testdev", new_secret = self.secrets[1])

        # erase key from slot 0
        self.eraseKeyQmp("testdev", old_secret = self.secrets[0])

        # this will fail as the old password is no longer valid
        self.addKeyQmp("testdev", new_secret = self.secrets[2])

        # this will work
        self.addKeyQmp("testdev", new_secret = self.secrets[2], secret = self.secrets[1])

        # close and erase the image for good
        self.closeImageQmp("testdev")
        os.remove(test_img)

    def testUseForceLuke(self):

        self.createImg(test_img, self.secrets[0]);
        self.openImageQmp("testdev", test_img, self.secrets[0])

        # Add bunch of secrets
        self.addKeyQmp("testdev", new_secret = self.secrets[1], slot=4)
        self.addKeyQmp("testdev", new_secret = self.secrets[4], slot=2)

        # overwrite an active secret
        self.addKeyQmp("testdev", new_secret = self.secrets[5], slot=2)
        self.addKeyQmp("testdev", new_secret = self.secrets[5], slot=2, force=True)

        self.addKeyQmp("testdev", new_secret = self.secrets[0])

        # Now erase all the secrets
        self.eraseKeyQmp("testdev", old_secret = self.secrets[5])
        self.eraseKeyQmp("testdev", slot=4)

        # erase last keyslot
        self.eraseKeyQmp("testdev", old_secret = self.secrets[0])
        self.eraseKeyQmp("testdev", old_secret = self.secrets[0], force=True)

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


if __name__ == '__main__':
    iotests.verify_working_luks()
    # Encrypted formats support
    iotests.activate_logging()
    iotests.main(supported_fmts = ['qcow2', 'luks'])