1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
import httplib |
17
|
|
|
|
18
|
|
|
import six |
19
|
|
|
|
20
|
|
|
from st2common.persistence.rbac import UserRoleAssignment |
21
|
|
|
from st2common.models.db.rbac import UserRoleAssignmentDB |
22
|
|
|
from st2common.rbac.types import PermissionType |
23
|
|
|
|
24
|
|
|
from base import APIControllerWithRBACTestCase |
25
|
|
|
|
26
|
|
|
http_client = six.moves.http_client |
27
|
|
|
|
28
|
|
|
__all__ = [ |
29
|
|
|
'APIControllersRBACTestCase' |
30
|
|
|
] |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class APIControllersRBACTestCase(APIControllerWithRBACTestCase): |
34
|
|
|
""" |
35
|
|
|
Test class which hits all the API endpoints which are behind the RBAC wall with a user which |
36
|
|
|
has no permissions and makes sure API returns access denied. |
37
|
|
|
""" |
38
|
|
|
|
39
|
|
|
def setUp(self): |
40
|
|
|
super(APIControllersRBACTestCase, self).setUp() |
41
|
|
|
|
42
|
|
|
self.role_assignment_db_model = UserRoleAssignmentDB(user='user', role='role') |
43
|
|
|
UserRoleAssignment.add_or_update(self.role_assignment_db_model) |
44
|
|
|
|
45
|
|
|
def test_api_endpoints_behind_rbac_wall(self): |
46
|
|
|
|
47
|
|
|
supported_endpoints = [ |
48
|
|
|
# Stream |
49
|
|
|
{ |
50
|
|
|
'path': '/v1/stream', |
51
|
|
|
'method': 'GET' |
52
|
|
|
} |
53
|
|
|
] |
54
|
|
|
|
55
|
|
|
self.use_user(self.users['no_permissions']) |
56
|
|
|
for endpoint in supported_endpoints: |
57
|
|
|
response = self._perform_request_for_endpoint(endpoint=endpoint) |
58
|
|
|
expected_msg = ('User "%s" doesn\'t have required permission "%s"' % |
59
|
|
|
(self.users['no_permissions'].name, PermissionType.STREAM_VIEW)) |
60
|
|
|
|
61
|
|
|
msg = '%s "%s" didn\'t return 403 status code (body=%s)' % (endpoint['method'], |
62
|
|
|
endpoint['path'], |
63
|
|
|
response.body) |
64
|
|
|
self.assertEqual(response.status_code, httplib.FORBIDDEN, msg) |
65
|
|
|
self.assertRegexpMatches(response.json['faultstring'], expected_msg) |
66
|
|
|
|
67
|
|
|
def _perform_request_for_endpoint(self, endpoint): |
68
|
|
|
if endpoint['method'] == 'GET': |
69
|
|
|
response = self.app.get(endpoint['path'], expect_errors=True) |
70
|
|
|
elif endpoint['method'] == 'POST': |
71
|
|
|
return self.app.post_json(endpoint['path'], endpoint['payload'], expect_errors=True) |
72
|
|
|
elif endpoint['method'] == 'PUT': |
73
|
|
|
return self.app.put_json(endpoint['path'], endpoint['payload'], expect_errors=True) |
74
|
|
|
elif endpoint['method'] == 'DELETE': |
75
|
|
|
return self.app.delete(endpoint['path'], expect_errors=True) |
76
|
|
|
else: |
77
|
|
|
raise ValueError('Unsupported method: %s' % (endpoint['method'])) |
78
|
|
|
|
79
|
|
|
return response |
80
|
|
|
|