Skip to content

Commit

Permalink
CB-5574. Update testConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisSinelnikov committed Jan 6, 2025
1 parent 6349118 commit 1911c0a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
* Copyright (C) 2010-2025 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.
Expand Down Expand Up @@ -45,5 +45,10 @@ boolean deleteConnection(
@Nullable @WebObjectId String projectId,
@NotNull String connectionId) throws DBWebException;

WebConnectionInfo testConnection(

Check warning on line 48 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/ConnectionController.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/ConnectionController.java#L48

Missing a Javadoc comment.
@NotNull WebSession webSession,
@Nullable String projectId,
@NotNull WebConnectionConfig connectionConfig) throws DBWebException;

WebPropertyInfo[] getExternalInfo(WebSession webSession);

Check warning on line 53 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/ConnectionController.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/ConnectionController.java#L53

Missing a Javadoc comment.
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
*/
package io.cloudbeaver.service;

import io.cloudbeaver.DBWConstants;
import io.cloudbeaver.DBWebException;
import io.cloudbeaver.WebServiceUtils;
import io.cloudbeaver.WebSessionProjectImpl;
import io.cloudbeaver.*;
import io.cloudbeaver.model.WebConnectionConfig;
import io.cloudbeaver.model.WebConnectionInfo;
import io.cloudbeaver.model.WebPropertyInfo;
Expand All @@ -37,6 +34,8 @@
import org.jkiss.dbeaver.model.websocket.WSConstants;
import org.jkiss.dbeaver.model.websocket.event.datasource.WSDataSourceProperty;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.runtime.jobs.ConnectionTestJob;
import org.jkiss.dbeaver.utils.RuntimeUtils;
import org.jkiss.utils.CommonUtils;

