mridulm commented on code in PR #46515:
URL: https://github.com/apache/spark/pull/46515#discussion_r1598897504


##########
docs/security.md:
##########
@@ -207,6 +207,14 @@ The following table describes the different options 
available for configuring th
   </td>
   <td>2.2.0</td>
 </tr>
+<tr>
+  <td><code>spark.network.crypto.cipher</code></td>
+  <td>AES/CTR/NoPadding</td>
+  <td>
+    Cipher mode to use. Defaults "AES/CTR/NoPadding". Also supports 
"AES/GCM/NoPadding"

Review Comment:
   Can we add more details on this ? What value should users set this to, 
why/impact, backward compatibility implication, ...



##########
common/network-common/src/main/java/org/apache/spark/network/crypto/GcmTransportCipher.java:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.spark.network.crypto;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.crypto.tink.subtle.AesGcmJce;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.*;
+import org.apache.spark.network.util.AbstractFileRegion;
+import io.netty.buffer.ByteBuf;
+
+import javax.crypto.spec.SecretKeySpec;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.WritableByteChannel;
+import java.security.GeneralSecurityException;
+
+public class GcmTransportCipher implements TransportCipher {
+    private static final byte[] DEFAULT_AAD = new byte[0];
+
+    private final SecretKeySpec aesKey;
+    public GcmTransportCipher(SecretKeySpec aesKey)  {
+        this.aesKey = aesKey;
+    }
+
+    @VisibleForTesting
+    EncryptionHandler getEncryptionHandler() {
+        return new EncryptionHandler();
+    }
+
+    @VisibleForTesting
+    DecryptionHandler getDecryptionHandler() {
+        return new DecryptionHandler();
+    }
+
+    public void addToChannel(Channel ch) {
+        ch.pipeline()
+            .addFirst("GcmTransportEncryption", getEncryptionHandler())
+            .addFirst("GcmTransportDecryption", getDecryptionHandler());
+    }
+
+    @VisibleForTesting
+    class EncryptionHandler extends ChannelOutboundHandlerAdapter {
+        @Override
+        public void write(ChannelHandlerContext ctx, Object msg, 
ChannelPromise promise)
+                throws Exception {
+            ByteBuffer inputBuffer;
+            int bytesToRead;
+            if (msg instanceof ByteBuf byteBuf) {
+                bytesToRead = byteBuf.readableBytes();
+                // This is allocating a buffer that is the size of the input
+                inputBuffer = ByteBuffer.allocate(bytesToRead);
+                // This will block while copying
+                while (inputBuffer.position() < bytesToRead) {
+                    byteBuf.readBytes(inputBuffer);
+                }
+            } else if (msg instanceof AbstractFileRegion fileRegion) {
+                bytesToRead = (int) fileRegion.count();
+                // This is allocating a buffer that is the size of the input
+                inputBuffer = ByteBuffer.allocate(bytesToRead);
+                ByteBufferWriteableChannel writeableChannel =
+                        new ByteBufferWriteableChannel(inputBuffer);
+                long transferred = 0;
+                // This will block while copying
+                while (transferred < bytesToRead) {
+                    transferred +=
+                            fileRegion.transferTo(writeableChannel, 
fileRegion.transferred());
+                }

Review Comment:
   This can be quite large - and result in a nontrivial memory consumption.
   We will need to do something similar to 
`CtrTransportCipher.EncryptedMessage` and transfer in chunks.



##########
common/network-common/src/main/java/org/apache/spark/network/crypto/GcmTransportCipher.java:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.spark.network.crypto;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.crypto.tink.subtle.AesGcmJce;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.*;
+import org.apache.spark.network.util.AbstractFileRegion;
+import io.netty.buffer.ByteBuf;
+
+import javax.crypto.spec.SecretKeySpec;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.WritableByteChannel;
+import java.security.GeneralSecurityException;
+
+public class GcmTransportCipher implements TransportCipher {
+    private static final byte[] DEFAULT_AAD = new byte[0];
+
+    private final SecretKeySpec aesKey;
+    public GcmTransportCipher(SecretKeySpec aesKey)  {
+        this.aesKey = aesKey;
+    }
+
+    @VisibleForTesting
+    EncryptionHandler getEncryptionHandler() {
+        return new EncryptionHandler();
+    }
+
+    @VisibleForTesting
+    DecryptionHandler getDecryptionHandler() {
+        return new DecryptionHandler();
+    }
+
+    public void addToChannel(Channel ch) {
+        ch.pipeline()
+            .addFirst("GcmTransportEncryption", getEncryptionHandler())
+            .addFirst("GcmTransportDecryption", getDecryptionHandler());
+    }
+
+    @VisibleForTesting
+    class EncryptionHandler extends ChannelOutboundHandlerAdapter {
+        @Override
+        public void write(ChannelHandlerContext ctx, Object msg, 
ChannelPromise promise)
+                throws Exception {
+            ByteBuffer inputBuffer;
+            int bytesToRead;
+            if (msg instanceof ByteBuf byteBuf) {
+                bytesToRead = byteBuf.readableBytes();
+                // This is allocating a buffer that is the size of the input
+                inputBuffer = ByteBuffer.allocate(bytesToRead);
+                // This will block while copying
+                while (inputBuffer.position() < bytesToRead) {
+                    byteBuf.readBytes(inputBuffer);
+                }
+            } else if (msg instanceof AbstractFileRegion fileRegion) {
+                bytesToRead = (int) fileRegion.count();
+                // This is allocating a buffer that is the size of the input
+                inputBuffer = ByteBuffer.allocate(bytesToRead);
+                ByteBufferWriteableChannel writeableChannel =
+                        new ByteBufferWriteableChannel(inputBuffer);
+                long transferred = 0;
+                // This will block while copying
+                while (transferred < bytesToRead) {
+                    transferred +=
+                            fileRegion.transferTo(writeableChannel, 
fileRegion.transferred());
+                }
+            } else {
+                throw new IllegalArgumentException("Unsupported message type: 
" + msg.getClass());
+            }
+            AesGcmJce cipher = new AesGcmJce(aesKey.getEncoded());
+            byte[] encrypted = cipher.encrypt(inputBuffer.array(), 
DEFAULT_AAD);
+            ByteBuf wrappedEncrypted = Unpooled.wrappedBuffer(encrypted);
+            ctx.write(wrappedEncrypted, promise);
+        }
+    }
+
+    @VisibleForTesting
+    class DecryptionHandler extends ChannelInboundHandlerAdapter {
+        @Override
+        public void channelRead(ChannelHandlerContext ctx, Object msg)
+                throws GeneralSecurityException {
+            if (msg instanceof ByteBuf byteBuf) {
+                // This is allocating a buffer that is the size of the input
+                byte[] encrypted = new byte[byteBuf.readableBytes()];
+                byteBuf.readBytes(encrypted);
+                AesGcmJce cipher = new AesGcmJce(aesKey.getEncoded());
+                byte[] decrypted = cipher.decrypt(encrypted, DEFAULT_AAD);
+                ByteBuf wrappedDecrypted = Unpooled.wrappedBuffer(decrypted);
+                ctx.fireChannelRead(wrappedDecrypted);

Review Comment:
   Wrap it in a try/finally and release `byteBuf` ?



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to