-
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
API: Extend FileIO and add EncryptingFileIO. #9592
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b968740
API: Extend FileIO and add EncryptingFileIO.
rdblue a3432f4
Use new FileIO mehtods in ManifestFiles, fix ContentCache.
rdblue 1fbc85a
Fix test failures and review comments.
rdblue 18f758e
Add back public method as deprecated.
rdblue 491754a
Fix revapi for ContentCache.
rdblue 888dffc
Deprecate ContentCache methods that expose private classes.
rdblue a3013c1
Apply spotless.
rdblue 3d51914
Return CacheEntry.
rdblue 0859f18
Fix uses of getIfPresent.
rdblue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
210 changes: 210 additions & 0 deletions
210
api/src/main/java/org/apache/iceberg/encryption/EncryptingFileIO.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,210 @@ | ||
/* | ||
* 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.io.Closeable; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.io.UncheckedIOException; | ||
import java.nio.ByteBuffer; | ||
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.io.FileIO; | ||
import org.apache.iceberg.io.InputFile; | ||
import org.apache.iceberg.io.OutputFile; | ||
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
import org.apache.iceberg.relocated.com.google.common.collect.Iterables; | ||
|
||
public class EncryptingFileIO implements FileIO, Serializable { | ||
public static EncryptingFileIO create(FileIO io, EncryptionManager em) { | ||
return new EncryptingFileIO(io, em); | ||
} | ||
|
||
private final FileIO io; | ||
private final EncryptionManager em; | ||
|
||
EncryptingFileIO(FileIO io, EncryptionManager em) { | ||
this.io = io; | ||
this.em = em; | ||
} | ||
|
||
public Map<String, InputFile> bulkDecrypt(Iterable<? extends ContentFile<?>> files) { | ||
Iterable<InputFile> decrypted = em.decrypt(Iterables.transform(files, this::wrap)); | ||
|
||
ImmutableMap.Builder<String, InputFile> builder = ImmutableMap.builder(); | ||
for (InputFile in : decrypted) { | ||
builder.put(in.location(), in); | ||
} | ||
|
||
return builder.buildKeepingLast(); | ||
} | ||
|
||
public EncryptionManager encryptionManager() { | ||
return em; | ||
} | ||
|
||
@Override | ||
public InputFile newInputFile(String path) { | ||
return io.newInputFile(path); | ||
} | ||
|
||
@Override | ||
public InputFile newInputFile(String path, long length) { | ||
return io.newInputFile(path, length); | ||
} | ||
|
||
@Override | ||
public InputFile newInputFile(DataFile file) { | ||
return newInputFile((ContentFile<?>) file); | ||
} | ||
|
||
@Override | ||
public InputFile newInputFile(DeleteFile file) { | ||
return newInputFile((ContentFile<?>) file); | ||
} | ||
|
||
private InputFile newInputFile(ContentFile<?> file) { | ||
if (file.keyMetadata() != null) { | ||
return newDecryptingInputFile( | ||
file.path().toString(), file.fileSizeInBytes(), file.keyMetadata()); | ||
} else { | ||
return newInputFile(file.path().toString(), 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()); | ||
} | ||
} | ||
|
||
public InputFile newDecryptingInputFile(String path, ByteBuffer buffer) { | ||
return em.decrypt(wrap(io.newInputFile(path), buffer)); | ||
} | ||
|
||
public InputFile newDecryptingInputFile(String path, long length, ByteBuffer buffer) { | ||
// TODO: is the length correct for the encrypted file? It may be the length of the plaintext | ||
// stream | ||
return em.decrypt(wrap(io.newInputFile(path, length), buffer)); | ||
} | ||
|
||
@Override | ||
public OutputFile newOutputFile(String path) { | ||
return io.newOutputFile(path); | ||
} | ||
|
||
public EncryptedOutputFile newEncryptingOutputFile(String path) { | ||
OutputFile plainOutputFile = io.newOutputFile(path); | ||
return em.encrypt(plainOutputFile); | ||
} | ||
|
||
@Override | ||
public void deleteFile(String path) { | ||
io.deleteFile(path); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
io.close(); | ||
|
||
if (em instanceof Closeable) { | ||
try { | ||
((Closeable) em).close(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Failed to close encryption manager", e); | ||
} | ||
} | ||
} | ||
|
||
private SimpleEncryptedInputFile wrap(ContentFile<?> file) { | ||
InputFile encryptedInputFile = io.newInputFile(file.path().toString(), file.fileSizeInBytes()); | ||
return new SimpleEncryptedInputFile(encryptedInputFile, toKeyMetadata(file.keyMetadata())); | ||
} | ||
|
||
private static SimpleEncryptedInputFile wrap(InputFile encryptedInputFile, ByteBuffer buffer) { | ||
return new SimpleEncryptedInputFile(encryptedInputFile, toKeyMetadata(buffer)); | ||
} | ||
|
||
private static EncryptionKeyMetadata toKeyMetadata(ByteBuffer buffer) { | ||
return buffer != null ? new SimpleKeyMetadata(buffer) : EmptyKeyMetadata.get(); | ||
} | ||
|
||
private static class SimpleEncryptedInputFile implements EncryptedInputFile { | ||
private final InputFile encryptedInputFile; | ||
private final EncryptionKeyMetadata keyMetadata; | ||
|
||
private SimpleEncryptedInputFile( | ||
InputFile encryptedInputFile, EncryptionKeyMetadata keyMetadata) { | ||
this.encryptedInputFile = encryptedInputFile; | ||
this.keyMetadata = keyMetadata; | ||
} | ||
|
||
@Override | ||
public InputFile encryptedInputFile() { | ||
return encryptedInputFile; | ||
} | ||
|
||
@Override | ||
public EncryptionKeyMetadata keyMetadata() { | ||
return keyMetadata; | ||
} | ||
} | ||
|
||
private static class SimpleKeyMetadata implements EncryptionKeyMetadata { | ||
private final ByteBuffer metadataBuffer; | ||
|
||
private SimpleKeyMetadata(ByteBuffer metadataBuffer) { | ||
this.metadataBuffer = metadataBuffer; | ||
} | ||
|
||
@Override | ||
public ByteBuffer buffer() { | ||
return metadataBuffer; | ||
} | ||
|
||
@Override | ||
public EncryptionKeyMetadata copy() { | ||
return new SimpleKeyMetadata(metadataBuffer.duplicate()); | ||
} | ||
} | ||
|
||
private static class EmptyKeyMetadata implements EncryptionKeyMetadata { | ||
private static final EmptyKeyMetadata INSTANCE = new EmptyKeyMetadata(); | ||
|
||
private static EmptyKeyMetadata get() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public ByteBuffer buffer() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public EncryptionKeyMetadata copy() { | ||
return this; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,7 +126,7 @@ public static ManifestReader<DataFile> read( | |
manifest.content() == ManifestContent.DATA, | ||
"Cannot read a delete manifest with a ManifestReader: %s", | ||
manifest); | ||
InputFile file = newInputFile(io, manifest.path(), manifest.length()); | ||
InputFile file = newInputFile(io, manifest); | ||
InheritableMetadata inheritableMetadata = InheritableMetadataFactory.fromManifest(manifest); | ||
return new ManifestReader<>( | ||
file, manifest.partitionSpecId(), specsById, inheritableMetadata, FileType.DATA_FILES); | ||
|
@@ -181,7 +181,7 @@ public static ManifestReader<DeleteFile> readDeleteManifest( | |
manifest.content() == ManifestContent.DELETES, | ||
"Cannot read a data manifest with a DeleteManifestReader: %s", | ||
manifest); | ||
InputFile file = newInputFile(io, manifest.path(), manifest.length()); | ||
InputFile file = newInputFile(io, manifest); | ||
InheritableMetadata inheritableMetadata = InheritableMetadataFactory.fromManifest(manifest); | ||
return new ManifestReader<>( | ||
file, manifest.partitionSpecId(), specsById, inheritableMetadata, FileType.DELETE_FILES); | ||
|
@@ -345,34 +345,24 @@ private static ManifestFile copyManifestInternal( | |
return writer.toManifestFile(); | ||
} | ||
|
||
private static InputFile newInputFile(FileIO io, String path, long length) { | ||
boolean enabled; | ||
|
||
try { | ||
enabled = cachingEnabled(io); | ||
} catch (UnsupportedOperationException e) { | ||
// There is an issue reading io.properties(). Disable caching. | ||
enabled = false; | ||
} | ||
|
||
if (enabled) { | ||
ContentCache cache = contentCache(io); | ||
Preconditions.checkNotNull( | ||
cache, | ||
"ContentCache creation failed. Check that all manifest caching configurations has valid value."); | ||
LOG.debug("FileIO-level cache stats: {}", CONTENT_CACHES.stats()); | ||
return cache.tryCache(io, path, length); | ||
private static InputFile newInputFile(FileIO io, ManifestFile manifest) { | ||
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 simplified the logic here in addition to passing |
||
InputFile input = io.newInputFile(manifest); | ||
if (cachingEnabled(io)) { | ||
return contentCache(io).tryCache(input); | ||
} | ||
|
||
// caching is not enable for this io or caught RuntimeException. | ||
return io.newInputFile(path, length); | ||
return input; | ||
} | ||
|
||
static boolean cachingEnabled(FileIO io) { | ||
try { | ||
return PropertyUtil.propertyAsBoolean( | ||
io.properties(), | ||
CatalogProperties.IO_MANIFEST_CACHE_ENABLED, | ||
CatalogProperties.IO_MANIFEST_CACHE_ENABLED_DEFAULT); | ||
} catch (UnsupportedOperationException e) { | ||
return false; | ||
} | ||
} | ||
|
||
static long cacheDurationMs(FileIO io) { | ||
|
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Yep, this is the plaintext stream length (which is recorded in the ContentFile and ManifestFile objects). I have updated the AesGcmInput classes to work with the verified plaintext length values (instead of untrusted file length values) - actually, the code becomes more compact and intuitive. I'll send this patch along with other e2e updates next week.