File: test_queue_encryption.py

package info (click to toggle)
python-azure-storage 20181109%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 76,472 kB
  • sloc: python: 28,724; makefile: 204; sh: 1
file content (482 lines) | stat: -rw-r--r-- 17,254 bytes parent folder | download
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import unittest
from base64 import (
    b64decode,
)
from json import (
    loads,
    dumps,
)

from azure.common import (
    AzureHttpError,
    AzureException,
)
from cryptography.hazmat import backends
from cryptography.hazmat.primitives.ciphers import Cipher
from cryptography.hazmat.primitives.ciphers.algorithms import AES
from cryptography.hazmat.primitives.ciphers.modes import CBC
from cryptography.hazmat.primitives.padding import PKCS7

from azure.storage.common._common_conversion import _decode_base64_to_bytes
from azure.storage.common._constants import __version__
from azure.storage.common._encryption import (
    _WrappedContentKey,
    _EncryptionAgent,
    _EncryptionData,
)
from azure.storage.common._error import (
    _ERROR_OBJECT_INVALID,
    _ERROR_DECRYPTION_FAILURE,
    _ERROR_ENCRYPTION_REQUIRED,
)
from azure.storage.queue._error import (
    _ERROR_MESSAGE_NOT_ENCRYPTED,
)
from azure.storage.queue import (
    QueueService,
)
from azure.storage.queue.models import QueueMessageFormat
from tests.encryption_test_helper import (
    KeyWrapper,
    KeyResolver,
    RSAKeyWrapper,
)
from tests.testcase import (
    StorageTestCase,
    record,
    TestMode,
)

# ------------------------------------------------------------------------------
TEST_QUEUE_PREFIX = 'encryptionqueue'


# ------------------------------------------------------------------------------


