-
Notifications
You must be signed in to change notification settings - Fork 447
[CELEBORN-2329][CIP22] Encryption at Rest Spark Impl #3689
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
Open
akpatnam25
wants to merge
10
commits into
apache:main
Choose a base branch
from
akpatnam25:ear-spark-impl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fa46869
[CELEBORN-2329][CIP22] Encryption at Rest Spark Impl
akpatnam25 f48960d
fix errors
akpatnam25 0cf8e57
trigger build
akpatnam25 af796b5
Merge branch 'apache:main' into ear-spark-impl
akpatnam25 4e37cad
[CELEBORN-2329] Address PR review comments for EAR Spark impl
akpatnam25 f48ae50
[CELEBORN-2329] Add backward-compatible overload for createColumnarSh…
akpatnam25 9fb4031
Add getCryptoHandler caching and EAR round-trip integration test
akpatnam25 9321d70
spotless
faadb25
[CELEBORN-2329] Address follow-up EAR review comments
akpatnam25 0323036
[CELEBORN-2329] Address Copilot follow-up review comments
akpatnam25 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
Some comments aren't visible on the classic Files Changed page.
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
71 changes: 71 additions & 0 deletions
71
client-spark/common/src/main/java/org/apache/spark/shuffle/celeborn/SparkCryptoHandler.java
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 |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * 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.shuffle.celeborn; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.DataInputStream; | ||
| import java.io.DataOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
|
|
||
| import org.apache.spark.SparkConf; | ||
| import org.apache.spark.security.CryptoStreamUtils; | ||
|
|
||
| import org.apache.celeborn.client.security.CryptoHandler; | ||
|
|
||
| public class SparkCryptoHandler implements CryptoHandler { | ||
| private final SparkConf sparkConf; | ||
| private final byte[] key; | ||
|
|
||
| public SparkCryptoHandler(SparkConf sparkConf, byte[] key) { | ||
| this.sparkConf = sparkConf; | ||
| this.key = key; | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] encrypt(byte[] input, int offset, int length) throws IOException { | ||
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
| DataOutputStream dos = new DataOutputStream(baos); | ||
| dos.writeInt(length); | ||
| try (OutputStream cos = CryptoStreamUtils.createCryptoOutputStream(dos, sparkConf, key)) { | ||
| cos.write(input, offset, length); | ||
| } | ||
| return baos.toByteArray(); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] decrypt(byte[] input, int offset, int length) throws IOException { | ||
| ByteArrayInputStream bais = new ByteArrayInputStream(input, offset, length); | ||
| DataInputStream dis = new DataInputStream(bais); | ||
| int decryptedLength = dis.readInt(); | ||
| // The encrypted payload format is: [4-byte plaintext length][ciphertext...]. | ||
| // So the maximum valid decrypted length is length - 4 (the ciphertext portion). | ||
| // A value outside this range indicates corruption or a wrong key. | ||
| if (decryptedLength < 0 || decryptedLength > length - 4) { | ||
|
akpatnam25 marked this conversation as resolved.
|
||
| throw new IOException( | ||
| "Invalid decrypted length: " + decryptedLength + ", encrypted length: " + length); | ||
| } | ||
| try (DataInputStream cis = | ||
| new DataInputStream(CryptoStreamUtils.createCryptoInputStream(dis, sparkConf, key))) { | ||
| byte[] decrypted = new byte[decryptedLength]; | ||
| cis.readFully(decrypted); | ||
| return decrypted; | ||
| } | ||
| } | ||
| } | ||
138 changes: 138 additions & 0 deletions
138
...park/common/src/test/java/org/apache/spark/shuffle/celeborn/SparkCryptoHandlerSuiteJ.java
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 |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * 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.shuffle.celeborn; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| import java.io.IOException; | ||
| import java.security.SecureRandom; | ||
| import java.util.Arrays; | ||
|
|
||
| import org.apache.spark.SparkConf; | ||
| import org.apache.spark.internal.config.package$; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import org.apache.celeborn.client.security.CryptoHandler; | ||
|
|
||
| public class SparkCryptoHandlerSuiteJ { | ||
|
|
||
| private byte[] key; | ||
| private CryptoHandler handler; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| key = new byte[16]; | ||
| new SecureRandom().nextBytes(key); | ||
| SparkConf sparkConf = new SparkConf(false); | ||
| sparkConf.set(package$.MODULE$.IO_ENCRYPTION_ENABLED(), true); | ||
| handler = new SparkCryptoHandler(sparkConf, key); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRoundTrip() throws IOException { | ||
| byte[] plaintext = "hello world, this is a test of encryption".getBytes(); | ||
|
|
||
| byte[] encrypted = handler.encrypt(plaintext, 0, plaintext.length); | ||
| assertFalse( | ||
| "Encrypted output should differ from plaintext", Arrays.equals(plaintext, encrypted)); | ||
|
|
||
| byte[] decrypted = handler.decrypt(encrypted, 0, encrypted.length); | ||
| assertArrayEquals(plaintext, decrypted); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEncryptedDiffersFromPlaintext() throws IOException { | ||
| byte[] plaintext = "deterministic test data for comparison".getBytes(); | ||
|
|
||
| byte[] encrypted = handler.encrypt(plaintext, 0, plaintext.length); | ||
| assertFalse( | ||
| "Encrypted output should differ from plaintext", Arrays.equals(plaintext, encrypted)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSameDataEncryptsThenDecrypts() throws IOException { | ||
| byte[] plaintext = "same data encrypted twice".getBytes(); | ||
|
|
||
| byte[] encrypted1 = handler.encrypt(plaintext, 0, plaintext.length); | ||
| byte[] encrypted2 = handler.encrypt(plaintext, 0, plaintext.length); | ||
|
|
||
| // Both should decrypt to the same plaintext | ||
| byte[] decrypted1 = handler.decrypt(encrypted1, 0, encrypted1.length); | ||
| byte[] decrypted2 = handler.decrypt(encrypted2, 0, encrypted2.length); | ||
|
|
||
| assertArrayEquals(plaintext, decrypted1); | ||
| assertArrayEquals(plaintext, decrypted2); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEncryptWithOffset() throws IOException { | ||
| byte[] actual = "offset test data".getBytes(); | ||
| int offset = 10; | ||
| byte[] padded = new byte[offset + actual.length + 20]; | ||
| System.arraycopy(actual, 0, padded, offset, actual.length); | ||
|
|
||
| byte[] encrypted = handler.encrypt(padded, offset, actual.length); | ||
| byte[] decrypted = handler.decrypt(encrypted, 0, encrypted.length); | ||
|
|
||
| assertArrayEquals(actual, decrypted); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDecryptWithWrongKeyFails() throws IOException { | ||
| byte[] plaintext = "secret data".getBytes(); | ||
| byte[] encrypted = handler.encrypt(plaintext, 0, plaintext.length); | ||
|
|
||
| byte[] wrongKey = new byte[16]; | ||
| new SecureRandom().nextBytes(wrongKey); | ||
| SparkConf sparkConf = new SparkConf(false); | ||
| sparkConf.set(package$.MODULE$.IO_ENCRYPTION_ENABLED(), true); | ||
| CryptoHandler wrongHandler = new SparkCryptoHandler(sparkConf, wrongKey); | ||
|
|
||
| byte[] decrypted = null; | ||
| try { | ||
| decrypted = wrongHandler.decrypt(encrypted, 0, encrypted.length); | ||
| } catch (IOException e) { | ||
| // acceptable — some implementations throw on wrong key | ||
| return; | ||
| } | ||
| // CryptoStreamUtils may return garbage instead of throwing | ||
| assertFalse( | ||
| "Decryption with wrong key should not produce original plaintext", | ||
| Arrays.equals(plaintext, decrypted)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testLargeData() throws IOException { | ||
| byte[] plaintext = new byte[64 * 1024]; // 64KB | ||
| new SecureRandom().nextBytes(plaintext); | ||
|
|
||
| byte[] encrypted = handler.encrypt(plaintext, 0, plaintext.length); | ||
| byte[] decrypted = handler.decrypt(encrypted, 0, encrypted.length); | ||
|
|
||
| assertArrayEquals(plaintext, decrypted); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyData() throws IOException { | ||
| byte[] encrypted = handler.encrypt(new byte[0], 0, 0); | ||
|
|
||
| byte[] decrypted = handler.decrypt(encrypted, 0, encrypted.length); | ||
| assertEquals(0, decrypted.length); | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.