Skip to content
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

Implement PXB-3155 - Move keyring vault to component #1500

Merged
merged 3 commits into from
Oct 16, 2023
Merged
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
1 change: 1 addition & 0 deletions components/keyrings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ ADD_SUBDIRECTORY(common)
ADD_SUBDIRECTORY(keyring_file)
ADD_SUBDIRECTORY(keyring_kmip)
ADD_SUBDIRECTORY(keyring_kms)
ADD_SUBDIRECTORY(keyring_vault)
14 changes: 14 additions & 0 deletions components/keyrings/common/config/config_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,19 @@ inline Config_reader::Config_reader(const std::string config_file_path)
file_stream.close();
}

bool Config_reader::has_element(const std::string &element_name) {
return !valid_ || !data_.HasMember(element_name);
}

bool Config_reader::is_number(const std::string &element_name) {
return !valid_ || !data_.HasMember(element_name) ||
!data_[element_name].IsNumber();
}

bool Config_reader::is_string(const std::string &element_name) {
return !valid_ || !data_.HasMember(element_name) ||
!data_[element_name].IsString();
}

} // namespace config
} // namespace keyring_common
34 changes: 34 additions & 0 deletions components/keyrings/common/config/config_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,40 @@ class Config_reader {
return false;
}

/**
Check if an element with the provided name exists.

@param [in] element_name Name of the element being checked

@returns status of operation
@retval false Element found.
@retval true Element is not found.
*/

bool has_element(const std::string &element_name);

/**
Check if an element value is of numeric type.

@param [in] element_name Name of the element being checked

@returns status of type check operation
@retval false Element found and it is of numeric type.
@retval true Element type is not a string or element is not found.
*/
bool is_number(const std::string &element_name);

/**
Check if an element value is of string type.

@param [in] element_name Name of the element being checked

@returns status of type check operation
@retval false Element found and it is of string type.
@retval true Element type is not a string or element is not found.
*/
bool is_string(const std::string &element_name);

private:
/** Configuration file path */
std::string config_file_path_;
Expand Down
50 changes: 50 additions & 0 deletions components/keyrings/common/data/keyring_alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright (c) 2023, Percona and/or its affiliates.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.

This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */

#ifndef KEYRING_ALLOC_INCLUDED
#define KEYRING_ALLOC_INCLUDED

#include "pfs_string.h"

namespace keyring_common::data {

template <class T>
T comp_keyring_malloc(size_t size) {
void *allocated_memory = my_malloc(KEY_mem_keyring, size, MYF(MY_WME));
return allocated_memory ? reinterpret_cast<T>(allocated_memory) : NULL;
}

class Comp_keyring_alloc {
public:
static void *operator new(size_t size) noexcept {
return comp_keyring_malloc<void *>(size);
}
static void *operator new[](size_t size) noexcept {
return comp_keyring_malloc<void *>(size);
}
static void operator delete(void *ptr, std::size_t) { my_free(ptr); }
static void operator delete[](void *ptr, std::size_t) { my_free(ptr); }
};

} // namespace keyring_common::data

#endif // KEYRING_ALLOC_INCLUDED
30 changes: 20 additions & 10 deletions components/keyrings/common/data/pfs_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#define PFS_STRING_INCLUDED

