Skip to content
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: 0 additions & 1 deletion components/keyrings/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ SET(KEYRING_COMMON_SOURCES
# Data representation
data/data.cc
data/meta.cc
data/pfs_string.cpp
# File reader/writer
data_file/reader.cc
data_file/writer.cc
Expand Down
50 changes: 0 additions & 50 deletions components/keyrings/common/data/keyring_alloc.h

This file was deleted.

4 changes: 0 additions & 4 deletions components/keyrings/common/data/pfs_string.cpp

This file was deleted.

104 changes: 3 additions & 101 deletions components/keyrings/common/data/pfs_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,14 @@
#ifndef PFS_STRING_INCLUDED
#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"
#include <string>

extern PSI_memory_key KEY_mem_keyring;

/**
Malloc_allocator is based on sql/malloc_allocator.h, but uses a fixed PSI key
instead
*/
template <class T = void *>
class Comp_malloc_allocator {
// This cannot be const if we want to be able to swap.
PSI_memory_key m_key = KEY_mem_keyring;

public:
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;

typedef T *pointer;
typedef const T *const_pointer;

typedef T &reference;
typedef const T &const_reference;

pointer address(reference r) const { return &r; }
const_pointer address(const_reference r) const { return &r; }

explicit Comp_malloc_allocator() {}

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

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

pointer allocate(size_type n, const_pointer hint [[maybe_unused]] = nullptr) {
if (n == 0) return nullptr;
if (n > max_size()) throw std::bad_alloc();

pointer p = static_cast<pointer>(
my_malloc(m_key, n * sizeof(T), MYF(MY_WME | ME_FATALERROR)));
if (p == nullptr) throw std::bad_alloc();
return p;
}

void deallocate(pointer p, size_type) { my_free(p); }

template <class U, class... Args>
void construct(U *p, Args &&... args) {
assert(p != nullptr);
try {
::new ((void *)p) U(std::forward<Args>(args)...);
} catch (...) {
assert(false); // Constructor should not throw an exception.
}
}

void destroy(pointer p) {
assert(p != nullptr);
try {
p->~T();
} catch (...) {
assert(false); // Destructor should not throw an exception
}
}

size_type max_size() const {
return std::numeric_limits<size_t>::max() / sizeof(T);
}

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

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

template <class T>
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 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>,
Comp_malloc_allocator<char>>;
using pfs_string = std::string;

using pfs_optional_string = std::optional<pfs_string>;

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

#endif // PFS_STRING_INCLUDED
2 changes: 2 additions & 0 deletions components/keyrings/keyring_file/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ MYSQL_ADD_COMPONENT(keyring_file
MODULE_ONLY
)

TARGET_LINK_OPTIONS(component_keyring_file PRIVATE "${LINK_FLAG_NO_UNDEFINED}")

IF(APPLE)
SET_TARGET_PROPERTIES(component_keyring_file PROPERTIES
LINK_FLAGS "-undefined dynamic_lookup")
Expand Down
3 changes: 3 additions & 0 deletions components/keyrings/keyring_kmip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ MYSQL_ADD_COMPONENT(keyring_kmip
LINK_LIBRARIES ${KEYRING_KMIP_LIBRARIES}
MODULE_ONLY
)

TARGET_LINK_OPTIONS(component_keyring_kmip PRIVATE "${LINK_FLAG_NO_UNDEFINED}")

IF(APPLE)
SET_TARGET_PROPERTIES(component_keyring_kmip PROPERTIES
LINK_FLAGS "-undefined dynamic_lookup")
Expand Down
36 changes: 25 additions & 11 deletions components/keyrings/keyring_kmip/backend/backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <memory>

#include "backend.h"
#include "my_dbug.h"

#include <mysql/components/minimal_chassis.h>

Expand All @@ -47,15 +46,13 @@ using keyring_common::utils::get_random_data;

Keyring_kmip_backend::Keyring_kmip_backend(config::Config_pod const &config)
: valid_(false), config_(config) {
DBUG_TRACE;
valid_ = true;
}

bool Keyring_kmip_backend::load_cache(
keyring_common::operations::Keyring_operations<
Keyring_kmip_backend, keyring_common::data::Data_extension<IdExt>>
&operations) {
DBUG_TRACE;
// We have to load keys and secrets with state==ACTIVE only
//TODO: implement better logic with the new KMIP library
try {
Expand Down Expand Up @@ -126,9 +123,16 @@ bool Keyring_kmip_backend::load_cache(
return true;
}
}

} catch (const std::exception &e) {
std::string err_msg = std::string("std exception in function '") +
__func__ + "': " + e.what();
LogComponentErr(ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str());
return true;
} catch (...) {
mysql_components_handle_std_exception(__func__);
std::string err_msg =
std::string("Unknown exception in function '") + __func__ + '\'';
LogComponentErr(ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str());
return true;
}

return false;
Expand All @@ -137,13 +141,11 @@ bool Keyring_kmip_backend::load_cache(
bool Keyring_kmip_backend::get(const Metadata &, Data &) const {
/* Shouldn't have reached here if we cache things. */
assert(0);
DBUG_TRACE;
return false;
}

bool Keyring_kmip_backend::store(const Metadata &metadata,
Data_extension<IdExt> &data) {
DBUG_TRACE;
if (!metadata.valid() || !data.valid()) return true;
kmippp::context::id_t id;
try {
Expand Down Expand Up @@ -184,8 +186,15 @@ bool Keyring_kmip_backend::store(const Metadata &metadata,
return true;
}
data.set_extension({id});
} catch (const std::exception &e) {
std::string err_msg = std::string("std exception in function '") +
__func__ + "': " + e.what();
LogComponentErr(ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str());
return true;
} catch (...) {
mysql_components_handle_std_exception(__func__);
std::string err_msg =
std::string("Unknown exception in function '") + __func__ + '\'';
LogComponentErr(ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str());
return true;
}
return false;
Expand All @@ -204,15 +213,21 @@ size_t Keyring_kmip_backend::size() const {
return keys.size() + secrets.size();
//we may have deactivated keys counted, so we need to count active keys only
//TODO: implement better logic with the new KMIP library
} catch (const std::exception &e) {
std::string err_msg = std::string("std exception in function '") +
__func__ + "': " + e.what();
LogComponentErr(ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str());
return 0;
} catch (...) {
mysql_components_handle_std_exception(__func__);
std::string err_msg =
std::string("Unknown exception in function '") + __func__ + '\'';
LogComponentErr(ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str());
return 0;
}
}

bool Keyring_kmip_backend::erase(const Metadata &metadata,
Data_extension<IdExt> &data) {
DBUG_TRACE;
if (!metadata.valid()) return true;

auto ctx = kmip_ctx();
Expand All @@ -238,7 +253,6 @@ bool Keyring_kmip_backend::erase(const Metadata &metadata,
bool Keyring_kmip_backend::generate(const Metadata &metadata,
Data_extension<IdExt> &data,
size_t length) {
DBUG_TRACE;
if (!metadata.valid()) return true;

std::unique_ptr<unsigned char[]> key(new unsigned char[length]);
Expand Down
2 changes: 0 additions & 2 deletions components/keyrings/keyring_kmip/keyring_kmip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,6 @@ PROVIDES_SERVICE(component_keyring_kmip, keyring_aes),
PROVIDES_SERVICE(component_keyring_kmip, log_builtins_string),
END_COMPONENT_PROVIDES();

PSI_memory_key KEY_mem_keyring_kmip;

/** List of dependencies */
BEGIN_COMPONENT_REQUIRES(component_keyring_kmip)
REQUIRES_SERVICE(registry), REQUIRES_SERVICE(log_builtins),
Expand Down
2 changes: 2 additions & 0 deletions components/keyrings/keyring_kms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ SET(KEYRING_KMS_LIBRARIES keyring_common ext::curl OpenSSL::SSL OpenSSL::Crypto)

MYSQL_ADD_COMPONENT(keyring_kms ${KEYRING_KMS_SOURCE} LINK_LIBRARIES ${KEYRING_KMS_LIBRARIES} MODULE_ONLY)

TARGET_LINK_OPTIONS(component_keyring_kms PRIVATE "${LINK_FLAG_NO_UNDEFINED}")

MY_CHECK_CXX_COMPILER_WARNING("-Wno-suggest-override" HAS_FLAG)
IF(HAS_FLAG)
TARGET_COMPILE_OPTIONS(component_keyring_kms PUBLIC "-Wno-suggest-override")
Expand Down
4 changes: 3 additions & 1 deletion components/keyrings/keyring_vault/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,16 @@ set(KEYRING_VAULT_SOURCE
component_callbacks.cc
)

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

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

TARGET_LINK_OPTIONS(component_keyring_vault PRIVATE "${LINK_FLAG_NO_UNDEFINED}")

target_compile_definitions(component_keyring_vault PRIVATE LOG_COMPONENT_TAG="component_keyring_vault")

target_include_directories(
Expand Down
12 changes: 9 additions & 3 deletions components/keyrings/keyring_vault/backend/backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,17 @@ bool Keyring_vault_backend::init() {
m_valid = true;

return false;
} catch (const std::exception &e) {
std::string err_msg = std::string("std exception in function '") +
__func__ + "': " + e.what();
LogComponentErr(ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str());
} catch (...) {
mysql_components_handle_std_exception(__func__);
curl_global_cleanup();
return true;
std::string err_msg =
std::string("Unknown exception in function '") + __func__ + '\'';
LogComponentErr(ERROR_LEVEL, ER_LOG_PRINTF_MSG, err_msg.c_str());
}
curl_global_cleanup();
return true;
}

bool Keyring_vault_backend::load_cache(
Expand Down
5 changes: 1 addition & 4 deletions components/keyrings/keyring_vault/backend/i_vault_curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ along with this program; if not, write to the Free Software
#define KEYRING_I_VAULT_CURL_INCLUDED

#include <components/keyrings/common/data/data.h>
#include <components/keyrings/common/data/keyring_alloc.h>
#include <components/keyrings/common/data/meta.h>
#include <components/keyrings/common/data/pfs_string.h>
#include <components/keyrings/keyring_vault/config/config.h>
Expand All @@ -29,13 +28,11 @@ along with this program; if not, write to the Free Software

namespace keyring_vault::backend {

using keyring_common::data::Comp_keyring_alloc;
using keyring_common::data::Data;
using keyring_common::meta::Metadata;
using keyring_vault::config::Vault_version_type;

class IKeyring_vault_curl : public Comp_keyring_alloc,
private boost::noncopyable {
class IKeyring_vault_curl : private boost::noncopyable {
public:
virtual bool init() = 0;
virtual bool list_keys(pfs_string *response) = 0;
Expand Down
Loading