ClickHouse

Форк
0
/
SchemaAllowedHandler.cpp 
109 строк · 3.5 Кб
1
#include "SchemaAllowedHandler.h"
2

3
#if USE_ODBC
4

5
#include <Server/HTTP/HTMLForm.h>
6
#include <Server/HTTP/WriteBufferFromHTTPServerResponse.h>
7
#include <IO/ReadHelpers.h>
8
#include <IO/WriteHelpers.h>
9
#include <Poco/Net/HTTPServerRequest.h>
10
#include <Poco/Net/HTTPServerResponse.h>
11
#include <Common/BridgeProtocolVersion.h>
12
#include <Common/logger_useful.h>
13
#include "validateODBCConnectionString.h"
14
#include "ODBCPooledConnectionFactory.h"
15
#include <sql.h>
16
#include <sqlext.h>
17

18

19
namespace DB
20
{
21
namespace
22
{
23
    bool isSchemaAllowed(nanodbc::ConnectionHolderPtr connection_holder)
24
    {
25
        uint32_t result = execute<uint32_t>(connection_holder,
26
                    [&](nanodbc::connection & connection) { return connection.get_info<uint32_t>(SQL_SCHEMA_USAGE); });
27
        return result != 0;
28
    }
29
}
30

31

32
void SchemaAllowedHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response, const ProfileEvents::Event & /*write_event*/)
33
{
34
    HTMLForm params(getContext()->getSettingsRef(), request, request.getStream());
35
    LOG_TRACE(log, "Request URI: {}", request.getURI());
36

37
    auto process_error = [&response, this](const std::string & message)
38
    {
39
        response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
40
        if (!response.sent())
41
            *response.send() << message << '\n';
42
        LOG_WARNING(log, fmt::runtime(message));
43
    };
44

45
    size_t version;
46

47
    if (!params.has("version"))
48
        version = 0; /// assumed version for too old servers which do not send a version
49
    else
50
    {
51
        String version_str = params.get("version");
52
        if (!tryParse(version, version_str))
53
        {
54
            process_error("Unable to parse 'version' string in request URL: '" + version_str + "' Check if the server and library-bridge have the same version.");
55
            return;
56
        }
57
    }
58

59
    if (version != XDBC_BRIDGE_PROTOCOL_VERSION)
60
    {
61
        /// backwards compatibility is considered unnecessary for now, just let the user know that the server and the bridge must be upgraded together
62
        process_error("Server and library-bridge have different versions: '" + std::to_string(version) + "' vs. '" + std::to_string(LIBRARY_BRIDGE_PROTOCOL_VERSION) + "'");
63
        return;
64
    }
65

66

67
    if (!params.has("connection_string"))
68
    {
69
        process_error("No 'connection_string' in request URL");
70
        return;
71
    }
72

73
     bool use_connection_pooling = params.getParsed<bool>("use_connection_pooling", true);
74

75
    try
76
    {
77
        std::string connection_string = params.get("connection_string");
78

79
        nanodbc::ConnectionHolderPtr connection;
80

81
        if (use_connection_pooling)
82
            connection = ODBCPooledConnectionFactory::instance().get(
83
                validateODBCConnectionString(connection_string), getContext()->getSettingsRef().odbc_bridge_connection_pool_size);
84
        else
85
            connection = std::make_shared<nanodbc::ConnectionHolder>(validateODBCConnectionString(connection_string));
86

87
        bool result = isSchemaAllowed(std::move(connection));
88

89
        WriteBufferFromHTTPServerResponse out(response, request.getMethod() == Poco::Net::HTTPRequest::HTTP_HEAD, keep_alive_timeout);
90
        try
91
        {
92
            writeBoolText(result, out);
93
            out.finalize();
94
        }
95
        catch (...)
96
        {
97
            out.finalize();
98
        }
99
    }
100
    catch (...)
101
    {
102
        process_error("Error getting schema usage from ODBC '" + getCurrentExceptionMessage(false) + "'");
103
        tryLogCurrentException(log);
104
    }
105
}
106

107
}
108

109
#endif
110

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.