Skip to content

Commit

Permalink
CB-5045 move configs to model package (#2705)
Browse files Browse the repository at this point in the history
* CB-5045 move configs to model package

* CB-5045 jetty 12 fix

* CB-5045 fix conflicts

---------

Co-authored-by: kseniaguzeeva <[email protected]>
Co-authored-by: Aleksandr Skoblikov <[email protected]>
  • Loading branch information
3 people authored Sep 19, 2024
1 parent efb42d3 commit b869bdd
Show file tree
Hide file tree
Showing 31 changed files with 206 additions and 247 deletions.
1 change: 1 addition & 0 deletions server/bundles/io.cloudbeaver.model/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Export-Package: io.cloudbeaver,
io.cloudbeaver.websocket,
io.cloudbeaver.model,
io.cloudbeaver.model.app,
io.cloudbeaver.model.config,
io.cloudbeaver.model.fs,
io.cloudbeaver.model.log,
io.cloudbeaver.model.rm,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cloudbeaver.server;
package io.cloudbeaver.model.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.cloudbeaver.DBWebException;
import io.cloudbeaver.WebServiceUtils;
import io.cloudbeaver.model.app.BaseAuthWebAppConfiguration;
import com.google.gson.annotations.Expose;
import io.cloudbeaver.auth.provider.local.LocalAuthProviderConstants;
import io.cloudbeaver.model.app.BaseWebAppConfiguration;
import io.cloudbeaver.model.app.WebAuthConfiguration;
import io.cloudbeaver.registry.WebAuthProviderDescriptor;
import io.cloudbeaver.registry.WebAuthProviderRegistry;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.connection.DBPDriver;
import org.jkiss.dbeaver.model.navigator.DBNBrowseSettings;
import org.jkiss.dbeaver.model.security.SMAuthProviderCustomConfiguration;
import org.jkiss.dbeaver.registry.DataSourceNavigatorSettings;
import org.jkiss.utils.ArrayUtils;
import org.jkiss.utils.CommonUtils;
Expand All @@ -38,11 +36,15 @@
/**
* Application configuration
*/
public class CBAppConfig extends BaseAuthWebAppConfiguration implements WebAuthConfiguration {
public class CBAppConfig extends BaseWebAppConfiguration implements WebAuthConfiguration {
private static final Log log = Log.getLog(CBAppConfig.class);
public static final DataSourceNavigatorSettings.Preset PRESET_WEB = new DataSourceNavigatorSettings.Preset("web", "Web", "Default view");

public static final DataSourceNavigatorSettings DEFAULT_VIEW_SETTINGS = PRESET_WEB.getSettings();
private final Set<SMAuthProviderCustomConfiguration> authConfigurations;
// Legacy auth configs, left for backward compatibility
@Expose(serialize = false)
private final Map<String, SMAuthProviderCustomConfiguration> authConfiguration;

private boolean supportsCustomConnections;
private boolean supportsConnectionBrowser;
Expand All @@ -66,9 +68,14 @@ public class CBAppConfig extends BaseAuthWebAppConfiguration implements WebAuthC
private DataSourceNavigatorSettings defaultNavigatorSettings;

private final Map<String, Object> resourceQuotas;
private String defaultAuthProvider;
private String[] enabledAuthProviders;

public CBAppConfig() {
super();
this.defaultAuthProvider = LocalAuthProviderConstants.PROVIDER_ID;
this.enabledAuthProviders = null;
this.authConfigurations = new LinkedHashSet<>();
this.authConfiguration = new LinkedHashMap<>();
this.anonymousAccessEnabled = false;
this.anonymousUserRole = DEFAULT_APP_ANONYMOUS_TEAM_NAME;
this.anonymousUserTeam = DEFAULT_APP_ANONYMOUS_TEAM_NAME;
Expand All @@ -91,6 +98,10 @@ public CBAppConfig() {

public CBAppConfig(CBAppConfig src) {
super(src);
this.defaultAuthProvider = src.defaultAuthProvider;
this.enabledAuthProviders = src.enabledAuthProviders;
this.authConfigurations = new LinkedHashSet<>(src.authConfigurations);
this.authConfiguration = new LinkedHashMap<>(src.authConfiguration);
this.anonymousAccessEnabled = src.anonymousAccessEnabled;
this.anonymousUserRole = src.anonymousUserRole;
this.anonymousUserTeam = src.anonymousUserTeam;
Expand Down Expand Up @@ -169,6 +180,10 @@ public String[] getEnabledDrivers() {
return enabledDrivers;
}

public void setEnabledDrivers(String[] enabledDrivers) {
this.enabledDrivers = enabledDrivers;
}

public String[] getDisabledDrivers() {
return disabledDrivers;
}
Expand Down Expand Up @@ -204,19 +219,6 @@ public Map<String, Object> getPluginConfig(@NotNull String pluginId) {
return getPluginConfig(pluginId, false);
}

public <T> T getPluginOptions(@NotNull String pluginId, @NotNull String option, Class<T> theClass) throws DBException {
Map<String, Object> iamSettingsMap = CBPlatform.getInstance().getApplication().getAppConfiguration().getPluginOption(
pluginId, option);
if (CommonUtils.isEmpty(iamSettingsMap)) {
throw new DBException("Settings '" + option + "' not specified in plugin '" + pluginId + "' configuration");
}

Gson gson = new GsonBuilder().create();
return gson.fromJson(
gson.toJsonTree(iamSettingsMap),
theClass);
}

////////////////////////////////////////////
// Quotas

Expand Down Expand Up @@ -264,43 +266,100 @@ public boolean isGrantConnectionsAccessToAnonymousTeam() {
return grantConnectionsAccessToAnonymousTeam;
}

public boolean isDriverForceEnabled(@NotNull String driverId) {
return ArrayUtils.containsIgnoreCase(getEnabledDrivers(), driverId);
}

public boolean isSystemVariablesResolvingEnabled() {
return systemVariablesResolvingEnabled;
}

@Override
public String getDefaultAuthProvider() {
return defaultAuthProvider;
}

// we disable embedded drivers by default and enable it in enabled drivers list
// that's why we need so complicated logic for disabling drivers
public void setDefaultAuthProvider(String defaultAuthProvider) {
this.defaultAuthProvider = defaultAuthProvider;
}

public void updateDisabledDriversConfig(String[] disabledDriversConfig) {
Set<String> disabledIds = new LinkedHashSet<>(Arrays.asList(disabledDriversConfig));
Set<String> enabledIds = new LinkedHashSet<>(Arrays.asList(this.enabledDrivers));
@Override
public String[] getEnabledAuthProviders() {
if (enabledAuthProviders == null) {
// No config - enable all providers (+backward compatibility)
return WebAuthProviderRegistry.getInstance().getAuthProviders()
.stream().map(WebAuthProviderDescriptor::getId).toArray(String[]::new);
}
return enabledAuthProviders;
}

// remove all disabled embedded drivers from enabled drivers list
enabledIds.removeAll(disabledIds);
public void setEnabledAuthProviders(String[] enabledAuthProviders) {
this.enabledAuthProviders = enabledAuthProviders;
}

// enable embedded driver if it is not in disabled drivers list
for (String driverId : this.disabledDrivers) {
if (disabledIds.contains(driverId)) {
// driver is also disabled
continue;
}
// driver is removed from disabled list
// we need to enable if it is embedded
try {
DBPDriver driver = WebServiceUtils.getDriverById(driverId);
if (driver.isEmbedded()) {
enabledIds.add(driverId);
@Override
public boolean isAuthProviderEnabled(String id) {
var authProviderDescriptor = WebAuthProviderRegistry.getInstance().getAuthProvider(id);
if (authProviderDescriptor == null) {
return false;
}

if (!ArrayUtils.contains(getEnabledAuthProviders(), id)) {
return false;
}
if (!ArrayUtils.isEmpty(authProviderDescriptor.getRequiredFeatures())) {
for (String rf : authProviderDescriptor.getRequiredFeatures()) {
if (!isFeatureEnabled(rf)) {
return false;
}
} catch (DBWebException e) {
log.error("Failed to find driver by id", e);
}
}
this.disabledDrivers = disabledDriversConfig;
this.enabledDrivers = enabledIds.toArray(String[]::new);
return true;
}

public boolean isDriverForceEnabled(@NotNull String driverId) {
return ArrayUtils.containsIgnoreCase(getEnabledDrivers(), driverId);
////////////////////////////////////////////
// Auth provider configs
@Override
public Set<SMAuthProviderCustomConfiguration> getAuthCustomConfigurations() {
return authConfigurations;
}

public boolean isSystemVariablesResolvingEnabled() {
return systemVariablesResolvingEnabled;
@Override
@Nullable
public SMAuthProviderCustomConfiguration getAuthProviderConfiguration(@NotNull String id) {
synchronized (authConfigurations) {
return authConfigurations.stream().filter(c -> c.getId().equals(id)).findAny().orElse(null);
}
}

public void addAuthProviderConfiguration(@NotNull SMAuthProviderCustomConfiguration config) {
synchronized (authConfigurations) {
authConfigurations.removeIf(c -> c.getId().equals(config.getId()));
authConfigurations.add(config);
}
}

public void setAuthProvidersConfigurations(Collection<SMAuthProviderCustomConfiguration> authProviders) {
synchronized (authConfigurations) {
authConfigurations.clear();
authConfigurations.addAll(authProviders);
}
}

public boolean deleteAuthProviderConfiguration(@NotNull String id) {
synchronized (authConfigurations) {
return authConfigurations.removeIf(c -> c.getId().equals(id));
}
}

public void loadLegacyCustomConfigs() {
// Convert legacy map of configs into list
if (!authConfiguration.isEmpty()) {
for (Map.Entry<String, SMAuthProviderCustomConfiguration> entry : authConfiguration.entrySet()) {
entry.getValue().setId(entry.getKey());
authConfigurations.add(entry.getValue());
}
authConfiguration.clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cloudbeaver.server;
package io.cloudbeaver.model.config;

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 io.cloudbeaver.server.CBConstants;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.Log;

Expand Down
Loading

0 comments on commit b869bdd

Please sign in to comment.