From e1462d1cffe5257a1b3e477674e921f9e3015f0e Mon Sep 17 00:00:00 2001 From: denis-chudov Date: Tue, 25 Jun 2019 19:43:12 +0300 Subject: [PATCH] GG-14565 Can't connect to cluster by JDBC with cache read-only permissions. --- .../ignite/internal/jdbc/JdbcConnection.java | 19 +++++-- .../jdbc/JdbcConnectionValidationTask.java | 53 +++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 modules/core/src/main/java/org/apache/ignite/internal/jdbc/JdbcConnectionValidationTask.java diff --git a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/JdbcConnection.java b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/JdbcConnection.java index 012dc77400094..42e18b13a8133 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/JdbcConnection.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/JdbcConnection.java @@ -64,8 +64,12 @@ */ @Deprecated public class JdbcConnection implements Connection { - /** Validation task name. */ - private static final String VALID_TASK_NAME = + /** Connection validation task name. */ + private static final String CONNECTION_VALID_TASK_NAME = + "org.apache.ignite.internal.jdbc.JdbcConnectionValidationTask"; + + /** Cache validation task name. */ + private static final String CACHE_VALID_TASK_NAME = "org.apache.ignite.internal.processors.cache.query.jdbc.GridCacheQueryJdbcValidationTask"; /** Ignite client. */ @@ -86,6 +90,9 @@ public class JdbcConnection implements Connection { /** Timeout. */ private int timeout; + /** Whether using security. */ + private boolean usingSecurity; + /** * Creates new connection. * @@ -114,10 +121,14 @@ public JdbcConnection(String url, Properties props) throws SQLException { String passwd = props.getProperty("password"); if (!F.isEmpty(user)) { + usingSecurity = true; + SecurityCredentials creds = new SecurityCredentials(user, passwd); cfg.setSecurityCredentialsProvider(new SecurityCredentialsBasicProvider(creds)); } + else + usingSecurity = false; // Disable all fetching and caching for metadata. cfg.setEnableMetricsCache(false); @@ -452,7 +463,9 @@ public JdbcConnection(String url, Properties props) throws SQLException { throw new SQLException("Invalid timeout: " + timeout); try { - return client.compute().executeAsync(VALID_TASK_NAME, cacheName).get(timeout, SECONDS); + return client.compute() + .executeAsync(usingSecurity ? CONNECTION_VALID_TASK_NAME : CACHE_VALID_TASK_NAME, cacheName) + .get(timeout, SECONDS); } catch (GridClientDisconnectedException | GridClientFutureTimeoutException e) { throw new SQLException("Failed to establish connection.", e); diff --git a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/JdbcConnectionValidationTask.java b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/JdbcConnectionValidationTask.java new file mode 100644 index 0000000000000..f4a57bf304ad5 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/JdbcConnectionValidationTask.java @@ -0,0 +1,53 @@ +/* + * Copyright 2019 GridGain Systems, Inc. and Contributors. + * + * Licensed under the GridGain Community Edition License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license + * + * 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 org.apache.ignite.internal.jdbc; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import org.apache.ignite.IgniteException; +import org.apache.ignite.compute.ComputeJob; +import org.apache.ignite.compute.ComputeJobAdapter; +import org.apache.ignite.compute.ComputeJobResult; +import org.apache.ignite.compute.ComputeTaskSplitAdapter; +import org.apache.ignite.internal.util.typedef.F; +import org.jetbrains.annotations.Nullable; + +/** + * This task is used for JDBCConnection validation. + * + * @deprecated Using Ignite client node based JDBC driver is preferable. + * See documentation of {@link org.apache.ignite.IgniteJdbcDriver} for details. + */ +@Deprecated +public class JdbcConnectionValidationTask extends ComputeTaskSplitAdapter { + /** */ + private static final long serialVersionUID = 0L; + + /** {@inheritDoc} */ + @Override protected Collection split(int gridSize, Object arg) throws IgniteException { + return Collections.singletonList(new ComputeJobAdapter() { + @Override public Object execute() throws IgniteException { + return true; + } + }); + } + + /** {@inheritDoc} */ + @Nullable @Override public Boolean reduce(List results) throws IgniteException { + return F.first(results).getData(); + } +}