aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Server/TLSHandler.h
blob: d4d7584d1210bdee7db203de3df7990eb7f260a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once

#include <Poco/Net/TCPServerConnection.h>
#include <Poco/SharedPtr.h>
#include <Common/Exception.h>
#include <Server/TCPProtocolStackData.h>

#if USE_SSL
#   include <Poco/Net/Context.h>
#   include <Poco/Net/SecureStreamSocket.h>
#   include <Poco/Net/SSLManager.h>
#endif

namespace DB
{

namespace ErrorCodes
{
    extern const int SUPPORT_IS_DISABLED;
}

class TLSHandler : public Poco::Net::TCPServerConnection
{
#if USE_SSL
    using SecureStreamSocket = Poco::Net::SecureStreamSocket;
    using SSLManager = Poco::Net::SSLManager;
    using Context = Poco::Net::Context;
#endif
    using StreamSocket = Poco::Net::StreamSocket;
public:
    explicit TLSHandler(const StreamSocket & socket, const std::string & key_, const std::string & certificate_, TCPProtocolStackData & stack_data_)
        : Poco::Net::TCPServerConnection(socket)
        , key(key_)
        , certificate(certificate_)
        , stack_data(stack_data_)
    {}

    void run() override
    {
#if USE_SSL
        auto ctx = SSLManager::instance().defaultServerContext();
        // if (!key.empty() && !certificate.empty())
        //     ctx = new Context(Context::Usage::SERVER_USE, key, certificate, ctx->getCAPaths().caLocation);
        socket() = SecureStreamSocket::attach(socket(), ctx);
        stack_data.socket = socket();
        stack_data.certificate = certificate;
#else
        throw Exception(ErrorCodes::SUPPORT_IS_DISABLED, "SSL support for TCP protocol is disabled because Poco library was built without NetSSL support.");
#endif
    }
private:
    std::string key [[maybe_unused]];
    std::string certificate [[maybe_unused]];
    TCPProtocolStackData & stack_data [[maybe_unused]];
};


}