-
Notifications
You must be signed in to change notification settings - Fork 32
Fix Azure DevOps Keycloak PKCE config #676
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
055100d
Fix Azure DevOps Keycloak PKCE config
ak684 2a18643
Add Keycloak realm template CI guard
ak684 d751903
Replace Keycloak guard script with tests
ak684 f0dbddf
Make Keycloak script test extraction indentation-agnostic
ak684 09685f4
Bump OpenHands chart version
ak684 39197ca
Merge remote-tracking branch 'origin/main' into alona/fix-azure-keycl…
ak684 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
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
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,114 @@ | ||
| """Tests for Keycloak realm chart invariants.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import re | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parents[1] | ||
| REALM_TEMPLATE = ( | ||
| REPO_ROOT | ||
| / "charts" | ||
| / "openhands" | ||
| / "files" | ||
| / "allhands-realm-github-provider.json.tmpl" | ||
| ) | ||
| KEYCLOAK_CONFIG_SCRIPT = ( | ||
| REPO_ROOT / "charts" / "openhands" / "templates" / "keycloak-config-script.yaml" | ||
| ) | ||
|
|
||
|
|
||
| def pkce_enabled_providers_missing_method(realm: dict) -> list[str]: | ||
| missing_pkce_method = [] | ||
| for provider in realm.get("identityProviders", []): | ||
| config = provider.get("config") or {} | ||
| if config.get("pkceEnabled") == "true" and not config.get("pkceMethod"): | ||
| missing_pkce_method.append(provider.get("alias", "<unknown>")) | ||
| return missing_pkce_method | ||
|
|
||
|
|
||
| def keycloak_api_call_body(script_template: str) -> str: | ||
| match = re.search( | ||
| r"(?ms)^(?P<indent>[ \t]*)keycloak_api_call\(\) \{\n" | ||
| r"(?P<body>.*?)^(?P=indent)\}", | ||
| script_template, | ||
| ) | ||
| assert match, "Could not find keycloak_api_call() in keycloak-config-script.yaml" | ||
| return match.group("body") | ||
|
|
||
|
|
||
| def assert_pkce_enabled_providers_set_method(realm: dict) -> None: | ||
| missing_pkce_method = pkce_enabled_providers_missing_method(realm) | ||
| assert not missing_pkce_method, ( | ||
| "Identity providers with pkceEnabled=true must set pkceMethod: " | ||
| + ", ".join(missing_pkce_method) | ||
| ) | ||
|
|
||
|
|
||
| def assert_keycloak_api_call_detects_error_message(script_template: str) -> None: | ||
| body = keycloak_api_call_body(script_template) | ||
| assert "errorMessage" in body, ( | ||
| "keycloak_api_call() must treat Keycloak errorMessage responses as errors" | ||
| ) | ||
|
|
||
|
|
||
| def test_realm_template_is_valid_json() -> None: | ||
| json.loads(REALM_TEMPLATE.read_text(encoding="utf-8")) | ||
|
|
||
|
|
||
| def test_pkce_enabled_identity_providers_set_pkce_method() -> None: | ||
| realm = json.loads(REALM_TEMPLATE.read_text(encoding="utf-8")) | ||
| assert_pkce_enabled_providers_set_method(realm) | ||
|
|
||
|
|
||
| def test_pkce_guard_catches_missing_method() -> None: | ||
| realm = { | ||
| "identityProviders": [ | ||
| { | ||
| "alias": "azure_devops", | ||
| "config": {"pkceEnabled": "true"}, | ||
| }, | ||
| { | ||
| "alias": "github", | ||
| "config": {"pkceEnabled": "false"}, | ||
| }, | ||
| ], | ||
| } | ||
|
|
||
| with pytest.raises(AssertionError, match="azure_devops"): | ||
| assert_pkce_enabled_providers_set_method(realm) | ||
|
|
||
|
|
||
| def test_keycloak_api_call_checks_error_message_responses() -> None: | ||
| script_template = KEYCLOAK_CONFIG_SCRIPT.read_text(encoding="utf-8") | ||
| assert_keycloak_api_call_detects_error_message(script_template) | ||
|
|
||
|
|
||
| def test_keycloak_api_call_extraction_is_not_tied_to_yaml_indent() -> None: | ||
| script_template = """\ | ||
| keycloak_api_call() { | ||
| ERROR=$(echo "$RESPONSE" | jq -r '.errorMessage') | ||
| } | ||
| """ | ||
|
|
||
| assert "errorMessage" in keycloak_api_call_body(script_template) | ||
|
|
||
|
|
||
| def test_keycloak_error_guard_catches_missing_error_message() -> None: | ||
| script_template = """\ | ||
| keycloak_api_call() { | ||
| COMMAND=$1 | ||
| export RESPONSE=$(eval $COMMAND) | ||
| ERROR=$(echo "$RESPONSE" | jq -r '.error') | ||
| if [ -n "$ERROR" ] && [ "null" != "$ERROR" ]; then | ||
| exit 1 | ||
| fi | ||
| } | ||
| """ | ||
|
|
||
| with pytest.raises(AssertionError, match="errorMessage"): | ||
| assert_keycloak_api_call_detects_error_message(script_template) |
Oops, something went wrong.
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.