From 32671ff239cc17dddc3da11b5a63db7a93e16fb1 Mon Sep 17 00:00:00 2001 From: Ainur <59531286+yagudin10@users.noreply.github.com> Date: Thu, 19 Feb 2026 07:03:50 +0200 Subject: [PATCH] dbeaver/pro#8196 validate projects on copying connection (#4141) Co-authored-by: Evgenia <139753579+EvgeniaBzzz@users.noreply.github.com> --- .../service/core/impl/WebServiceCore.java | 5 +++ .../test/platform/ConnectionsTest.java | 41 +++++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/core/impl/WebServiceCore.java b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/core/impl/WebServiceCore.java index 272eecda22..ef5fce6308 100644 --- a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/core/impl/WebServiceCore.java +++ b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/core/impl/WebServiceCore.java @@ -464,6 +464,11 @@ public class WebServiceCore implements DBWServiceCore { throw new DBException("Node '" + nodePath + "' is not a datasource node"); } DBPDataSourceContainer dataSourceTemplate = dbnDataSource.getDataSourceContainer(); + if (!dataSourceTemplate.isExternallyProvided()) { + if (!CommonUtils.equalObjects(dataSourceTemplate.getProject(), project)) { + throw new DBException("Copying connection to another project is not allowed"); + } + } DataSourceDescriptor newDataSource = dataSourceRegistry.createDataSource(dataSourceTemplate); diff --git a/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/ConnectionsTest.java b/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/ConnectionsTest.java index 70a6839dcb..ebda986a10 100644 --- a/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/ConnectionsTest.java +++ b/server/test/io.cloudbeaver.test.platform/src/io/cloudbeaver/test/platform/ConnectionsTest.java @@ -20,6 +20,7 @@ package io.cloudbeaver.test.platform; import io.cloudbeaver.CloudbeaverMockTest; import io.cloudbeaver.app.CEAppStarter; import io.cloudbeaver.test.WebGQLClient; +import org.jkiss.code.NotNull; import org.jkiss.dbeaver.DBException; import org.jkiss.dbeaver.model.data.json.JSONUtils; import org.jkiss.dbeaver.runtime.DBWorkbench; @@ -37,12 +38,21 @@ public class ConnectionsTest extends CloudbeaverMockTest { query userConnections { result: userConnections { id + nodePath } }"""; private static final String GQL_CONNECTIONS_CREATE = """ mutation createConnection($config: ConnectionConfig!, $projectId: ID) { result: createConnection(config: $config, projectId: $projectId) { id + nodePath + } + }"""; + private static final String GQL_COPY_CONNECTION_FROM_NODE = """ + mutation copyConnectionFromNode($nodePath: String!, $config: ConnectionConfig, $projectId: ID) { + result: copyConnectionFromNode(nodePath: $nodePath, config: $config, projectId: $projectId) { + id + nodePath } }"""; private static final String GQL_CONNECTIONS_DELETE = """ @@ -89,11 +99,36 @@ public class ConnectionsTest extends CloudbeaverMockTest { configuration.put("driverId", "postgresql:postgres-jdbc"); Map addedConnection = client.sendQuery(GQL_CONNECTIONS_CREATE, variables); + Assert.assertNotNull(addedConnection); + checkAddedConnection(client, addedConnection); + String addedConnectionId = JSONUtils.getString(addedConnection, "id"); + String nodePath = JSONUtils.getString(addedConnection, "nodePath"); + + Map config1 = Map.of("name", "connection copy"); + Map variables1 = new LinkedHashMap<>(); + variables1.put("projectId", "g_GlobalConfiguration"); + variables1.put("config", config1); + variables1.put("nodePath", nodePath); + + Assert.assertThrows( + DBException.class, + () -> client.sendQuery(GQL_COPY_CONNECTION_FROM_NODE, variables1) + ); + variables1.put("projectId", "u_test"); + Map copiedConnection = client.sendQuery(GQL_COPY_CONNECTION_FROM_NODE, variables1); + Assert.assertNotNull(copiedConnection); + checkAddedConnection(client, copiedConnection); + String copiedConnectionId = JSONUtils.getString(copiedConnection, "id"); + Assert.assertTrue(client.sendQuery(GQL_CONNECTIONS_DELETE, Map.of("id", addedConnectionId))); + Assert.assertTrue(client.sendQuery(GQL_CONNECTIONS_DELETE, Map.of("id", copiedConnectionId))); + } + + private void checkAddedConnection(@NotNull WebGQLClient client, @NotNull Map addedConnection) throws Exception { List> connections = client.sendQuery(GQL_CONNECTIONS_GET, null); Assert.assertTrue(connections.contains(addedConnection)); - String connectionId = JSONUtils.getString(addedConnection, "id"); - Assert.assertNotNull(connectionId); - Assert.assertTrue(client.sendQuery(GQL_CONNECTIONS_DELETE, Map.of("id", connectionId))); + Assert.assertNotNull(JSONUtils.getString(addedConnection, "id")); + String nodePath = JSONUtils.getString(addedConnection, "nodePath"); + Assert.assertNotNull(nodePath); } }