ptupitsyn commented on code in PR #1639:
URL: https://github.com/apache/ignite-3/pull/1639#discussion_r1099886497


##########
modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientHandlerModule.java:
##########
@@ -198,17 +201,26 @@ protected void initChannel(Channel ch) {
                             ch.pipeline().addLast(new IdleChannelHandler());
                         }
 
-                        ch.pipeline().addLast(
-                                new ClientMessageDecoder(),
-                                new ClientInboundMessageHandler(
-                                        igniteTables,
-                                        igniteTransactions,
-                                        queryProcessor,
-                                        configuration,
-                                        igniteCompute,
-                                        clusterService,
-                                        sql,
-                                        clusterId));
+                        if (configuration.ssl().enabled()) {
+                            SslContext sslContext =  
SslContextProvider.forServer(
+                                    configuration.ssl().keyStore(), 
ClientAuth.valueOf(configuration.ssl().clientAuth().toUpperCase())

Review Comment:
   What if `clientAuth` has ivalid string value?



##########
modules/client-handler/src/main/java/org/apache/ignite/client/handler/configuration/ClientConnectorSslConfigurationSchema.java:
##########
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.client.handler.configuration;
+
+import org.apache.ignite.configuration.annotation.Config;
+import org.apache.ignite.configuration.annotation.ConfigValue;
+import org.apache.ignite.configuration.annotation.Value;
+import org.apache.ignite.configuration.validation.OneOf;
+import 
org.apache.ignite.internal.network.configuration.KeyStoreConfigurationSchema;
+
+/** Client connector ssl configuration schema. */
+@Config
+public class ClientConnectorSslConfigurationSchema {
+    /** If set to true then ssl will be used for client operations. */
+    @Value(hasDefault = true)
+    public final boolean enabled = false;
+
+    /** Client authentication. */
+    @OneOf({"none", "optional", "required"})
+    @Value(hasDefault = true)
+    public final String clientAuth = "none";
+
+    /** Keystore configuration. */
+    @ConfigValue
+    public KeyStoreConfigurationSchema keyStore;

Review Comment:
   Let's add `trustStore` as well, it is required to authenticate clients.



##########
modules/client-handler/src/integrationTest/java/org/apache/ignite/client/handler/ItSslClientHandlerTest.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.client.handler;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.net.SocketException;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.msgpack.core.MessagePack;
+
+/** Ssl client integration test. */
+public class ItSslClientHandlerTest {
+
+    /** Magic bytes. */
+    private static final byte[] MAGIC = {0x49, 0x47, 0x4E, 0x49};
+
+    ClientHandlerModule serverModule;
+
+    TestServer testServer;
+
+    int serverPort;
+
+    String password;
+
+    String keyStorePkcs12Path;
+
+    @BeforeEach
+    void setUp() {
+        password = "changeit";
+        keyStorePkcs12Path = 
ItSslClientHandlerTest.class.getClassLoader().getResource("ssl/keystore.pfx").getPath();
+    }
+
+    @Test
+    @DisplayName("When ssl not configured (by default) the client can connect")
+    void sslNotConfigured(TestInfo testInfo) throws IOException {
+        // Given server started
+        testServer = new TestServer();
+        serverModule = testServer.start(testInfo);

Review Comment:
   Let's stop the server in `@AfterEach`



##########
modules/client/src/main/java/org/apache/ignite/client/IgniteClient.java:
##########
@@ -97,6 +97,9 @@ class Builder {
         /** Logger factory. */
         private @Nullable LoggerFactory loggerFactory;
 
+        /** Ssl configuration. */
+        private SslConfiguration sslConfiguration = 
SslConfiguration.disabled();

Review Comment:
   ```suggestion
           private @Nullable SslConfiguration sslConfiguration = 
SslConfiguration.disabled();
   ```



##########
modules/client/src/main/java/org/apache/ignite/client/IgniteClientConfiguration.java:
##########
@@ -141,4 +141,14 @@ public interface IgniteClientConfiguration {
      * @return Configured logger factory.
      */
     @Nullable LoggerFactory loggerFactory();
+
+    /**
+     * Returns the client ssl configuration. This configuration will be used 
to setup the ssl connection with
+     * the Ignite 3 nodes.
+     *
+     * <p>When {@code null} then no ssl is used.
+     *
+     * @return Client ssl configuration.
+     */
+    SslConfiguration sslConfiguration();

Review Comment:
   ```suggestion
       @Nullable SslConfiguration sslConfiguration();
   ```



##########
modules/client/src/main/java/org/apache/ignite/internal/client/IgniteClientConfigurationImpl.java:
##########
@@ -157,4 +162,9 @@ public long heartbeatTimeout() {
     public @Nullable RetryPolicy retryPolicy() {
         return retryPolicy;
     }
+
+    @Override

Review Comment:
   Missing inheritdoc, here and below.



##########
modules/client/src/main/java/org/apache/ignite/client/IgniteClient.java:
##########
@@ -97,6 +97,9 @@ class Builder {
         /** Logger factory. */
         private @Nullable LoggerFactory loggerFactory;
 
+        /** Ssl configuration. */

Review Comment:
   Here and below, SSL should be in upper case.



##########
modules/client/src/main/java/org/apache/ignite/internal/client/SslConfigurationImpl.java:
##########
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.internal.client;
+
+import org.apache.ignite.client.SslConfiguration;
+
+/** Ssl configuration. */
+public class SslConfigurationImpl implements SslConfiguration {
+    private final boolean enabled;
+
+    private final String trustStoreType;
+
+    private final String trustStorePath;
+
+    private final String trustStorePassword;
+
+    /** Main constructor. */
+    public SslConfigurationImpl(boolean enabled, String trustStoreType, String 
trustStorePath, String trustStorePassword) {

Review Comment:
   Please add `@Nullable` annotations where applicable.



##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/ssl/ItSslTest.java:
##########
@@ -0,0 +1,218 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.internal.ssl;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.nio.file.Path;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import org.apache.ignite.client.IgniteClient;
+import org.apache.ignite.client.IgniteClientConnectionException;
+import org.apache.ignite.client.SslConfiguration;
+import org.apache.ignite.internal.Cluster;
+import org.apache.ignite.internal.testframework.WorkDirectory;
+import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
+import org.intellij.lang.annotations.Language;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.TestInstance.Lifecycle;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/** Ssl support integration test. */
+@ExtendWith(WorkDirectoryExtension.class)
+public class ItSslTest {

Review Comment:
   Please add tests with and without `clientAuth`.



##########
modules/network/src/main/java/org/apache/ignite/internal/network/netty/NettyClient.java:
##########
@@ -48,44 +51,50 @@ public class NettyClient {
     /** Destination address. */
     private final SocketAddress address;
 
-    /** Future that resolves when the client finished the handshake. */
-    @Nullable
-    private volatile OrderingFuture<NettySender> senderFuture = null;
-
     /** Future that resolves when the client channel is opened. */
     private final CompletableFuture<Void> channelFuture = new 
CompletableFuture<>();
 
-    /** Client channel. */
-    @Nullable
-    private volatile Channel channel = null;
-
     /** Message listener. */
     private final Consumer<InNetworkObject> messageListener;
 
     /** Handshake manager. */
     private final HandshakeManager handshakeManager;
 
+    /** Ssl configuration. */
+    private final SslView configuration;

Review Comment:
   ```suggestion
       private final SslView sslConfiguration;
   ```



##########
modules/client/src/main/java/org/apache/ignite/client/IgniteClient.java:
##########
@@ -260,6 +263,18 @@ public Builder heartbeatTimeout(long heartbeatTimeout) {
             return this;
         }
 
+        /**
+         * Sets the ssl configuration.
+         *
+         * @param sslConfiguration Ssl configuration.
+         * @return This instance.
+         */
+        public Builder sslConfiguration(SslConfiguration sslConfiguration) {

Review Comment:
   ```suggestion
           public Builder sslConfiguration(@Nullable SslConfiguration 
sslConfiguration) {
   ```



##########
modules/client/src/main/java/org/apache/ignite/internal/client/io/netty/NettyClientConnectionMultiplexer.java:
##########
@@ -75,6 +86,36 @@ public void initChannel(SocketChannel ch) {
         }
     }
 
+    private static void setupSsl(SocketChannel ch, IgniteClientConfiguration 
clientCfg) {
+        if (clientCfg.sslConfiguration().enabled()) {
+            try {
+                String type = clientCfg.sslConfiguration().trustStoreType() == 
null
+                        ? "PKCS12"
+                        : clientCfg.sslConfiguration().trustStoreType();
+
+                KeyStore store = KeyStore.getInstance(type);
+                store.load(
+                        
Files.newInputStream(Path.of(clientCfg.sslConfiguration().trustStorePath())),
+                        clientCfg.sslConfiguration().trustStorePassword() == 
null
+                                ? null
+                                : 
clientCfg.sslConfiguration().trustStorePassword().toCharArray()
+                );
+
+                TrustManagerFactory trustManagerFactory = 
TrustManagerFactory.getInstance(
+                        TrustManagerFactory.getDefaultAlgorithm()
+                );
+                trustManagerFactory.init(store);
+
+                var context = SslContextBuilder.forClient()
+                        .trustManager(trustManagerFactory)
+                        .build();
+                ch.pipeline().addFirst("ssl", context.newHandler(ch.alloc()));
+            } catch (NoSuchAlgorithmException | KeyStoreException | 
CertificateException | IOException e) {
+                throw new 
IgniteClientConnectionException(CLIENT_SSL_CONFIGURATION_ERR, "Client ssl 
configuration error", e);

Review Comment:
   ```suggestion
                   throw new 
IgniteClientConnectionException(CLIENT_SSL_CONFIGURATION_ERR, "Client SSL 
configuration error: " + e.getMessage(), e);
   ```



##########
modules/jdbc/src/main/java/org/apache/ignite/internal/jdbc/ConnectionProperties.java:
##########
@@ -126,4 +126,60 @@ public interface ConnectionProperties {
      * @throws SQLException On error.
      */
     void setConnectionTimeout(Integer connTimeout) throws SQLException;
+
+    /**
+     * Set trust store path that will be used to setup ssl connection.
+     *
+     * @param trustStorePath Trust store path.
+     */
+    void setTrustStorePath(String trustStorePath);

Review Comment:
   Same as above - `keyStore` and `clientAuth` are missing.



##########
modules/client/src/main/java/org/apache/ignite/client/SslConfiguration.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.ignite.client;
+
+import org.apache.ignite.internal.client.SslConfigurationImpl;
+import org.jetbrains.annotations.Nullable;
+
+/** Client ssl configuration. */
+public interface SslConfiguration {
+
+    /** If set to {@code true} then the ssl connection will be used to 
interact with Ignite 3 node. */
+    boolean enabled();
+
+    /** Truststore path that will be used to setup the ssl connection. */
+    @Nullable String trustStorePath();

Review Comment:
   `keyStore` is missing - it is required for client auth.



##########
modules/client/src/main/java/org/apache/ignite/internal/client/IgniteClientConfigurationImpl.java:
##########
@@ -81,7 +84,8 @@ public IgniteClientConfigurationImpl(
             long heartbeatInterval,
             long heartbeatTimeout,
             @Nullable RetryPolicy retryPolicy,
-            @Nullable LoggerFactory loggerFactory
+            @Nullable LoggerFactory loggerFactory,
+            SslConfiguration sslConfiguration

Review Comment:
   ```suggestion
               @Nullable SslConfiguration sslConfiguration
   ```



##########
modules/client/src/main/java/org/apache/ignite/internal/client/io/netty/NettyClientConnectionMultiplexer.java:
##########
@@ -75,6 +86,36 @@ public void initChannel(SocketChannel ch) {
         }
     }
 
+    private static void setupSsl(SocketChannel ch, IgniteClientConfiguration 
clientCfg) {
+        if (clientCfg.sslConfiguration().enabled()) {

Review Comment:
   We could invert condition to reduce nesting: `if !enabled return`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to