summaryrefslogtreecommitdiff
path: root/functionaltests/client/v1/functional/test_acl.py
blob: d9ad6f24628829a366e1df1a8f74e467984a79fe (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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from testtools import testcase

from functionaltests.client import base
from functionaltests.common import cleanup
from functionaltests import utils
from oslo_utils import uuidutils

from barbicanclient import exceptions


create_secret_defaults_data = {
    "name": "AES key",
    "expiration": "2030-02-28T19:14:44.180394",
    "algorithm": "aes",
    "bit_length": 256,
    "mode": "cbc",
    "payload": "gF6+lLoF3ohA9aPRpt+6bQ==",
    "payload_content_type": "application/octet-stream",
    "payload_content_encoding": "base64",
}

create_container_defaults_data = {
    "name": "containername",
    "secrets": {}
}

create_container_rsa_data = {
    "name": "rsacontainer",
}


ACL_SUBMIT_DATA_POSITIVE = {
    'secret_no_users_access_flag': {
        'users': None, 'project_access': True,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'expect_users': [], 'expect_project_access': True},

    'secret_users_missing_access_flag': {
        'users': ['u1', 'u2'], 'project_access': None,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'expect_users': ['u1', 'u2'], 'expect_project_access': True},

    'container_users_no_project_access': {
        'users': ['u1', 'u2', 'u3'], 'project_access': False,
        'entity_ref_method': '_create_a_container', 'acl_type': 'container',
        'expect_users': ['u1', 'u2', 'u3'], 'expect_project_access': False},

    'container_empty_users_with_project_access': {
        'users': [], 'project_access': True,
        'entity_ref_method': '_create_a_container', 'acl_type': 'container',
        'expect_users': [], 'expect_project_access': True},
}

ACL_SUBMIT_DATA_NEGATIVE = {
    'secret_users_incorrect_access_flag': {
        'users': ['u1', 'u2'], 'project_access': 'Incorrect_flag',
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'expect_users': ['u1', 'u2'], 'expect_project_access': True,
        'expect_error': True, 'error_code': 400},

    'container_incorrect_users_as_str': {
        'users': 'u1', 'project_access': True,
        'entity_ref_method': '_create_a_container', 'acl_type': 'container',
        'expect_users': ['u1'], 'expect_project_access': True,
        'expect_error': True, 'error_code': None, 'error_class': ValueError},
}

ACL_DELETE_DATA = {
    'secret_no_users_access_flag': {
        'users': None, 'project_access': True, 'create_acl': True,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'expect_users': [], 'expect_project_access': True},

    'secret_users_missing_access_flag': {
        'users': ['u1', 'u2'], 'project_access': None, 'create_acl': True,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'expect_users': [], 'expect_project_access': True},

    'container_users_no_project_access': {
        'users': ['u1', 'u2', 'u3'], 'project_access': False,
        'create_acl': True,
        'entity_ref_method': '_create_a_container', 'acl_type': 'container',
        'expect_users': [], 'expect_project_access': True},

    'container_empty_users_with_project_access': {
        'users': [], 'project_access': True, 'create_acl': True,
        'entity_ref_method': '_create_a_container', 'acl_type': 'container',
        'expect_users': [], 'expect_project_access': True},

    'existing_secret_no_acl_defined': {
        'users': ['u1', 'u2'], 'project_access': False, 'create_acl': False,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'expect_users': [], 'expect_project_access': True,
        'expect_error': False},

    'acl_operation_specific_remove': {
        'users': ['u1', 'u2', 'u3'], 'project_access': False,
        'create_acl': True, 'per_op_acl_remove': True,
        'entity_ref_method': '_create_a_container', 'acl_type': 'container',
        'expect_users': [], 'expect_project_access': True},
}

ACL_ADD_USERS_DATA_POSITIVE = {
    'secret_no_initial_users_access_flag': {
        'users': None, 'project_access': True,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'add_users': ['u4'],
        'expect_users': ['u4'], 'expect_project_access': True},

    'secret_users_missing_access_flag': {
        'users': ['u1', 'u2'], 'project_access': None,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'add_users': ['u2', 'u4'],
        'expect_users': ['u1', 'u2', 'u4'], 'expect_project_access': True},

    'container_users_no_project_access_empty_add': {
        'users': ['u1', 'u2', 'u3'], 'project_access': False,
        'entity_ref_method': '_create_a_container', 'acl_type': 'container',
        'add_users': [],
        'expect_users': ['u1', 'u2', 'u3'], 'expect_project_access': False},

    'container_empty_users_with_project_access_none_add_users': {
        'users': [], 'project_access': True,
        'entity_ref_method': '_create_a_container', 'acl_type': 'container',
        'add_users': None,
        'expect_users': [], 'expect_project_access': True},

    'secret_users_modify_access_flag_in_add': {
        'users': ['u1', 'u2', 'u3'], 'project_access': False,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'add_users': [], 'add_project_access': True,
        'expect_users': ['u1', 'u2', 'u3'], 'expect_project_access': True},

}

ACL_ADD_USERS_DATA_NEGATIVE = {
    'secret_users_incorrect_access_flag_during_add': {
        'users': ['u1', 'u2'], 'project_access': False,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'add_users': ['u5'], 'add_project_access': 'Incorrect',
        'expect_users': ['u1', 'u2', 'u5'], 'expect_project_access': False,
        'expect_error': True, 'error_code': 400},
}

ACL_REMOVE_USERS_DATA_POSITIVE = {
    'secret_no_initial_users_access_flag': {
        'users': None, 'project_access': True,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'remove_users': ['u4'],
        'expect_users': [], 'expect_project_access': True},

    'secret_users_missing_access_flag': {
        'users': ['u1', 'u2'], 'project_access': None,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'remove_users': ['u2', 'u4'],
        'expect_users': ['u1'], 'expect_project_access': True},

    'secret_users_no_matching_users': {
        'users': ['u1', 'u2'], 'project_access': None,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'remove_users': ['u3', 'u4'],
        'expect_users': ['u1', 'u2'], 'expect_project_access': True},

    'container_users_no_project_access_empty_add': {
        'users': ['u1', 'u2', 'u3'], 'project_access': False,
        'entity_ref_method': '_create_a_container', 'acl_type': 'container',
        'remove_users': [],
        'expect_users': ['u1', 'u2', 'u3'], 'expect_project_access': False},

    'container_empty_users_with_project_access_none_add_users': {
        'users': [], 'project_access': True,
        'entity_ref_method': '_create_a_container', 'acl_type': 'container',
        'remove_users': None,
        'expect_users': [], 'expect_project_access': True},

    'secret_users_modify_access_flag_in_remove': {
        'users': ['u1', 'u2', 'u3'], 'project_access': False,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'remove_users': [], 'remove_project_access': True,
        'expect_users': ['u1', 'u2', 'u3'], 'expect_project_access': True},
}

ACL_REMOVE_USERS_DATA_NEGATIVE = {
    'secret_users_incorrect_access_flag_during_add': {
        'users': ['u1', 'u2'], 'project_access': False,
        'entity_ref_method': '_create_a_secret', 'acl_type': 'secret',
        'remove_users': ['u5'], 'remove_project_access': 'Incorrect',
        'expect_users': ['u1', 'u2'], 'expect_project_access': False,
        'expect_error': True, 'error_code': 400},
}


class BaseACLsTestCase(base.TestCase):

    def setUp(self):
        super(BaseACLsTestCase, self).setUp()

        self.cleanup = cleanup.CleanUp(self.barbicanclient)
        # Set up three secrets
        self.secret_ref_1, self.secret_1 = self._create_a_secret()
        self.secret_ref_2, self.secret_2 = self._create_a_secret()
        self.secret_ref_3, self.secret_3 = self._create_a_secret()

        self.secret_list = [self.secret_ref_1, self.secret_ref_2,
                            self.secret_ref_3]

        secrets_dict = {'secret_1': self.secret_1, 'secret_2': self.secret_2,
                        'secret_3': self.secret_3}

        create_container_defaults_data['secrets'] = secrets_dict

        create_container_rsa_data['public_key'] = self.secret_1
        create_container_rsa_data['private_key'] = self.secret_2
        create_container_rsa_data['private_key_passphrase'] = self.secret_3

    def tearDown(self):
        """Handles test cleanup.

        It should be noted that delete all secrets must be called before
        delete containers.
        """
        self.cleanup.delete_all_entities()
        super(BaseACLsTestCase, self).tearDown()

    def _create_a_secret(self):
        secret = self.barbicanclient.secrets.create(
            **create_secret_defaults_data)
        secret_ref = self.cleanup.add_entity(secret)

        return secret_ref, secret

    def _create_a_container(self):
        container = self.barbicanclient.containers.create(
            **create_container_defaults_data)
        container_ref = self.cleanup.add_entity(container)

        return container_ref, container


@utils.parameterized_test_case
class ACLsTestCase(BaseACLsTestCase):

    @testcase.attr('negative')
    def test_get_non_existent_secret_valid_uuid(self):
        """A get on a container that does not exist with valid UUID

        This should return a 404.
        """
        base_url = self.barbicanclient.acls._api.endpoint_override
        new_uuid = uuidutils.generate_uuid()
        url = '{0}/containers/{1}'.format(base_url, new_uuid)

        e = self.assertRaises(
            exceptions.HTTPClientError,
            self.barbicanclient.acls.get,
            url
        )

        self.assertEqual(404, e.status_code)

    @testcase.attr('negative')
    def test_delete_non_existent_secret_valid_uuid(self):
        """A delete on an ACL when secret with a valid UUID does not exist

        This should return a 404.
        """
        base_url = self.barbicanclient.acls._api.endpoint_override
        new_uuid = uuidutils.generate_uuid()
        url = '{0}/secrets/{1}'.format(base_url, new_uuid)

        acl_data = {'entity_ref': url}
        entity = self.barbicanclient.acls.create(**acl_data)

        e = self.assertRaises(
            exceptions.HTTPClientError,
            entity.remove
        )

        self.assertEqual(404, e.status_code)

    @utils.parameterized_dataset(ACL_SUBMIT_DATA_POSITIVE)
    @testcase.attr('positive')
    def test_acl_successful_submit(self, users, project_access,
                                   entity_ref_method, acl_type,
                                   expect_users, expect_project_access,
                                   **kwargs):
        """Submit operation on ACL entity which stores ACL setting in Barbican.

        """
        entity_ref, _ = getattr(self, entity_ref_method)()

        acl_data = {'entity_ref': entity_ref, 'users': users,
                    'project_access': project_access}
        entity = self.barbicanclient.acls.create(**acl_data)

        acl_ref = self.cleanup.add_entity(entity)
        self.assertIsNotNone(acl_ref)
        self.assertEqual(entity_ref + "/acl", acl_ref)

        acl_entity = self.barbicanclient.acls.get(entity.entity_ref)
        self.assertIsNotNone(acl_entity)

        # read acl as dictionary lookup
        acl = acl_entity.get('read')
        self.assertEqual(set(expect_users), set(acl.users))
        self.assertEqual(expect_project_access, acl.project_access)
        self.assertIsNotNone(acl.created)
        self.assertIsNotNone(acl.updated)
        self.assertEqual(acl_type, acl_entity._acl_type)

        # read acl as property lookup
        acl = acl_entity.read
        self.assertEqual(set(expect_users), set(acl.users))
        self.assertEqual(expect_project_access, acl.project_access)

    @utils.parameterized_dataset(ACL_SUBMIT_DATA_NEGATIVE)
    @testcase.attr('negative')
    def test_acl_incorrect_submit(self, users, project_access,
                                  entity_ref_method, acl_type, expect_users,
                                  expect_project_access, **kwargs):
        """Check incorrect submit operation failure on ACL entity."""
        entity_ref, _ = getattr(self, entity_ref_method)()

        acl_data = {'entity_ref': entity_ref, 'users': users,
                    'project_access': project_access}
        entity = self.barbicanclient.acls.create(**acl_data)

        error_class = kwargs.get('error_class', exceptions.HTTPClientError)
        e = self.assertRaises(
            error_class,
            entity.submit
        )
        if hasattr(e, 'status_code'):
            self.assertEqual(kwargs.get('error_code'), e.status_code)

    @utils.parameterized_dataset(ACL_DELETE_DATA)
    def test_acl_delete(self, users, project_access, entity_ref_method,
                        create_acl, acl_type, expect_users,
                        expect_project_access, **kwargs):
        """remove operation on ACL entity which stores ACL setting in Barbican.

        """
        entity_ref, _ = getattr(self, entity_ref_method)()

        acl_data = {'entity_ref': entity_ref, 'users': users,
                    'project_access': project_access}
        entity = self.barbicanclient.acls.create(**acl_data)

        entity_ref = entity.entity_ref
        if create_acl:
            self.cleanup.add_entity(entity)

        acl_op_remove = kwargs.get('per_op_acl_remove')
        if acl_op_remove:
            entity.read.remove()
        else:
            entity.remove()

        acl_entity = self.barbicanclient.acls.get(entity_ref)
        self.assertIsNotNone(acl_entity)

        # read acl as dictionary lookup
        acl = acl_entity.get('read')
        self.assertEqual(set(expect_users), set(acl.users))
        self.assertEqual(expect_project_access, acl.project_access)
        self.assertIsNone(acl.created)
        self.assertIsNone(acl.updated)
        self.assertEqual(acl_type, acl_entity._acl_type)

        # read acl as property lookup
        acl = acl_entity.read
        self.assertEqual(set(expect_users), set(acl.users))
        self.assertEqual(expect_project_access, acl.project_access)

    @utils.parameterized_dataset(ACL_ADD_USERS_DATA_POSITIVE)
    @testcase.attr('positive')
    def test_acl_successful_add_users(self, users, project_access,
                                      entity_ref_method, acl_type, add_users,
                                      expect_users, expect_project_access,
                                      **kwargs):
        """Checks client add users behavior on existing ACL entity.

        In this new users or project access flag is modified and verified for
        expected behavior
        """
        entity_ref, _ = getattr(self, entity_ref_method)()

        acl_data = {'entity_ref': entity_ref, 'users': users,
                    'project_access': project_access}
        entity = self.barbicanclient.acls.create(**acl_data)

        acl_ref = self.cleanup.add_entity(entity)
        self.assertIsNotNone(acl_ref)
        self.assertEqual(entity_ref + "/acl", acl_ref)

        server_acl = self.barbicanclient.acls.get(entity.entity_ref)

        if server_acl.get('read').users is not None and add_users:
            server_acl.get('read').users.extend(add_users)

        if kwargs.get('add_project_access') is not None:
            server_acl.get('read').project_access = \
                kwargs.get('add_project_access')

        acl_ref = server_acl.submit()
        self.assertIsNotNone(acl_ref)
        self.assertEqual(entity_ref + "/acl", acl_ref)

        acl_entity = self.barbicanclient.acls.get(server_acl.entity_ref)
        self.assertIsNotNone(acl_entity)

        # read acl as dictionary lookup
        acl = acl_entity.get('read')
        self.assertEqual(set(expect_users), set(acl.users))
        self.assertEqual(expect_project_access, acl.project_access)
        self.assertIsNotNone(acl.created)
        self.assertIsNotNone(acl.updated)
        self.assertEqual(acl_type, acl_entity._acl_type)

        # read acl as property lookup
        acl = acl_entity.read
        self.assertEqual(set(expect_users), set(acl.users))
        self.assertEqual(expect_project_access, acl.project_access)

    @utils.parameterized_dataset(ACL_ADD_USERS_DATA_NEGATIVE)
    @testcase.attr('negative')
    def test_acl_add_users_failure(self, users, project_access,
                                   entity_ref_method, acl_type, add_users,
                                   expect_users, expect_project_access,
                                   **kwargs):
        """Checks client add users failures on existing ACL entity.

        In this new users or project access flag is modified and verified for
        expected behavior
        """
        entity_ref, _ = getattr(self, entity_ref_method)()

        acl_data = {'entity_ref': entity_ref, 'users': users,
                    'project_access': project_access}
        entity = self.barbicanclient.acls.create(**acl_data)

        acl_ref = self.cleanup.add_entity(entity)
        self.assertIsNotNone(acl_ref)
        self.assertEqual(entity_ref + "/acl", acl_ref)

        server_acl = self.barbicanclient.acls.get(entity.entity_ref)

        if server_acl.get('read').users is not None and add_users:
            server_acl.get('read').users.extend(add_users)

        if kwargs.get('add_project_access') is not None:
            server_acl.get('read').project_access = \
                kwargs.get('add_project_access')

        error_class = kwargs.get('error_class', exceptions.HTTPClientError)
        e = self.assertRaises(
            error_class,
            server_acl.submit
        )
        if hasattr(e, 'status_code'):
            self.assertEqual(kwargs.get('error_code'), e.status_code)

    @utils.parameterized_dataset(ACL_REMOVE_USERS_DATA_POSITIVE)
    @testcase.attr('positive')
    def test_acl_remove_users_successful(self, users, project_access,
                                         entity_ref_method, acl_type,
                                         remove_users, expect_users,
                                         expect_project_access, **kwargs):
        """Checks client remove users behavior on existing ACL entity.

        In this users are removed from existing users list or project access
        flag is modified and then verified for expected behavior
        """
        entity_ref, _ = getattr(self, entity_ref_method)()

        acl_data = {'entity_ref': entity_ref, 'users': users,
                    'project_access': project_access}
        entity = self.barbicanclient.acls.create(**acl_data)

        acl_ref = self.cleanup.add_entity(entity)
        self.assertIsNotNone(acl_ref)
        self.assertEqual(entity_ref + "/acl", acl_ref)

        server_acl = self.barbicanclient.acls.get(entity.entity_ref)
        acl_users = server_acl.read.users
        if acl_users and remove_users:
            acl_users = set(acl_users).difference(remove_users)
            # Python sets are not JSON serializable. Cast acl_users to a list.
            server_acl.read.users = list(acl_users)

        if kwargs.get('remove_project_access') is not None:
            server_acl.read.project_access = \
                kwargs.get('remove_project_access')

        acl_ref = server_acl.submit()
        self.assertIsNotNone(acl_ref)
        self.assertEqual(entity_ref + "/acl", acl_ref)

        acl_entity = self.barbicanclient.acls.get(server_acl.entity_ref)
        self.assertIsNotNone(acl_entity)

        # read acl as dictionary lookup
        acl = acl_entity.get('read')
        self.assertEqual(set(expect_users), set(acl.users))
        self.assertEqual(expect_project_access, acl.project_access)
        self.assertIsNotNone(acl.created)
        self.assertIsNotNone(acl.updated)
        self.assertEqual(acl_type, acl_entity._acl_type)

        # read acl as property lookup
        acl = acl_entity.read
        self.assertEqual(set(expect_users), set(acl.users))
        self.assertEqual(expect_project_access, acl.project_access)

    @utils.parameterized_dataset(ACL_REMOVE_USERS_DATA_NEGATIVE)
    @testcase.attr('negative')
    def test_acl_remove_users_failure(self, users, project_access,
                                      entity_ref_method, acl_type,
                                      remove_users, expect_users,
                                      expect_project_access, **kwargs):
        """Checks client remove users failures on existing ACL entity.

        In this users are removed from existing users list or project access
        flag is modified and then verified for expected behavior
        """
        entity_ref, _ = getattr(self, entity_ref_method)()

        acl_data = {'entity_ref': entity_ref, 'users': users,
                    'project_access': project_access}
        entity = self.barbicanclient.acls.create(**acl_data)

        acl_ref = self.cleanup.add_entity(entity)
        self.assertIsNotNone(acl_ref)
        self.assertEqual(entity_ref + "/acl", acl_ref)

        server_acl = self.barbicanclient.acls.get(entity.entity_ref)
        acl_users = server_acl.read.users
        if acl_users and remove_users:
            acl_users = set(acl_users).difference(remove_users)
            # Python sets are not JSON serializable. Cast acl_users to a list.
            server_acl.read.users = list(acl_users)

        if kwargs.get('remove_project_access') is not None:
            server_acl.read.project_access = \
                kwargs.get('remove_project_access')

        error_class = kwargs.get('error_class', exceptions.HTTPClientError)
        e = self.assertRaises(
            error_class,
            server_acl.submit
        )
        if hasattr(e, 'status_code'):
            self.assertEqual(kwargs.get('error_code'), e.status_code)