-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Standard key manager #6884
Standard key manager #6884
Changes from 11 commits
ef53c2d
20fa8cd
94ab0cc
c2907f8
ee70332
4c6fb3a
a9e4271
aa3e562
7e38396
87ba3ec
925c73a
726e568
06fbac6
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,31 @@ | ||
/* | ||
* 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.iceberg.encryption; | ||
|
||
public class EncryptionProperties { | ||
|
||
private EncryptionProperties() {} | ||
|
||
public static final String ENCRYPTION_TABLE_KEY = "encryption.key-id"; | ||
|
||
public static final String ENCRYPTION_DEK_LENGTH = "encryption.data-key-length"; | ||
public static final int ENCRYPTION_DEK_LENGTH_DEFAULT = 16; | ||
|
||
public static final int ENCRYPTION_AAD_LENGTH_DEFAULT = 16; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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.iceberg.encryption; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.security.SecureRandom; | ||
import org.apache.iceberg.io.InputFile; | ||
import org.apache.iceberg.io.OutputFile; | ||
import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
import org.apache.iceberg.relocated.com.google.common.collect.Iterables; | ||
import org.apache.iceberg.util.ByteBuffers; | ||
|
||
public class StandardEncryptionManager implements EncryptionManager { | ||
private final transient KeyManagementClient kmsClient; | ||
private final String tableKeyId; | ||
private final int dataKeyLength; | ||
|
||
private transient volatile SecureRandom lazyRNG = null; | ||
|
||
/** | ||
* @param tableKeyId table encryption key id | ||
* @param dataKeyLength length of data encryption key (16/24/32 bytes) | ||
* @param kmsClient Client of KMS used to wrap/unwrap keys in envelope encryption | ||
*/ | ||
public StandardEncryptionManager( | ||
String tableKeyId, int dataKeyLength, KeyManagementClient kmsClient) { | ||
Preconditions.checkNotNull(tableKeyId, "Invalid encryption key ID: null"); | ||
Preconditions.checkNotNull(kmsClient, "Invalid KMS client: null"); | ||
this.tableKeyId = tableKeyId; | ||
this.kmsClient = kmsClient; | ||
this.dataKeyLength = dataKeyLength; | ||
} | ||
|
||
@Override | ||
public EncryptedOutputFile encrypt(OutputFile rawOutput) { | ||
ByteBuffer fileDek = ByteBuffer.allocate(dataKeyLength); | ||
workerRNG().nextBytes(fileDek.array()); | ||
|
||
ByteBuffer aadPrefix = ByteBuffer.allocate(EncryptionProperties.ENCRYPTION_AAD_LENGTH_DEFAULT); | ||
workerRNG().nextBytes(aadPrefix.array()); | ||
|
||
KeyMetadata encryptionMetadata = new KeyMetadata(fileDek, aadPrefix); | ||
|
||
return EncryptedFiles.encryptedOutput( | ||
new AesGcmOutputFile(rawOutput, fileDek.array(), aadPrefix.array()), | ||
encryptionMetadata, | ||
rawOutput); | ||
} | ||
|
||
@Override | ||
public InputFile decrypt(EncryptedInputFile encrypted) { | ||
KeyMetadata keyMetadata = KeyMetadata.castOrParse(encrypted.keyMetadata()); | ||
|
||
byte[] fileDek = ByteBuffers.toByteArray(keyMetadata.encryptionKey()); | ||
byte[] aadPrefix = ByteBuffers.toByteArray(keyMetadata.aadPrefix()); | ||
|
||
return new AesGcmInputFile(encrypted.encryptedInputFile(), fileDek, aadPrefix); | ||
} | ||
|
||
@Override | ||
public Iterable<InputFile> decrypt(Iterable<EncryptedInputFile> encrypted) { | ||
// Bulk decrypt is only applied to data files. Returning source input files for parquet. | ||
return Iterables.transform(encrypted, this::getSourceFile); | ||
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. This is incorrect. Based on the comment, I think the intent was to skip creating an Instead, I think this should call |
||
} | ||
|
||
private InputFile getSourceFile(EncryptedInputFile encryptedFile) { | ||
return encryptedFile.encryptedInputFile(); | ||
} | ||
|
||
private SecureRandom workerRNG() { | ||
if (this.lazyRNG == null) { | ||
this.lazyRNG = new SecureRandom(); | ||
} | ||
|
||
return lazyRNG; | ||
} | ||
|
||
public ByteBuffer wrapKey(ByteBuffer secretKey) { | ||
if (kmsClient == null) { | ||
throw new IllegalStateException("Null KmsClient. WrapKey can't be called from workers"); | ||
} | ||
|
||
return kmsClient.wrapKey(secretKey, tableKeyId); | ||
rdblue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public ByteBuffer unwrapKey(ByteBuffer wrappedSecretKey) { | ||
if (kmsClient == null) { | ||
throw new IllegalStateException("Null KmsClient. UnwrapKey can't be called from workers"); | ||
} | ||
|
||
return kmsClient.unwrapKey(wrappedSecretKey, tableKeyId); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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.iceberg.encryption; | ||
|
||
import static org.apache.iceberg.TableProperties.DEFAULT_FILE_FORMAT; | ||
import static org.apache.iceberg.TableProperties.DEFAULT_FILE_FORMAT_DEFAULT; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import org.apache.iceberg.CatalogProperties; | ||
import org.apache.iceberg.FileFormat; | ||
import org.apache.iceberg.util.PropertyUtil; | ||
|
||
public class StandardEncryptionManagerFactory implements Closeable { | ||
private final KeyManagementClient kmsClient; | ||
|
||
public StandardEncryptionManagerFactory(Map<String, String> catalogProperties) { | ||
String kmsType = catalogProperties.get(CatalogProperties.ENCRYPTION_KMS_TYPE); | ||
|
||
if (kmsType == null) { | ||
throw new IllegalStateException("Cannot create StandardEncryptionManagerFactory without KMS type"); | ||
} | ||
|
||
if (kmsType.equals(CatalogProperties.ENCRYPTION_KMS_CUSTOM_TYPE)) { | ||
String kmsClientImpl = catalogProperties.get(CatalogProperties.ENCRYPTION_KMS_CLIENT_IMPL); | ||
if (kmsClientImpl == null) { | ||
throw new IllegalStateException("Custom KMS client class is not defined"); | ||
} | ||
kmsClient = EncryptionUtil.createKmsClient(kmsClientImpl); | ||
kmsClient.initialize(catalogProperties); | ||
} else { | ||
// Currently support only custom types | ||
throw new UnsupportedOperationException("Undefined KMS type " + kmsType); | ||
} | ||
} | ||
|
||
public EncryptionManager create(Map<String, String> tableProperties) { | ||
String tableKeyId = tableProperties.get(EncryptionProperties.ENCRYPTION_TABLE_KEY); | ||
|
||
if (null == tableKeyId) { | ||
// Unencrypted table | ||
return PlaintextEncryptionManager.instance(); | ||
} | ||
|
||
if (kmsClient == null) { | ||
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. If I think the intent is to use one KMS for all tables in a given catalog. If that's the case, then this should only use catalog properties and it should be initialized in the 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. Per the comment above, it would be useful to support per-table KMS clients. The current implementation returns just one client, but it can be easily changed (eg using a map of "kms-client-class":KmsClientObject, initially. Later, we can use a more sophisticated mapping, that takes other kms parameters into a map key). 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. Following our discussion, moving the multi-kms logic into kms client implementations. |
||
throw new IllegalStateException("Encrypted table. No KMS client is configured in catalog"); | ||
} | ||
|
||
String fileFormat = | ||
PropertyUtil.propertyAsString( | ||
tableProperties, DEFAULT_FILE_FORMAT, DEFAULT_FILE_FORMAT_DEFAULT); | ||
|
||
if (FileFormat.fromString(fileFormat) != FileFormat.PARQUET) { | ||
throw new UnsupportedOperationException( | ||
"Iceberg encryption currently supports only parquet format for data files"); | ||
} | ||
|
||
int dataKeyLength = | ||
PropertyUtil.propertyAsInt( | ||
tableProperties, | ||
EncryptionProperties.ENCRYPTION_DEK_LENGTH, | ||
EncryptionProperties.ENCRYPTION_DEK_LENGTH_DEFAULT); | ||
|
||
return new StandardEncryptionManager(tableKeyId, dataKeyLength, kmsClient); | ||
} | ||
|
||
@Override | ||
public synchronized void close() throws IOException { | ||
if (kmsClient != null) { | ||
kmsClient.close(); | ||
} | ||
} | ||
} |
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.
This isn't used. Can we add it when the use is added?