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
4 changes: 3 additions & 1 deletion .github/workflows/cpp-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ jobs:
-DICEBERG_SQL_SQLITE=ON \
-DICEBERG_SQL_POSTGRESQL=ON \
-DICEBERG_SQL_MYSQL=ON \
-DICEBERG_BUILD_HIVE=ON
-DICEBERG_BUILD_HIVE=ON \
-DICEBERG_ENCRYPTION=ON \
-DICEBERG_BUILD_KMS=ON
cmake --build .
- name: Save sccache
if: always()
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
matrix:
include:
- cmake_build_type: Debug
extra_cmake_args: "-DICEBERG_CXX20_COMPAT=ON -DICEBERG_ENCRYPTION=ON"
extra_cmake_args: "-DICEBERG_CXX20_COMPAT=ON -DICEBERG_ENCRYPTION=ON -DICEBERG_BUILD_KMS=ON"
api_mode: C++20 Compat
- cmake_build_type: RelWithDebInfo
extra_cmake_args: "-DICEBERG_ENCRYPTION=ON"
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ option(ICEBERG_BUILD_BUNDLE "Build the battery included library" ON)
option(ICEBERG_ENCRYPTION
"Build table encryption support (Parquet modular encryption, AES GCM streams)" OFF)
option(ICEBERG_BUILD_REST "Build rest catalog client" ON)
option(ICEBERG_BUILD_KMS
"Build the AWS KMS, Google Cloud KMS and Azure Key Vault clients (iceberg_kms)"
OFF)
option(ICEBERG_BUILD_REST_INTEGRATION_TESTS "Build rest catalog integration tests" OFF)
option(ICEBERG_BUILD_VERIFICATION_TESTS
"Build conformance tests over the apache/iceberg-verification fixtures" OFF)
Expand Down
7 changes: 6 additions & 1 deletion cmake_modules/IcebergThirdpartyToolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,12 @@ if(ICEBERG_BUILD_BUNDLE)
resolve_zstd_dependency()
endif()

if(ICEBERG_BUILD_REST)
# The KMS clients sign AWS requests with OpenSSL.
if(ICEBERG_BUILD_KMS AND NOT TARGET OpenSSL::Crypto)
resolve_openssl_for_encryption()
endif()

if(ICEBERG_BUILD_REST OR ICEBERG_BUILD_KMS)
resolve_cpr_dependency()
endif()

Expand Down
4 changes: 4 additions & 0 deletions src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ if(ICEBERG_BUILD_BUNDLE)
add_subdirectory(parquet)
endif()

if(ICEBERG_BUILD_KMS)
add_subdirectory(kms)
endif()

iceberg_install_cmake_package(iceberg iceberg_targets)

