Skip to content
Open
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
3 changes: 2 additions & 1 deletion .github/workflows/cpp-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions src/iceberg/arrow/arrow_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -481,6 +482,13 @@ class ArrowOutputFile : public OutputFile {
std::string path_;
};

const std::shared_ptr<FileIO>& PlainFileIO(const std::shared_ptr<FileIO>& io) {
if (auto* encrypting_io = EncryptingFileIO::From(io)) {
return encrypting_io->io();
}
return io;
}

} // namespace

Result<std::string> ArrowFileSystemFileIO::ResolvePath(const std::string& file_location) {
Expand All @@ -507,9 +515,12 @@ Result<std::string> ArrowFileSystemFileIO::ResolvePath(const std::string& file_l
}

Result<std::shared_ptr<::arrow::io::RandomAccessFile>> OpenArrowInputStream(
const std::shared_ptr<FileIO>& io, const std::string& path,
const std::shared_ptr<FileIO>& file_io, const std::string& path,
std::optional<size_t> 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<ArrowFileSystemFileIO>(io)) {
ICEBERG_ASSIGN_OR_RAISE(auto resolved_path, arrow_io->ResolvePath(path));
Expand Down Expand Up @@ -539,8 +550,9 @@ Result<std::shared_ptr<::arrow::io::RandomAccessFile>> OpenArrowInputStream(
}

Result<std::shared_ptr<::arrow::io::OutputStream>> OpenArrowOutputStream(
const std::shared_ptr<FileIO>& io, const std::string& path, bool overwrite) {
ICEBERG_PRECHECK(io != nullptr, "FileIO cannot be null");
const std::shared_ptr<FileIO>& 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<ArrowFileSystemFileIO>(io)) {
ICEBERG_ASSIGN_OR_RAISE(auto resolved_path, arrow_io->ResolvePath(path));
Expand Down
151 changes: 151 additions & 0 deletions src/iceberg/encryption/encrypting_file_io.cc
Original file line number Diff line number Diff line change
@@ -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<FileIO> io, std::shared_ptr<EncryptionManager> em,
std::vector<uint8_t> key_metadata)
: io_(std::move(io)), em_(std::move(em)), key_metadata_(std::move(key_metadata)) {}

Result<std::unique_ptr<InputFile>> 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<std::unique_ptr<InputFile>> 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<std::unique_ptr<OutputFile>> 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<std::unique_ptr<InputFile>> Decrypt(std::unique_ptr<InputFile> raw) {
ICEBERG_ASSIGN_OR_RAISE(
auto encrypted,
EncryptedFiles::EncryptedInput(
std::move(raw), std::make_shared<BaseEncryptionKeyMetadata>(key_metadata_)));
return em_->Decrypt(std::move(encrypted));
}

std::shared_ptr<FileIO> io_;
std::shared_ptr<EncryptionManager> em_;
std::vector<uint8_t> key_metadata_;
};

} // namespace

EncryptingFileIO::EncryptingFileIO(std::shared_ptr<FileIO> io,
std::shared_ptr<EncryptionManager> 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<FileIO>& io) {
return dynamic_cast<EncryptingFileIO*>(io.get());
}

Result<std::shared_ptr<FileIO>> EncryptingFileIO::ForFile(
const std::shared_ptr<FileIO>& io, std::span<const uint8_t> 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<std::shared_ptr<FileIO>> EncryptingFileIO::ForKeyMetadata(
std::span<const uint8_t> key_metadata) const {
ICEBERG_PRECHECK(!key_metadata.empty(), "Key metadata cannot be empty");
return std::make_shared<DecryptingFileIO>(
io_, em_, std::vector<uint8_t>(key_metadata.begin(), key_metadata.end()));
}

Result<std::unique_ptr<EncryptedOutputFile>> 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<std::unique_ptr<InputFile>> EncryptingFileIO::NewInputFile(
std::string file_location) {
return io_->NewInputFile(std::move(file_location));
}

Result<std::unique_ptr<InputFile>> EncryptingFileIO::NewInputFile(
std::string file_location, size_t length) {
return io_->NewInputFile(std::move(file_location), length);
}

Result<std::unique_ptr<OutputFile>> EncryptingFileIO::NewOutputFile(
std::string file_location) {
return io_->NewOutputFile(std::move(file_location));
}

Result<std::string> EncryptingFileIO::ReadFile(const std::string& file_location,
std::optional<size_t> 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<std::string>& file_locations) {
return io_->DeleteFiles(file_locations);
}

SupportsStorageCredentials* EncryptingFileIO::AsSupportsStorageCredentials() {
return io_->AsSupportsStorageCredentials();
}

} // namespace iceberg
96 changes: 96 additions & 0 deletions src/iceberg/encryption/encrypting_file_io.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <vector>

#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<FileIO> io, std::shared_ptr<EncryptionManager> em);

/// \brief Return `io` itself when it is an EncryptingFileIO, or nullptr.
static EncryptingFileIO* From(const std::shared_ptr<FileIO>& 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<std::shared_ptr<FileIO>> ForFile(const std::shared_ptr<FileIO>& io,
std::span<const uint8_t> key_metadata);

const std::shared_ptr<FileIO>& io() const { return io_; }
const std::shared_ptr<EncryptionManager>& encryption() const { return em_; }

/// \brief A view whose NewInputFile() decrypts AES GCM Stream files with the given
/// serialized key metadata.
Result<std::shared_ptr<FileIO>> ForKeyMetadata(
std::span<const uint8_t> 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<std::unique_ptr<EncryptedOutputFile>> NewEncryptingOutputFile(
std::string file_location);

Result<std::unique_ptr<InputFile>> NewInputFile(std::string file_location) override;
Result<std::unique_ptr<InputFile>> NewInputFile(std::string file_location,
size_t length) override;
Result<std::unique_ptr<OutputFile>> NewOutputFile(std::string file_location) override;
Result<std::string> ReadFile(const std::string& file_location,
std::optional<size_t> 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<std::string>& file_locations) override;
SupportsStorageCredentials* AsSupportsStorageCredentials() override;

private:
std::shared_ptr<FileIO> io_;
std::shared_ptr<EncryptionManager> em_;
};

} // namespace iceberg
1 change: 1 addition & 0 deletions src/iceberg/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading
Loading