Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.hadoop.conf.Configuration;
Expand Down Expand Up @@ -54,6 +55,7 @@
import org.apache.iceberg.io.FileIO;
import org.apache.iceberg.io.LocationProvider;
import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.thrift.TException;
import org.slf4j.Logger;
Expand Down Expand Up @@ -86,7 +88,13 @@ public class HiveTableOperations extends BaseMetastoreTableOperations
private EncryptingFileIO encryptingFileIO;
private String tableKeyId;
private int encryptionDekLength;
private List<EncryptedKey> encryptedKeysFromMetadata;

// keys loaded from the latest metadata
private Optional<List<EncryptedKey>> encryptedKeysFromMetadata = Optional.empty();

// keys added to EM (e.g. as a result of a FileAppend) but not committed into the latest metadata
// yet
private Optional<List<EncryptedKey>> encryptedKeysPending = Optional.empty();

protected HiveTableOperations(
Configuration conf,
Expand Down Expand Up @@ -146,9 +154,13 @@ public EncryptionManager encryption() {
encryptionProperties.put(TableProperties.ENCRYPTION_TABLE_KEY, tableKeyId);
encryptionProperties.put(
TableProperties.ENCRYPTION_DEK_LENGTH, String.valueOf(encryptionDekLength));

List<EncryptedKey> keys = Lists.newLinkedList();
encryptedKeysFromMetadata.ifPresent(keys::addAll);
encryptedKeysPending.ifPresent(keys::addAll);

encryptionManager =
EncryptionUtil.createEncryptionManager(
encryptedKeysFromMetadata, encryptionProperties, keyManagementClient);
EncryptionUtil.createEncryptionManager(keys, encryptionProperties, keyManagementClient);
} else {
return PlaintextEncryptionManager.instance();
}
Expand Down Expand Up @@ -202,7 +214,26 @@ the table key parameter (along with existing snapshots) in the file, making the
? Integer.parseInt(dekLengthFromHMS)
: TableProperties.ENCRYPTION_DEK_LENGTH_DEFAULT;

encryptedKeysFromMetadata = current().encryptionKeys();
encryptedKeysFromMetadata = Optional.ofNullable(current().encryptionKeys());

if (encryptionManager != null) {
encryptedKeysPending = Optional.of(Lists.newLinkedList());

Set<String> keyIdsFromMetadata =
encryptedKeysFromMetadata.orElseGet(Lists::newLinkedList).stream()
.map(EncryptedKey::keyId)
.collect(Collectors.toSet());

for (EncryptedKey keyFromEM : EncryptionUtil.encryptionKeys(encryptionManager).values()) {
if (!keyIdsFromMetadata.contains(keyFromEM.keyId())) {
encryptedKeysPending.get().add(keyFromEM);
}
}

} else {
encryptedKeysPending = Optional.empty();
}

// Force re-creation of encryption manager with updated keys
encryptingFileIO = null;
encryptionManager = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.iceberg.AppendFiles;
import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.DataFile;
import org.apache.iceberg.FileScanTask;
import org.apache.iceberg.MetadataTableType;
import org.apache.iceberg.Parameters;
import org.apache.iceberg.Schema;
import org.apache.iceberg.Table;
import org.apache.iceberg.Transaction;
import org.apache.iceberg.encryption.Ciphers;
import org.apache.iceberg.encryption.UnitestKMS;
import org.apache.iceberg.io.InputFile;
Expand Down Expand Up @@ -114,6 +116,24 @@ public void testRefresh() {
assertThat(currentDataFiles(table)).isNotEmpty();
}

@TestTemplate
public void testTransaction() {
catalog.initialize(catalogName, catalogConfig);

Table table = catalog.loadTable(tableIdent);

List<DataFile> dataFiles = currentDataFiles(table);
Transaction transaction = table.newTransaction();
AppendFiles append = transaction.newAppend();

// add an arbitrary datafile
append.appendFile(dataFiles.get(0));
append.commit();
transaction.commitTransaction();

assertThat(currentDataFiles(table).size()).isEqualTo(dataFiles.size() + 1);
}

@TestTemplate
public void testInsertAndDelete() {
sql("INSERT INTO %s VALUES (4, 'd', 4.0), (5, 'e', 5.0), (6, 'f', float('NaN'))", tableName);
Expand Down