2016-02-03 10:20:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Red Hat, Inc. and/or its affiliates
|
|
|
|
* and other contributors as indicated by the @author tags.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-07-17 11:45:43 +00:00
|
|
|
package org.keycloak.migration;
|
|
|
|
|
2019-07-18 09:14:15 +00:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.regex.Pattern;
|
2015-07-17 11:45:43 +00:00
|
|
|
import org.jboss.logging.Logger;
|
2016-11-23 21:48:02 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo1_2_0;
|
|
|
|
import org.keycloak.migration.migrators.MigrateTo1_3_0;
|
|
|
|
import org.keycloak.migration.migrators.MigrateTo1_4_0;
|
|
|
|
import org.keycloak.migration.migrators.MigrateTo1_5_0;
|
|
|
|
import org.keycloak.migration.migrators.MigrateTo1_6_0;
|
|
|
|
import org.keycloak.migration.migrators.MigrateTo1_7_0;
|
|
|
|
import org.keycloak.migration.migrators.MigrateTo1_8_0;
|
2016-02-18 15:47:26 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo1_9_0;
|
2016-03-11 14:25:21 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo1_9_2;
|
2016-06-29 15:07:49 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo2_0_0;
|
2016-07-05 19:06:46 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo2_1_0;
|
2016-09-07 06:52:55 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo2_2_0;
|
2016-10-18 10:43:39 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo2_3_0;
|
2016-12-15 10:46:15 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo2_5_0;
|
2017-03-03 14:48:52 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo3_0_0;
|
2017-04-04 07:56:48 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo3_1_0;
|
2017-05-23 12:39:20 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo3_2_0;
|
2017-09-26 07:31:15 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo3_4_0;
|
2017-11-15 03:55:42 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo3_4_1;
|
2017-12-08 15:26:28 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo3_4_2;
|
2018-06-08 13:38:38 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo4_0_0;
|
2018-07-04 12:37:03 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo4_2_0;
|
2018-09-18 06:55:46 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo4_6_0;
|
2019-05-03 17:32:21 +00:00
|
|
|
import org.keycloak.migration.migrators.MigrateTo6_0_0;
|
2016-09-07 06:52:55 +00:00
|
|
|
import org.keycloak.migration.migrators.Migration;
|
2015-07-17 11:45:43 +00:00
|
|
|
import org.keycloak.models.KeycloakSession;
|
2017-11-21 22:46:48 +00:00
|
|
|
import org.keycloak.models.RealmModel;
|
|
|
|
import org.keycloak.representations.idm.RealmRepresentation;
|
2015-07-17 11:45:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
|
|
|
* @version $Revision: 1 $
|
|
|
|
*/
|
|
|
|
public class MigrationModelManager {
|
|
|
|
private static Logger logger = Logger.getLogger(MigrationModelManager.class);
|
|
|
|
|
2016-09-07 06:52:55 +00:00
|
|
|
private static final Migration[] migrations = {
|
2016-11-23 21:48:02 +00:00
|
|
|
new MigrateTo1_2_0(),
|
|
|
|
new MigrateTo1_3_0(),
|
|
|
|
new MigrateTo1_4_0(),
|
|
|
|
new MigrateTo1_5_0(),
|
|
|
|
new MigrateTo1_6_0(),
|
|
|
|
new MigrateTo1_7_0(),
|
|
|
|
new MigrateTo1_8_0(),
|
|
|
|
new MigrateTo1_9_0(),
|
2017-04-04 07:56:48 +00:00
|
|
|
new MigrateTo1_9_2(),
|
|
|
|
new MigrateTo2_0_0(),
|
|
|
|
new MigrateTo2_1_0(),
|
|
|
|
new MigrateTo2_2_0(),
|
|
|
|
new MigrateTo2_3_0(),
|
2017-03-03 14:48:52 +00:00
|
|
|
new MigrateTo2_5_0(),
|
2017-04-04 07:56:48 +00:00
|
|
|
new MigrateTo3_0_0(),
|
2017-05-23 12:39:20 +00:00
|
|
|
new MigrateTo3_1_0(),
|
2017-08-28 04:46:03 +00:00
|
|
|
new MigrateTo3_2_0(),
|
2017-11-15 03:55:42 +00:00
|
|
|
new MigrateTo3_4_0(),
|
2017-12-08 15:26:28 +00:00
|
|
|
new MigrateTo3_4_1(),
|
2018-06-08 13:38:38 +00:00
|
|
|
new MigrateTo3_4_2(),
|
2018-07-04 12:37:03 +00:00
|
|
|
new MigrateTo4_0_0(),
|
2018-09-18 06:55:46 +00:00
|
|
|
new MigrateTo4_2_0(),
|
2019-05-03 17:32:21 +00:00
|
|
|
new MigrateTo4_6_0(),
|
2019-07-25 22:24:33 +00:00
|
|
|
new MigrateTo6_0_0()
|
2016-09-07 06:52:55 +00:00
|
|
|
};
|
|
|
|
|
2015-07-17 11:45:43 +00:00
|
|
|
public static void migrate(KeycloakSession session) {
|
2016-09-07 06:52:55 +00:00
|
|
|
ModelVersion latest = migrations[migrations.length-1].getVersion();
|
2015-07-17 11:45:43 +00:00
|
|
|
MigrationModel model = session.realms().getMigrationModel();
|
|
|
|
ModelVersion stored = null;
|
2016-09-07 06:52:55 +00:00
|
|
|
if (model.getStoredVersion() != null) {
|
|
|
|
stored = new ModelVersion(model.getStoredVersion());
|
|
|
|
if (latest.equals(stored)) {
|
|
|
|
return;
|
2015-09-23 10:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-07 06:52:55 +00:00
|
|
|
|
|
|
|
for (Migration m : migrations) {
|
|
|
|
if (stored == null || stored.lessThan(m.getVersion())) {
|
|
|
|
if (stored != null) {
|
|
|
|
logger.debugf("Migrating older model to %s", m.getVersion());
|
|
|
|
}
|
|
|
|
m.migrate(session);
|
2016-07-05 19:06:46 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-17 11:45:43 +00:00
|
|
|
|
2016-09-07 06:52:55 +00:00
|
|
|
model.setStoredVersion(latest.toString());
|
2015-07-17 11:45:43 +00:00
|
|
|
}
|
2017-11-21 22:46:48 +00:00
|
|
|
|
|
|
|
public static final ModelVersion RHSSO_VERSION_7_0_KEYCLOAK_VERSION = new ModelVersion("1.9.8");
|
2019-07-18 09:14:15 +00:00
|
|
|
public static final ModelVersion RHSSO_VERSION_7_1_KEYCLOAK_VERSION = new ModelVersion("2.5.5");
|
|
|
|
public static final ModelVersion RHSSO_VERSION_7_2_KEYCLOAK_VERSION = new ModelVersion("3.4.3");
|
|
|
|
public static final ModelVersion RHSSO_VERSION_7_3_KEYCLOAK_VERSION = new ModelVersion("4.8.3");
|
2017-11-21 22:46:48 +00:00
|
|
|
|
2019-07-18 09:14:15 +00:00
|
|
|
private static final Map<Pattern, ModelVersion> PATTERN_MATCHER = new LinkedHashMap<>();
|
|
|
|
static {
|
|
|
|
PATTERN_MATCHER.put(Pattern.compile("^7\\.0\\.\\d+\\.GA$"), RHSSO_VERSION_7_0_KEYCLOAK_VERSION);
|
|
|
|
PATTERN_MATCHER.put(Pattern.compile("^7\\.1\\.\\d+\\.GA$"), RHSSO_VERSION_7_1_KEYCLOAK_VERSION);
|
|
|
|
PATTERN_MATCHER.put(Pattern.compile("^7\\.2\\.\\d+\\.GA$"), RHSSO_VERSION_7_2_KEYCLOAK_VERSION);
|
|
|
|
PATTERN_MATCHER.put(Pattern.compile("^7\\.3\\.\\d+\\.GA$"), RHSSO_VERSION_7_3_KEYCLOAK_VERSION);
|
|
|
|
}
|
2017-11-21 22:46:48 +00:00
|
|
|
|
|
|
|
public static void migrateImport(KeycloakSession session, RealmModel realm, RealmRepresentation rep, boolean skipUserDependent) {
|
2019-07-18 09:14:15 +00:00
|
|
|
ModelVersion stored = null;
|
2017-11-21 22:46:48 +00:00
|
|
|
if (rep.getKeycloakVersion() != null) {
|
2019-07-18 09:14:15 +00:00
|
|
|
stored = convertRHSSOVersionToKeycloakVersion(rep.getKeycloakVersion());
|
2019-09-10 08:56:48 +00:00
|
|
|
if (stored == null) {
|
|
|
|
stored = new ModelVersion(rep.getKeycloakVersion());
|
|
|
|
}
|
2019-07-18 09:14:15 +00:00
|
|
|
}
|
|
|
|
if (stored == null) {
|
|
|
|
stored = migrations[0].getVersion();
|
2017-11-21 22:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Migration m : migrations) {
|
|
|
|
if (stored == null || stored.lessThan(m.getVersion())) {
|
|
|
|
if (stored != null) {
|
|
|
|
logger.debugf("Migrating older json representation to %s", m.getVersion());
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
m.migrateImport(session, realm, rep, skipUserDependent);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Failed to migrate json representation for version: " + m.getVersion(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 09:14:15 +00:00
|
|
|
|
|
|
|
public static ModelVersion convertRHSSOVersionToKeycloakVersion(String version) {
|
|
|
|
// look for the keycloakVersion pattern to identify it as RH SSO
|
|
|
|
for (Pattern pattern : PATTERN_MATCHER.keySet()) {
|
|
|
|
if (pattern.matcher(version).find()) {
|
|
|
|
return PATTERN_MATCHER.get(pattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// chceck if the version is in format for CD releases, e.g.: "keycloakVersion": "6"
|
|
|
|
if (Pattern.compile("^[0-9]*$").matcher(version).find()) {
|
|
|
|
return new ModelVersion(Integer.parseInt(version), 0, 0);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-07-17 11:45:43 +00:00
|
|
|
}
|