Skip to content

Commit 6d55819

Browse files
authored
Merge pull request #3573 from StackStorm/rbac_rule_creation_fix
Allow users to create rules which reference actions which don't exist in the system when RBAC is enabled
2 parents a6290c4 + 4d71a53 commit 6d55819

5 files changed

Lines changed: 55 additions & 1 deletion

File tree

CHANGELOG.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ Fixed
3636
``http_proxy`` or ``https_proxy`` environment variables for ``st2api`` and ``st2actionrunner``
3737
processes and pack commands will work with proxy. Refer to documentation for details on
3838
proxy configuration. (bug-fix) #3137
39-
* Fix no-member linting error on U16 by ignoring mistralclient.api.v2.executions module.
39+
* Fix an API bug and allow users to create rules which reference actions which don't yet exist in
40+
the system when RBAC is enabled and user doesn't have system admin permission. (bug fix)
41+
#3572 #3573
4042

43+
Reported by sibirajal.
4144

4245
2.3.1 - July 07, 2017
4346
---------------------

st2api/tests/unit/controllers/v1/test_rules_rbac.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def setUp(self):
6565
fixtures_pack=FIXTURES_PACK,
6666
fixtures_dict={'rules': [file_name]})['rules'][file_name]
6767

68+
file_name = 'rule_action_doesnt_exist.yaml'
69+
RuleControllerRBACTestCase.RULE_3 = self.fixtures_loader.load_fixtures(
70+
fixtures_pack=FIXTURES_PACK,
71+
fixtures_dict={'rules': [file_name]})['rules'][file_name]
72+
6873
# Insert mock users, roles and assignments
6974

7075
# Users
@@ -156,6 +161,17 @@ def test_post_webhook_trigger_no_trigger_and_action_permission(self):
156161
self.assertEqual(resp.status_code, httplib.FORBIDDEN)
157162
self.assertEqual(resp.json['faultstring'], expected_msg)
158163

164+
def test_post_user_has_no_permission_on_action_which_doesnt_exist_in_db(self):
165+
# User has rule_create, but no action_execute on the action which doesn't exist in the db
166+
user_db = self.users['rule_create_webhook_create']
167+
self.use_user(user_db)
168+
169+
resp = self.__do_post(RuleControllerRBACTestCase.RULE_3)
170+
expected_msg = ('User "rule_create_webhook_create" doesn\'t have required (action_execute)'
171+
' permission to use action wolfpack.action-doesnt-exist-woo')
172+
self.assertEqual(resp.status_code, httplib.FORBIDDEN)
173+
self.assertEqual(resp.json['faultstring'], expected_msg)
174+
159175
def test_post_no_webhook_trigger(self):
160176
# Test a scenario when user with only "rule_create" permission selects a non-webhook
161177
# trigger for which we don't perform any permission checking right now

st2common/st2common/rbac/resolvers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def _user_has_resource_permission(self, user_db, pack_uid, resource_uid, permiss
212212
self._log('Checking user resource permissions', extra=log_context)
213213

214214
# First check the system role permissions
215+
self._log('Checking grants via system role permissions', extra=log_context)
215216
has_system_role_permission = self._user_has_system_role_permission(
216217
user_db=user_db, permission_type=permission_type)
217218

@@ -235,6 +236,7 @@ def _user_has_resource_permission(self, user_db, pack_uid, resource_uid, permiss
235236
permission_types = [permission_type]
236237

237238
# Check direct grants on the specified resource
239+
self._log('Checking direct grans on the specified resource', extra=log_context)
238240
resource_types = [self.resource_type]
239241
permission_grants = get_all_permission_grants_for_user(user_db=user_db,
240242
resource_uid=resource_uid,
@@ -245,6 +247,7 @@ def _user_has_resource_permission(self, user_db, pack_uid, resource_uid, permiss
245247
return True
246248

247249
# Check grants on the parent pack
250+
self._log('Checking grants on the parent resource', extra=log_context)
248251
resource_types = [ResourceType.PACK]
249252
permission_grants = get_all_permission_grants_for_user(user_db=user_db,
250253
resource_uid=pack_uid,

st2common/st2common/rbac/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
from oslo_config import cfg
2323

24+
from st2common.models.db.action import ActionDB
25+
from st2common.models.system.common import ResourceReference
2426
from st2common.exceptions.rbac import AccessDeniedError
2527
from st2common.exceptions.rbac import ResourceTypeAccessDeniedError
2628
from st2common.exceptions.rbac import ResourceAccessDeniedError
@@ -162,11 +164,20 @@ def user_has_rule_action_permission(user_db, action_ref):
162164
"""
163165
Check that the currently logged-in has necessary permissions on the action used / referenced
164166
inside the rule.
167+
168+
Note: Rules can reference actions which don't yet exist in the system.
165169
"""
166170
if not cfg.CONF.rbac.enable:
167171
return True
168172

169173
action_db = action_utils.get_action_by_ref(ref=action_ref)
174+
175+
if not action_db:
176+
# We allow rules to be created for actions which don't yet exist in the
177+
# system
178+
ref = ResourceReference.from_string_reference(ref=action_ref)
179+
action_db = ActionDB(pack=ref.pack, name=ref.name, ref=action_ref)
180+
170181
action_resolver = resolvers.get_resolver_for_resource_type(ResourceType.ACTION)
171182
has_action_permission = action_resolver.user_has_resource_db_permission(
172183
user_db=user_db, resource_db=action_db, permission_type=PermissionType.ACTION_EXECUTE)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
action:
3+
parameters:
4+
ip1: '{{trigger.t1_p}}'
5+
ip2: '{{trigger}}'
6+
ref: wolfpack.action-doesnt-exist-woo
7+
criteria:
8+
trigger.t1_p:
9+
pattern: t1_p_v
10+
type: equals
11+
description: ''
12+
enabled: true
13+
name: rule_action_doesnt_exist
14+
pack: examples
15+
tags:
16+
- name: tag1
17+
value: dont-care
18+
- name: tag2
19+
value: dont-care
20+
trigger:
21+
type: wolfpack.triggertype-1

0 commit comments

Comments
 (0)