-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| name: test-functional-keycloak | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ main ] | ||
| pull_request: | ||
| branches: [ main ] | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-22.04 | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v6 | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@v6 | ||
| with: | ||
| python-version: 3.12 | ||
|
|
||
| - name: Upgrade and install packages | ||
| run: | | ||
| bash ./ci/setup-ubuntu.sh | ||
|
|
||
| - name: Install Keycloak | ||
| run: | | ||
| bash ./ci/setup-keycloak.sh | ||
|
|
||
| - name: Install ColdFront and plugin | ||
| run: | | ||
| ./ci/setup.sh | ||
|
|
||
| - name: Run functional tests | ||
| run: | | ||
| ./ci/run_functional_tests_keycloak.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #!/bin/bash | ||
| set -xe | ||
|
|
||
| if [[ ! "${CI}" == "true" ]]; then | ||
| source /tmp/coldfront_venv/bin/activate | ||
| fi | ||
|
|
||
| export DJANGO_SETTINGS_MODULE="local_settings" | ||
| export PYTHONWARNINGS="ignore:Unverified HTTPS request" | ||
|
|
||
| export KEYCLOAK_BASE_URL="http://localhost:8080" | ||
| export KEYCLOAK_REALM="master" | ||
| export KEYCLOAK_CLIENT_ID="coldfront" | ||
| export KEYCLOAK_CLIENT_SECRET="nomoresecret" | ||
|
|
||
| coverage run --source="." -m django test coldfront_plugin_cloud.tests.functional.keycloak | ||
| coverage report |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -xe | ||
|
|
||
| sudo docker rm -f keycloak | ||
|
|
||
| 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 -fsS http://localhost:8080/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" | ||
| } | ||
| ]' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,4 @@ python-novaclient | |
| python-neutronclient | ||
| python-swiftclient | ||
| pytz | ||
| requests | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import os | ||
| import functools | ||
|
|
||
| import requests | ||
| from pydantic import BaseModel, ConfigDict, RootModel | ||
|
|
||
|
|
||
| class KeyCloakGroup(BaseModel): | ||
| """Keycloak group response model""" | ||
|
|
||
| model_config = ConfigDict(extra="allow") | ||
| id: str | ||
| name: str | ||
|
|
||
|
|
||
| class GroupResponse(RootModel): | ||
| """Wrapper for group list responses""" | ||
|
|
||
| root: list[KeyCloakGroup] | ||
|
|
||
|
|
||
| class KeyCloakUser(BaseModel): | ||
| """Keycloak user response model""" | ||
|
|
||
| model_config = ConfigDict(extra="allow") | ||
| id: str | ||
| username: str | ||
|
|
||
|
|
||
| class UserResponse(RootModel): | ||
| """Wrapper for user list responses""" | ||
|
|
||
| root: list[KeyCloakUser] | ||
|
|
||
|
|
||
| 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") | ||
| self.client_secret = os.getenv("KEYCLOAK_CLIENT_SECRET") | ||
|
|
||
| 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) | ||
| r.raise_for_status() | ||
| headers = { | ||
| "Authorization": ("Bearer %s" % r.json()["access_token"]), | ||
| "Content-Type": "application/json", | ||
| } | ||
| session = requests.session() | ||
| session.headers.update(headers) | ||
| return session | ||
|
QuanMPhm marked this conversation as resolved.
|
||
|
|
||
| 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) | ||
|
naved001 marked this conversation as resolved.
|
||
|
|
||
| # If group already exists, ignore and move on | ||
| if response.status_code not in (201, 409): | ||
| response.raise_for_status() | ||
|
|
||
| def get_group_id(self, group_name) -> str | None: | ||
| """Return None if group not found""" | ||
| query = { | ||
| "search": group_name, | ||
| "exact": "true", | ||
| } | ||
| url = f"{self.base_url}/admin/realms/{self.realm}/groups" | ||
| r = self.api_client.get(url, params=query) | ||
| r.raise_for_status() | ||
| groups = GroupResponse.model_validate(r.json()) | ||
| return groups.root[0].id if groups.root else None | ||
|
|
||
| def get_user_id(self, cf_username) -> str | None: | ||
| """Return None if user not found""" | ||
| # (Quan) Coldfront usernames map to Keycloak usernames | ||
| # https://github.com/nerc-project/coldfront-plugin-cloud/pull/249#discussion_r2953393852 | ||
| query = {"username": cf_username, "exact": "true"} | ||
| url = f"{self.base_url}/admin/realms/{self.realm}/users" | ||
| r = self.api_client.get(url, params=query) | ||
| r.raise_for_status() | ||
| users = UserResponse.model_validate(r.json()) | ||
| return users.root[0].id if users.root 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() | ||
|
knikolla marked this conversation as resolved.
|
||
|
|
||
| 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() | ||
|
knikolla marked this conversation as resolved.
|
||
|
|
||
| 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() | ||
| groups = GroupResponse.model_validate(r.json()) | ||
| return [group.name for group in groups.root] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
You are viewing a condensed version of this merge commit. You can view the full changes here.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.