From 50fc083d97553fa01c83586b95cb001236dfd0dc Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Mon, 21 Nov 2016 11:43:09 -0500 Subject: [PATCH] fix migration scripts --- .../custom/MigrateUserFedToComponent.java | 6 +- .../PortLdapUserFedToComponentModel.java | 38 ++++ .../META-INF/jpa-changelog-2.4.0.xml | 2 +- .../META-INF/jpa-changelog-2.4.1.xml | 25 +++ .../META-INF/jpa-changelog-master.xml | 1 + .../impl/DefaultMongoUpdaterProvider.java | 4 +- .../AbstractMigrateUserFedToComponent.java | 167 ++++++++++++++++++ .../updater/impl/updates/Update2_4_0.java | 131 +------------- .../updater/impl/updates/Update2_4_1.java | 38 ++++ 9 files changed, 276 insertions(+), 136 deletions(-) create mode 100644 model/jpa/src/main/java/org/keycloak/connections/jpa/updater/liquibase/custom/PortLdapUserFedToComponentModel.java create mode 100755 model/jpa/src/main/resources/META-INF/jpa-changelog-2.4.1.xml create mode 100644 model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/updates/AbstractMigrateUserFedToComponent.java create mode 100644 model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/updates/Update2_4_1.java diff --git a/model/jpa/src/main/java/org/keycloak/connections/jpa/updater/liquibase/custom/MigrateUserFedToComponent.java b/model/jpa/src/main/java/org/keycloak/connections/jpa/updater/liquibase/custom/MigrateUserFedToComponent.java index 8d22fbde8f..bb298300cc 100644 --- a/model/jpa/src/main/java/org/keycloak/connections/jpa/updater/liquibase/custom/MigrateUserFedToComponent.java +++ b/model/jpa/src/main/java/org/keycloak/connections/jpa/updater/liquibase/custom/MigrateUserFedToComponent.java @@ -29,19 +29,17 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; /** - * @author Marek Posolda + * @author Bill Burke */ public class MigrateUserFedToComponent extends AbstractUserFedToComponent { @Override protected void generateStatementsImpl() throws CustomChangeException { - String providerId = LDAPConstants.LDAP_PROVIDER; - convertFedProviderToComponent(LDAPConstants.LDAP_PROVIDER, "org.keycloak.storage.ldap.mappers.LDAPStorageMapper"); convertFedProviderToComponent("kerberos", null); } @Override protected String getTaskId() { - return "Update 2.4.0.Final"; + return "Update 2.4.1.Final"; } } diff --git a/model/jpa/src/main/java/org/keycloak/connections/jpa/updater/liquibase/custom/PortLdapUserFedToComponentModel.java b/model/jpa/src/main/java/org/keycloak/connections/jpa/updater/liquibase/custom/PortLdapUserFedToComponentModel.java new file mode 100644 index 0000000000..dc9e6073c3 --- /dev/null +++ b/model/jpa/src/main/java/org/keycloak/connections/jpa/updater/liquibase/custom/PortLdapUserFedToComponentModel.java @@ -0,0 +1,38 @@ +/* + * 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. + */ + +package org.keycloak.connections.jpa.updater.liquibase.custom; + +import liquibase.exception.CustomChangeException; +import org.keycloak.models.LDAPConstants; + +/** + * @author Bill Burke + */ +public class PortLdapUserFedToComponentModel extends AbstractUserFedToComponent { + + @Override + protected void generateStatementsImpl() throws CustomChangeException { + String providerId = LDAPConstants.LDAP_PROVIDER; + convertFedProviderToComponent(LDAPConstants.LDAP_PROVIDER, "org.keycloak.storage.ldap.mappers.LDAPStorageMapper"); + } + + @Override + protected String getTaskId() { + return "Update 2.4.0.Final"; + } +} diff --git a/model/jpa/src/main/resources/META-INF/jpa-changelog-2.4.0.xml b/model/jpa/src/main/resources/META-INF/jpa-changelog-2.4.0.xml index e8af8daac3..0aebd9a0d2 100755 --- a/model/jpa/src/main/resources/META-INF/jpa-changelog-2.4.0.xml +++ b/model/jpa/src/main/resources/META-INF/jpa-changelog-2.4.0.xml @@ -19,7 +19,7 @@ - + \ No newline at end of file diff --git a/model/jpa/src/main/resources/META-INF/jpa-changelog-2.4.1.xml b/model/jpa/src/main/resources/META-INF/jpa-changelog-2.4.1.xml new file mode 100755 index 0000000000..216ce651c8 --- /dev/null +++ b/model/jpa/src/main/resources/META-INF/jpa-changelog-2.4.1.xml @@ -0,0 +1,25 @@ + + + + + + + + + + \ No newline at end of file diff --git a/model/jpa/src/main/resources/META-INF/jpa-changelog-master.xml b/model/jpa/src/main/resources/META-INF/jpa-changelog-master.xml index ebe8cf3d54..4fe18b6857 100755 --- a/model/jpa/src/main/resources/META-INF/jpa-changelog-master.xml +++ b/model/jpa/src/main/resources/META-INF/jpa-changelog-master.xml @@ -44,4 +44,5 @@ + diff --git a/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/DefaultMongoUpdaterProvider.java b/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/DefaultMongoUpdaterProvider.java index c7f5be8d52..78adfb70c0 100755 --- a/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/DefaultMongoUpdaterProvider.java +++ b/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/DefaultMongoUpdaterProvider.java @@ -35,6 +35,7 @@ import org.keycloak.connections.mongo.updater.impl.updates.Update1_8_0; import org.keycloak.connections.mongo.updater.impl.updates.Update1_9_2; import org.keycloak.connections.mongo.updater.impl.updates.Update2_3_0; import org.keycloak.connections.mongo.updater.impl.updates.Update2_4_0; +import org.keycloak.connections.mongo.updater.impl.updates.Update2_4_1; import org.keycloak.models.KeycloakSession; import java.util.Date; @@ -61,7 +62,8 @@ public class DefaultMongoUpdaterProvider implements MongoUpdaterProvider { Update1_8_0.class, Update1_9_2.class, Update2_3_0.class, - Update2_4_0.class + Update2_4_0.class, + Update2_4_1.class }; @Override diff --git a/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/updates/AbstractMigrateUserFedToComponent.java b/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/updates/AbstractMigrateUserFedToComponent.java new file mode 100644 index 0000000000..d4db6d3209 --- /dev/null +++ b/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/updates/AbstractMigrateUserFedToComponent.java @@ -0,0 +1,167 @@ +/* + * 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. + */ +package org.keycloak.connections.mongo.updater.impl.updates; + +import com.mongodb.BasicDBList; +import com.mongodb.BasicDBObject; +import com.mongodb.DBCollection; +import com.mongodb.DBCursor; +import org.jboss.logging.Logger; +import org.keycloak.storage.UserStorageProvider; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public abstract class AbstractMigrateUserFedToComponent extends Update { + private final Logger logger = Logger.getLogger(getClass()); + + public void portUserFedToComponent(String providerId) { + DBCollection realms = db.getCollection("realms"); + DBCursor cursor = realms.find(); + while (cursor.hasNext()) { + BasicDBObject realm = (BasicDBObject) cursor.next(); + + String realmId = realm.getString("_id"); + Set removedProviders = new HashSet<>(); + + BasicDBList componentEntities = (BasicDBList) realm.get("componentEntities"); + BasicDBList federationProviders = (BasicDBList) realm.get("userFederationProviders"); + for (Object obj : federationProviders) { + BasicDBObject fedProvider = (BasicDBObject)obj; + if (fedProvider.getString("providerName").equals(providerId)) { + String id = fedProvider.getString("id"); + removedProviders.add(id); + int priority = fedProvider.getInt("priority"); + String displayName = fedProvider.getString("displayName"); + int fullSyncPeriod = fedProvider.getInt("fullSyncPeriod"); + int changedSyncPeriod = fedProvider.getInt("changedSyncPeriod"); + int lastSync = fedProvider.getInt("lastSync"); + BasicDBObject component = new BasicDBObject(); + component.put("id", id); + component.put("name", displayName); + component.put("providerType", UserStorageProvider.class.getName()); + component.put("providerId", providerId); + component.put("parentId", realmId); + + BasicDBObject config = new BasicDBObject(); + config.put("priority", Collections.singletonList(Integer.toString(priority))); + config.put("fullSyncPeriod", Collections.singletonList(Integer.toString(fullSyncPeriod))); + config.put("changedSyncPeriod", Collections.singletonList(Integer.toString(changedSyncPeriod))); + config.put("lastSync", Collections.singletonList(Integer.toString(lastSync))); + + BasicDBObject fedConfig = (BasicDBObject)fedProvider.get("config"); + if (fedConfig != null) { + for (Map.Entry attr : new HashSet<>(fedConfig.entrySet())) { + String attrName = attr.getKey(); + String attrValue = attr.getValue().toString(); + config.put(attrName, Collections.singletonList(attrValue)); + + } + } + + + component.put("config", config); + + componentEntities.add(component); + + } + } + Iterator it = federationProviders.iterator(); + while (it.hasNext()) { + BasicDBObject fedProvider = (BasicDBObject)it.next(); + String id = fedProvider.getString("id"); + if (removedProviders.contains(id)) { + it.remove(); + } + + } + realms.update(new BasicDBObject().append("_id", realmId), realm); + } + } + + public void portUserFedMappersToComponent(String providerId, String mapperType) { + //logger.info("*** port mappers"); + DBCollection realms = db.getCollection("realms"); + DBCursor cursor = realms.find(); + while (cursor.hasNext()) { + BasicDBObject realm = (BasicDBObject) cursor.next(); + + String realmId = realm.getString("_id"); + Set removedProviders = new HashSet<>(); + + BasicDBList componentEntities = (BasicDBList) realm.get("componentEntities"); + BasicDBList federationProviders = (BasicDBList) realm.get("userFederationProviders"); + BasicDBList fedMappers = (BasicDBList) realm.get("userFederationMappers"); + for (Object obj : federationProviders) { + BasicDBObject fedProvider = (BasicDBObject)obj; + String providerName = fedProvider.getString("providerName"); + //logger.info("looking for mappers of fed provider: " + providerName); + if (providerName.equals(providerId)) { + String id = fedProvider.getString("id"); + //logger.info("found fed provider: " + id + ", looking at mappers"); + for (Object obj2 : fedMappers) { + BasicDBObject fedMapper = (BasicDBObject)obj2; + String federationProviderId = fedMapper.getString("federationProviderId"); + //logger.info("looking at mapper with federationProviderId: " + federationProviderId); + if (federationProviderId.equals(id)) { + String name = fedMapper.getString("name"); + String mapperId = fedMapper.getString("id"); + removedProviders.add(mapperId); + String mapperProviderId = fedMapper.getString("federationMapperType"); + BasicDBObject component = new BasicDBObject(); + component.put("id", mapperId); + component.put("name", name); + component.put("providerType", mapperType); + component.put("providerId", mapperProviderId); + component.put("parentId", id); + + BasicDBObject fedConfig = (BasicDBObject)fedMapper.get("config"); + BasicDBObject config = new BasicDBObject(); + if (fedConfig != null) { + for (Map.Entry attr : new HashSet<>(fedConfig.entrySet())) { + String attrName = attr.getKey(); + String attrValue = attr.getValue().toString(); + config.put(attrName, Collections.singletonList(attrValue)); + + } + } + component.put("config", config); + componentEntities.add(component); + } + } + } + } + Iterator it = fedMappers.iterator(); + while (it.hasNext()) { + BasicDBObject fedMapper = (BasicDBObject)it.next(); + String id = fedMapper.getString("id"); + if (removedProviders.contains(id)) { + it.remove(); + } + + } + realms.update(new BasicDBObject().append("_id", realmId), realm); + } + } +} diff --git a/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/updates/Update2_4_0.java b/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/updates/Update2_4_0.java index 8900b3ae2b..883a0519a0 100644 --- a/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/updates/Update2_4_0.java +++ b/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/updates/Update2_4_0.java @@ -37,8 +37,7 @@ import java.util.Set; /** * @author Marek Posolda */ -public class Update2_4_0 extends Update { - private final Logger logger = Logger.getLogger(getClass()); +public class Update2_4_0 extends AbstractMigrateUserFedToComponent { @Override public String getId() { @@ -49,134 +48,6 @@ public class Update2_4_0 extends Update { public void update(KeycloakSession session) { portUserFedMappersToComponent(LDAPConstants.LDAP_PROVIDER, "org.keycloak.storage.ldap.mappers.LDAPStorageMapper"); portUserFedToComponent(LDAPConstants.LDAP_PROVIDER); - portUserFedToComponent("kerberos"); } - public void portUserFedToComponent(String providerId) { - DBCollection realms = db.getCollection("realms"); - DBCursor cursor = realms.find(); - while (cursor.hasNext()) { - BasicDBObject realm = (BasicDBObject) cursor.next(); - - String realmId = realm.getString("_id"); - Set removedProviders = new HashSet<>(); - - BasicDBList componentEntities = (BasicDBList) realm.get("componentEntities"); - BasicDBList federationProviders = (BasicDBList) realm.get("userFederationProviders"); - for (Object obj : federationProviders) { - BasicDBObject fedProvider = (BasicDBObject)obj; - if (fedProvider.getString("providerName").equals(providerId)) { - String id = fedProvider.getString("id"); - removedProviders.add(id); - int priority = fedProvider.getInt("priority"); - String displayName = fedProvider.getString("displayName"); - int fullSyncPeriod = fedProvider.getInt("fullSyncPeriod"); - int changedSyncPeriod = fedProvider.getInt("changedSyncPeriod"); - int lastSync = fedProvider.getInt("lastSync"); - BasicDBObject component = new BasicDBObject(); - component.put("id", id); - component.put("name", displayName); - component.put("providerType", UserStorageProvider.class.getName()); - component.put("providerId", providerId); - component.put("parentId", realmId); - - BasicDBObject config = new BasicDBObject(); - config.put("priority", Collections.singletonList(Integer.toString(priority))); - config.put("fullSyncPeriod", Collections.singletonList(Integer.toString(fullSyncPeriod))); - config.put("changedSyncPeriod", Collections.singletonList(Integer.toString(changedSyncPeriod))); - config.put("lastSync", Collections.singletonList(Integer.toString(lastSync))); - - BasicDBObject fedConfig = (BasicDBObject)fedProvider.get("config"); - if (fedConfig != null) { - for (Map.Entry attr : new HashSet<>(fedConfig.entrySet())) { - String attrName = attr.getKey(); - String attrValue = attr.getValue().toString(); - config.put(attrName, Collections.singletonList(attrValue)); - - } - } - - - component.put("config", config); - - componentEntities.add(component); - - } - } - Iterator it = federationProviders.iterator(); - while (it.hasNext()) { - BasicDBObject fedProvider = (BasicDBObject)it.next(); - String id = fedProvider.getString("id"); - if (removedProviders.contains(id)) { - it.remove(); - } - - } - realms.update(new BasicDBObject().append("_id", realmId), realm); - } - } - public void portUserFedMappersToComponent(String providerId, String mapperType) { - //logger.info("*** port mappers"); - DBCollection realms = db.getCollection("realms"); - DBCursor cursor = realms.find(); - while (cursor.hasNext()) { - BasicDBObject realm = (BasicDBObject) cursor.next(); - - String realmId = realm.getString("_id"); - Set removedProviders = new HashSet<>(); - - BasicDBList componentEntities = (BasicDBList) realm.get("componentEntities"); - BasicDBList federationProviders = (BasicDBList) realm.get("userFederationProviders"); - BasicDBList fedMappers = (BasicDBList) realm.get("userFederationMappers"); - for (Object obj : federationProviders) { - BasicDBObject fedProvider = (BasicDBObject)obj; - String providerName = fedProvider.getString("providerName"); - //logger.info("looking for mappers of fed provider: " + providerName); - if (providerName.equals(providerId)) { - String id = fedProvider.getString("id"); - //logger.info("found fed provider: " + id + ", looking at mappers"); - for (Object obj2 : fedMappers) { - BasicDBObject fedMapper = (BasicDBObject)obj2; - String federationProviderId = fedMapper.getString("federationProviderId"); - //logger.info("looking at mapper with federationProviderId: " + federationProviderId); - if (federationProviderId.equals(id)) { - String name = fedMapper.getString("name"); - String mapperId = fedMapper.getString("id"); - removedProviders.add(mapperId); - String mapperProviderId = fedMapper.getString("federationMapperType"); - BasicDBObject component = new BasicDBObject(); - component.put("id", mapperId); - component.put("name", name); - component.put("providerType", mapperType); - component.put("providerId", mapperProviderId); - component.put("parentId", id); - - BasicDBObject fedConfig = (BasicDBObject)fedMapper.get("config"); - BasicDBObject config = new BasicDBObject(); - if (fedConfig != null) { - for (Map.Entry attr : new HashSet<>(fedConfig.entrySet())) { - String attrName = attr.getKey(); - String attrValue = attr.getValue().toString(); - config.put(attrName, Collections.singletonList(attrValue)); - - } - } - component.put("config", config); - componentEntities.add(component); - } - } - } - } - Iterator it = fedMappers.iterator(); - while (it.hasNext()) { - BasicDBObject fedMapper = (BasicDBObject)it.next(); - String id = fedMapper.getString("id"); - if (removedProviders.contains(id)) { - it.remove(); - } - - } - realms.update(new BasicDBObject().append("_id", realmId), realm); - } - } } diff --git a/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/updates/Update2_4_1.java b/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/updates/Update2_4_1.java new file mode 100644 index 0000000000..206579fb04 --- /dev/null +++ b/model/mongo/src/main/java/org/keycloak/connections/mongo/updater/impl/updates/Update2_4_1.java @@ -0,0 +1,38 @@ +/* + * 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. + */ + +package org.keycloak.connections.mongo.updater.impl.updates; + +import org.keycloak.models.KeycloakSession; +import org.keycloak.models.LDAPConstants; + +/** + * @author Marek Posolda + */ +public class Update2_4_1 extends AbstractMigrateUserFedToComponent { + + @Override + public String getId() { + return "2.4.1"; + } + + @Override + public void update(KeycloakSession session) { + portUserFedToComponent("kerberos"); + } + +}