Repository navigation
Fix SCIM unfiltered search returning non-imported federated users #51683
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
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
140 changes: 140 additions & 0 deletions
140
scim/tests/base/src/test/java/org/keycloak/tests/scim/tck/FederatedUserTest.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,140 @@ | ||
| package org.keycloak.tests.scim.tck; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.keycloak.common.Profile.Feature; | ||
| import org.keycloak.common.util.MultivaluedHashMap; | ||
| import org.keycloak.representations.idm.ComponentRepresentation; | ||
| import org.keycloak.representations.idm.UserRepresentation; | ||
| import org.keycloak.scim.client.ResourceFilter; | ||
| import org.keycloak.scim.protocol.response.ListResponse; | ||
| import org.keycloak.scim.resource.user.User; | ||
| import org.keycloak.storage.UserStorageProvider; | ||
| import org.keycloak.testframework.annotations.KeycloakIntegrationTest; | ||
| import org.keycloak.testframework.server.KeycloakServerConfig; | ||
| import org.keycloak.testframework.server.KeycloakServerConfigBuilder; | ||
| import org.keycloak.testframework.util.ApiUtil; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.keycloak.storage.UserStorageProviderModel.IMPORT_ENABLED; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.hasItem; | ||
| import static org.hamcrest.Matchers.hasItems; | ||
| import static org.hamcrest.Matchers.not; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| /** | ||
| * Tests that verify SCIM search behavior with non-imported federated users. | ||
| * | ||
| * @see <a href="https://github.com/keycloak/keycloak/issues/51343">Issue 51343</a> | ||
| */ | ||
| @KeycloakIntegrationTest(config = FederatedUserTest.FederatedUserServerConfig.class) | ||
| public class FederatedUserTest extends AbstractScimTest { | ||
|
|
||
| private static final String PROVIDER_ID = "user-password-map-arq"; | ||
|
|
||
| /** | ||
| * Verifies that SCIM filtered and unfiltered searches return consistent results | ||
| * when non-imported federated users exist. | ||
| * | ||
| * Before the fix, unfiltered searches went through UserStorageManager (which | ||
| * queries federated providers), while filtered searches used direct JPA queries | ||
| * against the local UserEntity table - so non-imported federated users would | ||
| * only appear in unfiltered results. | ||
| */ | ||
| @Test | ||
| public void testFilteredAndUnfilteredSearchConsistency() { | ||
| // create local users first, before registering the federation provider - | ||
| // otherwise the provider (which implements UserRegistrationProvider) intercepts | ||
| // user creation and the users end up as federated, not local. | ||
| createLocalUser("local-alice"); | ||
| createLocalUser("local-bob"); | ||
|
|
||
| // register the federation provider with import disabled and create federated users | ||
| String federationComponentId = registerFederationProvider(); | ||
| try { | ||
| String charlieId = createFederatedUser("fed-charlie"); | ||
| String dianaId = createFederatedUser("fed-diana"); | ||
|
|
||
| // unfiltered SCIM search - now uses local storage after the fix | ||
| ListResponse<User> unfilteredResponse = client.users().getAll(); | ||
|
sguilhen marked this conversation as resolved.
Resolved
|
||
| assertNotNull(unfilteredResponse); | ||
| List<String> unfilteredUserNames = toUserNames(unfilteredResponse); | ||
|
|
||
| // filtered SCIM search (broad filter matching all users) - goes through JPA | ||
| String broadFilter = ResourceFilter.filter().pr("userName").build(); | ||
| ListResponse<User> filteredResponse = client.users().getAll(broadFilter); | ||
| assertNotNull(filteredResponse); | ||
| List<String> filteredUserNames = toUserNames(filteredResponse); | ||
|
|
||
| // both local users should appear in both result sets | ||
| assertThat(unfilteredUserNames, hasItems("local-alice", "local-bob")); | ||
| assertThat(filteredUserNames, hasItems("local-alice", "local-bob")); | ||
|
|
||
| // both searches should return the same number of results | ||
| assertEquals(unfilteredResponse.getTotalResults(), filteredResponse.getTotalResults(), | ||
| "Filtered and unfiltered SCIM searches should return the same number of results. " | ||
| + "Unfiltered returned: " + unfilteredUserNames + ", filtered returned: " + filteredUserNames); | ||
|
|
||
| // non-imported federated users should not appear in SCIM results | ||
| assertThat(unfilteredUserNames, not(hasItem("fed-charlie"))); | ||
| assertThat(unfilteredUserNames, not(hasItem("fed-diana"))); | ||
| assertThat(filteredUserNames, not(hasItem("fed-charlie"))); | ||
| assertThat(filteredUserNames, not(hasItem("fed-diana"))); | ||
|
|
||
| // non-imported federated users should not be retrievable by ID either | ||
| assertNull(client.users().get(charlieId), "GET by ID should not return non-imported federated user"); | ||
| assertNull(client.users().get(dianaId), "GET by ID should not return non-imported federated user"); | ||
| } finally { | ||
| realm.admin().components().component(federationComponentId).remove(); | ||
| } | ||
| } | ||
|
|
||
| private List<String> toUserNames(ListResponse<User> response) { | ||
| return response.getResources().stream() | ||
| .map(User::getUserName) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private void createLocalUser(String username) { | ||
| User user = new User(); | ||
| user.setUserName(username); | ||
| user.setActive(true); | ||
| user = client.users().create(user); | ||
| assertNotNull(user); | ||
| } | ||
|
|
||
| private String registerFederationProvider() { | ||
| ComponentRepresentation provider = new ComponentRepresentation(); | ||
| provider.setName("test-user-federation"); | ||
| provider.setProviderId(PROVIDER_ID); | ||
| provider.setProviderType(UserStorageProvider.class.getName()); | ||
| provider.setConfig(new MultivaluedHashMap<String, String>()); | ||
| provider.getConfig().putSingle("priority", Integer.toString(0)); | ||
| provider.getConfig().putSingle(IMPORT_ENABLED, Boolean.toString(false)); | ||
|
sguilhen marked this conversation as resolved.
Resolved
|
||
|
|
||
| return ApiUtil.getCreatedId(realm.admin().components().add(provider)); | ||
|
sguilhen marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| private String createFederatedUser(String username) { | ||
| UserRepresentation user = new UserRepresentation(); | ||
| user.setUsername(username); | ||
| user.setEnabled(true); | ||
| return ApiUtil.getCreatedId(realm.admin().users().create(user)); | ||
| } | ||
|
|
||
| public static class FederatedUserServerConfig implements KeycloakServerConfig { | ||
|
|
||
| @Override | ||
| public KeycloakServerConfigBuilder configure(KeycloakServerConfigBuilder config) { | ||
| return config | ||
| .features(Feature.SCIM_API) | ||
| .dependency("org.keycloak.tests", "keycloak-tests-custom-providers"); | ||
| } | ||
| } | ||
| } | ||
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.