diff --git a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/CBApplication.java b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/CBApplication.java index 9218cd7a0e..0512b5c7f1 100644 --- a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/CBApplication.java +++ b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/CBApplication.java @@ -27,7 +27,8 @@ import io.cloudbeaver.model.session.WebAuthInfo; import io.cloudbeaver.registry.WebServiceRegistry; import io.cloudbeaver.server.jetty.CBJettyServer; import io.cloudbeaver.service.DBWServiceInitializer; -import io.cloudbeaver.service.security.SecurityPluginService; +import io.cloudbeaver.service.security.CBEmbeddedSecurityController; +import io.cloudbeaver.service.security.EmbeddedSecurityControllerFactory; import io.cloudbeaver.utils.WebAppUtils; import org.eclipse.core.runtime.Platform; import org.eclipse.equinox.app.IApplicationContext; @@ -105,7 +106,7 @@ public class CBApplication extends BaseWebApplication implements WebAuthApplicat // Configurations protected final Map productConfiguration = new HashMap<>(); - private final Map databaseConfiguration = new HashMap<>(); + protected final Map databaseConfiguration = new HashMap<>(); private final CBAppConfig appConfiguration = new CBAppConfig(); private Map externalProperties = new LinkedHashMap<>(); @@ -299,6 +300,13 @@ public class CBApplication extends BaseWebApplication implements WebAuthApplicat } + try { + initializeServer(); + } catch (DBException e) { + log.error("Error initializing server", e); + return null; + } + { try { initializeSecurityController(); @@ -323,12 +331,6 @@ public class CBApplication extends BaseWebApplication implements WebAuthApplicat System.setSecurityManager(new SecurityManager()); } - try { - initializeServer(); - } catch (DBException e) { - log.error("Error initializing server", e); - return null; - } runWebServer(); log.debug("Shutdown"); @@ -451,7 +453,7 @@ public class CBApplication extends BaseWebApplication implements WebAuthApplicat } protected SMAdminController createGlobalSecurityController() throws DBException { - return SecurityPluginService.createSecurityService(this, databaseConfiguration); + return new EmbeddedSecurityControllerFactory().createSecurityService(this, databaseConfiguration); } @Nullable @@ -681,6 +683,13 @@ public class CBApplication extends BaseWebApplication implements WebAuthApplicat } private void shutdown() { + try { + if (securityController instanceof CBEmbeddedSecurityController) { + ((CBEmbeddedSecurityController) securityController).shutdown(); + } + } catch (Exception e) { + log.error(e); + } log.debug("Cloudbeaver Server is stopping"); //$NON-NLS-1$ } @@ -763,8 +772,13 @@ public class CBApplication extends BaseWebApplication implements WebAuthApplicat return readConfiguration(runtimeConfigFile); } - protected void finishSecurityServiceConfiguration(@NotNull String adminName, @Nullable String adminPassword, @NotNull List authInfoList) throws DBException { - SecurityPluginService.finishConfiguration(adminName, adminPassword, authInfoList); + protected void finishSecurityServiceConfiguration(@NotNull String adminName, + @Nullable String adminPassword, + @NotNull List authInfoList + ) throws DBException { + if (securityController instanceof CBEmbeddedSecurityController) { + ((CBEmbeddedSecurityController) securityController).finishConfiguration(adminName, adminPassword, authInfoList); + } } public synchronized void flushConfiguration() throws DBException { diff --git a/server/bundles/io.cloudbeaver.service.security/META-INF/MANIFEST.MF b/server/bundles/io.cloudbeaver.service.security/META-INF/MANIFEST.MF index a3f993fe15..f2c832af2e 100644 --- a/server/bundles/io.cloudbeaver.service.security/META-INF/MANIFEST.MF +++ b/server/bundles/io.cloudbeaver.service.security/META-INF/MANIFEST.MF @@ -15,5 +15,6 @@ Require-Bundle: org.jkiss.dbeaver.model;visibility:=reexport, io.cloudbeaver.model Export-Package: io.cloudbeaver.auth.provider.local, io.cloudbeaver.auth.provider.rp, - io.cloudbeaver.service.security + io.cloudbeaver.service.security, + io.cloudbeaver.service.security.db Automatic-Module-Name: io.cloudbeaver.service.security diff --git a/server/bundles/io.cloudbeaver.service.security/plugin.xml b/server/bundles/io.cloudbeaver.service.security/plugin.xml index 647baf5c65..29bc0765c8 100644 --- a/server/bundles/io.cloudbeaver.service.security/plugin.xml +++ b/server/bundles/io.cloudbeaver.service.security/plugin.xml @@ -2,10 +2,6 @@ - - - - metaParameters) throws DBCException { + public void createUser(String userId, Map metaParameters) throws DBException { if (isSubjectExists(userId)) { throw new DBCException("User or role '" + userId + "' already exists"); } @@ -1624,6 +1626,18 @@ public class CBEmbeddedSecurityController implements SMAdminController, SMAuthen } } + public void shutdown() { + database.shutdown(); + } + + public void finishConfiguration( + @NotNull String adminName, + @Nullable String adminPassword, + @NotNull List authInfoList + ) throws DBException { + database.finishConfiguration(adminName, adminPassword, authInfoList); + } + /////////////////////////////////////////// // Utils diff --git a/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/EmbeddedSecurityControllerFactory.java b/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/EmbeddedSecurityControllerFactory.java new file mode 100644 index 0000000000..ef20577f12 --- /dev/null +++ b/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/EmbeddedSecurityControllerFactory.java @@ -0,0 +1,63 @@ +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2022 DBeaver Corp and others + * + * 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 io.cloudbeaver.service.security; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.InstanceCreator; +import io.cloudbeaver.model.app.WebApplication; +import io.cloudbeaver.service.security.db.CBDatabase; +import io.cloudbeaver.service.security.db.CBDatabaseConfig; +import org.jkiss.dbeaver.DBException; + +import java.util.Map; + +/** + * Embedded Security Controller Factory + */ +public class EmbeddedSecurityControllerFactory { + + /** + * Create new security controller instance + */ + public CBEmbeddedSecurityController createSecurityService( + WebApplication application, + Map databaseConfig + ) throws DBException { + CBDatabaseConfig databaseConfiguration = new CBDatabaseConfig(); + InstanceCreator dbConfigCreator = type -> databaseConfiguration; + InstanceCreator dbPoolConfigCreator = type -> databaseConfiguration.getPool(); + Gson gson = new GsonBuilder() + .registerTypeAdapter(CBDatabaseConfig.class, dbConfigCreator) + .registerTypeAdapter(CBDatabaseConfig.Pool.class, dbPoolConfigCreator) + .create(); + gson.fromJson(gson.toJsonTree(databaseConfig), CBDatabaseConfig.class); + + var database = new CBDatabase(application, databaseConfiguration); + var securityController = createEmbeddedSecurityController(application, database); + //FIXME circular dependency + database.setAdminSecurityController(securityController); + + database.initialize(); + securityController.initializeMetaInformation(); + return securityController; + } + + protected CBEmbeddedSecurityController createEmbeddedSecurityController(WebApplication application, CBDatabase database) { + return new CBEmbeddedSecurityController(application, database); + } +} diff --git a/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/SecurityPluginService.java b/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/SecurityPluginService.java deleted file mode 100644 index 78a45fc18d..0000000000 --- a/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/SecurityPluginService.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2022 DBeaver Corp and others - * - * 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 io.cloudbeaver.service.security; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.InstanceCreator; -import io.cloudbeaver.model.app.WebApplication; -import io.cloudbeaver.model.session.WebAuthInfo; -import io.cloudbeaver.service.security.internal.CBEmbeddedSecurityController; -import io.cloudbeaver.service.security.internal.db.CBDatabase; -import io.cloudbeaver.service.security.internal.db.CBDatabaseConfig; -import org.jkiss.dbeaver.DBException; -import org.jkiss.dbeaver.Log; -import org.jkiss.dbeaver.model.security.SMAdminController; -import org.jkiss.dbeaver.runtime.IPluginService; - -import java.util.List; -import java.util.Map; - -public class SecurityPluginService implements IPluginService { - private static final Log log = Log.getLog(SecurityPluginService.class); - - private static CBDatabase DB_INSTANCE; - private static CBEmbeddedSecurityController CONTROLLER_INSTANCE; - - public static void finishConfiguration(String adminName, String adminPassword, List authInfoList) throws DBException { - DB_INSTANCE.finishConfiguration(adminName, adminPassword, authInfoList); - } - - @Override - public void activateService() { - - } - - public static synchronized SMAdminController createSecurityService(WebApplication application, Map databaseConfig) throws DBException { - if (CONTROLLER_INSTANCE != null) { - return CONTROLLER_INSTANCE; - } - CBDatabaseConfig databaseConfiguration = new CBDatabaseConfig(); - InstanceCreator dbConfigCreator = type -> databaseConfiguration; - InstanceCreator dbPoolConfigCreator = type -> databaseConfiguration.getPool(); - Gson gson = new GsonBuilder() - .registerTypeAdapter(CBDatabaseConfig.class, dbConfigCreator) - .registerTypeAdapter(CBDatabaseConfig.Pool.class, dbPoolConfigCreator) - .create(); - gson.fromJson(gson.toJsonTree(databaseConfig), CBDatabaseConfig.class); - - DB_INSTANCE = new CBDatabase(application, databaseConfiguration); - CONTROLLER_INSTANCE = new CBEmbeddedSecurityController(application, DB_INSTANCE); - //FIXME circular dependency - DB_INSTANCE.setAdminSecurityController(CONTROLLER_INSTANCE); - - DB_INSTANCE.initialize(); - CONTROLLER_INSTANCE.initializeMetaInformation(); - return CONTROLLER_INSTANCE; - } - - @Override - public void deactivateService() { - if(DB_INSTANCE == null) { - return; - } - try { - DB_INSTANCE.shutdown(); - } catch (Exception e) { - log.error(e); - } - } -} diff --git a/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/db/CBDatabase.java b/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabase.java similarity index 99% rename from server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/db/CBDatabase.java rename to server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabase.java index c4fd43b8f1..20217c60f5 100644 --- a/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/db/CBDatabase.java +++ b/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabase.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cloudbeaver.service.security.internal.db; +package io.cloudbeaver.service.security.db; import com.google.gson.Gson; import com.google.gson.GsonBuilder; diff --git a/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/db/CBDatabaseConfig.java b/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabaseConfig.java similarity index 97% rename from server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/db/CBDatabaseConfig.java rename to server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabaseConfig.java index 5b0cecaf75..d40c42586e 100644 --- a/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/db/CBDatabaseConfig.java +++ b/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabaseConfig.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cloudbeaver.service.security.internal.db; +package io.cloudbeaver.service.security.db; /** * Database configuration diff --git a/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/db/CBDatabaseInitialData.java b/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabaseInitialData.java similarity index 95% rename from server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/db/CBDatabaseInitialData.java rename to server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabaseInitialData.java index 7f11a86528..df55a6169b 100644 --- a/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/db/CBDatabaseInitialData.java +++ b/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabaseInitialData.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.cloudbeaver.service.security.internal.db; +package io.cloudbeaver.service.security.db; import org.jkiss.dbeaver.model.security.user.SMRole; diff --git a/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/utils/DBConfigurationUtils.java b/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/utils/DBConfigurationUtils.java index 15f5f8075b..de4952c769 100644 --- a/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/utils/DBConfigurationUtils.java +++ b/server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/internal/utils/DBConfigurationUtils.java @@ -16,7 +16,7 @@ */ package io.cloudbeaver.service.security.internal.utils; -import io.cloudbeaver.service.security.internal.db.CBDatabaseConfig; +import io.cloudbeaver.service.security.db.CBDatabaseConfig; import org.jkiss.code.Nullable; import org.jkiss.utils.CommonUtils;