Skip to content

Allow pushing user-allocation membership to Keycloak#249

Open
QuanMPhm wants to merge 1 commit intonerc-project:mainfrom
QuanMPhm:ops_948/auth_kc
Open

Allow pushing user-allocation membership to Keycloak#249
QuanMPhm wants to merge 1 commit intonerc-project:mainfrom
QuanMPhm:ops_948/auth_kc

Conversation

@QuanMPhm
Copy link
Copy Markdown
Contributor

@QuanMPhm QuanMPhm commented Oct 3, 2025

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.


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?
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QuanMPhm Coldfront usernames map to Keycloak usernames, not emails.

@QuanMPhm
Copy link
Copy Markdown
Contributor Author

QuanMPhm commented Oct 6, 2025

@knikolla Two more questions:

  1. Do we also want validate_allocations to add PIs to Keycloak groups for pre-existing allocations?
  2. When a PI adds a user to an Coldfront project or allocation, do those users also get added to a the project's Keycloak group?

@QuanMPhm
Copy link
Copy Markdown
Contributor Author

@knikolla I've addressed your comments except one. Also, do you have responses to these questions?

Comment on lines +40 to +54
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)

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

@knikolla
Copy link
Copy Markdown
Collaborator

@QuanMPhm please resolve conflicts. Are there any questions that I missed answering?

@QuanMPhm
Copy link
Copy Markdown
Contributor Author

@knikolla Just this one. I will resolve the conflicts while waiting for your answer

@knikolla
Copy link
Copy Markdown
Collaborator

@knikolla Just this one. I will resolve the conflicts while waiting for your answer

Responded.

@QuanMPhm QuanMPhm force-pushed the ops_948/auth_kc branch 2 times, most recently from a217f31 to 0358cb7 Compare March 20, 2026 20:28
@QuanMPhm QuanMPhm marked this pull request as ready for review March 20, 2026 20:41
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

Authentication to Keycloak is done via client credentials grant

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 not registered on Coldfront at all). This is relevant
for functional tests for this new feature.
# Role already exists, ignore
pass

super().assign_role_on_user(username, project_id)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the resource allocators, I think this should be handled in

def add_user_to_allocation(allocation_user_pk):

return user

def assign_role_on_user(self, username, project_id):
self.kc_admin_client.create_group(project_id)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using the group ID, how about introducing a new Resource Attribute to Resources that accepts a format string. For example defaulting to "{resource_name}/{project_name}"

This would allow operator to specify their own format for group names for the clusters.

So you'd read the resource attribute and then call format on the string providing a few documented options for the available variables.

>>> template = "{resource_name}/{project_name}"
>>> group = template.format(**{"resource_name": "NERC-OCP", "project_name": "1234"}
... )
>>> 
>>> group
'NERC-OCP/1234'

Copy link
Copy Markdown
Collaborator

@knikolla knikolla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a quick first pass and provided some comments.

Also this needs to be possible configurable via a setting.

@knikolla
Copy link
Copy Markdown
Collaborator

@QuanMPhm Actually, another thought, do you think it would make sense to implement this in the Keycloak plugin? https://github.com/nerc-project/coldfront-plugin-keycloak

It could listen to signals in the same way that the cloud plugin listens to signals. It already has a keycloak client implemented.

And there is nothing in pushing users to a Keycloak group that is specific to either OpenShift or OpenStack.

@QuanMPhm
Copy link
Copy Markdown
Contributor Author

@knikolla I see that it does make sense to seperate the Keycloak functionality from the rest of the plugin. It makes sense to me. I forgot that repo existed. There would need to be some overhaul to add integration and unit tests to coldfront-plugin-keycloak. Is that fine?

@knikolla
Copy link
Copy Markdown
Collaborator

@knikolla I see that it does make sense to seperate the Keycloak functionality from the rest of the plugin. It makes sense to me. I forgot that repo existed. There would need to be some overhaul to add integration and unit tests to coldfront-plugin-keycloak. Is that fine?

For now let's keep it here (as not to frontload the work) and we can easily split it out later if needed. Perhaps try implementing it here via signals so as to keep it loosely coupled so that if we need to split it later it doesn't require a lot of uncoupling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate centralizing authorization for NERC users in Keycloak

2 participants