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
5 changes: 0 additions & 5 deletions components/keyrings/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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 All @@ -51,10 +50,6 @@ IF(COMPONENT_COMPILE_VISIBILITY)
SET(COMPILE_OPTIONS_ARG COMPILE_OPTIONS "${COMPONENT_COMPILE_VISIBILITY}")
ENDIF()

INCLUDE_DIRECTORIES(SYSTEM
${BOOST_PATCHES_DIR}
${BOOST_INCLUDE_DIR})

ADD_CONVENIENCE_LIBRARY(
keyring_common
${KEYRING_COMMON_SOURCES}
Expand Down
4 changes: 0 additions & 4 deletions components/keyrings/common/data/pfs_string.cpp

This file was deleted.

99 changes: 2 additions & 97 deletions components/keyrings/common/data/pfs_string.h
Original file line number Diff line number Diff line change
@@ -1,103 +1,8 @@

#ifndef PFS_STRING_INCLUDED
#define PFS_STRING_INCLUDED

#include <limits>
#include "my_sys.h"
#include "mysql/service_mysql_alloc.h"
#include "sql/psi_memory_key.h"

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 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 Malloc_allocator() {}

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

template <class U>
Malloc_allocator &operator=(const 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 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) {
return a1.psi_key() == a2.psi_key();
}

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

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

#endif // PFS_STRING_INCLUDED
3 changes: 3 additions & 0 deletions components/keyrings/keyring_file/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ MYSQL_ADD_COMPONENT(keyring_file
LINK_LIBRARIES ${KEYRING_FILE_LIBRARIES}
MODULE_ONLY
)

MY_TARGET_LINK_OPTIONS(component_keyring_file "${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 @@ -83,6 +83,9 @@ MYSQL_ADD_COMPONENT(keyring_kmip
LINK_LIBRARIES ${KEYRING_KMIP_LIBRARIES}
MODULE_ONLY
)

MY_TARGET_LINK_OPTIONS(component_keyring_kmip "${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 ${SSL_LIBRARIES})

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

MY_TARGET_LINK_OPTIONS(component_keyring_kms "${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
1 change: 0 additions & 1 deletion components/test/keyring_encryption_test/options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <my_inttypes.h> /* typedefs */
#include <my_macros.h> /* STRINGIFY_ARG */
#include <mysql.h> /* MYSQL */
#include <mysql/service_mysql_alloc.h>/* my_strdup */
#include <mysql_com.h> /* get_tty_password */
#include <print_version.h> /* print_version */
#include <typelib.h> /* find_type_or_exit */
Expand Down
4 changes: 1 addition & 3 deletions include/my_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,7 @@ extern size_t my_fwrite(FILE *stream, const uchar *Buffer, size_t Count,
myf MyFlags);
extern my_off_t my_fseek(FILE *stream, my_off_t pos, int whence);
extern my_off_t my_ftell(FILE *stream);
#if !defined(HAVE_MEMSET_S)
void memset_s(void *dest, size_t dest_max, int c, size_t n);
#endif
void my_memset_s(void *dest, size_t dest_max, int c, size_t n);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-identifier-length ⚠️
parameter name c is too short, expected at least 2 characters


/* implemented in my_syslog.c */

Expand Down
6 changes: 6 additions & 0 deletions mysql-test/include/have_keyring_file_plugin.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# Check if the variable KEYRING_PLUGIN is set
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-diagnostic-error
invalid preprocessing directive

#
if (!$KEYRING_PLUGIN) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-diagnostic-error
expected unqualified-id

--skip keyring_file not available.
}
6 changes: 0 additions & 6 deletions mysql-test/include/have_keyring_kmip_plugin.inc

This file was deleted.

27 changes: 27 additions & 0 deletions mysql-test/include/keyring_tests/mats/dynamic_loading.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# ==== Purpose ====
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-diagnostic-error
invalid preprocessing directive

#
# Check if the provided library ('.so') can be successfully loaded with 'dlopen(..., RTLD_NOW)'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-diagnostic-error
invalid preprocessing directive

#
# ==== Usage ====
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-diagnostic-error
invalid preprocessing directive

#
# --let $DLOPEN_CHECKER_LIBRARY_PATH = <path_to_the_library>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-diagnostic-error
invalid preprocessing directive

# --source include/keyring_tests/mats/dynamic_loading.inc
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-diagnostic-error
invalid preprocessing directive

#
# ==== Parameters ====
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-diagnostic-error
invalid preprocessing directive

#
# DLOPEN_CHECKER_LIBRARY_PATH
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-diagnostic-error
invalid preprocessing directive

# Full path to the library that needs to be checked for unresolved symbols ('.so')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-diagnostic-error
invalid preprocessing directive

#

--let $dlopen_checker_source = $MYSQL_TEST_DIR/std_data/dlopen_checker.cpp
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-diagnostic-error
expected unqualified-id

--let $dlopen_checker_binary = $MYSQL_TMP_DIR/dlopen_checker

--echo *** Building dlopen_checker utility
--exec g++ -std=c++17 -ldl -o $dlopen_checker_binary $dlopen_checker_source

--echo *** Checking for unresolved symbols
--replace_result $DLOPEN_CHECKER_LIBRARY_PATH <LIBRARY_PATH>
--exec $dlopen_checker_binary $DLOPEN_CHECKER_LIBRARY_PATH

--echo *** Deleting dlopen_checker utility
--remove_file $dlopen_checker_binary
27 changes: 27 additions & 0 deletions mysql-test/std_data/dlopen_checker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <cstdlib>
#include <iostream>
#include <memory>
#include <utility>

#include <dlfcn.h>

int main(int argc, char **argv) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <path_to_library.so>\n";
return 1;
}

const char *lib_path = argv[1];
auto dl_closer{[](void *dl_handle) {
if (dl_handle != nullptr) dlclose(dl_handle);
}};
using handle_guard = std::unique_ptr<void, decltype(dl_closer)>;
handle_guard handle{dlopen(lib_path, RTLD_NOW), std::move(dl_closer)};
if (!handle) {
std::cerr << "dlopen() failed: " << dlerror() << '\n';
return EXIT_FAILURE;
}

std::cout << "dlopen() succeeded: " << lib_path << '\n';
return EXIT_SUCCESS;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*** Building dlopen_checker utility
*** Checking for unresolved symbols
dlopen() succeeded: <LIBRARY_PATH>
*** Deleting dlopen_checker utility
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--source include/have_component_keyring_file.inc

--let $DLOPEN_CHECKER_LIBRARY_PATH = $KEYRING_FILE_COMPONENT_DIR/$KEYRING_FILE_COMPONENT
--source include/keyring_tests/mats/dynamic_loading.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*** Building dlopen_checker utility
*** Checking for unresolved symbols
dlopen() succeeded: <LIBRARY_PATH>
*** Deleting dlopen_checker utility
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--source include/have_component_keyring_file.inc

--let $DLOPEN_CHECKER_LIBRARY_PATH = $KEYRING_KMIP_COMPONENT_DIR/$KEYRING_KMIP_COMPONENT
--source include/keyring_tests/mats/dynamic_loading.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*** Building dlopen_checker utility
*** Checking for unresolved symbols
dlopen() succeeded: <LIBRARY_PATH>
*** Deleting dlopen_checker utility
4 changes: 4 additions & 0 deletions mysql-test/suite/component_keyring_kms/t/dynamic_loading.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--source include/have_component_keyring_file.inc

--let $DLOPEN_CHECKER_LIBRARY_PATH = $KEYRING_KMS_COMPONENT_DIR/$KEYRING_KMS_COMPONENT
--source include/keyring_tests/mats/dynamic_loading.inc
Loading