Repository navigation
Migrate authz tests to new framework #51836
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
Open
edewit
wants to merge
35
commits into
keycloak:main
Choose a base branch
from
edewit:migration-authz-50754
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
bd4dbfe
Migrate authz package tests to the new testsuite (part 1)
edewit 61a240b
Migrate authz admin AuthorizationTest to the new testsuite
edewit 4c1e9ef
Migrate authz package tests to the new testsuite (part 2)
edewit e2b9bcb
fixed tests
edewit 21988ea
pr review comments
edewit ca1dca9
fix authz migration CI regressions
edewit 3f077f8
test fix
edewit 497c0e4
fix merge error
edewit 7d40312
pr review
edewit 731089a
Restore crypto provider after AuthzClientTest
edewit a1b7c7f
Wire authz admin tests through AbstractAuthzTest realm import
edewit 3a5102d
Fix ResourceManagementWithAuthzClientTest authz config method clash
edewit 6ba30e5
Fix authz Base2 suite provider and realm setup issues
edewit dc2a335
Apply spotless formatting to authz test files
edewit 7445f4c
Fix authz Base2 URL rewriting, scripts, and realm setup
edewit a17709e
Address authz migration review feedback and fix test compilation
edewit 44b3c4a
Apply spotless formatting to authz review fixes
edewit 7236d6b
Fix adminClient field shadowing in AbstractAuthzTest
edewit 461449a
Deploy testsuite providers jar for authz Base2 tests
edewit c7d8f37
Fix authz test URLs and realm import hooks for Base2 suite
edewit f3a3b83
Use testing event queue and JUnit Jupiter in authz tests
edewit 6a30f02
Enable test events listener on imported authz realms
edewit 008894c
Deploy authz script policies and fix remaining Base2 failures
edewit 4f4886c
Avoid enabling AUTHORIZATION in Base2 suite config
edewit ad799d7
Fix authz Base2 CI failures and shared server config
edewit 94e2414
Address review comments for authz test migration
edewit 9745851
moved
edewit 84895d2
Fix authz event polling for non-default event types
edewit 6e52e2e
removed migrated constuctor that's no longer used
edewit 3c74323
added assertion to see if hack still works
edewit 839cdf2
Document why testframework surefire args include --add-opens
edewit 534c9d8
Fix invalid XML comment in pom.xml that broke Maven parsing
edewit baf845f
Replace leftover Arquillian testingClient calls in authz tests
edewit 4d52269
Drop unused Keycloak IT setup from local authz unit tests
edewit 1ccbbec
Remove unused AdminApiUtil import after rebase onto upstream/main
edewit 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
242 changes: 242 additions & 0 deletions
242
tests/base/src/test/java/org/keycloak/tests/authz/AbstractAuthzTest.java
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,242 @@ | ||
| package org.keycloak.tests.authz; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.InputStream; | ||
| import java.lang.reflect.Field; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.IdentityHashMap; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import org.keycloak.admin.client.Keycloak; | ||
| import org.keycloak.admin.client.resource.AuthorizationResource; | ||
| import org.keycloak.jose.jws.JWSInput; | ||
| import org.keycloak.jose.jws.JWSInputException; | ||
| import org.keycloak.models.utils.KeycloakModelUtils; | ||
| import org.keycloak.representations.AccessToken; | ||
| import org.keycloak.representations.idm.ClientRepresentation; | ||
| import org.keycloak.representations.idm.EventRepresentation; | ||
| import org.keycloak.representations.idm.RealmRepresentation; | ||
| import org.keycloak.representations.idm.authorization.PolicyRepresentation; | ||
| import org.keycloak.testframework.annotations.InjectAdminClient; | ||
| import org.keycloak.testframework.oauth.OAuthClient; | ||
| import org.keycloak.testframework.oauth.SectorIdentifierRedirectUrisProvider; | ||
| import org.keycloak.testframework.oauth.annotations.InjectOAuthClient; | ||
| import org.keycloak.testframework.oauth.annotations.InjectSectorIdentifierRedirectUrisProvider; | ||
| import org.keycloak.testframework.realm.ManagedRealm; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
|
|
||
| /** | ||
| * @author mhajas | ||
| */ | ||
| public abstract class AbstractAuthzTest extends AuthzTestRealmSupport { | ||
|
|
||
| private final List<String> importedRealmNames = new ArrayList<>(); | ||
| private final LinkedList<EventRepresentation> eventQueue = new LinkedList<>(); | ||
| private final Set<String> processedEventIds = new HashSet<>(); | ||
|
|
||
| @InjectAdminClient | ||
| protected Keycloak injectedAdminClient; | ||
|
|
||
| @InjectOAuthClient | ||
| protected OAuthClient oauth; | ||
|
edewit marked this conversation as resolved.
Resolved
|
||
|
|
||
| @InjectSectorIdentifierRedirectUrisProvider | ||
| protected SectorIdentifierRedirectUrisProvider sectorIdentifierRedirectUrisProvider; | ||
|
|
||
| @BeforeEach | ||
| public void beforeAuthzTest() { | ||
| adminClient = injectedAdminClient; | ||
| // OAuthClient is CLASS-scoped; clear mutable state left by earlier test methods. | ||
| oauth.scope(null); | ||
| importedRealmNames.clear(); | ||
|
edewit marked this conversation as resolved.
Resolved
|
||
| eventQueue.clear(); | ||
| processedEventIds.clear(); | ||
| testRealmReps = new ArrayList<>(); | ||
| addTestRealms(testRealmReps); | ||
| ensureInjectedOAuthRedirectUris(testRealmReps); | ||
| importTestRealms(); | ||
| testRealmReps.forEach(r -> importedRealmNames.add(r.getRealm())); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void afterAuthzTest() { | ||
| try { | ||
| runManagedCleanupBeforeRealmRemoval(); | ||
| } finally { | ||
| for (String realmName : importedRealmNames) { | ||
| removeRealm(realmName); | ||
| } | ||
| importedRealmNames.clear(); | ||
| eventQueue.clear(); | ||
| processedEventIds.clear(); | ||
| } | ||
|
edewit marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| protected void runManagedCleanupBeforeRealmRemoval() { | ||
| Set<ManagedRealm> processedManagedRealms = Collections.newSetFromMap(new IdentityHashMap<>()); | ||
|
|
||
| for (Class<?> type = getClass(); type != null; type = type.getSuperclass()) { | ||
| for (Field field : type.getDeclaredFields()) { | ||
| if (!ManagedRealm.class.isAssignableFrom(field.getType())) { | ||
| continue; | ||
| } | ||
|
|
||
| field.setAccessible(true); | ||
| try { | ||
| ManagedRealm managedRealm = (ManagedRealm) field.get(this); | ||
| if (managedRealm != null && processedManagedRealms.add(managedRealm)) { | ||
| managedRealm.runCleanup(); | ||
| } | ||
| } catch (IllegalAccessException e) { | ||
| throw new RuntimeException("Failed to run managed realm cleanup", e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected AccessToken toAccessToken(String rpt) { | ||
| AccessToken accessToken; | ||
|
|
||
| try { | ||
| accessToken = new JWSInput(rpt).readJsonContent(AccessToken.class); | ||
| } catch (JWSInputException cause) { | ||
| throw new RuntimeException("Failed to deserialize RPT", cause); | ||
| } | ||
| return accessToken; | ||
| } | ||
|
|
||
| protected PolicyRepresentation createAlwaysGrantPolicy(AuthorizationResource authorization) { | ||
| PolicyRepresentation policy = new PolicyRepresentation(); | ||
| policy.setName(KeycloakModelUtils.generateId()); | ||
| policy.setType("always-grant"); | ||
| authorization.policies().create(policy).close(); | ||
| return policy; | ||
| } | ||
|
|
||
| protected PolicyRepresentation createAlwaysDenyPolicy(AuthorizationResource authorization) { | ||
| PolicyRepresentation policy = new PolicyRepresentation(); | ||
| policy.setName(KeycloakModelUtils.generateId()); | ||
| policy.setType("always-deny"); | ||
| authorization.policies().create(policy).close(); | ||
| return policy; | ||
| } | ||
|
|
||
| protected PolicyRepresentation createOnlyOwnerPolicy(AuthorizationResource authorization) { | ||
| PolicyRepresentation PolicyRepresentation(); | ||
|
|
||
| onlyOwnerPolicy.setName(KeycloakModelUtils.generateId()); | ||
| onlyOwnerPolicy.setType("allow-resource-owner"); | ||
|
|
||
| authorization.policies().create(onlyOwnerPolicy).close(); | ||
|
|
||
| return onlyOwnerPolicy; | ||
| } | ||
|
|
||
| protected InputStream authzConfigurationStream(InputStream input) { | ||
| try { | ||
| String authServerRoot = authServerRoot(); | ||
| String config = new String(input.readAllBytes(), StandardCharsets.UTF_8) | ||
| .replace("http://localhost:8180/auth", authServerRoot) | ||
| .replace("https://localhost:8543/auth", authServerRoot); | ||
| return new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8)); | ||
|
Comment on lines
+145
to
+149
|
||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to read authz configuration", e); | ||
| } | ||
| } | ||
|
|
||
| private void ensureInjectedOAuthRedirectUris(List<RealmRepresentation> realms) { | ||
| String redirectUri = oauth.getRedirectUri(); | ||
| if (redirectUri == null || redirectUri.isBlank()) { | ||
| return; | ||
| } | ||
|
|
||
| for (RealmRepresentation realm : realms) { | ||
| if (realm.getClients() == null) { | ||
| continue; | ||
| } | ||
|
|
||
| for (ClientRepresentation client : realm.getClients()) { | ||
| if (!Boolean.TRUE.equals(client.isPublicClient())) { | ||
| continue; | ||
| } | ||
|
|
||
| if (client.getRedirectUris() == null) { | ||
| client.setRedirectUris(new ArrayList<>()); | ||
| } | ||
| if (!client.getRedirectUris().contains(redirectUri)) { | ||
| client.getRedirectUris().add(redirectUri); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected String authServerRoot() { | ||
| String root = oauth.getBaseUrl(); | ||
| int realmSegmentIndex = root.indexOf("/realms/"); | ||
| if (realmSegmentIndex >= 0) { | ||
| root = root.substring(0, realmSegmentIndex); | ||
| } | ||
| return root; | ||
| } | ||
|
|
||
| protected String pairwiseSectorIdentifierUri() { | ||
| return sectorIdentifierRedirectUrisProvider.getUri(); | ||
| } | ||
|
|
||
| protected void configureSectorIdentifierRedirectUris(String... redirectUris) { | ||
| sectorIdentifierRedirectUrisProvider.setSectorIdentifierRedirectUris(Arrays.asList(redirectUris)); | ||
| } | ||
|
|
||
| protected EventRepresentation pollTestEvent() { | ||
| long deadline = System.currentTimeMillis() + 5000; | ||
| while (System.currentTimeMillis() < deadline) { | ||
| fetchEventsIntoQueue(); | ||
| EventRepresentation event = eventQueue.poll(); | ||
| if (event != null) { | ||
| return event; | ||
| } | ||
| try { | ||
| Thread.sleep(50); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| break; | ||
| } | ||
| } | ||
| fetchEventsIntoQueue(); | ||
| return eventQueue.poll(); | ||
| } | ||
|
|
||
| protected void clearTestEvents() { | ||
| eventQueue.clear(); | ||
| processedEventIds.clear(); | ||
| for (String realmName : importedRealmNames) { | ||
| try { | ||
| adminClient.realm(realmName).clearEvents(); | ||
| } catch (Exception ignore) { | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void fetchEventsIntoQueue() { | ||
| for (String realmName : importedRealmNames) { | ||
| List<EventRepresentation> events = adminClient.realm(realmName) | ||
| .getEvents(null, null, null, null, null, null, null, null, "asc"); | ||
| if (events == null) { | ||
| continue; | ||
| } | ||
| for (EventRepresentation event : events) { | ||
| if (event.getId() != null && processedEventIds.add(event.getId())) { | ||
| eventQueue.add(event); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.