forked from apache/iceberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core: Add StandardEncryptionManager (apache#9277)
Co-Authored-By: Jian Tang <jian_tang@apple.com> Co-authored-by: Gidon Gershinsky <ggershinsky@apple.com>
- Loading branch information
Showing
11 changed files
with
372 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
107 changes: 107 additions & 0 deletions
107
core/src/main/java/org/apache/iceberg/encryption/EncryptionUtil.java
This file contains 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,107 @@ | ||
/* | ||
* 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.util.Map; | ||
import org.apache.iceberg.CatalogProperties; | ||
import org.apache.iceberg.FileFormat; | ||
import org.apache.iceberg.TableProperties; | ||
import org.apache.iceberg.common.DynConstructors; | ||
import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
import org.apache.iceberg.util.PropertyUtil; | ||
|
||
public class EncryptionUtil { | ||
|
||
private EncryptionUtil() {} | ||
|
||
public static KeyManagementClient createKmsClient(Map<String, String> catalogProperties) { | ||
String kmsType = catalogProperties.get(CatalogProperties.ENCRYPTION_KMS_TYPE); | ||
String kmsImpl = catalogProperties.get(CatalogProperties.ENCRYPTION_KMS_IMPL); | ||
|
||
Preconditions.checkArgument( | ||
kmsType == null || kmsImpl == null, | ||
"Cannot set both KMS type (%s) and KMS impl (%s)", | ||
kmsType, | ||
kmsImpl); | ||
|
||
// TODO: Add KMS implementations | ||
Preconditions.checkArgument(kmsType == null, "Unsupported KMS type: %s", kmsType); | ||
|
||
KeyManagementClient kmsClient; | ||
DynConstructors.Ctor<KeyManagementClient> ctor; | ||
try { | ||
ctor = DynConstructors.builder(KeyManagementClient.class).impl(kmsImpl).buildChecked(); | ||
} catch (NoSuchMethodException e) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Cannot initialize KeyManagementClient, missing no-arg constructor for class %s", | ||
kmsImpl), | ||
e); | ||
} | ||
|
||
try { | ||
kmsClient = ctor.newInstance(); | ||
} catch (ClassCastException e) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Cannot initialize kms client, %s does not implement KeyManagementClient interface", | ||
kmsImpl), | ||
e); | ||
} | ||
|
||
kmsClient.initialize(catalogProperties); | ||
|
||
return kmsClient; | ||
} | ||
|
||
public static EncryptionManager createEncryptionManager( | ||
Map<String, String> tableProperties, KeyManagementClient kmsClient) { | ||
Preconditions.checkArgument(kmsClient != null, "Invalid KMS client: null"); | ||
String tableKeyId = tableProperties.get(TableProperties.ENCRYPTION_TABLE_KEY); | ||
|
||
if (null == tableKeyId) { | ||
// Unencrypted table | ||
return PlaintextEncryptionManager.instance(); | ||
} | ||
|
||
String fileFormat = | ||
PropertyUtil.propertyAsString( | ||
tableProperties, | ||
TableProperties.DEFAULT_FILE_FORMAT, | ||
TableProperties.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, | ||
TableProperties.ENCRYPTION_DEK_LENGTH, | ||
TableProperties.ENCRYPTION_DEK_LENGTH_DEFAULT); | ||
|
||
Preconditions.checkState( | ||
dataKeyLength == 16 || dataKeyLength == 24 || dataKeyLength == 32, | ||
"Invalid data key length: %s (must be 16, 24, or 32)", | ||
dataKeyLength); | ||
|
||
return new StandardEncryptionManager(tableKeyId, dataKeyLength, kmsClient); | ||
} | ||
} |
This file contains 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 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 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.