Skip to content

Commit

Permalink
CB-4917 remove applications from configuration controller and fix des…
Browse files Browse the repository at this point in the history
…erialization (#2531)
  • Loading branch information
yagudin10 authored Apr 8, 2024
1 parent d34d59e commit fd82b24
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import io.cloudbeaver.server.jetty.CBJettyServer;
import io.cloudbeaver.service.DBWServiceInitializer;
import io.cloudbeaver.service.DBWServiceServerConfigurator;
import io.cloudbeaver.service.security.CBEmbeddedSecurityController;
import io.cloudbeaver.service.security.SMControllerConfiguration;
import io.cloudbeaver.service.session.WebSessionManager;
import io.cloudbeaver.utils.WebDataSourceUtils;
Expand Down Expand Up @@ -95,7 +94,8 @@ public abstract class CBApplication<T extends CBServerConfig> extends BaseWebApp
public static CBApplication getInstance() {
return (CBApplication) BaseApplicationImpl.getInstance();
}
private File homeDirectory;

private final File homeDirectory;

// Persistence
protected SMAdminController securityController;
Expand All @@ -109,6 +109,7 @@ public static CBApplication getInstance() {
private WebSessionManager sessionManager;

public CBApplication() {
this.homeDirectory = new File(initHomeFolder());
}

public String getServerURL() {
Expand Down Expand Up @@ -185,7 +186,7 @@ public Map<String, Object> getProductConfiguration() {
}

public SMControllerConfiguration getSecurityManagerConfiguration() {
return getServerConfigurationController().getSecurityManagerConfiguration();
return getServerConfiguration().getSecurityManagerConfiguration();
}

public SMAdminController getSecurityController() {
Expand All @@ -195,7 +196,6 @@ public SMAdminController getSecurityController() {
@Override
protected void startServer() {
CBPlatform.setApplication(this);
initHomeFolder();
try {
if (!loadServerConfiguration()) {
return;
Expand Down Expand Up @@ -466,14 +466,9 @@ protected String initHomeFolder() {
if (CommonUtils.isEmpty(homeFolder)) {
homeFolder = ".";
}
homeDirectory = new File(homeFolder);
return homeFolder;
}

protected void validateConfiguration(Map<String, Object> appConfig) throws DBException {

}

private void runWebServer() {
log.debug(
String.format("Starting Jetty server (%d on %s) ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,35 @@ public class CBApplicationCE extends CBApplication<CBServerConfig> {
private final CBServerConfigurationControllerEmbedded<CBServerConfig> serverConfigController;

public CBApplicationCE() {
serverConfigController = new CBServerConfigurationControllerEmbedded<>(new CBServerConfig());
super();
this.serverConfigController = new CBServerConfigurationControllerEmbedded<>(new CBServerConfig(), getHomeDirectory());
}

@Override
public SMController createSecurityController(@NotNull SMCredentialsProvider credentialsProvider) throws DBException {
return new EmbeddedSecurityControllerFactory().createSecurityService(
return new EmbeddedSecurityControllerFactory<>().createSecurityService(
this,
getServerConfiguration().getDatabaseConfiguration(),
credentialsProvider,
getServerConfigurationController().getSecurityManagerConfiguration()
getServerConfiguration().getSecurityManagerConfiguration()
);
}
@Override
public SMAdminController getAdminSecurityController(@NotNull SMCredentialsProvider credentialsProvider) throws DBException {
return new EmbeddedSecurityControllerFactory().createSecurityService(
return new EmbeddedSecurityControllerFactory<>().createSecurityService(
this,
getServerConfiguration().getDatabaseConfiguration(),
credentialsProvider,
getServerConfigurationController().getSecurityManagerConfiguration()
getServerConfiguration().getSecurityManagerConfiguration()
);
}

protected SMAdminController createGlobalSecurityController() throws DBException {
return new EmbeddedSecurityControllerFactory().createSecurityService(
return new EmbeddedSecurityControllerFactory<>().createSecurityService(
this,
getServerConfiguration().getDatabaseConfiguration(),
new NoAuthCredentialsProvider(),
getServerConfigurationController().getSecurityManagerConfiguration()
getServerConfiguration().getSecurityManagerConfiguration()
);
}

Expand All @@ -94,8 +95,8 @@ public CBServerConfigurationControllerEmbedded<CBServerConfig> getServerConfigur

protected void shutdown() {
try {
if (securityController instanceof CBEmbeddedSecurityController) {
((CBEmbeddedSecurityController) securityController).shutdown();
if (securityController instanceof CBEmbeddedSecurityController<?> embeddedSecurityController) {
embeddedSecurityController.shutdown();
}
} catch (Exception e) {
log.error(e);
Expand All @@ -108,8 +109,8 @@ protected void finishSecurityServiceConfiguration(
@Nullable String adminPassword,
@NotNull List<AuthInfo> authInfoList
) throws DBException {
if (securityController instanceof CBEmbeddedSecurityController) {
((CBEmbeddedSecurityController) securityController).finishConfiguration(adminName, adminPassword, authInfoList);
if (securityController instanceof CBEmbeddedSecurityController<?> embeddedSecurityController) {
embeddedSecurityController.finishConfiguration(adminName, adminPassword, authInfoList);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.gson.annotations.SerializedName;
import io.cloudbeaver.auth.CBAuthConstants;
import io.cloudbeaver.model.app.WebServerConfiguration;
import io.cloudbeaver.service.security.SMControllerConfiguration;
import io.cloudbeaver.service.security.db.WebDatabaseConfig;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.Log;
Expand Down Expand Up @@ -50,6 +51,8 @@ public class CBServerConfig implements WebServerConfiguration {
private boolean enableSecurityManager = false;
private final Map<String, Object> productSettings = new HashMap<>();

@SerializedName("sm")
protected final SMControllerConfiguration securityManagerConfiguration = new SMControllerConfiguration();
@SerializedName("database")
private WebDatabaseConfig databaseConfiguration = new WebDatabaseConfig();
private String staticContent = "";
Expand Down Expand Up @@ -191,4 +194,8 @@ public boolean isEnableSecurityManager() {
public Map<String, Object> getProductSettings() {
return productSettings;
}

public SMControllerConfiguration getSecurityManagerConfiguration() {
return securityManagerConfiguration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ public abstract class CBServerConfigurationController<T extends CBServerConfig>
private static final Log log = Log.getLog(CBServerConfigurationController.class);

// Configurations
protected final SMControllerConfiguration securityManagerConfiguration = new SMControllerConfiguration();
@NotNull
private final T serverConfiguration;
private final CBAppConfig appConfiguration = new CBAppConfig();
private Map<String, String> externalProperties = new LinkedHashMap<>();
private Map<String, Object> originalConfigurationProperties = new LinkedHashMap<>();
@NotNull
protected final Path homeDirectory;
private final Map<String, String> externalProperties = new LinkedHashMap<>();
private final Map<String, Object> originalConfigurationProperties = new LinkedHashMap<>();

protected CBServerConfigurationController(T serverConfiguration) {
protected CBServerConfigurationController(@NotNull T serverConfiguration, @NotNull Path homeDirectory) {
this.serverConfiguration = serverConfiguration;
this.homeDirectory = homeDirectory;
}

public String getAuthServiceURL() {
Expand Down Expand Up @@ -159,7 +162,7 @@ public T parseServerConfiguration() {
}
config.setServerURL("http://" + hostName + ":" + config.getServerPort());
}
var homeDirectory = CBApplication.getInstance().getHomeDirectory().toString();

config.setContentRoot(WebAppUtils.getRelativePath(config.getContentRoot(), homeDirectory));
config.setRootURI(readRootUri(config.getRootURI()));
config.setDriversLocation(WebAppUtils.getRelativePath(config.getDriversLocation(), homeDirectory));
Expand Down Expand Up @@ -223,7 +226,7 @@ protected void readProductConfiguration(Map<String, Object> serverConfig, Gson g
CBConstants.PARAM_PRODUCT_CONFIGURATION,
CBConstants.DEFAULT_PRODUCT_CONFIGURATION
),
CBApplication.getInstance().getHomeDirectory().toString()
homeDirectory
);
if (!CommonUtils.isEmpty(productConfigPath)) {
File productConfigFile = new File(productConfigPath);
Expand Down Expand Up @@ -300,6 +303,7 @@ protected GsonBuilder getGsonBuilder() {
// Stupid way to populate existing objects but ok google (https://github.com/google/gson/issues/431)
InstanceCreator<CBAppConfig> appConfigCreator = type -> appConfiguration;
InstanceCreator<DataSourceNavigatorSettings> navSettingsCreator = type -> (DataSourceNavigatorSettings) appConfiguration.getDefaultNavigatorSettings();
var securityManagerConfiguration = getServerConfiguration().getSecurityManagerConfiguration();
InstanceCreator<SMControllerConfiguration> smConfigCreator = type -> securityManagerConfiguration;
InstanceCreator<T> serverConfigCreator = type -> serverConfiguration;
InstanceCreator<PasswordPolicyConfiguration> smPasswordPoliceConfigCreator =
Expand Down Expand Up @@ -602,10 +606,6 @@ public Map<String, Object> getProductConfiguration() {
return getServerConfiguration().getProductSettings();
}

public SMControllerConfiguration getSecurityManagerConfiguration() {
return securityManagerConfiguration;
}

private String readRootUri(String uri) {
//slashes are needed to correctly display static resources on ui
if (!uri.endsWith("/")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jkiss.dbeaver.model.data.json.JSONUtils;
import org.jkiss.utils.CommonUtils;

import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;

Expand All @@ -35,8 +36,8 @@ public class CBServerConfigurationControllerEmbedded<T extends CBServerConfig> e

private static final Log log = Log.getLog(CBServerConfigurationControllerEmbedded.class);

public CBServerConfigurationControllerEmbedded(T serverConfig) {
super(serverConfig);
public CBServerConfigurationControllerEmbedded(@NotNull T serverConfig, @NotNull Path homeDirectory) {
super(serverConfig, homeDirectory);
}

@NotNull
Expand Down Expand Up @@ -79,7 +80,7 @@ private void savePasswordPolicyConfig(Map<String, Object> originServerConfig, Ma
);
Gson gson = getGson();
Map<String, Object> passwordPolicyConfig = gson.fromJson(
gson.toJsonTree(securityManagerConfiguration.getPasswordPolicyConfiguration()),
gson.toJsonTree(getServerConfiguration().getSecurityManagerConfiguration().getPasswordPolicyConfiguration()),
JSONUtils.MAP_TYPE_TOKEN
);
if (!CommonUtils.isEmpty(passwordPolicyConfig)) {
Expand All @@ -98,10 +99,8 @@ protected GsonBuilder getGsonBuilder() {
GsonBuilder gsonBuilder = super.getGsonBuilder();
var databaseConfiguration = getServerConfiguration().getDatabaseConfiguration();
InstanceCreator<WebDatabaseConfig> dbConfigCreator = type -> databaseConfiguration;
InstanceCreator<WebDatabaseConfig.Pool> dbPoolConfigCreator = type -> databaseConfiguration.getPool();
return gsonBuilder
.registerTypeAdapter(WebDatabaseConfig.class, dbConfigCreator)
.registerTypeAdapter(WebDatabaseConfig.Pool.class, dbPoolConfigCreator);
.registerTypeAdapter(WebDatabaseConfig.class, dbConfigCreator);
}


Expand Down

0 comments on commit fd82b24

Please sign in to comment.