Encapsulate MigrationModelManager in legacy module

Closes #12214
This commit is contained in:
Alexander Schwartz 2022-06-22 21:27:32 +02:00 committed by Hynek Mlnařík
parent 99ca37bd27
commit 4b499c869c
11 changed files with 79 additions and 30 deletions

View file

@ -31,6 +31,7 @@
<module name="org.keycloak.keycloak-server-spi"/> <module name="org.keycloak.keycloak-server-spi"/>
<module name="org.keycloak.keycloak-server-spi-private"/> <module name="org.keycloak.keycloak-server-spi-private"/>
<module name="org.keycloak.keycloak-model-legacy"/> <module name="org.keycloak.keycloak-model-legacy"/>
<module name="org.keycloak.keycloak-model-legacy-private"/>
<module name="javax.persistence.api"/> <module name="javax.persistence.api"/>
<module name="org.jboss.logging"/> <module name="org.jboss.logging"/>
<module name="org.liquibase" services="import"/> <module name="org.liquibase" services="import"/>

View file

@ -31,6 +31,7 @@
<module name="org.keycloak.keycloak-server-spi"/> <module name="org.keycloak.keycloak-server-spi"/>
<module name="org.keycloak.keycloak-server-spi-private"/> <module name="org.keycloak.keycloak-server-spi-private"/>
<module name="org.keycloak.keycloak-model-legacy"/> <module name="org.keycloak.keycloak-model-legacy"/>
<module name="org.keycloak.keycloak-model-legacy-private"/>
<module name="javax.persistence.api"/> <module name="javax.persistence.api"/>
<module name="org.jboss.logging"/> <module name="org.jboss.logging"/>
<module name="org.liquibase" services="import"/> <module name="org.liquibase" services="import"/>

View file

@ -66,6 +66,10 @@
<groupId>org.keycloak</groupId> <groupId>org.keycloak</groupId>
<artifactId>keycloak-model-legacy</artifactId> <artifactId>keycloak-model-legacy</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-legacy-private</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.liquibase</groupId> <groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId> <artifactId>liquibase-core</artifactId>

View file

@ -21,21 +21,20 @@ import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel; import org.keycloak.models.RealmModel;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.storage.DatastoreProvider; import org.keycloak.storage.DatastoreProvider;
import org.keycloak.storage.datastore.LegacyDatastoreProvider;
/** /**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a> * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $ * @version $Revision: 1 $
*/ */
@Deprecated // as this will be moved to a legacy module
public class MigrationModelManager { public class MigrationModelManager {
public static void migrate(KeycloakSession session) { public static void migrate(KeycloakSession session) {
session.getProvider(DatastoreProvider.class).getMigrationManager().migrate(); ((LegacyDatastoreProvider) session.getProvider(DatastoreProvider.class)).getMigrationManager().migrate();
} }
public static void migrateImport(KeycloakSession session, RealmModel realm, RealmRepresentation rep, boolean skipUserDependent) { public static void migrateImport(KeycloakSession session, RealmModel realm, RealmRepresentation rep, boolean skipUserDependent) {
session.getProvider(DatastoreProvider.class).getMigrationManager().migrate(realm, rep, skipUserDependent); ((LegacyDatastoreProvider) session.getProvider(DatastoreProvider.class)).getMigrationManager().migrate(realm, rep, skipUserDependent);
} }
} }

View file

@ -26,7 +26,6 @@ import org.keycloak.representations.idm.RealmRepresentation;
* *
* @author Alexander Schwartz * @author Alexander Schwartz
*/ */
@Deprecated
public interface MigrationManager { public interface MigrationManager {
void migrate(); void migrate();

View file

@ -223,8 +223,6 @@ public class LegacyDatastoreProvider implements DatastoreProvider, LegacyStoreMa
return new LegacyExportImportManager(session); return new LegacyExportImportManager(session);
} }
@Override
@Deprecated
public MigrationManager getMigrationManager() { public MigrationManager getMigrationManager() {
return new LegacyMigrationManager(session); return new LegacyMigrationManager(session);
} }

View file

@ -20,6 +20,7 @@ package org.keycloak.storage.datastore;
import org.keycloak.Config; import org.keycloak.Config;
import org.keycloak.Config.Scope; import org.keycloak.Config.Scope;
import org.keycloak.common.Profile; import org.keycloak.common.Profile;
import org.keycloak.migration.MigrationModelManager;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.utils.PostMigrationEvent; import org.keycloak.models.utils.PostMigrationEvent;
@ -33,6 +34,7 @@ import org.keycloak.services.scheduled.ClusterAwareScheduledTaskRunner;
import org.keycloak.services.scheduled.ScheduledTaskRunner; import org.keycloak.services.scheduled.ScheduledTaskRunner;
import org.keycloak.storage.DatastoreProvider; import org.keycloak.storage.DatastoreProvider;
import org.keycloak.storage.DatastoreProviderFactory; import org.keycloak.storage.DatastoreProviderFactory;
import org.keycloak.storage.LegacyStoreMigrateRepresentationEvent;
import org.keycloak.storage.LegacyStoreSyncEvent; import org.keycloak.storage.LegacyStoreSyncEvent;
import org.keycloak.storage.managers.UserStorageSyncManager; import org.keycloak.storage.managers.UserStorageSyncManager;
import org.keycloak.timer.TimerProvider; import org.keycloak.timer.TimerProvider;
@ -86,6 +88,9 @@ public class LegacyDatastoreProviderFactory implements DatastoreProviderFactory,
} else if (event instanceof LegacyStoreSyncEvent) { } else if (event instanceof LegacyStoreSyncEvent) {
LegacyStoreSyncEvent ev = (LegacyStoreSyncEvent) event; LegacyStoreSyncEvent ev = (LegacyStoreSyncEvent) event;
UserStorageSyncManager.notifyToRefreshPeriodicSyncAll(ev.getSession(), ev.getRealm(), ev.getRemoved()); UserStorageSyncManager.notifyToRefreshPeriodicSyncAll(ev.getSession(), ev.getRealm(), ev.getRemoved());
} else if (event instanceof LegacyStoreMigrateRepresentationEvent) {
LegacyStoreMigrateRepresentationEvent ev = (LegacyStoreMigrateRepresentationEvent) event;
MigrationModelManager.migrateImport(ev.getSession(), ev.getRealm(), ev.getRep(), ev.isSkipUserDependent());
} }
} }