public class ConnectionControllerCE implements ConnectionController {
Expand Down Expand Up @@ -197,6 +196,81 @@ public boolean deleteConnection(
return true;
}

@Override
public WebConnectionInfo testConnection(@NotNull WebSession webSession, @Nullable String projectId, @NotNull WebConnectionConfig connectionConfig) throws DBWebException {

Check warning on line 200 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/ConnectionControllerCE.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/ConnectionControllerCE.java#L200

Line is longer than 140 characters (found 174).
String connectionId = connectionConfig.getConnectionId();

connectionConfig.setSaveCredentials(true); // It is used in createConnectionFromConfig

DataSourceDescriptor dataSource = (DataSourceDescriptor) WebDataSourceUtils.getLocalOrGlobalDataSource(
webSession, projectId, connectionId);

WebProjectImpl project = getProjectById(webSession, projectId);
DBPDataSourceRegistry sessionRegistry = project.getDataSourceRegistry();
DataSourceDescriptor testDataSource;
if (dataSource != null) {
try {
// Check that creds are saved to trigger secrets resolve
dataSource.isCredentialsSaved();
} catch (DBException e) {
throw new DBWebException("Can't determine whether datasource credentials are saved", e);
}

testDataSource = (DataSourceDescriptor) dataSource.createCopy(dataSource.getRegistry());
WebServiceUtils.setConnectionConfiguration(
testDataSource.getDriver(),
testDataSource.getConnectionConfiguration(),
connectionConfig
);
if (connectionConfig.getSelectedSecretId() != null) {
try {
dataSource.listSharedCredentials()
.stream()
.filter(secret -> connectionConfig.getSelectedSecretId().equals(secret.getSubjectId()))
.findFirst()
.ifPresent(testDataSource::setSelectedSharedCredentials);

} catch (DBException e) {
throw new DBWebException("Failed to load secret value: " + connectionConfig.getSelectedSecretId());
}
}
WebServiceUtils.saveAuthProperties(
testDataSource,
testDataSource.getConnectionConfiguration(),
connectionConfig.getCredentials(),
true,
false,
true
);
} else {
testDataSource = (DataSourceDescriptor) WebServiceUtils.createConnectionFromConfig(connectionConfig,
sessionRegistry);
}
webSession.provideAuthParameters(webSession.getProgressMonitor(),
testDataSource,
testDataSource.getConnectionConfiguration());
testDataSource.setSavePassword(true); // We need for test to avoid password callback
if (DataSourceDescriptor.class.isAssignableFrom(testDataSource.getClass())) {
testDataSource.setAccessCheckRequired(!webSession.hasPermission(DBWConstants.PERMISSION_ADMIN));
}
try {
ConnectionTestJob ct = new ConnectionTestJob(testDataSource, param -> {
});
ct.run(webSession.getProgressMonitor());
if (ct.getConnectError() != null) {
throw new DBWebException("Connection failed", ct.getConnectError());
}
WebConnectionInfo connectionInfo = new WebConnectionInfo(webSession, testDataSource);
connectionInfo.setConnectError(ct.getConnectError());
connectionInfo.setServerVersion(ct.getServerVersion());
connectionInfo.setClientVersion(ct.getClientVersion());
connectionInfo.setConnectTime(RuntimeUtils.formatExecutionTime(ct.getConnectTime()));
return connectionInfo;
} catch (DBException e) {
throw new DBWebException("Error connecting to database", e);
}
}

private WebSessionProjectImpl getProjectById(WebSession webSession, String projectId) throws DBWebException {
WebSessionProjectImpl project = webSession.getProjectById(projectId);
if (project == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
* Copyright (C) 2010-2025 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.
Expand Down Expand Up @@ -580,77 +580,7 @@ public WebConnectionInfo copyConnectionFromNode(
public WebConnectionInfo testConnection(
@NotNull WebSession webSession, @Nullable String projectId, @NotNull WebConnectionConfig connectionConfig
) throws DBWebException {
String connectionId = connectionConfig.getConnectionId();

connectionConfig.setSaveCredentials(true); // It is used in createConnectionFromConfig

DataSourceDescriptor dataSource = (DataSourceDescriptor) WebDataSourceUtils.getLocalOrGlobalDataSource(
webSession, projectId, connectionId);

WebProjectImpl project = getProjectById(webSession, projectId);
DBPDataSourceRegistry sessionRegistry = project.getDataSourceRegistry();
DataSourceDescriptor testDataSource;
if (dataSource != null) {
try {
// Check that creds are saved to trigger secrets resolve
dataSource.isCredentialsSaved();
} catch (DBException e) {
throw new DBWebException("Can't determine whether datasource credentials are saved", e);
}

testDataSource = (DataSourceDescriptor) dataSource.createCopy(dataSource.getRegistry());
WebServiceUtils.setConnectionConfiguration(
testDataSource.getDriver(),
testDataSource.getConnectionConfiguration(),
connectionConfig
);
if (connectionConfig.getSelectedSecretId() != null) {
try {
dataSource.listSharedCredentials()
.stream()
.filter(secret -> connectionConfig.getSelectedSecretId().equals(secret.getSubjectId()))
.findFirst()
.ifPresent(testDataSource::setSelectedSharedCredentials);

} catch (DBException e) {
throw new DBWebException("Failed to load secret value: " + connectionConfig.getSelectedSecretId());
}
}
WebServiceUtils.saveAuthProperties(
testDataSource,
testDataSource.getConnectionConfiguration(),
connectionConfig.getCredentials(),
true,
false,
true
);
} else {
testDataSource = (DataSourceDescriptor) WebServiceUtils.createConnectionFromConfig(connectionConfig,
sessionRegistry);
}
webSession.provideAuthParameters(webSession.getProgressMonitor(),
testDataSource,
testDataSource.getConnectionConfiguration());
testDataSource.setSavePassword(true); // We need for test to avoid password callback
if (DataSourceDescriptor.class.isAssignableFrom(testDataSource.getClass())) {
testDataSource.setAccessCheckRequired(!webSession.hasPermission(DBWConstants.PERMISSION_ADMIN));
}
try {
ConnectionTestJob ct = new ConnectionTestJob(testDataSource, param -> {
});
ct.run(webSession.getProgressMonitor());
if (ct.getConnectError() != null) {
throw new DBWebException("Connection failed", ct.getConnectError());
}
WebConnectionInfo connectionInfo = new WebConnectionInfo(webSession, testDataSource);
connectionInfo.setConnectError(ct.getConnectError());
connectionInfo.setServerVersion(ct.getServerVersion());
connectionInfo.setClientVersion(ct.getClientVersion());
connectionInfo.setConnectTime(RuntimeUtils.formatExecutionTime(ct.getConnectTime()));
return connectionInfo;
} catch (DBException e) {
throw new DBWebException("Error connecting to database", e);
}
return WebAppUtils.getWebApplication().getConnectionController().testConnection(webSession, projectId, connectionConfig);
}

@Override
Expand Down

0 comments on commit 1911c0a

Please sign in to comment.