# When linking a system Thrift, downstream find_package(Iceberg) calls
Expand Down
3 changes: 3 additions & 0 deletions src/iceberg/cpp20_compatibility_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
#include "iceberg/inspect/metadata_table.h"
#include "iceberg/inspect/refs_table.h"
#include "iceberg/inspect/snapshots_table.h"
#include "iceberg/kms/iceberg_kms_export.h"
#include "iceberg/kms/kms_clients.h"
#include "iceberg/kms/kms_http_client.h"
#include "iceberg/labels.h"
#include "iceberg/location_provider.h"
#include "iceberg/logging/cerr_logger.h"
Expand Down
5 changes: 3 additions & 2 deletions src/iceberg/encryption/key_management_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ Result<std::shared_ptr<KeyManagementClient>> KmsRegistry::Load(
auto it = state.factories.find(std::string(name));
if (it == state.factories.end()) {
return NotFound(
"KMS client not found: {}. No built-in clients ship yet; register one with "
"KmsRegistry::Register and select it with encryption.kms-impl",
"KMS client not found: {}. Built-in clients must be enabled at build time "
"(ICEBERG_BUILD_KMS, library iceberg_kms); custom clients must be registered "
"with KmsRegistry::Register",
name);
}
factory = it->second;
Expand Down
58 changes: 58 additions & 0 deletions src/iceberg/kms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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.

set(ICEBERG_KMS_SOURCES aws_sigv4.cc kms_clients.cc kms_http_client.cc)

set(ICEBERG_KMS_STATIC_BUILD_INTERFACE_LIBS)
set(ICEBERG_KMS_SHARED_BUILD_INTERFACE_LIBS)
set(ICEBERG_KMS_STATIC_INSTALL_INTERFACE_LIBS)
set(ICEBERG_KMS_SHARED_INSTALL_INTERFACE_LIBS)

list(APPEND
ICEBERG_KMS_STATIC_BUILD_INTERFACE_LIBS
"$<IF:$<TARGET_EXISTS:iceberg_static>,iceberg_static,iceberg_shared>"
cpr::cpr
OpenSSL::Crypto)
list(APPEND
ICEBERG_KMS_SHARED_BUILD_INTERFACE_LIBS
"$<IF:$<TARGET_EXISTS:iceberg_shared>,iceberg_shared,iceberg_static>"
cpr::cpr
OpenSSL::Crypto)
list(APPEND
ICEBERG_KMS_STATIC_INSTALL_INTERFACE_LIBS
"$<IF:$<TARGET_EXISTS:iceberg::iceberg_static>,iceberg::iceberg_static,iceberg::iceberg_shared>"
"$<IF:$<BOOL:${CPR_VENDORED}>,iceberg::cpr,cpr::cpr>"
OpenSSL::Crypto)
list(APPEND
ICEBERG_KMS_SHARED_INSTALL_INTERFACE_LIBS
"$<IF:$<TARGET_EXISTS:iceberg::iceberg_shared>,iceberg::iceberg_shared,iceberg::iceberg_static>"
"$<IF:$<BOOL:${CPR_VENDORED}>,iceberg::cpr,cpr::cpr>"
OpenSSL::Crypto)

add_iceberg_lib(iceberg_kms
SOURCES
${ICEBERG_KMS_SOURCES}
SHARED_LINK_LIBS
${ICEBERG_KMS_SHARED_BUILD_INTERFACE_LIBS}
STATIC_LINK_LIBS
${ICEBERG_KMS_STATIC_BUILD_INTERFACE_LIBS}
STATIC_INSTALL_INTERFACE_LIBS
${ICEBERG_KMS_STATIC_INSTALL_INTERFACE_LIBS}
SHARED_INSTALL_INTERFACE_LIBS
${ICEBERG_KMS_SHARED_INSTALL_INTERFACE_LIBS})

iceberg_install_all_headers(iceberg/kms)
169 changes: 169 additions & 0 deletions src/iceberg/kms/aws_sigv4.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* 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 <algorithm>
#include <array>
#include <cctype>
#include <format>
#include <vector>

#include <openssl/evp.h>
#include <openssl/hmac.h>

#include "iceberg/kms/aws_sigv4_internal.h"
#include "iceberg/util/macros.h"

namespace iceberg::kms {

namespace {

std::string ToHex(const unsigned char* data, size_t length) {
static constexpr std::string_view kDigits = "0123456789abcdef";
std::string out;
out.reserve(length * 2);
for (size_t i = 0; i < length; ++i) {
out.push_back(kDigits[data[i] >> 4]);
out.push_back(kDigits[data[i] & 0xF]);
}
return out;
}

Result<std::string> HmacSha256(std::string_view key, std::string_view data) {
std::array<unsigned char, EVP_MAX_MD_SIZE> out{};
unsigned int length = 0;
if (HMAC(EVP_sha256(), key.data(), static_cast<int>(key.size()),
reinterpret_cast<const unsigned char*>(data.data()), data.size(), out.data(),
&length) == nullptr) {
return IOError("HMAC-SHA256 failed");
}
return std::string(reinterpret_cast<const char*>(out.data()), length);
}

std::string ToLower(std::string_view s) {
std::string out(s);
std::ranges::transform(out, out.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
return out;
}

std::string Trim(std::string_view s) {
auto begin = s.find_first_not_of(" \t");
if (begin == std::string_view::npos) {
return "";
}
auto end = s.find_last_not_of(" \t");
return std::string(s.substr(begin, end - begin + 1));
}

struct UrlParts {
std::string host;
std::string path;
std::string query;
};

Result<UrlParts> ParseUrl(std::string_view url) {
auto scheme_end = url.find("://");
if (scheme_end == std::string_view::npos) {
return InvalidArgument("Invalid URL: {}", url);
}
auto rest = url.substr(scheme_end + 3);
auto path_start = rest.find('/');
UrlParts parts;
parts.host = std::string(rest.substr(0, path_start));
auto path_and_query = path_start == std::string_view::npos ? std::string_view("/")
: rest.substr(path_start);
auto query_start = path_and_query.find('?');
parts.path = std::string(path_and_query.substr(0, query_start));
if (query_start != std::string_view::npos) {
parts.query = std::string(path_and_query.substr(query_start + 1));
}
return parts;
}

} // namespace

Result<std::string> Sha256Hex(std::string_view data) {
std::array<unsigned char, EVP_MAX_MD_SIZE> digest{};
unsigned int length = 0;
if (EVP_Digest(data.data(), data.size(), digest.data(), &length, EVP_sha256(),
nullptr) != 1) {
return IOError("SHA-256 failed");
}
return ToHex(digest.data(), length);
}

Status SignAwsRequest(HttpRequest& request, std::string_view method,
std::string_view region, std::string_view service,
const AwsCredentials& credentials, std::string_view amz_date) {
ICEBERG_PRECHECK(amz_date.size() == 16, "Invalid AWS date: {}", amz_date);
ICEBERG_PRECHECK(
!credentials.access_key_id.empty() && !credentials.secret_access_key.empty(),
"AWS credentials are required to sign KMS requests");
ICEBERG_ASSIGN_OR_RAISE(auto url, ParseUrl(request.url));

request.headers["X-Amz-Date"] = std::string(amz_date);
if (!credentials.session_token.empty()) {
request.headers["X-Amz-Security-Token"] = credentials.session_token;
}

// Canonical headers: lower-case names, sorted, with Host
std::map<std::string, std::string> canonical;
canonical["host"] = url.host;
for (const auto& [name, value] : request.headers) {
canonical[ToLower(name)] = Trim(value);
}
std::string canonical_headers;
std::string signed_headers;
for (const auto& [name, value] : canonical) {
canonical_headers += name + ":" + value + "\n";
if (!signed_headers.empty()) {
signed_headers += ";";
}
signed_headers += name;
}

ICEBERG_ASSIGN_OR_RAISE(auto payload_hash, Sha256Hex(request.body));
// The KMS JSON API uses no query parameters; a query, if any, must already be in
// canonical (sorted, encoded) form.
std::string canonical_request =
std::format("{}\n{}\n{}\n{}\n{}\n{}", method, url.path, url.query,
canonical_headers, signed_headers, payload_hash);

std::string date(amz_date.substr(0, 8));
std::string scope = std::format("{}/{}/{}/aws4_request", date, region, service);
ICEBERG_ASSIGN_OR_RAISE(auto canonical_hash, Sha256Hex(canonical_request));
std::string string_to_sign =
std::format("AWS4-HMAC-SHA256\n{}\n{}\n{}", amz_date, scope, canonical_hash);

ICEBERG_ASSIGN_OR_RAISE(auto k_date,
HmacSha256("AWS4" + credentials.secret_access_key, date));
ICEBERG_ASSIGN_OR_RAISE(auto k_region, HmacSha256(k_date, region));
ICEBERG_ASSIGN_OR_RAISE(auto k_service, HmacSha256(k_region, service));
ICEBERG_ASSIGN_OR_RAISE(auto k_signing, HmacSha256(k_service, "aws4_request"));
ICEBERG_ASSIGN_OR_RAISE(auto signature, HmacSha256(k_signing, string_to_sign));

request.headers["Authorization"] = std::format(
"AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders={}, Signature={}",
credentials.access_key_id, scope, signed_headers,
ToHex(reinterpret_cast<const unsigned char*>(signature.data()), signature.size()));
return {};
}

} // namespace iceberg::kms
53 changes: 53 additions & 0 deletions src/iceberg/kms/aws_sigv4_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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/kms/aws_sigv4_internal.h
/// AWS Signature Version 4 signing of HTTP requests.

#include <map>
#include <string>
#include <string_view>

#include "iceberg/kms/kms_http_client.h"
#include "iceberg/result.h"

namespace iceberg::kms {

struct AwsCredentials {
std::string access_key_id;
std::string secret_access_key;
std::string session_token;
};

/// \brief Lower-case hex SHA-256 of `data`.
Result<std::string> Sha256Hex(std::string_view data);

/// \brief Sign `request` (method `method`) for AWS SigV4.
///
/// Adds the X-Amz-Date, X-Amz-Security-Token (if any) and Authorization headers. All
/// headers of the request, and Host (from the URL), are signed.
///
/// \param amz_date the request time, as yyyyMMdd'T'HHmmss'Z'
Status SignAwsRequest(HttpRequest& request, std::string_view method,
std::string_view region, std::string_view service,
const AwsCredentials& credentials, std::string_view amz_date);

} // namespace iceberg::kms
37 changes: 37 additions & 0 deletions src/iceberg/kms/iceberg_kms_export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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/kms/iceberg_kms_export.h
/// \brief Define symbol visibility macros for the cloud KMS client library.

#if defined(_WIN32) || defined(__CYGWIN__)
# ifdef ICEBERG_KMS_STATIC
# define ICEBERG_KMS_EXPORT
# elif defined(ICEBERG_KMS_EXPORTING)
# define ICEBERG_KMS_EXPORT __declspec(dllexport)
# else
# define ICEBERG_KMS_EXPORT __declspec(dllimport)
# endif
#else // Not Windows
# ifndef ICEBERG_KMS_EXPORT
# define ICEBERG_KMS_EXPORT __attribute__((visibility("default")))
# endif
#endif
Loading
Loading