-
Notifications
You must be signed in to change notification settings - Fork 13
Allow pushing user-allocation membership to Keycloak #249
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
base: main
Are you sure you want to change the base?
Conversation
|
|
||
| def get_user_id(self, cf_username) -> str | None: | ||
| """Return None if user not found""" | ||
| # TODO (Quan): Confirm that Coldfront usernames map to Keycloak emails, not email, or something else? |
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.
| user_id = self.kc_admin_client.get_user_id(user.username) | ||
| assert project_id in self.kc_admin_client.get_user_groups(user_id) | ||
|
|
||
| # TODO (Quan): Confirm that user should also be removed from group on role removal |
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.
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.
Yes
9a53156 to
d7da5c4
Compare
|
@knikolla Two more questions:
|
| @functools.cached_property | ||
| def api_client(self): | ||
| params = { | ||
| "grant_type": "password", |
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.
See if you can use the client credentials flow instead of admin password here.
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.
With the current version of Keycloak being used for the integration test (25.0), the Keycloak container doesn't seem to have a way to easily configure a client with access to the REST API. I could write a CI script that calls the API to create said client. Based on the latest Keycloak documentation, another option could be to use a more up-to-date version of Keycloak, which has an option to configure an admin client?
src/coldfront_plugin_cloud/tasks.py
Outdated
|
|
||
| allocator.set_quota(project_id) | ||
|
|
||
| # After setting everything on cluster, add user to Keycloak group |
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.
This function is growing too long (doing too many things) and I think there might a better place for the code in this block.
Perhaps the base class in base.py can abstract some of the duties, among create_project, get_or_create_federated_user and assign_role_on_user.
| user_id = self.kc_admin_client.get_user_id(user.username) | ||
| assert project_id in self.kc_admin_client.get_user_groups(user_id) | ||
|
|
||
| # TODO (Quan): Confirm that user should also be removed from group on role removal |
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.
Yes
d7da5c4 to
3b80589
Compare
|
@knikolla I've addressed your comments except one. Also, do you have responses to these questions? |
A Keycloak admin client has been added When `activate_allocation` is called, the user is added to a Keycloak group named after the project ID on the remote cluster. If the user does not already exist in Keycloak, the case is ignored for now When `deactivate_allocation` is called, the user is removed from the Keycloak group Unit tests have been updated to remove dependancy on Keycloak A comment in `validate_allocations` has been updated to reflect the more restrictive validation behavior, where users on cluster projects will be removed if they are not part of the Coldfront allocation (rather than if they are registered on Coldfront at all). This is relevant for functional tests for this new feature.
3b80589 to
cb1d628
Compare
| def assign_role_on_user(self, username, project_id): | ||
| self.kc_admin_client.create_group(project_id) | ||
| if user_id := self.kc_admin_client.get_user_id(username): | ||
| group_id = self.kc_admin_client.get_group_id(project_id) | ||
| self.kc_admin_client.add_user_to_group(user_id, group_id) | ||
| else: | ||
| logger.warning( | ||
| f"User {username} not found in Keycloak, cannot add to group." | ||
| ) | ||
|
|
||
| def remove_role_from_user(self, username, project_id): | ||
| user_id = self.kc_admin_client.get_user_id(username) | ||
| group_id = self.kc_admin_client.get_group_id(project_id) | ||
| self.kc_admin_client.remove_user_from_group(user_id, group_id) | ||
|
|
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.
@knikolla @naved001 The Openstack functional tests are failing because the plugin tries to add the coldfront-swift-init user to Openstack projects. The user is added to the cluster proejct, but since they're not registered on Keycloak, they're not added to the Keycloak group. This causes remove_role_from_user() in src/coldfront_plugin_cloud/base.py to raise an 404 error when it uses the Keycloak API to add a non-existant user to a group.
This can be resolved if we allow remove_role_from_user() to ignore if the user is not found, which was the agreed behavior for assign_role_on_user(). Is that acceptable?
Closes nerc-project/operations#948. More details in the commit message
There are still some questions I have below, so this is still a draft for now.