ClickHouse/src/Server/CertificateReloader.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
3.4 KiB
C++
Raw Normal View History

#pragma once
#include "config.h"
2021-07-09 02:27:01 +00:00
#if USE_SSL
#include <string>
2021-07-09 02:27:01 +00:00
#include <filesystem>
2024-05-22 10:28:40 +00:00
#include <list>
#include <unordered_map>
#include <mutex>
#include <Poco/Logger.h>
#include <Poco/Util/AbstractConfiguration.h>
2021-07-09 02:27:01 +00:00
#include <openssl/ssl.h>
#include <openssl/x509v3.h>
#include <Poco/Crypto/RSAKey.h>
#include <Poco/Crypto/X509Certificate.h>
#include <Common/MultiVersion.h>
2024-01-23 17:04:50 +00:00
#include <Common/Logger.h>
namespace DB
{
2021-07-06 23:15:30 +00:00
/// The CertificateReloader singleton performs 2 functions:
2021-07-10 03:01:24 +00:00
/// 1. Dynamic reloading of TLS key-pair when requested by server:
/// Server config reloader notifies CertificateReloader when the config changes.
/// On changed config, CertificateReloader reloads certs from disk.
/// 2. Implement `SSL_CTX_set_cert_cb` to set certificate for a new connection:
2021-07-10 03:01:24 +00:00
/// OpenSSL invokes a callback to setup a connection.
2021-07-06 23:15:30 +00:00
class CertificateReloader
{
public:
using stat_t = struct stat;
2024-05-22 10:28:40 +00:00
struct Data
{
Poco::Crypto::X509Certificate::List certs_chain;
Poco::Crypto::EVPPKey key;
Data(std::string cert_path, std::string key_path, std::string pass_phrase);
};
struct File
{
const char * description;
explicit File(const char * description_) : description(description_) {}
std::string path;
std::filesystem::file_time_type modification_time;
bool changeIfModified(std::string new_path, LoggerPtr logger);
};
2024-05-22 10:28:40 +00:00
struct MultiData
{
2024-06-18 09:20:50 +00:00
SSL_CTX * ctx = nullptr;
2024-05-22 10:28:40 +00:00
MultiVersion<Data> data;
bool init_was_not_made = true;
File cert_file{"certificate"};
File key_file{"key"};
explicit MultiData(SSL_CTX * ctx_) : ctx(ctx_) {}
2024-05-22 10:28:40 +00:00
};
/// Singleton
2021-07-06 23:15:30 +00:00
CertificateReloader(CertificateReloader const &) = delete;
void operator=(CertificateReloader const &) = delete;
static CertificateReloader & instance()
{
2021-07-06 23:15:30 +00:00
static CertificateReloader instance;
return instance;
}
2024-05-22 10:28:40 +00:00
/// Handle configuration reload for default path
void tryLoad(const Poco::Util::AbstractConfiguration & config);
/// Handle configuration reload client for default path
void tryLoadClient(const Poco::Util::AbstractConfiguration & config);
/// Handle configuration reload
2024-05-22 10:28:40 +00:00
void tryLoad(const Poco::Util::AbstractConfiguration & config, SSL_CTX * ctx, const std::string & prefix);
/// Handle configuration reload for all contexts
void tryReloadAll(const Poco::Util::AbstractConfiguration & config);
2021-07-10 03:01:24 +00:00
/// A callback for OpenSSL
2024-05-22 10:28:40 +00:00
int setCertificate(SSL * ssl, const MultiData * pdata);
private:
2022-04-21 12:26:30 +00:00
CertificateReloader() = default;
2024-05-22 10:28:40 +00:00
/// Initialize the callback and perform the initial cert loading
void init(MultiData * pdata) TSA_REQUIRES(data_mutex);
2024-05-22 10:28:40 +00:00
/// Unsafe implementation
void tryLoadImpl(const Poco::Util::AbstractConfiguration & config, SSL_CTX * ctx, const std::string & prefix) TSA_REQUIRES(data_mutex);
std::list<MultiData>::iterator findOrInsert(SSL_CTX * ctx, const std::string & prefix) TSA_REQUIRES(data_mutex);
LoggerPtr log = getLogger("CertificateReloader");
2021-07-09 02:27:01 +00:00
std::list<MultiData> data TSA_GUARDED_BY(data_mutex);
std::unordered_map<std::string, std::list<MultiData>::iterator> data_index TSA_GUARDED_BY(data_mutex);
mutable std::mutex data_mutex;
};
/// A callback for OpenSSL
int setCertificateCallback(SSL * ssl, const CertificateReloader::Data * current_data, LoggerPtr log);
2021-07-09 02:27:01 +00:00
}
#endif