Repository navigation
API, Core: Introduce a generic abstraction for file encryption #17545
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,36 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
|
|
||
| /** A file that may be encrypted. */ | ||
| public interface EncryptedFile { | ||
|
|
||
| /** Returns the location of the file. */ | ||
| String location(); | ||
|
|
||
| /** Returns the size of the file in bytes. */ | ||
| long fileSizeInBytes(); | ||
|
|
||
| /** | ||
| * Returns metadata about how this file is encrypted, or null if the file is stored in plain text. | ||
| */ | ||
| ByteBuffer keyMetadata(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,10 +25,7 @@ | |
| import java.nio.ByteBuffer; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.iceberg.ContentFile; | ||
| import org.apache.iceberg.DataFile; | ||
| import org.apache.iceberg.DeleteFile; | ||
| import org.apache.iceberg.ManifestFile; | ||
| import org.apache.iceberg.EncryptedFile; | ||
| import org.apache.iceberg.ManifestListFile; | ||
| import org.apache.iceberg.io.BulkDeletionFailureException; | ||
| import org.apache.iceberg.io.DelegateFileIO; | ||
|
|
@@ -68,11 +65,11 @@ public static EncryptingFileIO combine(FileIO io, EncryptionManager em) { | |
| this.em = em; | ||
| } | ||
|
|
||
| public Map<String, InputFile> bulkDecrypt(Iterable<? extends ContentFile<?>> files) { | ||
| public Map<String, InputFile> bulkDecrypt(Iterable<? extends EncryptedFile> files) { | ||
|
Contributor
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 requires changes in caller code. Are we comfortable with this?
Contributor
Author
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. We are widening the parameter type, so revapi doesn't complain. I'm not entirely sure if this counts as breaking change. Internally, once #18486 is merged, the Spark usage is gone, and the only non-test usage will be from For custom implementations: the constructor of |
||
| ImmutableMap.Builder<String, InputFile> builder = ImmutableMap.builder(); | ||
|
|
||
| List<EncryptedInputFile> encryptedFiles = Lists.newArrayList(); | ||
| for (ContentFile<?> file : files) { | ||
| for (EncryptedFile file : files) { | ||
| if (file.keyMetadata() != null) { | ||
| encryptedFiles.add(wrap(file)); | ||
| } else { | ||
|
|
@@ -104,42 +101,24 @@ public InputFile newInputFile(String path, long length) { | |
| } | ||
|
|
||
| @Override | ||
| public InputFile newInputFile(DataFile file) { | ||
| return newInputFile((ContentFile<?>) file); | ||
| public InputFile newInputFile(ManifestListFile manifestList) { | ||
| if (manifestList.encryptionKeyID() != null) { | ||
| ByteBuffer keyMetadata = manifestList.decryptKeyMetadata(em); | ||
| return newDecryptingInputFile(manifestList.location(), keyMetadata); | ||
| } else { | ||
| return newInputFile(manifestList.location()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public InputFile newInputFile(DeleteFile file) { | ||
| return newInputFile((ContentFile<?>) file); | ||
| } | ||
|
|
||
| private InputFile newInputFile(ContentFile<?> file) { | ||
| public InputFile newInputFile(EncryptedFile file) { | ||
| if (file.keyMetadata() != null) { | ||
| return newDecryptingInputFile(file.location(), file.fileSizeInBytes(), file.keyMetadata()); | ||
| } else { | ||
| return newInputFile(file.location(), file.fileSizeInBytes()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public InputFile newInputFile(ManifestFile manifest) { | ||
| if (manifest.keyMetadata() != null) { | ||
| return newDecryptingInputFile(manifest.path(), manifest.length(), manifest.keyMetadata()); | ||
| } else { | ||
| return newInputFile(manifest.path(), manifest.length()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public InputFile newInputFile(ManifestListFile manifestList) { | ||
| if (manifestList.encryptionKeyID() != null) { | ||
| ByteBuffer keyMetadata = manifestList.decryptKeyMetadata(em); | ||
| return newDecryptingInputFile(manifestList.location(), keyMetadata); | ||
| } else { | ||
| return newInputFile(manifestList.location()); | ||
| } | ||
| } | ||
|
|
||
| public InputFile newDecryptingInputFile(String path, ByteBuffer buffer) { | ||
| return em.decrypt(wrap(io.newInputFile(path), buffer)); | ||
| } | ||
|
|
@@ -181,7 +160,7 @@ public void close() { | |
| } | ||
| } | ||
|
|
||
| private SimpleEncryptedInputFile wrap(ContentFile<?> file) { | ||
| private SimpleEncryptedInputFile wrap(EncryptedFile file) { | ||
| InputFile encryptedInputFile = io.newInputFile(file.location(), file.fileSizeInBytes()); | ||
| return new SimpleEncryptedInputFile(encryptedInputFile, toKeyMetadata(file.keyMetadata())); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import java.util.Map; | ||
| import org.apache.iceberg.DataFile; | ||
| import org.apache.iceberg.DeleteFile; | ||
| import org.apache.iceberg.EncryptedFile; | ||
| import org.apache.iceberg.ManifestFile; | ||
| import org.apache.iceberg.ManifestListFile; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
|
|
@@ -48,27 +49,15 @@ default InputFile newInputFile(String path, long length) { | |
| } | ||
|
|
||
| default InputFile newInputFile(DataFile file) { | ||
| Preconditions.checkArgument( | ||
| file.keyMetadata() == null, | ||
| "Cannot decrypt data file: %s (use EncryptingFileIO)", | ||
| file.location()); | ||
| return newInputFile(file.location(), file.fileSizeInBytes()); | ||
| return newInputFile((EncryptedFile) file); | ||
| } | ||
|
|
||
| default InputFile newInputFile(DeleteFile file) { | ||
| Preconditions.checkArgument( | ||
| file.keyMetadata() == null, | ||
| "Cannot decrypt delete file: %s (use EncryptingFileIO)", | ||
| file.location()); | ||
| return newInputFile(file.location(), file.fileSizeInBytes()); | ||
| return newInputFile((EncryptedFile) file); | ||
| } | ||
|
|
||
| default InputFile newInputFile(ManifestFile manifest) { | ||
| Preconditions.checkArgument( | ||
| manifest.keyMetadata() == null, | ||
| "Cannot decrypt manifest: %s (use EncryptingFileIO)", | ||
| manifest.path()); | ||
| return newInputFile(manifest.path(), manifest.length()); | ||
| return newInputFile((EncryptedFile) manifest); | ||
| } | ||
|
|
||
| default InputFile newInputFile(ManifestListFile manifestList) { | ||
|
Contributor
Author
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 dropped
Contributor
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. Rather than deprecating each of these, we could update all of the exiting
Contributor
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. For me it seems like a good compromise, to leave out |
||
|
|
@@ -80,6 +69,14 @@ default InputFile newInputFile(ManifestListFile manifestList) { | |
| return newInputFile(manifestList.location()); | ||
| } | ||
|
|
||
| default InputFile newInputFile(EncryptedFile file) { | ||
| Preconditions.checkArgument( | ||
| file.keyMetadata() == null, | ||
| "Cannot decrypt file: %s (use EncryptingFileIO)", | ||
| file.location()); | ||
| return newInputFile(file.location(), file.fileSizeInBytes()); | ||
| } | ||
|
|
||
| /** Get a {@link OutputFile} instance to write bytes to the file at the given path. */ | ||
| OutputFile newOutputFile(String path); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
| import org.apache.iceberg.ContentFile; | ||
| import org.apache.iceberg.ContentScanTask; | ||
| import org.apache.iceberg.DeleteFile; | ||
| import org.apache.iceberg.EncryptedFile; | ||
| import org.apache.iceberg.MetadataColumns; | ||
| import org.apache.iceberg.Partitioning; | ||
| import org.apache.iceberg.ScanTask; | ||
|
|
@@ -185,7 +186,10 @@ private Map<String, InputFile> inputFiles() { | |
| if (lazyInputFiles == null) { | ||
| this.lazyInputFiles = | ||
| fileIO.bulkDecrypt( | ||
| () -> taskGroup.tasks().stream().flatMap(this::referencedFiles).iterator()); | ||
| () -> | ||
|
Contributor
Author
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. These changes will be gone with #18486 |
||
| taskGroup.tasks().stream() | ||
| .<EncryptedFile>flatMap(this::referencedFiles) | ||
| .iterator()); | ||
| } | ||
|
|
||
| return lazyInputFiles; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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 think we should deprecate
path()and replace usage tolocation(), similarly to how we have these inContentFilealready.Same for
length()andfileSizeInBytes()