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 ;
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 ;
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 ( ) ,
new MigrateTo4_6_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 " ) ;
public static final ModelVersion RHSSO_VERSION_7_1_KEYCLOAK_VERSION = new ModelVersion ( " 2.5.0 " ) ;
public static final ModelVersion RHSSO_VERSION_7_2_KEYCLOAK_VERSION = new ModelVersion ( " 3.4.2 " ) ;
public static void migrateImport ( KeycloakSession session , RealmModel realm , RealmRepresentation rep , boolean skipUserDependent ) {
ModelVersion latest = migrations [ migrations . length - 1 ] . getVersion ( ) ;
ModelVersion stored = migrations [ 0 ] . getVersion ( ) ;
if ( rep . getKeycloakVersion ( ) ! = null ) {
stored = new ModelVersion ( rep . getKeycloakVersion ( ) ) ;
// hack for importing RH-SSO json export
// NOTE!!!!! We need to do something once we reach community version 7. If community version is 7 or higher, look for the GA qualifier to identify it as RH SSO
if ( latest . getMajor ( ) < 7 | | ( stored . getMajor ( ) = = 7 & & stored . getQualifier ( ) . equals ( " GA " ) ) ) {
if ( stored . getMajor ( ) = = 7 ) {
if ( stored . getMinor ( ) = = 0 ) {
stored = RHSSO_VERSION_7_0_KEYCLOAK_VERSION ;
} else if ( stored . getMinor ( ) = = 1 ) {
stored = RHSSO_VERSION_7_1_KEYCLOAK_VERSION ;
} else if ( stored . getMinor ( ) = = 2 ) {
stored = RHSSO_VERSION_7_2_KEYCLOAK_VERSION ;
}
}
}
// strip out qualifier
stored = new ModelVersion ( stored . major , stored . minor , stored . micro ) ;
if ( latest . equals ( stored ) | | latest . lessThan ( stored ) ) {
return ;
}
}
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 ) ;
}
}
}
}
2015-07-17 11:45:43 +00:00
}