forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.cpp
More file actions
1728 lines (1447 loc) · 58 KB
/
Copy pathConnection.cpp
File metadata and controls
1728 lines (1447 loc) · 58 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstddef>
#include <memory>
#include <Poco/Net/NetException.h>
#include <Core/Defines.h>
#include <Core/Settings.h>
#include <Compression/CompressedReadBuffer.h>
#include <Compression/CompressedWriteBuffer.h>
#include <IO/LimitReadBuffer.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
#include <IO/copyData.h>
#include <IO/TimeoutSetter.h>
#include <Formats/NativeReader.h>
#include <Formats/NativeWriter.h>
#include <Client/ClientApplicationBase.h>
#include <Client/Connection.h>
#include <Client/ConnectionParameters.h>
#include <Client/sanitizeUntrustedServerString.h>
#include <Common/logger_useful.h>
#include <Common/ClickHouseRevision.h>
#include <Common/Exception.h>
#include <Common/NetException.h>
#include <Common/CurrentMetrics.h>
#include <Common/DNSResolver.h>
#include <Common/StringUtils.h>
#include <Common/OpenSSLHelpers.h>
#include <Common/formatReadable.h>
#include <Common/randomSeed.h>
#include <Core/Block.h>
#include <Core/ProtocolDefines.h>
#include <Interpreters/ClientInfo.h>
#include <Interpreters/OpenTelemetrySpanLog.h>
#include <Interpreters/ClusterFunctionReadTask.h>
#include <Compression/CompressionFactory.h>
#include <QueryPipeline/Pipe.h>
#include <QueryPipeline/QueryPipelineBuilder.h>
#include <Processors/ISink.h>
#include <Processors/Executors/PipelineExecutor.h>
#include <Processors/QueryPlan/QueryPlan.h>
#include <Common/FailPoint.h>
#include <Client/JWTProvider.h>
#include <Common/config_version.h>
#include <Core/Types.h>
#include "config.h"
#include <base/scope_guard.h>
#include <fmt/ranges.h>
#if USE_SSL
# include <Poco/Net/SecureStreamSocket.h>
#endif
namespace CurrentMetrics
{
extern const Metric SendScalars;
extern const Metric SendExternalTables;
}
namespace ProfileEvents
{
extern const Event DistributedConnectionReconnectCount;
extern const Event DistributedConnectionConnectCount;
}
namespace DB
{
namespace Setting
{
extern const SettingsBool allow_experimental_codecs;
extern const SettingsBool allow_suspicious_codecs;
extern const SettingsString network_compression_method;
extern const SettingsInt64 network_zstd_compression_level;
}
namespace FailPoints
{
extern const char receive_timeout_on_table_status_response[];
extern const char unexpected_packet_in_table_status_response[];
}
namespace ErrorCodes
{
extern const int NETWORK_ERROR;
extern const int SOCKET_TIMEOUT;
extern const int UNEXPECTED_PACKET_FROM_SERVER;
extern const int UNKNOWN_PACKET_FROM_SERVER;
extern const int SUPPORT_IS_DISABLED;
extern const int BAD_ARGUMENTS;
extern const int EMPTY_DATA_PASSED;
extern const int LOGICAL_ERROR;
extern const int TOO_LARGE_ARRAY_SIZE;
}
Connection::~Connection()
{
if (Connection::isConnected())
Connection::disconnect();
}
Connection::Connection(const String & host_, UInt16 port_,
const String & default_database_,
const String & user_, const String & password_,
const String & proto_send_chunked_, const String & proto_recv_chunked_,
[[maybe_unused]] const SSHKey & ssh_private_key_,
[[maybe_unused]] const String & jwt_,
const String & quota_key_,
const String & cluster_,
const String & cluster_secret_,
const String & client_name_,
Protocol::Compression compression_,
Protocol::Secure secure_,
const String & tls_sni_override_,
const String & bind_host_
#if USE_JWT_CPP && USE_SSL
, std::shared_ptr<JWTProvider> jwt_provider_
#endif
)
: host(host_), port(port_), default_database(default_database_)
, user(user_), password(password_)
, proto_send_chunked(proto_send_chunked_), proto_recv_chunked(proto_recv_chunked_)
#if USE_SSH
, ssh_private_key(ssh_private_key_)
#endif
, quota_key(quota_key_)
#if USE_JWT_CPP && USE_SSL
, jwt(jwt_)
, jwt_provider(jwt_provider_)
#endif
, cluster(cluster_)
, cluster_secret(cluster_secret_)
, client_name(client_name_)
, compression(compression_)
, secure(secure_)
, tls_sni_override(tls_sni_override_)
, bind_host(bind_host_)
, log_wrapper(*this)
{
/// Don't connect immediately, only on first need.
if (user.empty())
user = "default";
setDescription();
}
void Connection::connect(const ConnectionTimeouts & timeouts)
{
/// if connection was broken it is necessary to cancel it before reconnecting
disconnect();
ProfileEvents::increment(ProfileEvents::DistributedConnectionConnectCount);
try
{
LOG_TRACE(log_wrapper.get(), "Connecting. Database: {}. User: {}{}{}. Bind_Host: {}",
default_database.empty() ? "(not specified)" : default_database,
user,
static_cast<bool>(secure) ? ". Secure" : "",
static_cast<bool>(compression) ? "" : ". Uncompressed",
bind_host.empty() ? "(not specified)" : bind_host);
auto addresses = DNSResolver::instance().resolveAddressList(host, port);
const auto & connection_timeout = static_cast<bool>(secure) ? timeouts.secure_connection_timeout : timeouts.connection_timeout;
for (auto it = addresses.begin(); it != addresses.end();)
{
have_more_addresses_to_connect = it != std::prev(addresses.end());
LOG_TRACE(log_wrapper.get(), "Connecting to {}:{} (using address {}, {}/{})", host, port, it->toString(), std::distance(addresses.begin(), it) + 1, addresses.size());
if (isConnected())
disconnect();
if (static_cast<bool>(secure))
{
#if USE_SSL
socket = std::make_unique<Poco::Net::SecureStreamSocket>();
/// we resolve the ip when we open SecureStreamSocket, so to make Server Name Indication (SNI)
/// work we need to pass host name separately. It will be send into TLS Hello packet to let
/// the server know which host we want to talk with (single IP can process requests for multiple hosts using SNI).
static_cast<Poco::Net::SecureStreamSocket *>(socket.get())
->setPeerHostName(tls_sni_override.empty() ? host : tls_sni_override);
/// we want to postpone SSL handshake until first read or write operation
/// so any errors during negotiation would be properly processed
static_cast<Poco::Net::SecureStreamSocket*>(socket.get())->setLazyHandshake(true);
if (!bind_host.empty())
{
Poco::Net::SocketAddress socket_address(bind_host, 0);
static_cast<Poco::Net::SecureStreamSocket*>(socket.get())->bind(socket_address, true);
}
#else
throw Exception(ErrorCodes::SUPPORT_IS_DISABLED, "tcp_secure protocol is disabled because poco library was built without NetSSL support.");
#endif
}
else
{
socket = std::make_unique<Poco::Net::StreamSocket>();
if (!bind_host.empty())
{
Poco::Net::SocketAddress socket_address(bind_host, 0);
static_cast<Poco::Net::StreamSocket *>(socket.get())->bind(socket_address, true);
}
}
try
{
if (async_callback)
{
address_connect_timeout_expired = false;
socket->connectNB(*it);
while (!socket->poll(0, Poco::Net::Socket::SELECT_READ | Poco::Net::Socket::SELECT_WRITE | Poco::Net::Socket::SELECT_ERROR))
{
async_callback(socket->impl()->sockfd(), connection_timeout, AsyncEventTimeoutType::CONNECT, description, AsyncTaskExecutor::READ | AsyncTaskExecutor::WRITE | AsyncTaskExecutor::ERROR);
if (address_connect_timeout_expired)
throw Poco::TimeoutException("Connection timeout expired for address: " + it->toString());
}
if (auto err = socket->impl()->socketError())
socket->impl()->error(err); // Throws an exception /// NOLINT(readability-static-accessed-through-instance)
socket->setBlocking(true);
}
else
{
socket->connect(*it, connection_timeout);
}
current_resolved_address = *it;
have_more_addresses_to_connect = false;
break;
}
catch (DB::NetException & e)
{
LOG_TRACE(log_wrapper.get(), "Failed to connect to {}:{}, address: {}, error: {}", host, port, it->toString(), e.displayText());
if (++it == addresses.end())
throw;
continue;
}
catch (Poco::Net::NetException & e)
{
LOG_TRACE(log_wrapper.get(), "Failed to connect to {}:{}, address: {}, error: {}", host, port, it->toString(), e.displayText());
if (++it == addresses.end())
throw;
continue;
}
catch (Poco::TimeoutException & e)
{
LOG_TRACE(log_wrapper.get(), "Failed to connect to {}:{}, address: {}, error: {}", host, port, it->toString(), e.displayText());
if (++it == addresses.end())
throw;
continue;
}
}
/// Use handshake timeout as send and receive timeout. Note that in the case of secure sockets,
/// these timeouts also apply to the TLS handshake. The TLS handshake is deferred until the
/// first send/receive operation (sendHello in our case).
socket->setReceiveTimeout(timeouts.handshake_timeout);
socket->setSendTimeout(timeouts.handshake_timeout);
socket->setNoDelay(true);
int tcp_keep_alive_timeout_in_sec = timeouts.tcp_keep_alive_timeout.totalSeconds();
if (tcp_keep_alive_timeout_in_sec)
{
socket->setKeepAlive(true);
#if defined(TCP_KEEPIDLE)
// TCP_KEEPIDLE: Linux, illumos
// Note: Check TCP_KEEPIDLE first because illumos defines TCP_KEEPALIVE
// but doesn't implement it.
socket->setOption(IPPROTO_TCP, TCP_KEEPIDLE, tcp_keep_alive_timeout_in_sec);
#elif defined(TCP_KEEPALIVE)
// TCP_KEEPALIVE: macOS
socket->setOption(IPPROTO_TCP, TCP_KEEPALIVE, tcp_keep_alive_timeout_in_sec);
#endif
}
in = std::make_shared<ReadBufferFromPocoSocketChunked>(*socket);
in->setAsyncCallback(async_callback);
out = std::make_shared<WriteBufferFromPocoSocketChunked>(*socket);
out->setAsyncCallback(async_callback);
connected = true;
setDescription();
sendHello();
receiveHello();
if (server_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS)
{
/// Client side of chunked protocol negotiation.
/// Server advertises its protocol capabilities (separate for send and receive channels) by sending
/// in its 'Hello' response one of four types - chunked, notchunked, chunked_optional, notchunked_optional.
/// Not optional types are strict meaning that server only supports this type, optional means that
/// server prefer this type but capable to work in opposite.
/// Client selects which type it is going to communicate based on the settings from config or arguments,
/// and sends either "chunked" or "notchunked" protocol request in addendum section of handshake.
/// Client can detect if server's protocol capabilities are not compatible with client's settings (for example
/// server strictly requires chunked protocol but client's settings only allows notchunked protocol) - in such case
/// client should interrupt this connection. However if client continues with incompatible protocol type request, server
/// will send appropriate exception and disconnect client.
auto is_chunked = [](const String & chunked_srv_str, const String & chunked_cl_str, const String & direction)
{
bool chunked_srv = chunked_srv_str.starts_with("chunked");
bool optional_srv = chunked_srv_str.ends_with("_optional");
bool chunked_cl = chunked_cl_str.starts_with("chunked");
bool optional_cl = chunked_cl_str.ends_with("_optional");
if (optional_srv)
return chunked_cl;
if (optional_cl)
return chunked_srv;
if (chunked_cl != chunked_srv)
throw NetException(
ErrorCodes::NETWORK_ERROR,
"Incompatible protocol: {} set to {}, server requires {}",
direction,
chunked_cl ? "chunked" : "notchunked",
chunked_srv ? "chunked" : "notchunked");
return chunked_srv;
};
proto_send_chunked = is_chunked(proto_recv_chunked_srv, proto_send_chunked, "send") ? "chunked" : "notchunked";
proto_recv_chunked = is_chunked(proto_send_chunked_srv, proto_recv_chunked, "recv") ? "chunked" : "notchunked";
}
else
{
if (proto_send_chunked == "chunked" || proto_recv_chunked == "chunked")
throw NetException(
ErrorCodes::NETWORK_ERROR,
"Incompatible protocol: server's version is too old and doesn't support chunked protocol while client settings require it.");
}
if (server_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_ADDENDUM)
sendAddendum();
if (proto_send_chunked == "chunked")
out->enableChunked();
if (proto_recv_chunked == "chunked")
in->enableChunked();
LOG_TRACE(log_wrapper.get(), "Connected to {} server version {}.{}.{}.",
server_name, server_version_major, server_version_minor, server_version_patch);
/// Now that the handshake is complete, use the regular timeouts
socket->setReceiveTimeout(timeouts.receive_timeout);
socket->setSendTimeout(timeouts.send_timeout);
}
catch (DB::NetException & e)
{
disconnect();
/// Remove this possible stale entry from cache
DNSResolver::instance().removeHostFromCache(host);
/// Add server address to exception. Exception will preserve stack trace.
e.addMessage("({})", getDescription(/*with_extra*/ true));
throw;
}
catch (Poco::Net::NetException & e)
{
disconnect();
/// Remove this possible stale entry from cache
DNSResolver::instance().removeHostFromCache(host);
/// Add server address to exception. Also Exception will remember new stack trace. It's a pity that more precise exception type is lost.
throw NetException(ErrorCodes::NETWORK_ERROR, "{} ({})", e.displayText(), getDescription(/*with_extra*/ true));
}
catch (Poco::TimeoutException & e)
{
disconnect();
/// Remove this possible stale entry from cache
DNSResolver::instance().removeHostFromCache(host);
/// Add server address to exception. Also Exception will remember new stack trace. It's a pity that more precise exception type is lost.
/// This exception can only be thrown from socket->connect(), so add information about connection timeout.
const auto & connection_timeout = static_cast<bool>(secure) ? timeouts.secure_connection_timeout : timeouts.connection_timeout;
throw NetException(
ErrorCodes::SOCKET_TIMEOUT,
"{} ({}, connection timeout {} ms)",
e.displayText(),
getDescription(/*with_extra*/ true),
connection_timeout.totalMilliseconds());
}
catch (...)
{
disconnect();
throw;
}
}
void Connection::cancel() noexcept
{
if (maybe_compressed_out)
maybe_compressed_out->cancel();
if (out)
out->cancel();
if (socket)
socket->close();
reset();
}
void Connection::reset() noexcept
{
maybe_compressed_out.reset();
out.reset();
socket.reset();
nonce.reset();
connected = false;
}
void Connection::disconnect()
{
in.reset();
last_input_packet_type.reset();
// no point to finalize tcp connections
cancel();
}
void Connection::sendHello()
{
/** Disallow control characters in user controlled parameters
* to mitigate the possibility of SSRF.
* The user may do server side requests with 'remote' table function.
* Malicious user with full r/w access to ClickHouse
* may use 'remote' table function to forge requests
* to another services in the network other than ClickHouse (examples: SMTP).
* Limiting number of possible characters in user-controlled part of handshake
* will mitigate this possibility but doesn't solve it completely.
*/
auto has_control_character = [](const std::string & s)
{
for (auto c : s)
if (isControlASCII(c))
return true;
return false;
};
if (has_control_character(default_database)
|| has_control_character(user)
|| has_control_character(password))
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Parameters 'default_database', 'user' and 'password' must not contain ASCII control characters");
writeVarUInt(Protocol::Client::Hello, *out);
writeStringBinary(std::string(VERSION_NAME) + " " + client_name, *out);
writeVarUInt(VERSION_MAJOR, *out);
writeVarUInt(VERSION_MINOR, *out);
// NOTE For backward compatibility of the protocol, client cannot send its version_patch.
writeVarUInt(DBMS_TCP_PROTOCOL_VERSION, *out);
writeStringBinary(default_database, *out);
/// If interserver-secret is used, one do not need password
/// (NOTE we do not check for DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET, since we cannot ignore inter-server secret if it was requested)
if (!cluster_secret.empty())
{
writeStringBinary(EncodedUserInfo::USER_INTERSERVER_MARKER, *out);
writeStringBinary("" /* password */, *out);
#if USE_SSL
sendClusterNameAndSalt();
#else
throw Exception(ErrorCodes::SUPPORT_IS_DISABLED,
"Inter-server secret support is disabled, because ClickHouse was built without SSL library");
#endif
}
#if USE_SSH
else if (!ssh_private_key.isEmpty())
{
/// Inform server that we will authenticate using SSH keys.
writeStringBinary(String(EncodedUserInfo::SSH_KEY_AUTHENTICAION_MARKER) + user, *out);
writeStringBinary(password, *out);
performHandshakeForSSHAuth();
}
#endif
#if USE_JWT_CPP && USE_SSL
else if (!jwt.empty())
{
writeStringBinary(EncodedUserInfo::JWT_AUTHENTICAION_MARKER, *out);
writeStringBinary(jwt, *out);
}
#endif
else
{
writeStringBinary(user, *out);
writeStringBinary(password, *out);
}
out->next();
}
void Connection::sendAddendum()
{
if (server_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_QUOTA_KEY)
writeStringBinary(quota_key, *out);
if (server_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS)
{
writeStringBinary(proto_send_chunked, *out);
writeStringBinary(proto_recv_chunked, *out);
}
if (server_revision >= DBMS_MIN_REVISION_WITH_VERSIONED_PARALLEL_REPLICAS_PROTOCOL)
writeVarUInt(DBMS_PARALLEL_REPLICAS_PROTOCOL_VERSION, *out);
out->next();
}
#if USE_SSH
void Connection::performHandshakeForSSHAuth()
{
String challenge;
{
writeVarUInt(Protocol::Client::SSHChallengeRequest, *out);
out->next();
UInt64 packet_type = 0;
if (in->eof())
throw Poco::Net::NetException("Connection reset by peer");
readVarUInt(packet_type, *in);
if (packet_type == Protocol::Server::SSHChallenge)
{
readStringBinary(challenge, *in);
}
else if (packet_type == Protocol::Server::Exception)
receiveException()->rethrow();
else
throwUnexpectedPacket(packet_type, "SSHChallenge or Exception");
}
writeVarUInt(Protocol::Client::SSHChallengeResponse, *out);
auto pack_string_for_ssh_sign = [&](String challenge_)
{
String message;
message.append(std::to_string(DBMS_TCP_PROTOCOL_VERSION));
message.append(default_database);
message.append(user);
message.append(challenge_);
return message;
};
String to_sign = pack_string_for_ssh_sign(challenge);
String signature = ssh_private_key.signString(to_sign);
writeStringBinary(signature, *out);
out->next();
}
#endif
void Connection::receiveHello()
{
/// Receive hello packet.
UInt64 packet_type = 0;
/// Prevent read after eof in readVarUInt in case of reset connection
/// (Poco should throw such exception while reading from socket but
/// sometimes it doesn't for unknown reason)
if (in->eof())
throw Poco::Net::NetException("Connection reset by peer");
readVarUInt(packet_type, *in);
if (packet_type == Protocol::Server::Hello)
{
readStringBinary(server_name, *in, DBMS_MAX_HELLO_STRING_SIZE);
sanitizeUntrustedServerString(server_name);
readVarUInt(server_version_major, *in);
readVarUInt(server_version_minor, *in);
readVarUInt(server_revision, *in);
if (server_revision >= DBMS_MIN_REVISION_WITH_VERSIONED_PARALLEL_REPLICAS_PROTOCOL)
readVarUInt(server_parallel_replicas_protocol_version, *in);
if (server_revision >= DBMS_MIN_REVISION_WITH_SERVER_TIMEZONE)
{
readStringBinary(server_timezone, *in, DBMS_MAX_HELLO_STRING_SIZE);
sanitizeUntrustedServerString(server_timezone);
}
if (server_revision >= DBMS_MIN_REVISION_WITH_SERVER_DISPLAY_NAME)
{
readStringBinary(server_display_name, *in, DBMS_MAX_HELLO_STRING_SIZE);
sanitizeUntrustedServerString(server_display_name);
}
if (server_revision >= DBMS_MIN_REVISION_WITH_VERSION_PATCH)
readVarUInt(server_version_patch, *in);
else
server_version_patch = server_revision;
if (server_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_CHUNKED_PACKETS)
{
/// These are tiny protocol tokens ("chunked" / "notchunked") compared in
/// `is_chunked`; cap them so a hostile server cannot force a large allocation.
readStringBinary(proto_send_chunked_srv, *in, DBMS_MAX_HELLO_STRING_SIZE);
readStringBinary(proto_recv_chunked_srv, *in, DBMS_MAX_HELLO_STRING_SIZE);
}
if (server_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_PASSWORD_COMPLEXITY_RULES)
{
UInt64 rules_size = 0;
readVarUInt(rules_size, *in);
/// `rules_size` is server-controlled and feeds a `reserve`; reject absurd
/// values so a hostile server cannot force a huge allocation. The server
/// enforces the same cap at construction time (see TCPHandler::sendHello).
if (rules_size > DBMS_MAX_PASSWORD_COMPLEXITY_RULES)
throw Exception(ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER,
"Server declared {} password-complexity rules, maximum allowed is {}",
rules_size, DBMS_MAX_PASSWORD_COMPLEXITY_RULES);
password_complexity_rules.reserve(rules_size);
for (size_t i = 0; i < rules_size; ++i)
{
String original_pattern;
String exception_message;
readStringBinary(original_pattern, *in, DBMS_MAX_HELLO_STRING_SIZE);
readStringBinary(exception_message, *in, DBMS_MAX_HELLO_STRING_SIZE);
sanitizeUntrustedServerString(original_pattern);
sanitizeUntrustedServerString(exception_message);
password_complexity_rules.push_back({std::move(original_pattern), std::move(exception_message)});
}
}
if (server_revision >= DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET_V2)
{
chassert(!nonce.has_value());
UInt64 read_nonce = 0;
readIntBinary(read_nonce, *in);
nonce.emplace(read_nonce);
}
if (server_revision >= DBMS_MIN_REVISION_WITH_SERVER_SETTINGS)
{
Settings settings;
settings.read(*in, SettingsWriteFormat::STRINGS_WITH_FLAGS);
settings_from_server = settings.changes();
}
if (server_revision >= DBMS_MIN_REVISION_WITH_QUERY_PLAN_SERIALIZATION)
{
readVarUInt(server_query_plan_serialization_version, *in);
}
worker_cluster_function_protocol_version = DBMS_CLUSTER_INITIAL_PROCESSING_PROTOCOL_VERSION;
if (server_revision >= DBMS_MIN_REVISION_WITH_VERSIONED_CLUSTER_FUNCTION_PROTOCOL)
{
readVarUInt(worker_cluster_function_protocol_version, *in);
}
}
else if (packet_type == Protocol::Server::Exception)
receiveException()->rethrow();
else
throwUnexpectedPacket(packet_type, "Hello or Exception");
}
void Connection::setDefaultDatabase(const String & database)
{
default_database = database;
}
const String & Connection::getDefaultDatabase() const
{
return default_database;
}
const SettingsChanges & Connection::settingsFromServer() const
{
chassert(isConnected());
return settings_from_server;
}
const String & Connection::getDescription(bool with_extra) const /// NOLINT
{
if (with_extra)
return full_description;
return description;
}
const String & Connection::getHost() const
{
return host;
}
UInt16 Connection::getPort() const
{
return port;
}
void Connection::getServerVersion(const ConnectionTimeouts & timeouts,
String & name,
UInt64 & version_major,
UInt64 & version_minor,
UInt64 & version_patch,
UInt64 & revision)
{
if (!isConnected())
connect(timeouts);
name = server_name;
version_major = server_version_major;
version_minor = server_version_minor;
version_patch = server_version_patch;
revision = server_revision;
}
UInt64 Connection::getServerRevision(const ConnectionTimeouts & timeouts)
{
if (!isConnected())
connect(timeouts);
return server_revision;
}
const String & Connection::getServerTimezone(const ConnectionTimeouts & timeouts)
{
if (!isConnected())
connect(timeouts);
return server_timezone;
}
const String & Connection::getServerDisplayName(const ConnectionTimeouts & timeouts)
{
if (!isConnected())
connect(timeouts);
return server_display_name;
}
void Connection::forceConnected(const ConnectionTimeouts & timeouts)
{
if (!isConnected())
{
connect(timeouts);
}
else if (!ping(timeouts))
{
ProfileEvents::increment(ProfileEvents::DistributedConnectionReconnectCount);
LOG_TRACE(log_wrapper.get(), "Connection was closed, will reconnect.");
connect(timeouts);
}
}
#if USE_SSL
void Connection::sendClusterNameAndSalt()
{
pcg64_fast rng(randomSeed());
UInt64 rand = rng();
salt = encodeSHA256(&rand, sizeof(rand));
writeStringBinary(cluster, *out);
writeStringBinary(salt, *out);
}
#endif
bool Connection::ping(const ConnectionTimeouts & timeouts)
{
try
{
TimeoutSetter timeout_setter(*socket, timeouts.sync_request_timeout, true);
UInt64 pong = 0;
writeVarUInt(Protocol::Client::Ping, *out);
out->finishChunk();
out->next();
if (in->eof())
return false;
readVarUInt(pong, *in);
/// Could receive late packets with progress. TODO: Maybe possible to fix.
while (pong == Protocol::Server::Progress)
{
receiveProgress();
if (in->eof())
return false;
readVarUInt(pong, *in);
}
if (pong != Protocol::Server::Pong)
throwUnexpectedPacket(pong, "Pong", &timeout_setter);
}
catch (const Poco::Exception & e)
{
/// Explicitly disconnect since ping() can receive EndOfStream,
/// and in this case this ping() will return false,
/// while next ping() may return true.
disconnect();
LOG_TRACE(log_wrapper.get(), fmt::runtime(e.displayText()));
return false;
}
return true;
}
TablesStatusResponse Connection::getTablesStatus(const ConnectionTimeouts & timeouts,
const TablesStatusRequest & request)
{
if (!isConnected())
connect(timeouts);
fiu_do_on(FailPoints::receive_timeout_on_table_status_response, {
sleepForSeconds(5);
throw NetException(ErrorCodes::SOCKET_TIMEOUT, "Injected timeout exceeded while reading from socket ({}:{})", host, port);
});
/// Simulate a connection that was returned to the pool out of sync by a previous query, so that
/// reading the table status here sees a stale packet instead of the expected response.
fiu_do_on(FailPoints::unexpected_packet_in_table_status_response, {
throw NetException(
ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER,
"Injected unexpected packet while reading table status from {}:{}", host, port);
});
TimeoutSetter timeout_setter(*socket, timeouts.sync_request_timeout, true);
writeVarUInt(Protocol::Client::TablesStatusRequest, *out);
request.write(*out, server_revision);
out->finishChunk();
out->next();
UInt64 response_type = 0;
readVarUInt(response_type, *in);
if (response_type == Protocol::Server::Exception)
receiveException()->rethrow();
else if (response_type != Protocol::Server::TablesStatusResponse)
throwUnexpectedPacket(response_type, "TablesStatusResponse", &timeout_setter);
TablesStatusResponse response;
response.read(*in, server_revision);
return response;
}
void Connection::sendQuery(
const ConnectionTimeouts & timeouts,
const String & query,
const NameToNameMap & query_parameters,
const String & query_id_,
UInt64 stage,
const Settings * settings,
const ClientInfo * client_info,
bool with_pending_data,
const std::vector<String> & external_roles,
std::function<void(const Progress &)>)
{
OpenTelemetry::SpanHolder span("Connection::sendQuery()", OpenTelemetry::SpanKind::CLIENT);
span.addAttribute("clickhouse.query_id", query_id_);
span.addAttribute("clickhouse.query", query);
span.addAttribute("target", [this] () { return this->getHost() + ":" + std::to_string(this->getPort()); });
ClientInfo new_client_info;
const auto ¤t_trace_context = OpenTelemetry::CurrentContext();
if (client_info && current_trace_context.isTraceEnabled())
{
// use current span as the parent of remote span
new_client_info = *client_info;
new_client_info.client_trace_context = current_trace_context;
client_info = &new_client_info;
}
#if USE_JWT_CPP && USE_SSL
if (jwt_provider && !jwt.empty())
{
if (JWTProvider::getJwtExpiry(jwt) < (Poco::Timestamp() + Poco::Timespan(30, 0)))
{
String new_jwt = jwt_provider->getJWT();
if (!new_jwt.empty())
{
jwt = new_jwt;
// We have a new token, so we need to reconnect.
// The current connection is still using the old token.
disconnect();
}
}
}
#endif
if (!connected)
connect(timeouts);
/// Query is not executed within sendQuery() function.
///
/// And what this means that temporary timeout (via TimeoutSetter) is not
/// enough, since next query can use timeout from the previous query in this case.
socket->setReceiveTimeout(timeouts.receive_timeout);
socket->setSendTimeout(timeouts.send_timeout);
if (settings)
{
std::optional<int> level;
std::string method = Poco::toUpper((*settings)[Setting::network_compression_method].toString());
/// Bad custom logic
/// We only allow any of following generic codecs. CompressionCodecFactory will happily return other
/// codecs (e.g. T64) but these may be specialized and not support all data types, i.e. SELECT 'abc' may
/// be broken afterwards.
if (method != "NONE" && method != "ZSTD" && method != "LZ4" && method != "LZ4HC")
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Setting 'network_compression_method' must be NONE, ZSTD, LZ4 or LZ4HC");
/// More bad custom logic
if (method == "ZSTD")
level = (*settings)[Setting::network_zstd_compression_level];
CompressionCodecFactory::instance().validateCodec(
method,
level,
!(*settings)[Setting::allow_suspicious_codecs],
(*settings)[Setting::allow_experimental_codecs]);
compression_codec = CompressionCodecFactory::instance().get(method, level);
}
else
compression_codec = CompressionCodecFactory::instance().getDefaultCodec();
query_id = query_id_;
/// Avoid reusing connections that had been left in the intermediate state
/// (i.e. not all packets had been sent).
bool completed = false;
SCOPE_EXIT({
if (!completed)
disconnect();
});
writeVarUInt(Protocol::Client::Query, *out);
writeStringBinary(query_id, *out);
/// Client info.
if (server_revision >= DBMS_MIN_REVISION_WITH_CLIENT_INFO)
{
if (client_info && !client_info->empty())
client_info->write(*out, server_revision);
else
ClientInfo().write(*out, server_revision);
}
/// Per query settings.
if (settings)
{
std::optional<Settings> modified_settings;
const Settings * settings_to_send = settings;
if (!settings_from_server.empty())
{
/// Don't send settings that we got from the server in the first place.
modified_settings.emplace(*settings);
for (const SettingChange & change : settings_from_server)
{
Field value;
if (settings->tryGet(change.name, value) && value == change.value)
{
// Mark as unchanged so it's not sent.
modified_settings->setDefaultValue(change.name);
chassert(!modified_settings->isChanged(change.name));
}
}
settings_to_send = &*modified_settings;
}
auto settings_format = (server_revision >= DBMS_MIN_REVISION_WITH_SETTINGS_SERIALIZED_AS_STRINGS) ? SettingsWriteFormat::STRINGS_WITH_FLAGS
: SettingsWriteFormat::BINARY;
settings_to_send->write(*out, settings_format);
}
else
writeStringBinary("" /* empty string is a marker of the end of settings */, *out);
String external_roles_str;
if (server_revision >= DBMS_MIN_PROTOCOL_VERSION_WITH_INTERSERVER_EXTERNALLY_GRANTED_ROLES)
{
WriteBufferFromString buffer(external_roles_str);
writeVectorBinary(external_roles, buffer);
buffer.finalize();
LOG_TRACE(log_wrapper.get(), "Sending external_roles with query: [{}] ({})", fmt::join(external_roles, ", "), external_roles.size());
writeStringBinary(external_roles_str, *out);
}
/// Interserver secret
if (server_revision >= DBMS_MIN_REVISION_WITH_INTERSERVER_SECRET)
{