Skip to content

Commit

Permalink
CB-5570 do not load data sources in web session init (#2887)
Browse files Browse the repository at this point in the history
* CB-5570 do not load data sources in web session init

* CB-5570 extract methods about web connection info from web session

* CB-5570 remove accessible connections from web session

* CB-5570 add javadoc

* CB-5570 Merge conflicts resolve

* CB-5570 return emply list for template connections in te

* chore: reorganize localization for connections

---------

Co-authored-by: serge-rider <serge@dbeaver.com>
Co-authored-by: kseniaguzeeva <112612526+kseniaguzeeva@users.noreply.github.com>
Co-authored-by: Evgenia Bezborodova <139753579+EvgeniaBzzz@users.noreply.github.com>
Co-authored-by: Aleksei Potsetsuev <wrouds@gmail.com>
Co-authored-by: mr-anton-t <42037741+mr-anton-t@users.noreply.github.com>
  • Loading branch information
6 people authored Sep 30, 2024
1 parent c3c15bf commit eb33e0e
Show file tree
Hide file tree
Showing 61 changed files with 713 additions and 674 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,18 @@ public abstract class BaseWebProjectImpl extends BaseProjectImpl implements RMCo
@NotNull
private final Path path;
@NotNull
protected final DataSourceFilter dataSourceFilter;
private final RMController resourceController;

public BaseWebProjectImpl(
@NotNull DBPWorkspace workspace,
@NotNull RMController resourceController,
@NotNull SMSessionContext sessionContext,
@NotNull RMProject project,
@NotNull DataSourceFilter dataSourceFilter
@NotNull RMProject project
) {
super(workspace, sessionContext);
this.resourceController = resourceController;
this.path = RMUtils.getProjectPath(project);
this.project = project;
this.dataSourceFilter = dataSourceFilter;
}

@NotNull
Expand Down Expand Up @@ -104,11 +101,6 @@ public boolean isUseSecretStorage() {
return false;
}

@NotNull
public RMProject getRmProject() {
return this.project;
}

/**
* Method for Bulk Update of resources properties paths
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@
public abstract class WebProjectImpl extends BaseWebProjectImpl {
private static final Log log = Log.getLog(WebProjectImpl.class);
@NotNull
private final DBPPreferenceStore preferenceStore;
protected final DBPPreferenceStore preferenceStore;
public WebProjectImpl(
@NotNull DBPWorkspace workspace,
@NotNull RMController resourceController,
@NotNull SMSessionContext sessionContext,
@NotNull RMProject project,
@NotNull DataSourceFilter dataSourceFilter,
@NotNull DBPPreferenceStore preferenceStore
) {
super(workspace, resourceController, sessionContext, project, dataSourceFilter);
super(workspace, resourceController, sessionContext, project);
this.preferenceStore = preferenceStore;
}

Expand Down Expand Up @@ -82,8 +81,13 @@ public DBTTaskManager getTaskManager() {
protected DBPDataSourceRegistry createDataSourceRegistry() {
return new WebDataSourceRegistryProxy(
new DataSourceRegistryRM(this, getResourceController(), preferenceStore),
dataSourceFilter
getDataSourceFilter()
);
}

@NotNull
public DataSourceFilter getDataSourceFilter() {
return (ds) -> true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 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;

import io.cloudbeaver.model.session.WebSession;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.DBPEvent;
import org.jkiss.dbeaver.model.rm.RMProject;
import org.jkiss.dbeaver.model.security.SMObjectType;
import org.jkiss.dbeaver.model.security.user.SMObjectPermissions;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Global project.
* Connections there can be not accessible.
*/
public class WebSessionGlobalProjectImpl extends WebSessionProjectImpl {
private static final Log log = Log.getLog(WebSessionGlobalProjectImpl.class);
private Set<String> accessibleConnectionIds = Collections.emptySet();

public WebSessionGlobalProjectImpl(@NotNull WebSession webSession, @NotNull RMProject project) {
super(webSession, project);
}

/**
* Update info about accessible connections from a database.
*/
public synchronized void refreshAccessibleConnectionIds() {
this.accessibleConnectionIds = readAccessibleConnectionIds();
}

@NotNull
private Set<String> readAccessibleConnectionIds() {
try {
return webSession.getSecurityController()
.getAllAvailableObjectsPermissions(SMObjectType.datasource)
.stream()
.map(SMObjectPermissions::getObjectId)
.collect(Collectors.toSet());
} catch (DBException e) {
webSession.addSessionError(e);
log.error("Error reading connection grants", e);
return Collections.emptySet();
}
}

/**
* Checks if connection is accessible for current user.
*/
public boolean isDataSourceAccessible(@NotNull DBPDataSourceContainer dataSource) {
return dataSource.isExternallyProvided() ||
dataSource.isTemporary() ||
webSession.hasPermission(DBWConstants.PERMISSION_ADMIN) ||
accessibleConnectionIds.contains(dataSource.getId());
}

/**
* Adds a connection if it became accessible.
* The method is processed when connection permissions were updated.
*/
public synchronized void addAccessibleConnectionToCache(@NotNull String dsId) {
if (!getRMProject().isGlobal()) {
return;
}
this.accessibleConnectionIds.add(dsId);
var registry = getDataSourceRegistry();
var dataSource = registry.getDataSource(dsId);
if (dataSource != null) {
addConnection(dataSource);
// reflect changes is navigator model
registry.notifyDataSourceListeners(new DBPEvent(DBPEvent.Action.OBJECT_ADD, dataSource, true));
}
}

/**
* Removes a connection if it became not accessible.
* The method is processed when connection permissions were updated.
*/
public synchronized void removeAccessibleConnectionFromCache(@NotNull String dsId) {
if (!getRMProject().isGlobal()) {
return;
}
var registry = getDataSourceRegistry();
var dataSource = registry.getDataSource(dsId);
if (dataSource != null) {
this.accessibleConnectionIds.remove(dsId);
removeConnection(dataSource);
// reflect changes is navigator model
registry.notifyDataSourceListeners(new DBPEvent(DBPEvent.Action.OBJECT_REMOVE, dataSource));
dataSource.dispose();
}
}

@NotNull
public DataSourceFilter getDataSourceFilter() {
return this::isDataSourceAccessible;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,39 @@
*/
package io.cloudbeaver;

import io.cloudbeaver.model.WebConnectionInfo;
import io.cloudbeaver.model.session.WebSession;
import io.cloudbeaver.utils.WebDataSourceUtils;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.app.DBPDataSourceRegistry;
import org.jkiss.dbeaver.model.app.DBPDataSourceRegistryCache;
import org.jkiss.dbeaver.model.navigator.DBNModel;
import org.jkiss.dbeaver.model.rm.RMProject;
import org.jkiss.dbeaver.model.websocket.event.WSEventType;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.runtime.jobs.DisconnectJob;

public class WebSessionProjectImpl extends WebProjectImpl {
import java.util.*;
import java.util.stream.Collectors;

private final WebSession webSession;
public class WebSessionProjectImpl extends WebProjectImpl {
private static final Log log = Log.getLog(WebSessionProjectImpl.class);
protected final WebSession webSession;
private final Map<String, WebConnectionInfo> connections = new HashMap<>();
private boolean registryIsLoaded = false;

public WebSessionProjectImpl(
@NotNull WebSession webSession,
@NotNull RMProject project,
@NotNull DataSourceFilter dataSourceFilter
@NotNull RMProject project
) {
super(
webSession.getWorkspace(),
webSession.getRmController(),
webSession.getSessionContext(),
project,
dataSourceFilter,
webSession.getUserPreferenceStore()
);
this.webSession = webSession;
Expand All @@ -47,4 +59,156 @@ public WebSessionProjectImpl(
public DBNModel getNavigatorModel() {
return webSession.getNavigatorModel();
}

@NotNull
@Override
protected DBPDataSourceRegistry createDataSourceRegistry() {
DBPDataSourceRegistry dataSourceRegistry = super.createDataSourceRegistry();
dataSourceRegistry.setAuthCredentialsProvider(webSession);
return dataSourceRegistry;
}

private synchronized void addDataSourcesToCache() {
if (registryIsLoaded) {
return;
}
getDataSourceRegistry().getDataSources().forEach(this::addConnection);
Throwable lastError = getDataSourceRegistry().getLastError();
if (lastError != null) {
webSession.addSessionError(lastError);
log.error("Error refreshing connections from project '" + getId() + "'", lastError);
}
registryIsLoaded = true;
}

@Override
public void dispose() {
super.dispose();
Map<String, WebConnectionInfo> conCopy;
synchronized (this.connections) {
conCopy = new HashMap<>(this.connections);
this.connections.clear();
}

for (WebConnectionInfo connectionInfo : conCopy.values()) {
if (connectionInfo.isConnected()) {
new DisconnectJob(connectionInfo.getDataSourceContainer()).schedule();
}
}
}


/**
* Returns web connection info from cache (if exists).
*/
@Nullable
public WebConnectionInfo findWebConnectionInfo(@NotNull String connectionId) {
synchronized (connections) {
return connections.get(connectionId);
}
}

/**
* Returns web connection info from cache, adds it to cache if not present.
* Throws exception if connection is not found.
*/
@NotNull
public WebConnectionInfo getWebConnectionInfo(@NotNull String connectionId) throws DBWebException {
WebConnectionInfo connectionInfo = findWebConnectionInfo(connectionId);
if (connectionInfo != null) {
return connectionInfo;
}
DBPDataSourceContainer dataSource = getDataSourceRegistry().getDataSource(connectionId);
if (dataSource != null) {
return addConnection(dataSource);
}
throw new DBWebException("Connection '%s' not found".formatted(connectionId));
}

/**
* Adds connection to project cache.
*/
@NotNull
public synchronized WebConnectionInfo addConnection(@NotNull DBPDataSourceContainer dataSourceContainer) {
WebConnectionInfo connection = new WebConnectionInfo(webSession, dataSourceContainer);
synchronized (connections) {
connections.put(dataSourceContainer.getId(), connection);
}
return connection;
}

/**
* Removes connection from project cache.
*/
public void removeConnection(@NotNull DBPDataSourceContainer dataSourceContainer) {
WebConnectionInfo webConnectionInfo = connections.get(dataSourceContainer.getId());
if (webConnectionInfo != null) {
webConnectionInfo.clearCache();
synchronized (connections) {
connections.remove(dataSourceContainer.getId());
}
}
}

/**
* Loads connection from registry if they are not loaded.
*
* @return connections from cache.
*/
public List<WebConnectionInfo> getConnections() {
if (!registryIsLoaded) {
addDataSourcesToCache();
registryIsLoaded = true;
}
synchronized (connections) {
return new ArrayList<>(connections.values());
}
}

/**
* updates data sources based on event in web session
*
* @param dataSourceIds list of updated connections
* @param type type of event
*/
public synchronized boolean updateProjectDataSources(@NotNull List<String> dataSourceIds, @NotNull WSEventType type) {
var sendDataSourceUpdatedEvent = false;
DBPDataSourceRegistry registry = getDataSourceRegistry();
// save old connections
var oldDataSources = dataSourceIds.stream()
.map(registry::getDataSource)
.filter(Objects::nonNull)
.collect(Collectors.toMap(
DBPDataSourceContainer::getId,
ds -> new DataSourceDescriptor((DataSourceDescriptor) ds, ds.getRegistry())
));
if (type == WSEventType.DATASOURCE_CREATED || type == WSEventType.DATASOURCE_UPDATED) {
registry.refreshConfig(dataSourceIds);
}
for (String dsId : dataSourceIds) {
DataSourceDescriptor ds = (DataSourceDescriptor) registry.getDataSource(dsId);
if (ds == null) {
continue;
}
switch (type) {
case DATASOURCE_CREATED -> {
addConnection(ds);
sendDataSourceUpdatedEvent = true;
}
case DATASOURCE_UPDATED -> // if settings were changed we need to send event
sendDataSourceUpdatedEvent |= !ds.equalSettings(oldDataSources.get(dsId));
case DATASOURCE_DELETED -> {
WebDataSourceUtils.disconnectDataSource(webSession, ds);
if (registry instanceof DBPDataSourceRegistryCache dsrc) {
dsrc.removeDataSourceFromList(ds);
}
removeConnection(ds);
sendDataSourceUpdatedEvent = true;
}
default -> {
}
}
}
return sendDataSourceUpdatedEvent;
}
}
Loading

0 comments on commit eb33e0e

Please sign in to comment.