-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-47172][CORE] Add support for AES-GCM for RPC encryption #46515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0bfe017
[SPARK-47172] Add support for AES-GCM for RPC encryption
sweisdb b9baf22
[SPARK-47172] Updating GcmTransportCipher to use buffered streaming
sweisdb 71cd10d
Adding missing import
sweisdb 5f8cd7f
[SPARK-47172] Addressing reviewer comments
sweisdb 81a3ca0
[SPARK-47172] Addressing reviewer comments
sweisdb 5bc503a
[SPARK-47172] Addressing reviewer comments
sweisdb 4ceeee3
[SPARK-47172] Moving ByteBufferWriteableChannel to its own class
sweisdb 4df0367
[SPARK-47172] Addressing comments & Refactoring AuthEngine test suite…
sweisdb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[SPARK-47172] Updating GcmTransportCipher to use buffered streaming
- Loading branch information
commit b9baf2277c4d17a37aa5ffe91ba36fa9fd58f834
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,9 +18,12 @@ | |||||
| package org.apache.spark.network.crypto; | ||||||
|
|
||||||
| import com.google.common.annotations.VisibleForTesting; | ||||||
| import com.google.crypto.tink.subtle.AesGcmJce; | ||||||
| import com.google.common.base.Preconditions; | ||||||
| import com.google.crypto.tink.subtle.AesGcmHkdfStreaming; | ||||||
| import com.google.crypto.tink.subtle.StreamSegmentEncrypter; | ||||||
| import io.netty.buffer.Unpooled; | ||||||
| import io.netty.channel.*; | ||||||
| import io.netty.util.ReferenceCounted; | ||||||
| import org.apache.spark.network.util.AbstractFileRegion; | ||||||
| import io.netty.buffer.ByteBuf; | ||||||
|
|
||||||
|
|
@@ -33,80 +36,284 @@ | |||||
|
|
||||||
| public class GcmTransportCipher implements TransportCipher { | ||||||
| private static final byte[] DEFAULT_AAD = new byte[0]; | ||||||
|
|
||||||
| private static final int LENGTH_HEADER_BYTES = 8; | ||||||
| @VisibleForTesting | ||||||
| static final int CIPHERTEXT_BUFFER_SIZE = 1024; | ||||||
| private final SecretKeySpec aesKey; | ||||||
|
|
||||||
| public GcmTransportCipher(SecretKeySpec aesKey) { | ||||||
| this.aesKey = aesKey; | ||||||
| } | ||||||
|
|
||||||
| @VisibleForTesting | ||||||
| EncryptionHandler getEncryptionHandler() { | ||||||
| EncryptionHandler getEncryptionHandler() throws GeneralSecurityException { | ||||||
| return new EncryptionHandler(); | ||||||
| } | ||||||
|
|
||||||
| @VisibleForTesting | ||||||
| DecryptionHandler getDecryptionHandler() { | ||||||
| DecryptionHandler getDecryptionHandler() throws GeneralSecurityException { | ||||||
| return new DecryptionHandler(); | ||||||
| } | ||||||
|
|
||||||
| public void addToChannel(Channel ch) { | ||||||
| public void addToChannel(Channel ch) throws GeneralSecurityException { | ||||||
| ch.pipeline() | ||||||
| .addFirst("GcmTransportEncryption", getEncryptionHandler()) | ||||||
| .addFirst("GcmTransportDecryption", getDecryptionHandler()); | ||||||
| } | ||||||
|
|
||||||
| @VisibleForTesting | ||||||
| class EncryptionHandler extends ChannelOutboundHandlerAdapter { | ||||||
| private final ByteBuffer plaintextBuffer; | ||||||
| private final ByteBuffer ciphertextBuffer; | ||||||
| private final AesGcmHkdfStreaming aesGcmHkdfStreaming; | ||||||
|
|
||||||
| EncryptionHandler() throws GeneralSecurityException { | ||||||
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| aesGcmHkdfStreaming = new AesGcmHkdfStreaming( | ||||||
| aesKey.getEncoded(), | ||||||
| "HmacSha256", | ||||||
| aesKey.getEncoded().length, | ||||||
| CIPHERTEXT_BUFFER_SIZE, | ||||||
| 0); | ||||||
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| plaintextBuffer = ByteBuffer.allocate(aesGcmHkdfStreaming.getPlaintextSegmentSize()); | ||||||
| ciphertextBuffer = ByteBuffer.allocate(aesGcmHkdfStreaming.getCiphertextSegmentSize()); | ||||||
| } | ||||||
|
|
||||||
| @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); | ||||||
| GcmEncryptedMessage encryptedMessage = new GcmEncryptedMessage( | ||||||
| aesGcmHkdfStreaming, | ||||||
| msg, | ||||||
| plaintextBuffer, | ||||||
| ciphertextBuffer); | ||||||
| ctx.write(encryptedMessage, promise); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| static class GcmEncryptedMessage extends AbstractFileRegion { | ||||||
| private final Object plaintextMessage; | ||||||
| private final ByteBuffer plaintextBuffer; | ||||||
| private final ByteBuffer ciphertextBuffer; | ||||||
| private final long bytesToRead; | ||||||
| private long bytesRead = 0; | ||||||
| private final StreamSegmentEncrypter encrypter; | ||||||
| private boolean headerWritten = false; | ||||||
| private long transferred = 0; | ||||||
| private final long encryptedCount; | ||||||
|
|
||||||
| GcmEncryptedMessage(AesGcmHkdfStreaming aesGcmHkdfStreaming, | ||||||
| Object plaintextMessage, | ||||||
| ByteBuffer plaintextBuffer, | ||||||
| ByteBuffer ciphertextBuffer) throws GeneralSecurityException { | ||||||
| Preconditions.checkArgument( | ||||||
| plaintextMessage instanceof ByteBuf || plaintextMessage instanceof FileRegion, | ||||||
| "Unrecognized message type: %s", plaintextMessage.getClass().getName()); | ||||||
| this.plaintextMessage = plaintextMessage; | ||||||
| this.bytesToRead = getReadableBytes(); | ||||||
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| this.plaintextBuffer = plaintextBuffer; | ||||||
| this.plaintextBuffer.clear(); | ||||||
| this.ciphertextBuffer = ciphertextBuffer; | ||||||
| this.ciphertextBuffer.clear(); | ||||||
| this.encrypter = aesGcmHkdfStreaming.newStreamSegmentEncrypter(DEFAULT_AAD); | ||||||
| this.encryptedCount = | ||||||
| LENGTH_HEADER_BYTES + aesGcmHkdfStreaming.expectedCiphertextSize(bytesToRead); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public long position() { | ||||||
| return 0; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public long transferred() { | ||||||
| return transferred; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public long count() { | ||||||
| return encryptedCount; | ||||||
| } | ||||||
sweisdb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| @Override | ||||||
| public long transferTo(WritableByteChannel target, long position) throws IOException { | ||||||
sweisdb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| Preconditions.checkArgument(position == transferred(), | ||||||
| "Invalid position."); | ||||||
| int transferredThisCall = 0; | ||||||
| // The format of the output is: | ||||||
| // [8 byte length][Internal IV and header][Ciphertext][Auth Tag] | ||||||
| if (!headerWritten) { | ||||||
| ByteBuffer expectedLength = ByteBuffer | ||||||
| .allocate(LENGTH_HEADER_BYTES) | ||||||
| .putLong(encryptedCount) | ||||||
| .flip(); | ||||||
| target.write(expectedLength); | ||||||
| int headerWritten = LENGTH_HEADER_BYTES + target.write(encrypter.getHeader()); | ||||||
| transferredThisCall += headerWritten; | ||||||
| this.transferred += headerWritten; | ||||||
| this.headerWritten = true; | ||||||
| } | ||||||
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| while (bytesRead < bytesToRead) { | ||||||
| long readableBytes = getReadableBytes(); | ||||||
mridulm marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| boolean lastSegment = readableBytes <= plaintextBuffer.capacity(); | ||||||
| plaintextBuffer.clear(); | ||||||
| int readLimit = | ||||||
| (int) Math.min(readableBytes, plaintextBuffer.capacity()); | ||||||
| plaintextBuffer.limit(readLimit); | ||||||
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| if (plaintextMessage instanceof ByteBuf byteBuf) { | ||||||
| byteBuf.readBytes(plaintextBuffer); | ||||||
sweisdb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| long inputBytesRead = readableBytes - byteBuf.readableBytes(); | ||||||
| bytesRead += inputBytesRead; | ||||||
| } else if (plaintextMessage instanceof AbstractFileRegion fileRegion) { | ||||||
|
||||||
| } else if (plaintextMessage instanceof AbstractFileRegion fileRegion) { | |
| } else if (plaintextMessage instanceof FileRegion fileRegion) { |
sweisdb marked this conversation as resolved.
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sweisdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.