From 88ca2fca9f4bf0ac5a6720f39c50c54446f2dab1 Mon Sep 17 00:00:00 2001 From: alice-shang Date: Thu, 8 Oct 2026 21:12:21 +0900 Subject: [PATCH 1/2] feat(encryption): add EncryptingFileIO EncryptingFileIO is the FileIO of an encrypted table: the catalog's FileIO paired with the table's EncryptionManager. Plain calls pass through unchanged (unencrypted files, and Parquet files, which are encrypted natively). AES GCM Stream files are handled as follows: - ForFile(io, key_metadata) / ForKeyMetadata(key_metadata): a decrypting view for a file whose key metadata is known (manifests, Avro data files, Puffin files). It wraps the raw file in an EncryptedInputFile and decrypts it through EncryptionManager::Decrypt. - NewEncryptingOutputFile(path): wraps a new output file through EncryptionManager::Encrypt and returns the EncryptedOutputFile, whose key metadata the caller records once writing is complete. This mirrors Java's EncryptingFileIO.newEncryptingOutputFile. The Arrow I/O adapter unwraps an EncryptingFileIO for plain reads and writes, so Arrow file systems keep their fast path. --- src/iceberg/CMakeLists.txt | 1 + src/iceberg/arrow/arrow_io.cc | 20 ++- src/iceberg/encryption/encrypting_file_io.cc | 151 ++++++++++++++++++ src/iceberg/encryption/encrypting_file_io.h | 96 +++++++++++ src/iceberg/test/CMakeLists.txt | 1 + src/iceberg/test/encrypting_file_io_test.cc | 129 +++++++++++++++ .../test/standard_encryption_manager_test.cc | 42 +++++ 7 files changed, 436 insertions(+), 4 deletions(-) create mode 100644 src/iceberg/encryption/encrypting_file_io.cc create mode 100644 src/iceberg/encryption/encrypting_file_io.h create mode 100644 src/iceberg/test/encrypting_file_io_test.cc diff --git a/src/iceberg/CMakeLists.txt b/src/iceberg/CMakeLists.txt index f9604e2575..0668bf09de 100644 --- a/src/iceberg/CMakeLists.txt +++ b/src/iceberg/CMakeLists.txt @@ -34,6 +34,7 @@ set(ICEBERG_SOURCES deletes/position_delete_range_consumer.cc deletes/roaring_position_bitmap.cc encryption/encrypted_files.cc + encryption/encrypting_file_io.cc encryption/encryption_manager.cc encryption/key_management_client.cc encryption/standard_key_metadata.cc diff --git a/src/iceberg/arrow/arrow_io.cc b/src/iceberg/arrow/arrow_io.cc index 998f8c5ce8..bc77cba61f 100644 --- a/src/iceberg/arrow/arrow_io.cc +++ b/src/iceberg/arrow/arrow_io.cc @@ -35,6 +35,7 @@ #include "iceberg/arrow/arrow_io_internal.h" #include "iceberg/arrow/arrow_io_util.h" #include "iceberg/arrow/arrow_status_internal.h" +#include "iceberg/encryption/encrypting_file_io.h" #include "iceberg/util/macros.h" namespace iceberg::arrow { @@ -481,6 +482,13 @@ class ArrowOutputFile : public OutputFile { std::string path_; }; +const std::shared_ptr& PlainFileIO(const std::shared_ptr& io) { + if (auto* encrypting_io = EncryptingFileIO::From(io)) { + return encrypting_io->io(); + } + return io; +} + } // namespace Result ArrowFileSystemFileIO::ResolvePath(const std::string& file_location) { @@ -507,9 +515,12 @@ Result ArrowFileSystemFileIO::ResolvePath(const std::string& file_l } Result> OpenArrowInputStream( - const std::shared_ptr& io, const std::string& path, + const std::shared_ptr& file_io, const std::string& path, std::optional length) { - ICEBERG_PRECHECK(io != nullptr, "FileIO cannot be null"); + ICEBERG_PRECHECK(file_io != nullptr, "FileIO cannot be null"); + // An EncryptingFileIO passes plain reads through: use its FileIO directly, so that + // Arrow file systems keep their fast path. + const auto& io = PlainFileIO(file_io); if (auto arrow_io = std::dynamic_pointer_cast(io)) { ICEBERG_ASSIGN_OR_RAISE(auto resolved_path, arrow_io->ResolvePath(path)); @@ -539,8 +550,9 @@ Result> OpenArrowInputStream( } Result> OpenArrowOutputStream( - const std::shared_ptr& io, const std::string& path, bool overwrite) { - ICEBERG_PRECHECK(io != nullptr, "FileIO cannot be null"); + const std::shared_ptr& file_io, const std::string& path, bool overwrite) { + ICEBERG_PRECHECK(file_io != nullptr, "FileIO cannot be null"); + const auto& io = PlainFileIO(file_io); if (auto arrow_io = std::dynamic_pointer_cast(io)) { ICEBERG_ASSIGN_OR_RAISE(auto resolved_path, arrow_io->ResolvePath(path)); diff --git a/src/iceberg/encryption/encrypting_file_io.cc b/src/iceberg/encryption/encrypting_file_io.cc new file mode 100644 index 0000000000..eb1ac4b833 --- /dev/null +++ b/src/iceberg/encryption/encrypting_file_io.cc @@ -0,0 +1,151 @@ +/* + * 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. + */ + +#include "iceberg/encryption/encrypting_file_io.h" + +#include "iceberg/encryption/encrypted_files.h" +#include "iceberg/encryption/encryption_key_metadata.h" +#include "iceberg/util/macros.h" + +namespace iceberg { + +namespace { + +/// \brief Read-only per-file view: reads decrypt with fixed key metadata. +class DecryptingFileIO : public FileIO { + public: + DecryptingFileIO(std::shared_ptr io, std::shared_ptr em, + std::vector key_metadata) + : io_(std::move(io)), em_(std::move(em)), key_metadata_(std::move(key_metadata)) {} + + Result> NewInputFile(std::string file_location) override { + ICEBERG_ASSIGN_OR_RAISE(auto raw, io_->NewInputFile(std::move(file_location))); + return Decrypt(std::move(raw)); + } + + // The length recorded in manifests is the stored length; the trusted length used for + // truncation checks comes from the key metadata, so the passed length is only a hint + // for the underlying file. + Result> NewInputFile(std::string file_location, + size_t length) override { + ICEBERG_ASSIGN_OR_RAISE(auto raw, + io_->NewInputFile(std::move(file_location), length)); + return Decrypt(std::move(raw)); + } + + Result> NewOutputFile(std::string file_location) override { + return NotSupported("Cannot write {}: this FileIO only decrypts", file_location); + } + + Status DeleteFile(const std::string& file_location) override { + return io_->DeleteFile(file_location); + } + + private: + Result> Decrypt(std::unique_ptr raw) { + ICEBERG_ASSIGN_OR_RAISE( + auto encrypted, + EncryptedFiles::EncryptedInput( + std::move(raw), std::make_shared(key_metadata_))); + return em_->Decrypt(std::move(encrypted)); + } + + std::shared_ptr io_; + std::shared_ptr em_; + std::vector key_metadata_; +}; + +} // namespace + +EncryptingFileIO::EncryptingFileIO(std::shared_ptr io, + std::shared_ptr em) + : io_(std::move(io)), em_(std::move(em)) { + // Never nest: wrap the innermost plain FileIO. + if (auto* inner = From(io_)) { + io_ = inner->io_; + } +} + +EncryptingFileIO* EncryptingFileIO::From(const std::shared_ptr& io) { + return dynamic_cast(io.get()); +} + +Result> EncryptingFileIO::ForFile( + const std::shared_ptr& io, std::span key_metadata) { + if (key_metadata.empty()) { + return io; + } + auto* encrypting_io = From(io); + if (encrypting_io == nullptr) { + return NotSupported("Cannot read encrypted file: FileIO has no encryption manager"); + } + return encrypting_io->ForKeyMetadata(key_metadata); +} + +Result> EncryptingFileIO::ForKeyMetadata( + std::span key_metadata) const { + ICEBERG_PRECHECK(!key_metadata.empty(), "Key metadata cannot be empty"); + return std::make_shared( + io_, em_, std::vector(key_metadata.begin(), key_metadata.end())); +} + +Result> EncryptingFileIO::NewEncryptingOutputFile( + std::string file_location) { + ICEBERG_ASSIGN_OR_RAISE(auto plain, io_->NewOutputFile(std::move(file_location))); + return em_->Encrypt(std::move(plain)); +} + +Result> EncryptingFileIO::NewInputFile( + std::string file_location) { + return io_->NewInputFile(std::move(file_location)); +} + +Result> EncryptingFileIO::NewInputFile( + std::string file_location, size_t length) { + return io_->NewInputFile(std::move(file_location), length); +} + +Result> EncryptingFileIO::NewOutputFile( + std::string file_location) { + return io_->NewOutputFile(std::move(file_location)); +} + +Result EncryptingFileIO::ReadFile(const std::string& file_location, + std::optional length) { + return io_->ReadFile(file_location, length); +} + +Status EncryptingFileIO::WriteFile(const std::string& file_location, + std::string_view content) { + return io_->WriteFile(file_location, content); +} + +Status EncryptingFileIO::DeleteFile(const std::string& file_location) { + return io_->DeleteFile(file_location); +} + +Status EncryptingFileIO::DeleteFiles(const std::vector& file_locations) { + return io_->DeleteFiles(file_locations); +} + +SupportsStorageCredentials* EncryptingFileIO::AsSupportsStorageCredentials() { + return io_->AsSupportsStorageCredentials(); +} + +} // namespace iceberg diff --git a/src/iceberg/encryption/encrypting_file_io.h b/src/iceberg/encryption/encrypting_file_io.h new file mode 100644 index 0000000000..e1f4312af8 --- /dev/null +++ b/src/iceberg/encryption/encrypting_file_io.h @@ -0,0 +1,96 @@ +/* + * 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. + */ + +#pragma once + +/// \file iceberg/encryption/encrypting_file_io.h +/// FileIO that pairs a plain FileIO with a table's EncryptionManager. + +#include +#include +#include +#include +#include +#include +#include + +#include "iceberg/encryption/encrypted_file.h" +#include "iceberg/encryption/encryption_manager.h" +#include "iceberg/file_io.h" +#include "iceberg/iceberg_export.h" +#include "iceberg/result.h" + +namespace iceberg { + +/// \brief The FileIO of an encrypted table: a plain FileIO paired with the table's +/// EncryptionManager. +/// +/// FileIO calls pass through to the plain FileIO unchanged, for files that are not +/// encrypted or that are encrypted natively by their format (Parquet). AES GCM Stream +/// files (manifests, Avro data files, Puffin files) are read through a per-file view, +/// ForKeyMetadata(), which any reader that takes (FileIO, path) can use, and written with +/// NewEncryptingOutputFile(), which returns the key metadata to record for the file. +/// +/// Mirrors Java's org.apache.iceberg.encryption.EncryptingFileIO. +class ICEBERG_EXPORT EncryptingFileIO : public FileIO { + public: + EncryptingFileIO(std::shared_ptr io, std::shared_ptr em); + + /// \brief Return `io` itself when it is an EncryptingFileIO, or nullptr. + static EncryptingFileIO* From(const std::shared_ptr& io); + + /// \brief The FileIO to read a file whose `key_metadata` is given (manifests, and + /// AGS1-encrypted data files): `io` itself when key_metadata is empty, otherwise a + /// decrypting view. Fails if the file is encrypted but `io` has no encryption. + static Result> ForFile(const std::shared_ptr& io, + std::span key_metadata); + + const std::shared_ptr& io() const { return io_; } + const std::shared_ptr& encryption() const { return em_; } + + /// \brief A view whose NewInputFile() decrypts AES GCM Stream files with the given + /// serialized key metadata. + Result> ForKeyMetadata( + std::span key_metadata) const; + + /// \brief Wrap a new output file for encrypted writing. + /// + /// The returned EncryptedOutputFile carries the key metadata generated for the file, + /// which the caller records once writing is complete. Mirrors Java's + /// EncryptingFileIO.newEncryptingOutputFile. + Result> NewEncryptingOutputFile( + std::string file_location); + + Result> NewInputFile(std::string file_location) override; + Result> NewInputFile(std::string file_location, + size_t length) override; + Result> NewOutputFile(std::string file_location) override; + Result ReadFile(const std::string& file_location, + std::optional length) override; + Status WriteFile(const std::string& file_location, std::string_view content) override; + Status DeleteFile(const std::string& file_location) override; + Status DeleteFiles(const std::vector& file_locations) override; + SupportsStorageCredentials* AsSupportsStorageCredentials() override; + + private: + std::shared_ptr io_; + std::shared_ptr em_; +}; + +} // namespace iceberg diff --git a/src/iceberg/test/CMakeLists.txt b/src/iceberg/test/CMakeLists.txt index 125ba128a3..5d99327b7b 100644 --- a/src/iceberg/test/CMakeLists.txt +++ b/src/iceberg/test/CMakeLists.txt @@ -145,6 +145,7 @@ add_iceberg_test(expression_test add_iceberg_test(encryption_test SOURCES encryption_manager_test.cc + encrypting_file_io_test.cc kms_test.cc standard_key_metadata_test.cc) diff --git a/src/iceberg/test/encrypting_file_io_test.cc b/src/iceberg/test/encrypting_file_io_test.cc new file mode 100644 index 0000000000..7807cbfb61 --- /dev/null +++ b/src/iceberg/test/encrypting_file_io_test.cc @@ -0,0 +1,129 @@ +/* + * 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. + */ + +#include "iceberg/encryption/encrypting_file_io.h" + +#include + +#include "iceberg/encryption/encrypted_files.h" +#include "iceberg/test/matchers.h" +#include "iceberg/test/std_io.h" +#include "iceberg/test/temp_file_test_base.h" + +namespace iceberg { + +namespace { + +/// Records how files are opened; "encrypts" by passing data through unchanged. +class RecordingEncryptionManager : public EncryptionManager { + public: + using EncryptionManager::Decrypt; + using EncryptionManager::Encrypt; + + Result> Decrypt( + std::unique_ptr encrypted) override { + decrypted_with = encrypted->key_metadata().buffer(); + return std::move(*encrypted).DetachInputFile(); + } + + Result> Encrypt( + std::unique_ptr plain) override { + ++encrypt_calls; + return EncryptedFiles::EncryptedOutput( + std::move(plain), + std::make_shared(std::vector{9, 9})); + } + + std::optional> decrypted_with; + int encrypt_calls = 0; +}; + +} // namespace + +class EncryptingFileIOTest : public TempFileTestBase { + protected: + void SetUp() override { + TempFileTestBase::SetUp(); + plain_io_ = std::make_shared(); + em_ = std::make_shared(); + io_ = std::make_shared(plain_io_, em_); + } + + std::shared_ptr plain_io_; + std::shared_ptr em_; + std::shared_ptr io_; +}; + +TEST_F(EncryptingFileIOTest, PassesPlainCallsThrough) { + auto path = CreateNewTempFilePath(); + ASSERT_THAT(io_->WriteFile(path, "hello"), IsOk()); + ICEBERG_UNWRAP_OR_FAIL(auto content, io_->ReadFile(path, std::nullopt)); + EXPECT_EQ(content, "hello"); + EXPECT_FALSE(em_->decrypted_with.has_value()); + EXPECT_EQ(em_->encrypt_calls, 0); + ASSERT_THAT(io_->DeleteFile(path), IsOk()); +} + +TEST_F(EncryptingFileIOTest, ForFile) { + // Files without key metadata use the FileIO as is + ICEBERG_UNWRAP_OR_FAIL(auto same, EncryptingFileIO::ForFile(io_, {})); + EXPECT_EQ(same, io_); + + auto path = CreateNewTempFilePath(); + ASSERT_THAT(io_->WriteFile(path, "data"), IsOk()); + std::vector key_metadata{1, 2, 3}; + ICEBERG_UNWRAP_OR_FAIL(auto view, EncryptingFileIO::ForFile(io_, key_metadata)); + ICEBERG_UNWRAP_OR_FAIL(auto content, view->ReadFile(path, std::nullopt)); + EXPECT_EQ(content, "data"); + EXPECT_EQ(em_->decrypted_with, key_metadata); + + // An encrypted file cannot be read without an encryption manager + EXPECT_THAT(EncryptingFileIO::ForFile(plain_io_, key_metadata), + IsError(ErrorKind::kNotSupported)); +} + +TEST_F(EncryptingFileIOTest, ForKeyMetadataRejectsEmptyAndCannotWrite) { + auto* encrypting_io = EncryptingFileIO::From(io_); + ASSERT_NE(encrypting_io, nullptr); + EXPECT_EQ(EncryptingFileIO::From(plain_io_), nullptr); + EXPECT_THAT(encrypting_io->ForKeyMetadata({}), IsError(ErrorKind::kInvalidArgument)); + + std::vector key_metadata{4, 5}; + ICEBERG_UNWRAP_OR_FAIL(auto view, encrypting_io->ForKeyMetadata(key_metadata)); + EXPECT_THAT(view->NewOutputFile(CreateNewTempFilePath()), + IsError(ErrorKind::kNotSupported)); +} + +TEST_F(EncryptingFileIOTest, NewEncryptingOutputFile) { + auto* encrypting_io = EncryptingFileIO::From(io_); + ASSERT_NE(encrypting_io, nullptr); + + auto path = CreateNewTempFilePath(); + ICEBERG_UNWRAP_OR_FAIL(auto encrypted, encrypting_io->NewEncryptingOutputFile(path)); + EXPECT_EQ(em_->encrypt_calls, 1); + EXPECT_EQ(encrypted->encrypting_output_file().location(), path); + EXPECT_EQ(encrypted->key_metadata().buffer(), (std::vector{9, 9})); +} + +TEST_F(EncryptingFileIOTest, DoesNotNest) { + auto nested = std::make_shared(io_, em_); + EXPECT_EQ(nested->io(), plain_io_); +} + +} // namespace iceberg diff --git a/src/iceberg/test/standard_encryption_manager_test.cc b/src/iceberg/test/standard_encryption_manager_test.cc index 1453051a24..7dba3d93c5 100644 --- a/src/iceberg/test/standard_encryption_manager_test.cc +++ b/src/iceberg/test/standard_encryption_manager_test.cc @@ -31,11 +31,15 @@ #include #include +#include +#include #include +#include "iceberg/arrow/arrow_io_internal.h" #include "iceberg/arrow/arrow_io_util.h" #include "iceberg/encryption/encrypted_file.h" #include "iceberg/encryption/encrypted_files.h" +#include "iceberg/encryption/encrypting_file_io.h" #include "iceberg/encryption/encryption_register.h" #include "iceberg/encryption/encryption_util.h" #include "iceberg/encryption/in_memory_kms.h" @@ -486,6 +490,44 @@ TEST(StandardEncryptionManagerTest, RegistersOpaqueNativeMetadataWithoutFileLeng HasValue(::testing::Eq(metadata.buffer()))); } +TEST_F(StandardEncryptionManagerFileTest, EncryptingFileIOWritesAndReadsAesGcmStreams) { + std::shared_ptr local_io(arrow::MakeLocalFileIO()); + auto io = std::make_shared(local_io, manager_); + + auto path = CreateNewTempFilePath(); + std::string content(3 * 1024 * 1024 / 2, 'x'); + ICEBERG_UNWRAP_OR_FAIL(auto encrypted, io->NewEncryptingOutputFile(path)); + ICEBERG_UNWRAP_OR_FAIL(auto stream, encrypted->encrypting_output_file().Create()); + ASSERT_THAT(stream->Write(std::as_bytes(std::span(content))), IsOk()); + ASSERT_THAT(stream->Close(), IsOk()); + ICEBERG_UNWRAP_OR_FAIL(auto stored, stream->StoredLength()); + EXPECT_EQ(stored, static_cast(std::filesystem::file_size(path))); + + // Reading needs the stored length in the key metadata + auto* native = + dynamic_cast(&encrypted->key_metadata()); + ASSERT_NE(native, nullptr); + ICEBERG_UNWRAP_OR_FAIL(auto with_length, native->CopyWithLength(stored)); + ICEBERG_UNWRAP_OR_FAIL(auto read_view, + EncryptingFileIO::ForFile(io, with_length->buffer())); + ICEBERG_UNWRAP_OR_FAIL(auto read, read_view->ReadFile(path, std::nullopt)); + EXPECT_EQ(read, content); + + // Through the Arrow adapter used by the Avro and Parquet readers + ICEBERG_UNWRAP_OR_FAIL(auto arrow_file, + arrow::OpenArrowInputStream(read_view, path, stored)); + EXPECT_EQ(arrow_file->GetSize().ValueOrDie(), static_cast(content.size())); + auto buffer = arrow_file->ReadAt(1024 * 1024 - 2, 4).ValueOrDie(); + EXPECT_EQ(buffer->ToString(), "xxxx"); + + // Plain files of an encrypted table go straight to the Arrow file system + auto plain_path = CreateNewTempFilePath(); + ASSERT_THAT(io->WriteFile(plain_path, "plain"), IsOk()); + ICEBERG_UNWRAP_OR_FAIL(auto plain_file, + arrow::OpenArrowInputStream(io, plain_path, std::nullopt)); + EXPECT_EQ(plain_file->GetSize().ValueOrDie(), 5); +} + class EncryptionUtilTest : public ::testing::Test { protected: void TearDown() override { From 2a3103c716fc6dbb67621a1f70eeb0712f91dfba Mon Sep 17 00:00:00 2001 From: shangxinli Date: Thu, 3 Dec 2026 13:51:14 -0800 Subject: [PATCH 2/2] ci: build with ICEBERG_ENCRYPTION in cpp-linter so encryption sources get a compile database --- .github/workflows/cpp-linter.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cpp-linter.yml b/.github/workflows/cpp-linter.yml index 07a33701f9..9f82e07f01 100644 --- a/.github/workflows/cpp-linter.yml +++ b/.github/workflows/cpp-linter.yml @@ -90,7 +90,8 @@ jobs: -DICEBERG_SQL_SQLITE=ON \ -DICEBERG_SQL_POSTGRESQL=ON \ -DICEBERG_SQL_MYSQL=ON \ - -DICEBERG_BUILD_HIVE=ON + -DICEBERG_BUILD_HIVE=ON \ + -DICEBERG_ENCRYPTION=ON cmake --build . - name: Save sccache if: always()