-
Notifications
You must be signed in to change notification settings - Fork 14
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?
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -xe | ||
|
|
||
| sudo docker run -d --name keycloak \ | ||
| -e KEYCLOAK_ADMIN=admin \ | ||
| -e KEYCLOAK_ADMIN_PASSWORD=nomoresecret \ | ||
| -p 8080:8080 \ | ||
| -p 8443:8443 \ | ||
| quay.io/keycloak/keycloak:25.0 start-dev | ||
|
|
||
| # wait for keycloak to be ready | ||
| until curl -s http://localhost:8080/auth/realms/master; do | ||
| echo "Waiting for Keycloak to be ready..." | ||
| sleep 5 | ||
| done | ||
|
|
||
| # Create client and add admin role to client's service account | ||
| ACCESS_TOKEN=$(curl -X POST "http://localhost:8080/realms/master/protocol/openid-connect/token" \ | ||
| -d "username=admin" \ | ||
| -d "password=nomoresecret" \ | ||
| -d "grant_type=password" \ | ||
| -d "client_id=admin-cli" \ | ||
| -d "scope=openid" \ | ||
| | jq -r '.access_token') | ||
|
|
||
|
|
||
| curl -X POST "http://localhost:8080/admin/realms/master/clients" \ | ||
| -H "Authorization: Bearer $ACCESS_TOKEN" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "clientId": "coldfront", | ||
| "secret": "nomoresecret", | ||
| "redirectUris": ["http://localhost:8080/*"], | ||
| "serviceAccountsEnabled": true | ||
| }' | ||
|
|
||
| COLDFRONT_CLIENT_ID=$(curl -X GET "http://localhost:8080/admin/realms/master/clients?clientId=coldfront" \ | ||
| -H "Authorization: Bearer $ACCESS_TOKEN" | jq -r '.[0].id') | ||
|
|
||
|
|
||
| COLDFRONT_SERVICE_ACCOUNT_ID=$(curl -X GET "http://localhost:8080/admin/realms/master/clients/$COLDFRONT_CLIENT_ID/service-account-user" \ | ||
| -H "Authorization: Bearer $ACCESS_TOKEN" \ | ||
| -H "Content-Type: application/json" \ | ||
| | jq -r '.id') | ||
|
|
||
| ADMIN_ROLE_ID=$(curl -X GET "http://localhost:8080/admin/realms/master/roles/admin" \ | ||
| -H "Authorization: Bearer $ACCESS_TOKEN" | jq -r '.id') | ||
|
|
||
| # Add admin role to the service account user | ||
| curl -X POST "http://localhost:8080/admin/realms/master/users/$COLDFRONT_SERVICE_ACCOUNT_ID/role-mappings/realm" \ | ||
| -H "Authorization: Bearer $ACCESS_TOKEN" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '[ | ||
| { | ||
| "id": "'$ADMIN_ROLE_ID'", | ||
| "name": "admin" | ||
| } | ||
| ]' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,4 @@ python-novaclient | |
| python-neutronclient | ||
| python-swiftclient | ||
| pytz | ||
| requests | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import os | ||
| import functools | ||
|
|
||
| import requests | ||
|
|
||
|
|
||
| class KeyCloakAPIClient: | ||
| def __init__(self): | ||
| self.base_url = os.getenv("KEYCLOAK_BASE_URL") | ||
| self.realm = os.getenv("KEYCLOAK_REALM") | ||
| self.client_id = os.getenv("KEYCLOAK_CLIENT_ID", "coldfront") | ||
| self.client_secret = os.getenv("KEYCLOAK_CLIENT_SECRET", "nomoresecret") | ||
|
|
||
| self.token_url = ( | ||
| f"{self.base_url}/realms/{self.realm}/protocol/openid-connect/token" | ||
| ) | ||
|
|
||
| @functools.cached_property | ||
| def api_client(self): | ||
| params = { | ||
| "grant_type": "client_credentials", | ||
| "client_id": self.client_id, | ||
| "client_secret": self.client_secret, | ||
| } | ||
| r = requests.post(self.token_url, data=params).json() | ||
| headers = { | ||
| "Authorization": ("Bearer %s" % r["access_token"]), | ||
| "Content-Type": "application/json", | ||
| } | ||
| session = requests.session() | ||
| session.headers.update(headers) | ||
| return session | ||
|
|
||
| def create_group(self, group_name): | ||
| url = f"{self.base_url}/admin/realms/{self.realm}/groups" | ||
| payload = {"name": group_name} | ||
| response = self.api_client.post(url, json=payload) | ||
|
|
||
| # If group already exists, ignore and move on | ||
| if response.status_code not in (201, 409): | ||
| response.raise_for_status() | ||
|
|
||
| def create_user(self, cf_username): | ||
| """Helper function to create user in Keycloak, for testing purposes only""" | ||
| url = f"{self.base_url}/admin/realms/{self.realm}/users" | ||
| payload = { | ||
| "username": cf_username, | ||
| "enabled": True, | ||
| "email": cf_username, | ||
| } | ||
| r = self.api_client.post(url, json=payload) | ||
| r.raise_for_status() | ||
|
|
||
| def get_group_id(self, group_name) -> str | None: | ||
| """Return None if group not found""" | ||
| query = f"search={group_name}&exact=true" | ||
| url = f"{self.base_url}/admin/realms/{self.realm}/groups?{query}" | ||
| r = self.api_client.get(url).json() | ||
| return r[0]["id"] if r else None | ||
|
|
||
| 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? | ||
|
Contributor
Author
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.
Collaborator
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. @QuanMPhm Coldfront usernames map to Keycloak usernames, not emails. |
||
| query = f"username={cf_username}&exact=true" | ||
| url = f"{self.base_url}/admin/realms/{self.realm}/users?{query}" | ||
| r = self.api_client.get(url).json() | ||
| return r[0]["id"] if r else None | ||
|
|
||
| def add_user_to_group(self, user_id, group_id): | ||
| url = f"{self.base_url}/admin/realms/{self.realm}/users/{user_id}/groups/{group_id}" | ||
| r = self.api_client.put(url) | ||
| r.raise_for_status() | ||
|
|
||
| def remove_user_from_group(self, user_id, group_id): | ||
| url = f"{self.base_url}/admin/realms/{self.realm}/users/{user_id}/groups/{group_id}" | ||
| r = self.api_client.delete(url) | ||
| r.raise_for_status() | ||
|
|
||
| def get_user_groups(self, user_id) -> list[str]: | ||
| url = f"{self.base_url}/admin/realms/{self.realm}/users/{user_id}/groups" | ||
| r = self.api_client.get(url) | ||
| r.raise_for_status() | ||
| return [group["name"] for group in r.json()] | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -364,6 +364,8 @@ def assign_role_on_user(self, username, project_id): | |||
| # Role already exists, ignore | ||||
| pass | ||||
|
|
||||
| super().assign_role_on_user(username, project_id) | ||||
|
Collaborator
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. Instead of the resource allocators, I think this should be handled in
|
||||
|
|
||||
| def remove_role_from_user(self, username, project_id): | ||||
| """Remove a role from a user in a project using direct OpenShift API calls""" | ||||
| try: | ||||
|
|
@@ -386,6 +388,8 @@ def remove_role_from_user(self, username, project_id): | |||
| # Rolebinding doesn't exist, nothing to remove | ||||
| pass | ||||
|
|
||||
| super().remove_role_from_user(username, project_id) | ||||
|
|
||||
| def _create_project(self, project_name, project_id): | ||||
| pi_username = self.allocation.project.pi.username | ||||
|
|
||||
|
|
||||
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.
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.