View file

@ -21,14 +21,11 @@ import org.keycloak.models.ClientProvider;
import org.keycloak.models.ClientScopeProvider; import org.keycloak.models.ClientScopeProvider;
import org.keycloak.models.GroupProvider; import org.keycloak.models.GroupProvider;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RealmProvider; import org.keycloak.models.RealmProvider;
import org.keycloak.models.RoleProvider; import org.keycloak.models.RoleProvider;
import org.keycloak.models.UserProvider; import org.keycloak.models.UserProvider;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.storage.DatastoreProvider; import org.keycloak.storage.DatastoreProvider;
import org.keycloak.storage.ExportImportManager; import org.keycloak.storage.ExportImportManager;
import org.keycloak.storage.MigrationManager;
public class MapDatastoreProvider implements DatastoreProvider { public class MapDatastoreProvider implements DatastoreProvider {
@ -77,19 +74,4 @@ public class MapDatastoreProvider implements DatastoreProvider {
return new MapExportImportManager(session); return new MapExportImportManager(session);
} }
@Override
public MigrationManager getMigrationManager() {
return new MigrationManager() {
@Override
public void migrate() {
// Do not migrate the datasources
}
@Override
public void migrate(RealmModel realm, RealmRepresentation rep, boolean skipUserDependent) {
// Do not migrate the representation: assume it is the latest
}
};
}
} }

View file

@ -41,7 +41,4 @@ public interface DatastoreProvider extends Provider {
UserProvider users(); UserProvider users();
ExportImportManager getExportImportManager(); ExportImportManager getExportImportManager();
MigrationManager getMigrationManager();
} }

View file

@ -0,0 +1,63 @@
/*
* Copyright 2022 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.
*/
package org.keycloak.storage;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.provider.ProviderEvent;
import org.keycloak.representations.idm.RealmRepresentation;
/**
* Event for notifying legacy store, so it can do migrations on the representation as needed.
*
* CAUTION: This event is exceptional as it performs any necessary modificaton of the representation.
* This will be removed once the legacy store has been removed.
*/
public class LegacyStoreMigrateRepresentationEvent implements ProviderEvent {
private final KeycloakSession session;
private final RealmModel realm;
private final RealmRepresentation rep;
private final boolean skipUserDependent;
public LegacyStoreMigrateRepresentationEvent(KeycloakSession session, RealmModel realm, RealmRepresentation rep, boolean skipUserDependent) {
this.session = session;
this.realm = realm;
this.rep = rep;
this.skipUserDependent = skipUserDependent;
}
public static void fire(KeycloakSession session, RealmModel realm, RealmRepresentation rep, boolean skipUserDependent) {
session.getKeycloakSessionFactory().publish(new LegacyStoreMigrateRepresentationEvent(session, realm, rep, skipUserDependent));
}
public KeycloakSession getSession() {
return session;
}
public RealmModel getRealm() {
return realm;
}
public RealmRepresentation getRep() {
return rep;
}
public boolean isSkipUserDependent() {
return skipUserDependent;
}
}

View file

@ -19,7 +19,6 @@ package org.keycloak.services.managers;
import org.keycloak.Config; import org.keycloak.Config;
import org.keycloak.common.Profile; import org.keycloak.common.Profile;
import org.keycloak.common.enums.SslRequired; import org.keycloak.common.enums.SslRequired;
import org.keycloak.migration.MigrationModelManager;
import org.keycloak.models.AccountRoles; import org.keycloak.models.AccountRoles;
import org.keycloak.models.AdminRoles; import org.keycloak.models.AdminRoles;
import org.keycloak.models.BrowserSecurityHeaders; import org.keycloak.models.BrowserSecurityHeaders;
@ -52,6 +51,7 @@ import org.keycloak.representations.idm.RealmEventsConfigRepresentation;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RoleRepresentation; import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.sessions.AuthenticationSessionProvider; import org.keycloak.sessions.AuthenticationSessionProvider;
import org.keycloak.storage.LegacyStoreMigrateRepresentationEvent;
import org.keycloak.storage.LegacyStoreSyncEvent; import org.keycloak.storage.LegacyStoreSyncEvent;
import org.keycloak.services.clientregistration.policy.DefaultClientRegistrationPolicies; import org.keycloak.services.clientregistration.policy.DefaultClientRegistrationPolicies;
@ -592,7 +592,7 @@ public class RealmManager {
setupClientRegistrations(realm); setupClientRegistrations(realm);
if (rep.getKeycloakVersion() != null) { if (rep.getKeycloakVersion() != null) {
MigrationModelManager.migrateImport(session, realm, rep, skipUserDependent); LegacyStoreMigrateRepresentationEvent.fire(session, realm, rep, skipUserDependent);
} }
session.clientPolicy().updateRealmModelFromRepresentation(realm, rep); session.clientPolicy().updateRealmModelFromRepresentation(realm, rep);