Make sure realm is available from session when migrating to 23

Closes #25183

Signed-off-by: Pedro Igor <pigor.craveiro@gmail.com>
This commit is contained in:
Pedro Igor 2023-12-05 15:54:39 -03:00
parent c5ee3dc0ad
commit ab1173182c
7 changed files with 64 additions and 2 deletions

View file

@ -25,6 +25,7 @@ import org.jboss.logging.Logger;
import org.keycloak.authentication.AuthenticationFlow;
import org.keycloak.component.ComponentModel;
import org.keycloak.migration.ModelVersion;
import org.keycloak.models.KeycloakContext;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.representations.idm.RealmRepresentation;
@ -43,7 +44,16 @@ public class MigrateTo23_0_0 implements Migration {
@Override
public void migrate(KeycloakSession session) {
session.realms().getRealmsStream().forEach(this::migrateRealm);
session.realms().getRealmsStream().forEach(realm -> {
KeycloakContext context = session.getContext();
try {
context.setRealm(realm);
migrateRealm(realm);
} finally {
context.setRealm(null);
}
});
}
@Override

View file

@ -67,6 +67,7 @@ import org.keycloak.models.FederatedIdentityModel;
import org.keycloak.models.GroupModel;
import org.keycloak.models.IdentityProviderMapperModel;
import org.keycloak.models.IdentityProviderModel;
import org.keycloak.models.KeycloakContext;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.ModelException;
import org.keycloak.models.ProtocolMapperModel;
@ -121,7 +122,13 @@ public class RepresentationToModel {
public static void importRealm(KeycloakSession session, RealmRepresentation rep, RealmModel newRealm, boolean skipUserDependent) {
KeycloakContext context = session.getContext();
try {
context.setRealm(newRealm);
session.getProvider(DatastoreProvider.class).getExportImportManager().importRealm(rep, newRealm, skipUserDependent);
} finally {
context.setRealm(null);
}
}
public static void importRoles(RolesRepresentation realmRoles, RealmModel realm) {

View file

@ -31,6 +31,7 @@ import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@ -59,6 +60,7 @@ import org.jboss.shrinkwrap.descriptor.api.Descriptor;
import org.keycloak.common.crypto.FipsMode;
import org.keycloak.testsuite.arquillian.SuiteContext;
import org.keycloak.testsuite.model.StoreProvider;
import org.keycloak.utils.StringUtil;
public abstract class AbstractQuarkusDeployableContainer implements DeployableContainer<KeycloakQuarkusConfiguration> {
@ -206,10 +208,39 @@ public abstract class AbstractQuarkusDeployableContainer implements DeployableCo
}
addStorageOptions(storeProvider, commands);
addFeaturesOption(commands);
return commands;
}
protected void addFeaturesOption(List<String> commands) {
String defaultFeatures = configuration.getDefaultFeatures();
if (StringUtil.isBlank(defaultFeatures)) {
return;
}
if (commands.stream().anyMatch(List.of("import", "export")::contains)) {
return;
}
StringBuilder featuresOption = new StringBuilder("--features=").append(defaultFeatures);
Iterator<String> iterator = commands.iterator();
while (iterator.hasNext()) {
String command = iterator.next();
if (command.startsWith("--features")) {
featuresOption = new StringBuilder(command);
featuresOption.append(",").append(defaultFeatures);
iterator.remove();
break;
}
}
commands.add(featuresOption.toString());
}
protected List<String> configureArgs(List<String> commands) {
return commands;
}

View file

@ -47,6 +47,8 @@ public class KeycloakQuarkusConfiguration implements ContainerConfiguration {
private FipsMode fipsMode = FipsMode.valueOfOption(System.getProperty("auth.server.fips.mode"));
private String defaultFeatures;
@Override
public void validate() throws ConfigurationException {
int basePort = getBindHttpPort();
@ -229,4 +231,12 @@ public class KeycloakQuarkusConfiguration implements ContainerConfiguration {
public void setFipsMode(FipsMode fipsMode) {
this.fipsMode = fipsMode;
}
public void setDefaultFeatures(String defaultFeatures) {
this.defaultFeatures = defaultFeatures;
}
public String getDefaultFeatures() {
return defaultFeatures;
}
}

View file

@ -73,6 +73,7 @@ public class KeycloakQuarkusServerDeployableContainer extends AbstractQuarkusDep
commands.add(getCommand());
commands.add("-v");
commands.add(command);
addFeaturesOption(commands);
if (args != null) {
commands.addAll(Arrays.asList(args));
}

View file

@ -649,6 +649,7 @@
<property name="javaOpts">-Xms512m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=512m
-Djava.net.preferIPv4Stack=true -Dauth.server.db.host=some
</property>
<property name="defaultFeatures">${auth.server.feature}</property>
</configuration>
</container>

View file

@ -1534,6 +1534,8 @@
<auth.server.migration>true</auth.server.migration>
<keycloak.migration.home>${containers.home}/auth-server-migration</keycloak.migration.home>
<migration.import.props.previous>${migration.import.props.previous}</migration.import.props.previous>
<auth.server.feature>${auth.server.feature}</auth.server.feature>
<auth.server.feature>declarative-user-profile</auth.server.feature>
</systemPropertyVariables>
</configuration>
</plugin>