-
-
Notifications
You must be signed in to change notification settings - Fork 745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue/personal keys #5859
base: master
Are you sure you want to change the base?
Issue/personal keys #5859
Changes from 5 commits
602bf2b
f45874c
b4c16f3
9e79c80
1b1d70c
94b895b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ stream_output = True | |
|
||
[rbac] | ||
enable = False | ||
personal_keys = True | ||
|
||
[auth] | ||
host = 127.0.0.1 | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -74,11 +74,21 @@ def get_one(self, api_key_id_or_key, requester_user, show_secrets=None): | |||||
|
||||||
permission_type = PermissionType.API_KEY_VIEW | ||||||
rbac_utils = get_rbac_backend().get_utils_class() | ||||||
rbac_utils.assert_user_has_resource_db_permission( | ||||||
user_db=requester_user, | ||||||
resource_db=api_key_db, | ||||||
permission_type=permission_type, | ||||||
) | ||||||
if cfg.CONF.rbac.personal_keys: | ||||||
isKeyCreationAllowed = self.checkPersonalAPIKeyPermission( | ||||||
api_key_db, requester_user | ||||||
) | ||||||
if not isKeyCreationAllowed: | ||||||
LOG.exception( | ||||||
"User does not have permission to view apikey=%s.", api_key_db | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use python 3 f-strings instead of the older method of formatting up strings? |
||||||
) | ||||||
abort(http_client.BAD_REQUEST, "") | ||||||
else: | ||||||
rbac_utils.assert_user_has_resource_db_permission( | ||||||
user_db=requester_user, | ||||||
resource_db=api_key_db, | ||||||
permission_type=permission_type, | ||||||
) | ||||||
|
||||||
try: | ||||||
mask_secrets = self._get_mask_secrets( | ||||||
|
@@ -126,18 +136,42 @@ def get_all(self, requester_user, show_secrets=None, limit=None, offset=0): | |||||
|
||||||
return resp | ||||||
|
||||||
def checkPersonalAPIKeyPermission(self, api_key_api, requester_user): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. api_key_api seems an odd name, is there a more expressive name for variable we can use? |
||||||
""" | ||||||
Checks whether requested user is the creating/updating/fetching/deleting the api key. This is used when | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
'personal_keys' flag is set of 'rbac' group in conf file. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
""" | ||||||
if not api_key_api or not requester_user: | ||||||
return False | ||||||
rbac_utils = get_rbac_backend().get_utils_class() | ||||||
user_is_admin = rbac_utils.user_is_admin(user_db=requester_user) | ||||||
is_same_user = requester_user.name == api_key_api.user | ||||||
if (user_is_admin) or (is_same_user or api_key_api.user == ""): | ||||||
return True | ||||||
else: | ||||||
return False | ||||||
nzlosh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
def post(self, api_key_api, requester_user): | ||||||
""" | ||||||
Create a new entry. | ||||||
""" | ||||||
|
||||||
permission_type = PermissionType.API_KEY_CREATE | ||||||
rbac_utils = get_rbac_backend().get_utils_class() | ||||||
rbac_utils.assert_user_has_resource_api_permission( | ||||||
user_db=requester_user, | ||||||
resource_api=api_key_api, | ||||||
permission_type=permission_type, | ||||||
) | ||||||
if cfg.CONF.rbac.personal_keys: | ||||||
isKeyCreationAllowed = self.checkPersonalAPIKeyPermission( | ||||||
api_key_api, requester_user | ||||||
) | ||||||
if not isKeyCreationAllowed: | ||||||
LOG.exception( | ||||||
"User does not have permission to create apikey=%s.", api_key_api | ||||||
) | ||||||
abort(http_client.BAD_REQUEST, "") | ||||||
else: | ||||||
rbac_utils.assert_user_has_resource_api_permission( | ||||||
user_db=requester_user, | ||||||
resource_api=api_key_api, | ||||||
permission_type=permission_type, | ||||||
) | ||||||
|
||||||
api_key_db = None | ||||||
api_key = None | ||||||
|
@@ -184,11 +218,21 @@ def put(self, api_key_api, api_key_id_or_key, requester_user): | |||||
|
||||||
permission_type = PermissionType.API_KEY_MODIFY | ||||||
rbac_utils = get_rbac_backend().get_utils_class() | ||||||
rbac_utils.assert_user_has_resource_db_permission( | ||||||
user_db=requester_user, | ||||||
resource_db=api_key_db, | ||||||
permission_type=permission_type, | ||||||
) | ||||||
if cfg.CONF.rbac.personal_keys: | ||||||
isKeyCreationAllowed = self.checkPersonalAPIKeyPermission( | ||||||
api_key_db, requester_user | ||||||
) | ||||||
if not isKeyCreationAllowed: | ||||||
LOG.exception( | ||||||
"User does not have permission to update apikey=%s.", api_key_db | ||||||
) | ||||||
abort(http_client.BAD_REQUEST, "") | ||||||
else: | ||||||
rbac_utils.assert_user_has_resource_db_permission( | ||||||
user_db=requester_user, | ||||||
resource_db=api_key_db, | ||||||
permission_type=permission_type, | ||||||
) | ||||||
|
||||||
old_api_key_db = api_key_db | ||||||
api_key_db = ApiKeyAPI.to_model(api_key_api) | ||||||
|
@@ -233,11 +277,21 @@ def delete(self, api_key_id_or_key, requester_user): | |||||
|
||||||
permission_type = PermissionType.API_KEY_DELETE | ||||||
rbac_utils = get_rbac_backend().get_utils_class() | ||||||
rbac_utils.assert_user_has_resource_db_permission( | ||||||
user_db=requester_user, | ||||||
resource_db=api_key_db, | ||||||
permission_type=permission_type, | ||||||
) | ||||||
if cfg.CONF.rbac.personal_keys: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think having this in here rather than in rbac_utils doesn't work with the idea that the rbac is optional. Perhaps would be better that we always use rbac_utils to ask if user has db permission, and only if that returns false then you'd want to check the personal keys. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you help me as to where to add the code? I cannot understand where the rbac_utils methods are implemented.I have checked base.py in st2common/st2common/rbac/backends/ . Do I need to add the method there, check if conf.rbac.enabled is true and proceed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was more suggesting that you reverse the order in this method. e.g. First call rbac_utils method to see if they have permission to access database. Then if the rbac_utils calls says No, you add in your check for personal keys. There are multiple rbac backends, so if you wanted to add a new method you would need to add into all implementations. The base.py is just the base rbac class which always says not implemented. The noop.py is a always yes utils. Then in the st2-rbac-backend GIT repo you will find the implementation that is used when you really do use RBAC - which uses the rbac roles and auth files. |
||||||
isKeyCreationAllowed = self.checkPersonalAPIKeyPermission( | ||||||
api_key_db, requester_user | ||||||
) | ||||||
if not isKeyCreationAllowed: | ||||||
LOG.exception( | ||||||
"User does not have permission to delete apikey=%s.", api_key_db | ||||||
) | ||||||
abort(http_client.BAD_REQUEST, "") | ||||||
else: | ||||||
rbac_utils.assert_user_has_resource_db_permission( | ||||||
user_db=requester_user, | ||||||
resource_db=api_key_db, | ||||||
permission_type=permission_type, | ||||||
) | ||||||
|
||||||
ApiKey.delete(api_key_db) | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
testu:{SHA}V1t6eZLxnehb7CTBuj61Nq3lIh4= | ||
testu:{SHA}V1t6eZLxnehb7CTBuj61Nq3lIh4= |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,11 @@ def register_opts(ignore_errors=False): | |
"executions. All resources can only be viewed or executed by the owning user " | ||
"except the admin and system_user who can view or run everything.", | ||
), | ||
cfg.BoolOpt( | ||
"personal_keys", | ||
default=True, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above do we want default to be True? As that's not backwards compatible with previous versions of STackStorm? |
||
help="Enable all non-admin users to create/update/delete their own keys. They cannot create/update/delete other's keys.", | ||
), | ||
] | ||
|
||
do_register_opts(rbac_opts, "rbac", ignore_errors) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want the default to be True, or should it be False for backwards compatibility?