#include <limits>
#include <optional>
#include <sstream>
#include "my_sys.h"
#include "mysql/service_mysql_alloc.h"
#include "sql/psi_memory_key.h"
Expand All @@ -14,7 +16,7 @@ extern PSI_memory_key KEY_mem_keyring;
instead
*/
template <class T = void *>
class Malloc_allocator {
class Comp_malloc_allocator {
// This cannot be const if we want to be able to swap.
PSI_memory_key m_key = KEY_mem_keyring;

Expand All @@ -32,15 +34,15 @@ class Malloc_allocator {
pointer address(reference r) const { return &r; }
const_pointer address(const_reference r) const { return &r; }

explicit Malloc_allocator() {}
explicit Comp_malloc_allocator() {}

template <class U>
Malloc_allocator(const Malloc_allocator<U> &other [[maybe_unused]])
Comp_malloc_allocator(const Comp_malloc_allocator<U> &other [[maybe_unused]])
: m_key(other.psi_key()) {}

template <class U>
Malloc_allocator &operator=(const Malloc_allocator<U> &other
[[maybe_unused]]) {
Comp_malloc_allocator &operator=(const Comp_malloc_allocator<U> &other
[[maybe_unused]]) {
assert(m_key == other.psi_key()); // Don't swap key.
}

Expand Down Expand Up @@ -81,23 +83,31 @@ class Malloc_allocator {

template <class U>
struct rebind {
typedef Malloc_allocator<U> other;
typedef Comp_malloc_allocator<U> other;
};

PSI_memory_key psi_key() const { return m_key; }
};

template <class T>
bool operator==(const Malloc_allocator<T> &a1, const Malloc_allocator<T> &a2) {
bool operator==(const Comp_malloc_allocator<T> &a1,
const Comp_malloc_allocator<T> &a2) {
return a1.psi_key() == a2.psi_key();
}

template <class T>
bool operator!=(const Malloc_allocator<T> &a1, const Malloc_allocator<T> &a2) {
bool operator!=(const Comp_malloc_allocator<T> &a1,
const Comp_malloc_allocator<T> &a2) {
return a1.psi_key() != a2.psi_key();
}

using pfs_string =
std::basic_string<char, std::char_traits<char>, Malloc_allocator<char>>;
using pfs_string = std::basic_string<char, std::char_traits<char>,
Comp_malloc_allocator<char>>;

using pfs_optional_string = std::optional<pfs_string>;

using pfs_secure_ostringstream =
std::basic_ostringstream<char, std::char_traits<char>,
Comp_malloc_allocator<char>>;

#endif // PFS_STRING_INCLUDED
103 changes: 103 additions & 0 deletions components/keyrings/keyring_vault/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Copyright (c) 2023, Percona and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

# keyring_vault mtr suite is a default suite and must be installed always,
# regardless if the keyring_vault component is built and installed or not.
option(WITH_COMPONENT_KEYRING_VAULT "Building Keyring Vault Component" ON)
if(NOT WITH_COMPONENT_KEYRING_VAULT)
message(STATUS "Not building Keyring Vault Component")
return()
endif()

include(curl)
if(NOT DEFINED CURL_FOUND OR NOT CURL_FOUND)
message(FATAL_ERROR "Not building Keyring Vault Component, could not find CURL library")
return()
endif()

message(STATUS "Building Keyring Vault Component")

set(KEYRING_VAULT_SOURCE
# Component handling
keyring_vault.cc

# Config handling
config/config.cc

# Encryption handling
service_implementation/keyring_encryption_service_definition.cc

# Generator handling
service_implementation/keyring_generator_service_definition.cc

# Keyring load handling
service_implementation/keyring_load_service_definition.cc

# Keys metadata iterator handling
service_implementation/keyring_keys_metadata_iterator_service_definition.cc

# Metadata query handling
service_implementation/keyring_metadata_query_service_definition.cc

# Reader handling
service_implementation/keyring_reader_service_definition.cc

# Writer handling
service_implementation/keyring_writer_service_definition.cc

# Backend handling
backend/backend.cc
backend/vault_base64.cc
backend/vault_curl.cc
backend/vault_keys_container.cc
backend/vault_parser_composer.cc

# Component callbacks
component_callbacks.cc
)

set(KEYRING_VAULT_LIBRARIES keyring_common ext::curl extra::rapidjson)

MYSQL_ADD_COMPONENT(keyring_vault
${KEYRING_VAULT_SOURCE}
LINK_LIBRARIES ${KEYRING_VAULT_LIBRARIES}
MODULE_ONLY
)

target_compile_definitions(component_keyring_vault PRIVATE LOG_COMPONENT_TAG="component_keyring_vault")

target_include_directories(
component_keyring_vault
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)
target_include_directories(
component_keyring_vault
SYSTEM PRIVATE
${BOOST_PATCHES_DIR}
${BOOST_INCLUDE_DIR}
)

if(APPLE)
set_target_properties(component_keyring_vault PROPERTIES
LINK_FLAGS "-undefined dynamic_lookup")
endif()
Loading