Skip to content

Commit 889cbbf

Browse files
author
Sreesh Maheshwar
committed
Rebase properly
1 parent 00a6ffc commit 889cbbf

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

core/src/main/java/org/apache/iceberg/encryption/EncryptionUtil.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static KeyManagementClient createKmsClient(Map<String, String> catalogPro
7575
return kmsClient;
7676
}
7777

78-
static EncryptionManager createEncryptionManager(
78+
public static EncryptionManager createEncryptionManager(
7979
List<EncryptedKey> keys, Map<String, String> tableProperties, KeyManagementClient kmsClient) {
8080
Preconditions.checkArgument(kmsClient != null, "Invalid KMS client: null");
8181
String tableKeyId = tableProperties.get(TableProperties.ENCRYPTION_TABLE_KEY);
@@ -96,7 +96,7 @@ static EncryptionManager createEncryptionManager(
9696
"Invalid data key length: %s (must be 16, 24, or 32)",
9797
dataKeyLength);
9898

99-
return new StandardEncryptionManager(tableKeyId, dataKeyLength, kmsClient);
99+
return new StandardEncryptionManager(keys, tableKeyId, dataKeyLength, kmsClient);
100100
}
101101

102102
public static EncryptedOutputFile plainAsEncryptedOutput(OutputFile encryptingOutputFile) {
@@ -145,10 +145,10 @@ static ByteBuffer encryptManifestListKeyMetadata(
145145
return ByteBuffer.wrap(encryptedKeyMetadata);
146146
}
147147

148-
public static Map<String, EncryptedKey> encryptionKeys(EncryptionManager em) {
149-
Preconditions.checkState(
150-
em instanceof StandardEncryptionManager,
151-
"Encryption keys are only available for StandardEncryptionManager");
152-
return ((StandardEncryptionManager) em).encryptionKeys();
153-
}
148+
public static Map<String, EncryptedKey> encryptionKeys(EncryptionManager em) {
149+
Preconditions.checkState(
150+
em instanceof StandardEncryptionManager,
151+
"Encryption keys are only available for StandardEncryptionManager");
152+
return ((StandardEncryptionManager) em).encryptionKeys();
153+
}
154154
}

core/src/main/java/org/apache/iceberg/encryption/StandardEncryptionManager.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.nio.ByteBuffer;
2424
import java.security.SecureRandom;
2525
import java.util.Base64;
26+
import java.util.List;
2627
import java.util.Map;
2728
import java.util.concurrent.TimeUnit;
2829
import org.apache.iceberg.TableProperties;
@@ -46,9 +47,16 @@ private class TransientEncryptionState {
4647
private final Map<String, EncryptedKey> encryptionKeys;
4748
private final LoadingCache<String, ByteBuffer> unwrappedKeyCache;
4849

49-
private TransientEncryptionState(KeyManagementClient kmsClient) {
50+
private TransientEncryptionState(List<EncryptedKey> keys, KeyManagementClient kmsClient) {
5051
this.kmsClient = kmsClient;
5152
this.encryptionKeys = Maps.newLinkedHashMap();
53+
54+
for (EncryptedKey key : keys) {
55+
Preconditions.checkArgument(
56+
key.keyId() != null, "Key id cannot be null"); // Required by spec.
57+
encryptionKeys.put(key.keyId(), key);
58+
}
59+
5260
this.unwrappedKeyCache =
5361
Caffeine.newBuilder()
5462
.expireAfterWrite(1, TimeUnit.HOURS)
@@ -70,14 +78,28 @@ private TransientEncryptionState(KeyManagementClient kmsClient) {
7078
*/
7179
public StandardEncryptionManager(
7280
String tableKeyId, int dataKeyLength, KeyManagementClient kmsClient) {
81+
this(List.of(), tableKeyId, dataKeyLength, kmsClient);
82+
}
83+
84+
/**
85+
* @param keys a list of existing {@link EncryptedKey}s for this {@link EncryptionManager} to use
86+
* @param tableKeyId table encryption key id
87+
* @param dataKeyLength length of data encryption key (16/24/32 bytes)
88+
* @param kmsClient Client of KMS used to wrap/unwrap keys in envelope encryption
89+
*/
90+
public StandardEncryptionManager(
91+
List<EncryptedKey> keys,
92+
String tableKeyId,
93+
int dataKeyLength,
94+
KeyManagementClient kmsClient) {
7395
Preconditions.checkNotNull(tableKeyId, "Invalid encryption key ID: null");
7496
Preconditions.checkArgument(
7597
dataKeyLength == 16 || dataKeyLength == 24 || dataKeyLength == 32,
7698
"Invalid data key length: %s (must be 16, 24, or 32)",
7799
dataKeyLength);
78100
Preconditions.checkNotNull(kmsClient, "Invalid KMS client: null");
79101
this.tableKeyId = tableKeyId;
80-
this.transientState = new TransientEncryptionState(kmsClient);
102+
this.transientState = new TransientEncryptionState(keys, kmsClient);
81103
this.dataKeyLength = dataKeyLength;
82104
}
83105

@@ -199,6 +221,14 @@ public String addManifestListKeyMetadata(NativeEncryptionKeyMetadata keyMetadata
199221
return manifestListKeyID;
200222
}
201223

224+
public Map<String, EncryptedKey> encryptionKeys() {
225+
if (transientState == null) {
226+
throw new IllegalStateException("Cannot return encryption keys after serialization");
227+
}
228+
229+
return transientState.encryptionKeys;
230+
}
231+
202232
private String generateKeyId() {
203233
byte[] idBytes = new byte[16];
204234
workerRNG().nextBytes(idBytes);

core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import org.apache.iceberg.catalog.TableIdentifier;
5050
import org.apache.iceberg.encryption.EncryptionUtil;
5151
import org.apache.iceberg.encryption.KeyManagementClient;
52-
import org.apache.iceberg.encryption.EncryptionUtil;
5352
import org.apache.iceberg.exceptions.AlreadyExistsException;
5453
import org.apache.iceberg.exceptions.NoSuchNamespaceException;
5554
import org.apache.iceberg.exceptions.NoSuchTableException;

core/src/main/java/org/apache/iceberg/rest/RESTTableOperations.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void commit(TableMetadata base, TableMetadata metadata) {
152152
builder.addEncryptionKey(entry.getValue());
153153
}
154154
metadataToCommit = builder.build();
155-
// TODO(smaheshwar): Think about requirements.
155+
// TODO(smaheshwar-pltr): Think about requirements
156156
}
157157

158158
switch (updateType) {
@@ -246,7 +246,7 @@ public EncryptionManager encryption() {
246246
}
247247

248248
private void encryptionPropsFromMetadata(TableMetadata metadata) {
249-
// TODO(smaheshwar): Check generally for changed encryption-related properties!
249+
// TODO(smaheshwar-pltr): Check generally for changed encryption-related properties
250250
if (metadata == null || metadata.properties() == null) {
251251
return;
252252
}

spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/sql/TestRestTableEncryption.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@
5151
import org.junit.jupiter.api.Disabled;
5252
import org.junit.jupiter.api.TestTemplate;
5353

54-
// TODO(smaheshwar): This test is taken from https://github.com/apache/iceberg/pull/13066, with the
55-
// exception of testCtas, but adapted for the REST catalog. When that merges, we can directly use
56-
// those tests for the REST catalog as well by adding to the parameters method there, to have a
57-
// single test class for table encryption.
54+
// TODO(smaheshwar-pltr): This test is taken from https://github.com/apache/iceberg/pull/13066, with
55+
// the exception of testCtas, but adapted for the REST catalog. When that merges, we can directly
56+
// use those tests for the REST catalog as well by adding to the parameters method there, to have
57+
// a single test class for table encryption.
5858
public class TestRestTableEncryption extends CatalogTestBase {
5959
private static Map<String, String> appendCatalogEncryptionProperties(Map<String, String> props) {
6060
Map<String, String> newProps = Maps.newHashMap();

0 commit comments

Comments
 (0)