class StorageQueueEncryptionTest(StorageTestCase):
    def setUp(self):
        super(StorageQueueEncryptionTest, self).setUp()

        self.qs = self._create_storage_service(QueueService, self.settings)

        self.test_queues = []

    def tearDown(self):
        if not self.is_playback():
            for queue_name in self.test_queues:
                try:
                    self.qs.delete_queue(queue_name)
                except:
                    pass
        return super(StorageQueueEncryptionTest, self).tearDown()

    # --Helpers-----------------------------------------------------------------
    def _get_queue_reference(self, prefix=TEST_QUEUE_PREFIX):
        queue_name = self.get_resource_name(prefix)
        self.test_queues.append(queue_name)
        return queue_name

    def _create_queue(self, prefix=TEST_QUEUE_PREFIX):
        queue_name = self._get_queue_reference(prefix)
        self.qs.create_queue(queue_name)
        return queue_name

    # --------------------------------------------------------------------------

    @record
    def test_get_messages_encrypted_kek(self):
        # Arrange
        self.qs.key_encryption_key = KeyWrapper('key1')
        queue_name = self._create_queue()
        self.qs.put_message(queue_name, u'encrypted_message_2')

        # Act
        li = self.qs.get_messages(queue_name)

        # Assert
        self.assertEqual(li[0].content, u'encrypted_message_2')

    @record
    def test_get_messages_encrypted_resolver(self):
        # Arrange
        self.qs.key_encryption_key = KeyWrapper('key1')
        queue_name = self._create_queue()
        self.qs.put_message(queue_name, u'encrypted_message_2')
        key_resolver = KeyResolver()
        key_resolver.put_key(self.qs.key_encryption_key)
        self.qs.key_resolver_function = key_resolver.resolve_key
        self.qs.key_encryption_key = None  # Ensure that the resolver is used

        # Act
        li = self.qs.get_messages(queue_name)

        # Assert
        self.assertEqual(li[0].content, u'encrypted_message_2')

    @record
    def test_peek_messages_encrypted_kek(self):
        # Arrange
        self.qs.key_encryption_key = KeyWrapper('key1')
        queue_name = self._create_queue()
        self.qs.put_message(queue_name, u'encrypted_message_3')

        # Act
        li = self.qs.peek_messages(queue_name)

        # Assert
        self.assertEqual(li[0].content, u'encrypted_message_3')

    @record
    def test_peek_messages_encrypted_resolver(self):
        # Arrange
        self.qs.key_encryption_key = KeyWrapper('key1')
        queue_name = self._create_queue()
        self.qs.put_message(queue_name, u'encrypted_message_4')
        key_resolver = KeyResolver()
        key_resolver.put_key(self.qs.key_encryption_key)
        self.resolver = key_resolver.resolve_key

        # Act
        li = self.qs.peek_messages(queue_name)

        # Assert
        self.assertEqual(li[0].content, u'encrypted_message_4')

    def test_peek_messages_encrypted_kek_RSA(self):

        # We can only generate random RSA keys, so this must be run live or 
        # the playback test will fail due to a change in kek values.
        if TestMode.need_recording_file(self.test_mode):
            return

            # Arrange
        self.qs.key_encryption_key = RSAKeyWrapper('key2')
        queue_name = self._create_queue()
        self.qs.put_message(queue_name, u'encrypted_message_3')

        # Act
        li = self.qs.peek_messages(queue_name)

        # Assert
        self.assertEqual(li[0].content, u'encrypted_message_3')

    @record
    def test_update_encrypted_message(self):
        # Arrange
        queue_name = self._create_queue()
        self.qs.key_encryption_key = KeyWrapper('key1')
        self.qs.put_message(queue_name, u'Update Me')
        list_result1 = self.qs.get_messages(queue_name)

        # Act
        message = self.qs.update_message(queue_name,
                                         list_result1[0].id,
                                         list_result1[0].pop_receipt,
                                         0,
                                         content=u'Updated', )
        list_result2 = self.qs.get_messages(queue_name)

        # Assert
        message = list_result2[0]
        self.assertEqual(u'Updated', message.content)

    @record
    def test_update_encrypted_binary_message(self):
        # Arrange
        queue_name = self._create_queue()
        self.qs.key_encryption_key = KeyWrapper('key1')
        self.qs.encode_function = QueueMessageFormat.binary_base64encode
        self.qs.decode_function = QueueMessageFormat.binary_base64decode
        binary_message = self.get_random_bytes(100)
        self.qs.put_message(queue_name, binary_message)
        list_result1 = self.qs.get_messages(queue_name)

        # Act
        binary_message = self.get_random_bytes(100)
        self.qs.update_message(queue_name,
                               list_result1[0].id,
                               list_result1[0].pop_receipt,
                               0,
                               content=binary_message, )
        list_result2 = self.qs.get_messages(queue_name)

        # Assert
        message = list_result2[0]
        self.assertEqual(binary_message, message.content)

    @record
    def test_update_encrypted_raw_text_message(self):
        # Arrange
        queue_name = self._create_queue()
        self.qs.key_encryption_key = KeyWrapper('key1')
        self.qs.encode_function = QueueMessageFormat.noencode
        self.qs.decode_function = QueueMessageFormat.nodecode
        raw_text = u'Update Me'
        self.qs.put_message(queue_name, raw_text)
        list_result1 = self.qs.get_messages(queue_name)

        # Act
        raw_text = u'Updated'
        self.qs.update_message(queue_name,
                               list_result1[0].id,
                               list_result1[0].pop_receipt,
                               0,
                               content=raw_text, )
        list_result2 = self.qs.get_messages(queue_name)

        # Assert
        message = list_result2[0]
        self.assertEqual(raw_text, message.content)

    @record
    def test_update_encrypted_json_message(self):
        # Arrange
        queue_name = self._create_queue()
        self.qs.key_encryption_key = KeyWrapper('key1')
        self.qs.encode_function = QueueMessageFormat.noencode
        self.qs.decode_function = QueueMessageFormat.nodecode
        message_dict = {'val1': 1, 'val2': '2'}
        json_text = dumps(message_dict)
        self.qs.put_message(queue_name, json_text)
        list_result1 = self.qs.get_messages(queue_name)

        # Act
        message_dict['val1'] = 0
        message_dict['val2'] = 'updated'
        json_text = dumps(message_dict)
        self.qs.update_message(queue_name,
                               list_result1[0].id,
                               list_result1[0].pop_receipt,
                               0,
                               content=json_text, )
        list_result2 = self.qs.get_messages(queue_name)

        # Assert
        message = list_result2[0]
        self.assertEqual(message_dict, loads(message.content))

    @record
    def test_invalid_value_kek_wrap(self):
        # Arrange
        queue_name = self._create_queue()
        self.qs.key_encryption_key = KeyWrapper('key1')

        self.qs.key_encryption_key.get_kid = None
        try:
            self.qs.put_message(queue_name, u'message')
            self.fail()
        except AttributeError as e:
            self.assertEqual(str(e), _ERROR_OBJECT_INVALID.format('key encryption key', 'get_kid'))

        self.qs.key_encryption_key = KeyWrapper('key1')

        self.qs.key_encryption_key.get_kid = None
        with self.assertRaises(AttributeError):
            self.qs.put_message(queue_name, u'message')

        self.qs.key_encryption_key = KeyWrapper('key1')

        self.qs.key_encryption_key.wrap_key = None
        with self.assertRaises(AttributeError):
            self.qs.put_message(queue_name, u'message')

    @record
    def test_missing_attribute_kek_wrap(self):
        # Arrange
        queue_name = self._create_queue()

        valid_key = KeyWrapper('key1')

        # Act
        invalid_key_1 = lambda: None  # functions are objects, so this effectively creates an empty object
        invalid_key_1.get_key_wrap_algorithm = valid_key.get_key_wrap_algorithm
        invalid_key_1.get_kid = valid_key.get_kid
        # No attribute wrap_key
        self.qs.key_encryption_key = invalid_key_1
        with self.assertRaises(AttributeError):
            self.qs.put_message(queue_name, u'message')

        invalid_key_2 = lambda: None  # functions are objects, so this effectively creates an empty object
        invalid_key_2.wrap_key = valid_key.wrap_key
        invalid_key_2.get_kid = valid_key.get_kid
        # No attribute get_key_wrap_algorithm
        self.qs.key_encryption_key = invalid_key_2
        with self.assertRaises(AttributeError):
            self.qs.put_message(queue_name, u'message')

        invalid_key_3 = lambda: None  # functions are objects, so this effectively creates an empty object
        invalid_key_3.get_key_wrap_algorithm = valid_key.get_key_wrap_algorithm
        invalid_key_3.wrap_key = valid_key.wrap_key
        # No attribute get_kid
        self.qs.key_encryption_key = invalid_key_3
        with self.assertRaises(AttributeError):
            self.qs.put_message(queue_name, u'message')

    @record
    def test_invalid_value_kek_unwrap(self):
        # Arrange
        queue_name = self._create_queue()
        self.qs.key_encryption_key = KeyWrapper('key1')
        self.qs.put_message(queue_name, u'message')

        # Act
        self.qs.key_encryption_key.unwrap_key = None
        with self.assertRaises(AzureException):
            self.qs.peek_messages(queue_name)

        self.qs.key_encryption_key.get_kid = None
        with self.assertRaises(AzureException):
            self.qs.peek_messages(queue_name)

    @record
    def test_missing_attribute_kek_unrwap(self):
        # Arrange
        queue_name = self._create_queue()
        self.qs.key_encryption_key = KeyWrapper('key1')
        self.qs.put_message(queue_name, u'message')

        # Act
        valid_key = KeyWrapper('key1')
        invalid_key_1 = lambda: None  # functions are objects, so this effectively creates an empty object
        invalid_key_1.unwrap_key = valid_key.unwrap_key
        # No attribute get_kid
        self.qs.key_encryption_key = invalid_key_1
        try:
            self.qs.peek_messages(queue_name)
            self.fail()
        except AzureException as e:
            self.assertEqual(str(e), _ERROR_DECRYPTION_FAILURE)

        invalid_key_2 = lambda: None  # functions are objects, so this effectively creates an empty object
        invalid_key_2.get_kid = valid_key.get_kid
        # No attribute unwrap_key
        self.qs.key_encryption_key = invalid_key_2
        with self.assertRaises(AzureException):
            self.qs.peek_messages(queue_name)

    @record
    def test_validate_encryption(self):
        # Arrange
        queue_name = self._create_queue()
        kek = KeyWrapper('key1')
        self.qs.key_encryption_key = kek
        self.qs.put_message(queue_name, u'message')

        # Act
        self.qs.key_encryption_key = None  # Message will not be decrypted
        li = self.qs.peek_messages(queue_name)
        message = li[0].content
        message = loads(message)

        encryption_data = message['EncryptionData']

        wrapped_content_key = encryption_data['WrappedContentKey']
        wrapped_content_key = _WrappedContentKey(wrapped_content_key['Algorithm'],
                                                 b64decode(
                                                     wrapped_content_key['EncryptedKey'].encode(encoding='utf-8')),
                                                 wrapped_content_key['KeyId'])

        encryption_agent = encryption_data['EncryptionAgent']
        encryption_agent = _EncryptionAgent(encryption_agent['EncryptionAlgorithm'],
                                            encryption_agent['Protocol'])

        encryption_data = _EncryptionData(b64decode(encryption_data['ContentEncryptionIV'].encode(encoding='utf-8')),
                                          encryption_agent,
                                          wrapped_content_key,
                                          {'EncryptionLibrary': __version__})
        message = message['EncryptedMessageContents']
        content_encryption_key = kek.unwrap_key(encryption_data.wrapped_content_key.encrypted_key,
                                                encryption_data.wrapped_content_key.algorithm)

        # Create decryption cipher
        backend = backends.default_backend()
        algorithm = AES(content_encryption_key)
        mode = CBC(encryption_data.content_encryption_IV)
        cipher = Cipher(algorithm, mode, backend)

        # decode and decrypt data
        decrypted_data = _decode_base64_to_bytes(message)
        decryptor = cipher.decryptor()
        decrypted_data = (decryptor.update(decrypted_data) + decryptor.finalize())

        # unpad data
        unpadder = PKCS7(128).unpadder()
        decrypted_data = (unpadder.update(decrypted_data) + unpadder.finalize())

        decrypted_data = decrypted_data.decode(encoding='utf-8')

        # Assert
        self.assertEqual(decrypted_data, u'message')

    @record
    def test_put_with_strict_mode(self):
        # Arrange
        queue_name = self._create_queue()
        kek = KeyWrapper('key1')
        self.qs.key_encryption_key = kek
        self.qs.require_encryption = True

        self.qs.put_message(queue_name, u'message')
        self.qs.key_encryption_key = None

        # Assert
        try:
            self.qs.put_message(queue_name, u'message')
            self.fail()
        except ValueError as e:
            self.assertEqual(str(e), _ERROR_ENCRYPTION_REQUIRED)

    @record
    def test_get_with_strict_mode(self):
        # Arrange
        queue_name = self._create_queue()
        self.qs.put_message(queue_name, u'message')

        self.qs.require_encryption = True
        self.qs.key_encryption_key = KeyWrapper('key1')
        try:
            self.qs.get_messages(queue_name)
        except ValueError as e:
            self.assertEqual(str(e), _ERROR_MESSAGE_NOT_ENCRYPTED)

    @record
    def test_encryption_add_encrypted_64k_message(self):
        # Arrange
        queue_name = self._create_queue()
        message = u'a' * 1024 * 64

        # Act
        self.qs.put_message(queue_name, message)

        # Assert
        self.qs.key_encryption_key = KeyWrapper('key1')
        with self.assertRaises(AzureHttpError):
            self.qs.put_message(queue_name, message)

    @record
    def test_encryption_nonmatching_kid(self):
        # Arrange
        queue_name = self._create_queue()
        self.qs.key_encryption_key = KeyWrapper('key1')
        self.qs.put_message(queue_name, u'message')

        # Act
        self.qs.key_encryption_key.kid = 'Invalid'

        # Assert
        try:
            self.qs.get_messages(queue_name)
            self.fail()
        except AzureException as e:
            self.assertEqual(str(e), _ERROR_DECRYPTION_FAILURE)


# ------------------------------------------------------------------------------
if __name__ == '__main__':
    unittest.main()