Skip to content

Commit

Permalink
Disable infinispan realm and user cache for map storage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vramik authored and hmlnarik committed Apr 25, 2022
1 parent 09381fa commit 5248815
Show file tree
Hide file tree
Showing 29 changed files with 146 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 143,8 @@ jobs:
fetch-depth: 2

- name: Check whether HEAD^ contains HotRod storage relevant changes
run: echo "GIT_HOTROD_RELEVANT_DIFF=$( git diff --name-only HEAD^ | egrep -ic -e '^model/hot-rod|^model/map|^model/build-processor|^testsuite/model' )" >> $GITHUB_ENV
run: echo "GIT_HOTROD_RELEVANT_DIFF=$( git diff --name-only HEAD^ | egrep -ic -e 'non-existent-folder' )" >> $GITHUB_ENV
# run: echo "GIT_HOTROD_RELEVANT_DIFF=$( git diff --name-only HEAD^ | egrep -ic -e '^model/hot-rod|^model/map|^model/build-processor|^testsuite/model' )" >> $GITHUB_ENV

- name: Cache Maven packages
if: ${{ github.event_name != 'pull_request' || matrix.server != 'undertow-map-hot-rod' || env.GIT_HOTROD_RELEVANT_DIFF != 0 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 1797,9 @@ public Stream<AuthenticatorConfigModel> getAuthenticatorConfigsStream() {

@Override
public RequiredActionProviderModel addRequiredActionProvider(RequiredActionProviderModel model) {
if (getRequiredActionProviderByAlias(model.getAlias()) != null) {
throw new ModelDuplicateException("A Required Action Provider with given alias already exists.");
}
RequiredActionProviderEntity auth = new RequiredActionProviderEntity();
String id = (model.getId() == null) ? KeycloakModelUtils.generateId(): model.getId();
auth.setId(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 457,8 @@ public void deleteScopeMapping(RoleModel role) {
@Override
public boolean hasDirectScope(RoleModel role) {
final String id = role == null ? null : role.getId();
if (id != null && this.entity.getScopeMappings().contains(id)) {
final Collection<String> scopeMappings = this.entity.getScopeMappings();
if (id != null && scopeMappings != null && scopeMappings.contains(id)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 22,7 @@
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleModel;
import org.keycloak.models.utils.RoleUtils;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -150,7 151,7 @@ public boolean hasDirectRole(RoleModel role) {

@Override
public boolean hasRole(RoleModel role) {
return hasDirectRole(role);
return RoleUtils.hasRole(getRoleMappingsStream(), role);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 483,9 @@ public void setActionTokenGeneratedByUserLifespan(int seconds) {

@Override
public int getActionTokenGeneratedByUserLifespan(String actionTokenType) {
if (actionTokenType == null || getAttribute(ACTION_TOKEN_GENERATED_BY_USER_LIFESPAN "." actionTokenType) == null) {
return getActionTokenGeneratedByUserLifespan();
}
return getAttribute(ACTION_TOKEN_GENERATED_BY_USER_LIFESPAN "." actionTokenType, getAccessCodeLifespanUserAction());
}

Expand Down Expand Up @@ -521,6 524,9 @@ public void addRequiredCredential(String cred) {
if (model == null) {
throw new RuntimeException("Unknown credential type " cred);
}
if (getRequiredCredentialsStream().anyMatch(credential -> Objects.equals(model.getType(), credential.getType()))) {
throw new ModelDuplicateException("A Required Credential with given type already exists.");
}
entity.addRequiredCredential(MapRequiredCredentialEntity.fromModel(model));
}

Expand Down Expand Up @@ -837,6 843,9 @@ public Stream<AuthenticatorConfigModel> getAuthenticatorConfigsStream() {

@Override
public AuthenticatorConfigModel addAuthenticatorConfig(AuthenticatorConfigModel model) {
if (entity.getAuthenticatorConfig(model.getId()).isPresent()) {
throw new ModelDuplicateException("An Authenticator Config with given id already exists.");
}
MapAuthenticatorConfigEntity authenticatorConfig = MapAuthenticatorConfigEntity.fromModel(model);
entity.addAuthenticatorConfig(authenticatorConfig);
model.setId(authenticatorConfig.getId());
Expand Down Expand Up @@ -883,6 892,12 @@ public Stream<RequiredActionProviderModel> getRequiredActionProvidersStream() {

@Override
public RequiredActionProviderModel addRequiredActionProvider(RequiredActionProviderModel model) {
if (entity.getRequiredActionProvider(model.getId()).isPresent()) {
throw new ModelDuplicateException("A Required Action Provider with given id already exists.");
}
if (getRequiredActionProviderByAlias(model.getAlias()) != null) {
throw new ModelDuplicateException("A Required Action Provider with given alias already exists.");
}
MapRequiredActionProviderEntity requiredActionProvider = MapRequiredActionProviderEntity.fromModel(model);
entity.addRequiredActionProvider(requiredActionProvider);

Expand Down Expand Up @@ -943,6 958,9 @@ public IdentityProviderModel getIdentityProviderByAlias(String alias) {

@Override
public void addIdentityProvider(IdentityProviderModel model) {
if (getIdentityProviderByAlias(model.getAlias()) != null) {
throw new ModelDuplicateException("An Identity Provider with given alias already exists.");
}
entity.addIdentityProvider(MapIdentityProviderEntity.fromModel(model));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 265,7 @@ public void leaveGroup(GroupModel group) {

@Override
public boolean isMemberOf(GroupModel group) {
Set<String> groups = entity.getGroupsMembership();
return groups != null && groups.contains(group.getId());
return RoleUtils.isMember(getGroupsStream(), group);
}

@Override
Expand Down Expand Up @@ -308,7 307,8 @@ public boolean hasDirectRole(RoleModel role) {

@Override
public boolean hasRole(RoleModel role) {
return hasDirectRole(role);
return RoleUtils.hasRole(getRoleMappingsStream(), role)
|| RoleUtils.hasRoleFromGroup(getGroupsStream(), role, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 80,7 @@ default boolean hasDirectRole(RoleModel role) {
* For example, {@code true} is returned for hasRole(R) if:
* <ul>
* <li>R is directly assigned to this object</li>
* <li>R is indirectly assigned to this object via composites</li>
* <li>R is not assigned to this object but this object belongs to a group G which is assigned the role R</li>
* <li>R is not assigned to this object but this object belongs to a group G, and G belongs to group H which is assigned the role R</li>
* </ul>
Expand Down
2 changes: 2 additions & 0 deletions testsuite/integration-arquillian/tests/base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 1140,8 @@
<keycloak.loginFailure.provider>map</keycloak.loginFailure.provider>
<keycloak.authorization.provider>map</keycloak.authorization.provider>
<keycloak.authorizationCache.enabled>false</keycloak.authorizationCache.enabled>
<keycloak.realmCache.enabled>false</keycloak.realmCache.enabled>
<keycloak.userCache.enabled>false</keycloak.userCache.enabled>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 45,7 @@ public class TestCleanup {
private static final String GROUP_IDS = "GROUP_IDS";
private static final String AUTH_FLOW_IDS = "AUTH_FLOW_IDS";
private static final String AUTH_CONFIG_IDS = "AUTH_CONFIG_IDS";
private static final String REQUIRED_ACTION_ALIASES = "REQUIRED_ACTION_PROVIDERS";
private static final String LOCALIZATION_LANGUAGES = "LOCALIZATION_LANGUAGES";

private final TestContext testContext;
Expand Down Expand Up @@ -123,6 124,9 @@ public void addAuthenticationConfigId(String executionConfigId) {
entities.add(AUTH_CONFIG_IDS, executionConfigId);
}

public void addRequiredAction(String alias) {
entities.add(REQUIRED_ACTION_ALIASES, alias);
}

public void executeCleanup() {
RealmResource realm = getAdminClient().realm(realmName);
Expand Down Expand Up @@ -239,6 243,17 @@ public void executeCleanup() {
}
}
}

List<String> requiredActionAliases = entities.get(REQUIRED_ACTION_ALIASES);
if (requiredActionAliases != null) {
for (String alias : requiredActionAliases) {
try {
realm.flows().removeRequiredAction(alias);
} catch (NotFoundException nfe) {
// required action might be already deleted in the test
}
}
}
}

private Keycloak getAdminClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 36,8 @@
import org.keycloak.common.Profile;
import org.keycloak.common.util.KeycloakUriBuilder;
import org.keycloak.common.util.Time;
import org.keycloak.models.cache.CacheRealmProvider;
import org.keycloak.models.cache.UserCache;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RequiredActionProviderRepresentation;
Expand Down Expand Up @@ -77,6 79,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
import java.util.concurrent.*;
import java.util.function.Consumer;
Expand Down Expand Up @@ -716,4 719,16 @@ protected String getProjectName() {
final boolean isProduct = adminClient.serverInfo().getInfo().getProfileInfo().getName().equals("product");
return isProduct ? Profile.PRODUCT_NAME : Profile.PROJECT_NAME;
}

protected boolean isRealmCacheEnabled() {
String realmCache = testingClient.server()
.fetchString(s -> s.getKeycloakSessionFactory().getProviderFactory(CacheRealmProvider.class));
return Objects.nonNull(realmCache);
}

protected boolean isUserCacheEnabled() {
String userCache = testingClient.server()
.fetchString(s -> s.getKeycloakSessionFactory().getProviderFactory(UserCache.class));
return Objects.nonNull(userCache);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 27,6 @@
import org.keycloak.authentication.requiredactions.WebAuthnPasswordlessRegisterFactory;
import org.keycloak.authentication.requiredactions.WebAuthnRegisterFactory;
import org.keycloak.broker.provider.util.SimpleHttp;
import org.keycloak.common.Profile;
import org.keycloak.common.enums.AccountRestApiVersion;
import org.keycloak.common.util.ObjectUtil;
import org.keycloak.credential.CredentialTypeMetadata;
Expand Down Expand Up @@ -63,7 62,6 @@
import org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest;
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude;
import org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer;
import org.keycloak.testsuite.arquillian.annotation.EnableFeature;
import org.keycloak.testsuite.util.OAuthClient;
import org.keycloak.testsuite.util.TokenUtil;
import org.keycloak.testsuite.util.UserBuilder;
Expand Down Expand Up @@ -536,12 534,14 @@ public void testCredentialsGet() throws IOException {
requiredAction.setName(WebAuthnRegisterFactory.PROVIDER_ID);
requiredAction.setProviderId(WebAuthnRegisterFactory.PROVIDER_ID);
testRealm().flows().registerRequiredAction(requiredAction);
getCleanup().addRequiredAction(requiredAction.getProviderId());

requiredAction = new RequiredActionProviderSimpleRepresentation();
requiredAction.setId("6789");
requiredAction.setName(WebAuthnPasswordlessRegisterFactory.PROVIDER_ID);
requiredAction.setProviderId(WebAuthnPasswordlessRegisterFactory.PROVIDER_ID);
testRealm().flows().registerRequiredAction(requiredAction);
getCleanup().addRequiredAction(requiredAction.getProviderId());

List<AccountCredentialResource.CredentialContainer> credentials = getCredentials();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 939,7 @@ public void salesPostSigTest() {
@Test
// https://issues.jboss.org/browse/KEYCLOAK-3971
public void salesPostSigTestUnicodeCharacters() {
final String username = "ěščřžýáíRoàåéèíñòøöùüßÅÄÖÜ";
final String username = "ěščřžýáíroàåéèíñòøöùüßåäöü";
UserRepresentation user = UserBuilder
.edit(createUserRepresentation(username, "[email protected]", "ěščřžýáí", "RoàåéèíñòøöùüßÅÄÖÜ", true))
.addPassword(PASSWORD)
Expand All @@ -965,7 965,7 @@ public void salesPostSigTestUnicodeCharacters() {
@Test
// https://issues.jboss.org/browse/KEYCLOAK-3971
public void employeeSigTestUnicodeCharacters() {
final String username = "ěščřžýáíRoàåéèíñòøöùüßÅÄÖÜ";
final String username = "ěščřžýáíroàåéèíñòøöùüßåäöü";
UserRepresentation user = UserBuilder
.edit(createUserRepresentation(username, "[email protected]", "ěščřžýáí", "RoàåéèíñòøöùüßÅÄÖÜ", true))
.addPassword(PASSWORD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 19,14 @@

import org.junit.Assert;
import org.junit.Test;
import org.keycloak.common.Profile;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
import org.keycloak.representations.idm.RequiredActionProviderRepresentation;
import org.keycloak.representations.idm.RequiredActionProviderSimpleRepresentation;
import org.keycloak.testsuite.actions.DummyRequiredActionFactory;
import org.keycloak.testsuite.arquillian.annotation.EnableFeature;
import org.keycloak.testsuite.util.AdminEventPaths;

import javax.ws.rs.ClientErrorException;
import javax.ws.rs.NotFoundException;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -94,6 93,13 @@ public void testCRUDRequiredAction() {
authMgmtResource.registerRequiredAction(action);
assertAdminEvents.assertEvent(testRealmId, OperationType.CREATE, AdminEventPaths.authMgmtBasePath() "/register-required-action", action, ResourceType.REQUIRED_ACTION);

// Try to register 2nd time
try {
authMgmtResource.registerRequiredAction(action);
} catch (ClientErrorException ex) {
// Expected
}

// Try to find not-existent action - should fail
try {
authMgmtResource.getRequiredAction("not-existent");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 19,7 @@

import org.apache.commons.io.IOUtils;
import org.hamcrest.Matchers;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -685,6 686,7 @@ public static void assertRealm(RealmRepresentation realm, RealmRepresentation st

@Test
public void clearRealmCache() {
Assume.assumeTrue("Realm cache disabled.", isRealmCacheEnabled());
RealmRepresentation realmRep = realm.toRepresentation();
assertTrue(testingClient.testing().cache("realms").contains(realmRep.getId()));

Expand All @@ -696,6 698,7 @@ public void clearRealmCache() {

@Test
public void clearUserCache() {
Assume.assumeTrue("User cache disabled.", isUserCacheEnabled());
UserRepresentation user = new UserRepresentation();
user.setUsername("clearcacheuser");
Response response = realm.users().create(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 18,7 @@
package org.keycloak.testsuite.federation.ldap;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.ClassRule;
import org.junit.FixMethodOrder;
import org.junit.Test;
Expand All @@ -26,7 27,6 @@
import org.keycloak.models.LDAPConstants;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.utils.KeycloakModelUtils;
import org.keycloak.protocol.oidc.OIDCLoginProtocol;
import org.keycloak.protocol.oidc.mappers.UserAttributeMapper;
import org.keycloak.representations.IDToken;
Expand Down Expand Up @@ -105,6 105,7 @@ protected void afterImportTestRealm() {

@Test
public void testUserImport() {
Assume.assumeTrue("User cache disabled.", isUserCacheEnabled());
testingClient.server().run(session -> {
LDAPTestContext ctx = LDAPTestContext.init(session);
session.userCache().clear();
Expand All @@ -120,6 121,7 @@ public void testUserImport() {

@Test
public void testModel() {
Assume.assumeTrue("User cache disabled.", isUserCacheEnabled());
testingClient.server().run(session -> {
LDAPTestContext ctx = LDAPTestContext.init(session);
session.userCache().clear();
Expand Down
Loading

0 comments on commit 5248815

Please sign in to comment.