-
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
base: main
Are you sure you want to change the base?
Changes from all commits
fa46869
f48960d
0cf8e57
af796b5
4e37cad
f48ae50
9fb4031
9321d70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bound |
||
| 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; | ||
| } | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AES/CTR (Spark's default IO-encryption transform) gives confidentiality but no authentication, and
celeborn.client.shuffle.integrityCheck.enableddefaults tofalse. So a wrong key or a single bit-flip in ciphertext decrypts to garbage of the correct length with no exception, and by default nothing downstream detects it.testDecryptWithWrongKeyFailsdocuments this ("CryptoStreamUtils may return garbage instead of throwing"). At minimum, a doc note that corruption/wrong-key detection requires enabling the shuffle integrity check would help.Two smaller points on this class:
encrypt/decryptallocate a freshByteArrayOutputStream/ByteArrayInputStream+ crypto stream + a newbyte[]per batch on the push/read hot path; consider reusing Spark'sSerializerManager.wrapStreamand/or a reusable buffer.