Skip to content
Closed
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 @@ -29,6 +29,7 @@
import org.apache.iceberg.TableProperties;
import org.apache.iceberg.io.InputFile;
import org.apache.iceberg.io.OutputFile;
import org.apache.iceberg.io.PositionOutputStream;
import org.apache.iceberg.io.SeekableInputStream;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
Expand Down Expand Up @@ -289,6 +290,35 @@ public StandardKeyMetadata keyMetadata() {

@Override
public OutputFile encryptingOutputFile() {
return this;
}

@Override
public OutputFile plainOutputFile() {
return plainOutputFile;
}

@Override
public PositionOutputStream create() {
return lazyEncryptingOutputFile().create();
}

@Override
public PositionOutputStream createOrOverwrite() {
return lazyEncryptingOutputFile().createOrOverwrite();
}

@Override
public String location() {
return lazyEncryptingOutputFile().location();
}

@Override
public InputFile toInputFile() {
return lazyEncryptingOutputFile().toInputFile();
}

private OutputFile lazyEncryptingOutputFile() {
if (null == lazyEncryptingOutputFile) {
this.lazyEncryptingOutputFile =
new AesGcmOutputFile(
Expand All @@ -299,11 +329,6 @@ public OutputFile encryptingOutputFile() {

return lazyEncryptingOutputFile;
}

@Override
public OutputFile plainOutputFile() {
return plainOutputFile;
}
}

private static class StandardDecryptedInputFile implements NativeEncryptionInputFile {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,34 @@
import static org.apache.iceberg.Files.localOutput;
import static org.apache.iceberg.parquet.ParquetWritingTestUtils.createTempFile;
import static org.apache.iceberg.types.Types.NestedField.optional;
import static org.apache.parquet.hadoop.ParquetFileWriter.EF_MAGIC_STR;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.SecureRandom;
import java.util.List;
import org.apache.avro.generic.GenericData;
import org.apache.hadoop.conf.Configuration;
import org.apache.iceberg.Schema;
import org.apache.iceberg.avro.AvroSchemaUtil;
import org.apache.iceberg.data.GenericRecord;
import org.apache.iceberg.data.parquet.GenericParquetWriter;
import org.apache.iceberg.encryption.EncryptedOutputFile;
import org.apache.iceberg.encryption.EncryptionTestHelpers;
import org.apache.iceberg.encryption.NativeEncryptionKeyMetadata;
import org.apache.iceberg.encryption.NativeEncryptionOutputFile;
import org.apache.iceberg.hadoop.HadoopInputFile;
import org.apache.iceberg.hadoop.HadoopOutputFile;
import org.apache.iceberg.io.CloseableIterator;
import org.apache.iceberg.io.FileAppender;
import org.apache.iceberg.io.InputFile;
import org.apache.iceberg.io.SeekableInputStream;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.types.Types.IntegerType;
import org.apache.parquet.crypto.ParquetCryptoRuntimeException;
Expand Down Expand Up @@ -125,4 +138,57 @@ public void testReadEncryptedFile() throws IOException {
}
}
}

@Test
public void testReadAndWriteHadoopFile() throws IOException {
List<GenericRecord> records = Lists.newArrayListWithCapacity(RECORD_COUNT);
for (int i = 1; i <= RECORD_COUNT; i++) {
GenericRecord record = GenericRecord.create(SCHEMA.asStruct());
record.set(0, i);
records.add(record);
}

org.apache.hadoop.fs.Path path = new org.apache.hadoop.fs.Path(createTempFile(temp).toURI());

EncryptedOutputFile encryptedOutputFile =
EncryptionTestHelpers.createEncryptionManager()
.encrypt(HadoopOutputFile.fromPath(path, new Configuration()));
NativeEncryptionKeyMetadata keyMetadata =
((NativeEncryptionOutputFile) encryptedOutputFile).keyMetadata();
FileAppender<GenericRecord> writer =
Parquet.write(encryptedOutputFile.encryptingOutputFile())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this does not result in double encryption (sending PME to AesGcmOutputStream). Seemingly, encryptedOutputFile.encryptingOutputFile().create() calls new AesGcmOutputFile(..).create()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully understand your comment, but if you take a look at the Parquet.write implementation, there are different methods for Encrypted/non-Encrypted files, but all of them ends in using the EncryptedOutputFile.encryptingOutputFile().

With the new implementation the StandardEncryptedOutputFile.encryptingOutputFile() returns the same as StandardEncryptedOutputFile.encryptingOutputFile().encryptingOutputFile(). Parquet uses the OutputStreams, which are also the same, no matter how many times you call the encryptingOutputFile() before getting the OutputStream`

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I'll need to have a deeper look, will play with the code.

.withFileEncryptionKey(keyMetadata.encryptionKey())
.withAADPrefix(keyMetadata.aadPrefix())
.schema(SCHEMA)
.createWriterFunc(fileSchema -> GenericParquetWriter.create(SCHEMA, fileSchema))
.build();

try (writer) {
writer.addAll(Lists.newArrayList(records.toArray(new GenericRecord[] {})));
}

InputFile inputFile = HadoopInputFile.fromPath(path, new Configuration());
checkFileEncryption(inputFile);
try (CloseableIterator readRecords =
Parquet.read(inputFile)
.withFileEncryptionKey(keyMetadata.encryptionKey())
.withAADPrefix(keyMetadata.aadPrefix())
.project(SCHEMA)
.callInit()
.build()
.iterator()) {
for (int i = 1; i <= RECORD_COUNT; i++) {
GenericData.Record readRecord = (GenericData.Record) readRecords.next();
assertThat(readRecord.get(COLUMN_NAME)).isEqualTo(i);
}
}
}

private void checkFileEncryption(InputFile inputFile) throws IOException {
SeekableInputStream stream = inputFile.newStream();
byte[] magic = new byte[4];
stream.read(magic);
stream.close();
assertThat(magic).isEqualTo(EF_MAGIC_STR.getBytes(StandardCharsets.UTF_8));
}
}