-
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 all 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 |
---|---|---|
|
@@ -57,5 +57,9 @@ public static EncryptedOutputFile encryptedOutput( | |
encryptedOutputFile, BaseEncryptionKeyMetadata.fromByteArray(keyMetadata)); | ||
} | ||
|
||
public static EncryptedOutputFile plainAsEncryptedOutput(OutputFile encryptingOutputFile) { | ||
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 isn't used. Can we add it when the use is added? |
||
return new BaseEncryptedOutputFile(encryptingOutputFile, EncryptionKeyMetadata.empty()); | ||
} | ||
|
||
private EncryptedFiles() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* 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 static org.apache.iceberg.TableProperties.ENCRYPTION_DEK_LENGTH; | ||
import static org.apache.iceberg.TableProperties.ENCRYPTION_DEK_LENGTH_DEFAULT; | ||
import static org.apache.iceberg.TableProperties.ENCRYPTION_TABLE_KEY; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.Map; | ||
import org.apache.iceberg.CatalogProperties; | ||
import org.apache.iceberg.FileFormat; | ||
import org.apache.iceberg.common.DynConstructors; | ||
import org.apache.iceberg.util.PropertyUtil; | ||
|
||
public class EncryptionUtil { | ||
|
||
private EncryptionUtil() {} | ||
|
||
public static KeyMetadata parseKeyMetadata(ByteBuffer metadataBuffer) { | ||
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 isn't used and duplicates the wrapped method. I think we should remove it. |
||
return KeyMetadata.parse(metadataBuffer); | ||
} | ||
|
||
public static EncryptionKeyMetadata createKeyMetadata(ByteBuffer key, ByteBuffer aadPrefix) { | ||
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 isn't used and it looks like a few places use the KeyMetadata constructor directly. |
||
return new KeyMetadata(key, aadPrefix); | ||
} | ||
|
||
public static long gcmEncryptionLength(long plainFileLength) { | ||
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. Can we remove this since it isn't used? I'd rather not have dead and untested code. |
||
int numberOfFullBlocks = Math.toIntExact(plainFileLength / Ciphers.PLAIN_BLOCK_SIZE); | ||
int plainBytesInLastBlock = | ||
Math.toIntExact(plainFileLength - numberOfFullBlocks * Ciphers.PLAIN_BLOCK_SIZE); | ||
boolean fullBlocksOnly = (0 == plainBytesInLastBlock); | ||
int cipherBytesInLastBlock = | ||
fullBlocksOnly ? 0 : plainBytesInLastBlock + Ciphers.NONCE_LENGTH + Ciphers.GCM_TAG_LENGTH; | ||
int cipherBlockSize = Ciphers.PLAIN_BLOCK_SIZE + Ciphers.NONCE_LENGTH + Ciphers.GCM_TAG_LENGTH; | ||
return (long) Ciphers.GCM_STREAM_HEADER_LENGTH | ||
+ numberOfFullBlocks * cipherBlockSize | ||
+ cipherBytesInLastBlock; | ||
} | ||
|
||
public static KeyManagementClient createKmsClient(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)) { | ||
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. I think that it is much simpler if rather than requiring a type that is "custom", this instead checks that either type or impl is set. |
||
// Currently support only custom types | ||
throw new UnsupportedOperationException("Undefined KMS type " + kmsType); | ||
} | ||
|
||
String kmsClientImpl = catalogProperties.get(CatalogProperties.ENCRYPTION_KMS_CLIENT_IMPL); | ||
|
||
if (kmsClientImpl == null) { | ||
throw new IllegalStateException("Custom KMS client class is not defined"); | ||
} | ||
|
||
KeyManagementClient kmsClient; | ||
DynConstructors.Ctor<KeyManagementClient> ctor; | ||
try { | ||
ctor = DynConstructors.builder(KeyManagementClient.class).impl(kmsClientImpl).buildChecked(); | ||
} catch (NoSuchMethodException e) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Cannot initialize KeyManagementClient, missing no-arg constructor for class %s", | ||
kmsClientImpl), | ||
e); | ||
} | ||
|
||
try { | ||
kmsClient = ctor.newInstance(); | ||
} catch (ClassCastException e) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Cannot initialize kms client, %s does not implement KeyManagementClient interface", | ||
kmsClientImpl), | ||
e); | ||
} | ||
|
||
kmsClient.initialize(catalogProperties); | ||
|
||
return kmsClient; | ||
} | ||
|
||
public static EncryptionManager createEncryptionManager( | ||
Map<String, String> tableProperties, KeyManagementClient kmsClient) { | ||
String tableKeyId = tableProperties.get(ENCRYPTION_TABLE_KEY); | ||
|
||
if (null == tableKeyId) { | ||
// Unencrypted table | ||
return PlaintextEncryptionManager.instance(); | ||
} | ||
|
||
if (kmsClient == null) { | ||
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, ENCRYPTION_DEK_LENGTH, ENCRYPTION_DEK_LENGTH_DEFAULT); | ||
|
||
return new StandardEncryptionManager(tableKeyId, dataKeyLength, kmsClient); | ||
} | ||
|
||
public static boolean useNativeEncryption(EncryptionKeyMetadata keyMetadata) { | ||
return keyMetadata != null && keyMetadata instanceof KeyMetadata; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* 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.TableProperties; | ||
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; | ||
|
||
class StandardEncryptedOutputFile implements EncryptedOutputFile { | ||
|
||
private final OutputFile encryptingOutputFile; | ||
|
||
private final EncryptionKeyMetadata keyMetadata; | ||
private final OutputFile rawOutputFile; | ||
|
||
StandardEncryptedOutputFile( | ||
OutputFile encryptingOutputFile, | ||
EncryptionKeyMetadata keyMetadata, | ||
OutputFile rawOutputFile) { | ||
this.encryptingOutputFile = encryptingOutputFile; | ||
this.keyMetadata = keyMetadata; | ||
this.rawOutputFile = rawOutputFile; | ||
} | ||
|
||
@Override | ||
public OutputFile encryptingOutputFile() { | ||
return encryptingOutputFile; | ||
} | ||
|
||
@Override | ||
public EncryptionKeyMetadata keyMetadata() { | ||
return keyMetadata; | ||
} | ||
|
||
@Override | ||
public OutputFile rawOutputFile() { | ||
return rawOutputFile; | ||
} | ||
} | ||
|
||
/** | ||
* @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(TableProperties.ENCRYPTION_AAD_LENGTH_DEFAULT); | ||
workerRNG().nextBytes(aadPrefix.array()); | ||
|
||
KeyMetadata encryptionMetadata = new KeyMetadata(fileDek, aadPrefix); | ||
|
||
return new StandardEncryptedOutputFile( | ||
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); | ||
} | ||
} |
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.
I'm not sure about this name, but I've had a hard time coming up with a better one. The best I can come up with is
plainOutputFile
. What do you think about that? I think it's slightly clearer than "raw".It also looks like this isn't used in this PR, in which case I think we should include it in a PR where it is used.