Skip to content

Commit 2c62494

Browse files
committed
sesion cpp ssl
1 parent 664f485 commit 2c62494

File tree

13 files changed

+144
-38
lines changed

13 files changed

+144
-38
lines changed

.github/workflows/multi-language-client.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ jobs:
5656
run: |
5757
sudo apt-get update
5858
sudo apt-get install libboost-all-dev
59+
sudo apt-get install openssl libssl-dev
5960
- name: Install CPP Dependencies (Mac)
6061
# remove some xcode to release disk space
6162
if: runner.os == 'macOS'
6263
shell: bash
6364
run: |
6465
brew install boost
66+
brew install openssl
6567
sudo rm -rf /Applications/Xcode_14.3.1.app
6668
sudo rm -rf /Applications/Xcode_15.0.1.app
6769
sudo rm -rf /Applications/Xcode_15.1.app
@@ -74,6 +76,11 @@ jobs:
7476
choco install boost-msvc-14.3
7577
$boost_path = (Get-ChildItem -Path 'C:\local\' -Filter 'boost_*').FullName
7678
echo $boost_path >> $env:GITHUB_PATH
79+
80+
choco install openssl
81+
$sslPath = (Get-ChildItem 'C:\Program Files\OpenSSL*' -Directory | Select-Object -First 1).FullName
82+
echo "$sslPath\bin" >> $env:GITHUB_PATH
83+
echo "OPENSSL_ROOT_DIR=$sslPath" >> $env:GITHUB_ENV
7784
- name: Cache Maven packages
7885
uses: actions/cache@v4
7986
with:

iotdb-client/client-cpp/src/main/AbstractSessionBuilder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class AbstractSessionBuilder {
3939
bool enableRedirections = true;
4040
bool enableRPCCompression = false;
4141
std::vector<std::string> nodeUrls;
42+
bool useSSL = false;
43+
std::string trustCertFilePath;
4244
};
4345

4446
#endif // IOTDB_ABSTRACTSESSIONBUILDER_H

iotdb-client/client-cpp/src/main/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g -O2 ")
2626
# Add Thrift include directory
2727
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/../../thrift/include)
2828

29+
# Find OpenSSL Library
30+
FIND_PACKAGE(OpenSSL REQUIRED)
31+
IF(OpenSSL_FOUND)
32+
MESSAGE(STATUS "OpenSSL found: ${OPENSSL_VERSION}")
33+
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
34+
ELSE()
35+
MESSAGE(FATAL_ERROR "OpenSSL not found")
36+
ENDIF()
37+
2938
# Add Boost include path for MacOS
3039
INCLUDE_DIRECTORIES(/usr/local/include)
3140

@@ -55,4 +64,4 @@ ELSE()
5564
ENDIF()
5665

5766
# Link with Thrift static library
58-
TARGET_LINK_LIBRARIES(iotdb_session ${THRIFT_STATIC_LIB})
67+
TARGET_LINK_LIBRARIES(iotdb_session ${THRIFT_STATIC_LIB} ssl crypto)

iotdb-client/client-cpp/src/main/NodesSupplier.cpp

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,57 @@ std::vector<TEndPoint> StaticNodesSupplier::getEndPointList() {
6868
StaticNodesSupplier::~StaticNodesSupplier() = default;
6969

7070
std::shared_ptr<NodesSupplier> NodesSupplier::create(
71-
std::vector<TEndPoint> endpoints,
72-
std::string userName, std::string password, std::string zoneId,
73-
int32_t thriftDefaultBufferSize, int32_t thriftMaxFrameSize,
74-
int32_t connectionTimeoutInMs, bool useSSL, bool enableRPCCompression,
75-
std::string version, std::chrono::milliseconds refreshInterval,
71+
const std::vector<TEndPoint>& endpoints,
72+
const std::string& userName,
73+
const std::string& password,
74+
bool useSSL,
75+
const std::string& trustCertFilePath,
76+
const std::string& zoneId,
77+
int32_t thriftDefaultBufferSize,
78+
int32_t thriftMaxFrameSize,
79+
int32_t connectionTimeoutInMs,
80+
bool enableRPCCompression,
81+
const std::string& version,
82+
std::chrono::milliseconds refreshInterval,
7683
NodeSelectionPolicy policy) {
7784
if (endpoints.empty()) {
7885
return nullptr;
7986
}
8087
auto supplier = std::make_shared<NodesSupplier>(
81-
userName, password, zoneId, thriftDefaultBufferSize,
82-
thriftMaxFrameSize, connectionTimeoutInMs, useSSL,
83-
enableRPCCompression, version, std::move(endpoints), std::move(policy)
88+
userName, password, useSSL, trustCertFilePath, zoneId,
89+
thriftDefaultBufferSize, thriftMaxFrameSize, connectionTimeoutInMs,
90+
enableRPCCompression,
91+
version, endpoints, policy
8492
);
8593
supplier->startBackgroundRefresh(refreshInterval);
8694
return supplier;
8795
}
8896

8997
NodesSupplier::NodesSupplier(
90-
std::string userName, std::string password, const std::string& zoneId,
91-
int32_t thriftDefaultBufferSize, int32_t thriftMaxFrameSize,
92-
int32_t connectionTimeoutInMs, bool useSSL, bool enableRPCCompression,
93-
std::string version, std::vector<TEndPoint> endpoints, NodeSelectionPolicy policy) : userName_(std::move(userName)), password_(std::move(password)), zoneId_(zoneId),
94-
thriftDefaultBufferSize_(thriftDefaultBufferSize), thriftMaxFrameSize_(thriftMaxFrameSize),
95-
connectionTimeoutInMs_(connectionTimeoutInMs), useSSL_(useSSL), enableRPCCompression_(enableRPCCompression), version(version), endpoints_(std::move(endpoints)),
96-
selectionPolicy_(std::move(policy)) {
98+
const std::string& userName,
99+
const std::string& password,
100+
bool useSSL,
101+
const std::string& trustCertFilePath,
102+
const std::string& zoneId,
103+
int32_t thriftDefaultBufferSize,
104+
int32_t thriftMaxFrameSize,
105+
int32_t connectionTimeoutInMs,
106+
bool enableRPCCompression,
107+
const std::string& version,
108+
const std::vector<TEndPoint>& endpoints,
109+
NodeSelectionPolicy policy)
110+
: userName_(userName)
111+
, password_(password)
112+
, zoneId_(zoneId)
113+
, thriftDefaultBufferSize_(thriftDefaultBufferSize)
114+
, thriftMaxFrameSize_(thriftMaxFrameSize)
115+
, connectionTimeoutInMs_(connectionTimeoutInMs)
116+
, useSSL_(useSSL)
117+
, trustCertFilePath_(trustCertFilePath)
118+
, enableRPCCompression_(enableRPCCompression)
119+
, version_(version)
120+
, endpoints_(endpoints)
121+
, selectionPolicy_(policy) {
97122
deduplicateEndpoints();
98123
}
99124

@@ -157,7 +182,7 @@ std::vector<TEndPoint> NodesSupplier::fetchLatestEndpoints() {
157182
try {
158183
if (client_ == nullptr) {
159184
client_ = std::make_shared<ThriftConnection>(endpoint);
160-
client_->init(userName_, password_, enableRPCCompression_, zoneId_, version);
185+
client_->init(userName_, password_, enableRPCCompression_, useSSL_, trustCertFilePath_, zoneId_, version_);
161186
}
162187

163188
auto sessionDataSet = client_->executeQueryStatement(SHOW_DATA_NODES_COMMAND);

iotdb-client/client-cpp/src/main/NodesSupplier.h

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,36 @@ class NodesSupplier : public INodesSupplier {
7878
static const int CONNECTION_TIMEOUT_IN_MS;
7979

8080
static std::shared_ptr<NodesSupplier> create(
81-
std::vector<TEndPoint> endpoints,
82-
std::string userName, std::string password, std::string zoneId = "",
81+
const std::vector<TEndPoint>& endpoints,
82+
const std::string& userName,
83+
const std::string& password,
84+
bool useSSL = false,
85+
const std::string& trustCertFilePath = "",
86+
const std::string& zoneId = "",
8387
int32_t thriftDefaultBufferSize = ThriftConnection::THRIFT_DEFAULT_BUFFER_SIZE,
8488
int32_t thriftMaxFrameSize = ThriftConnection::THRIFT_MAX_FRAME_SIZE,
8589
int32_t connectionTimeoutInMs = ThriftConnection::CONNECTION_TIMEOUT_IN_MS,
86-
bool useSSL = false, bool enableRPCCompression = false,
87-
std::string version = "V_1_0",
90+
bool enableRPCCompression = false,
91+
const std::string& version = "V_1_0",
8892
std::chrono::milliseconds refreshInterval = std::chrono::milliseconds(TIMEOUT_IN_MS),
8993
NodeSelectionPolicy policy = RoundRobinPolicy::select
9094
);
9195

9296
NodesSupplier(
93-
std::string userName, std::string password, const std::string& zoneId,
94-
int32_t thriftDefaultBufferSize, int32_t thriftMaxFrameSize,
95-
int32_t connectionTimeoutInMs, bool useSSL, bool enableRPCCompression,
96-
std::string version, std::vector<TEndPoint> endpoints, NodeSelectionPolicy policy
97+
const std::string& userName,
98+
const std::string& password,
99+
bool useSSL,
100+
const std::string& trustCertFilePath,
101+
const std::string& zoneId,
102+
int32_t thriftDefaultBufferSize,
103+
int32_t thriftMaxFrameSize,
104+
int32_t connectionTimeoutInMs,
105+
bool enableRPCCompression,
106+
const std::string& version,
107+
const std::vector<TEndPoint>& endpoints,
108+
NodeSelectionPolicy policy
97109
);
110+
98111
std::vector<TEndPoint> getEndPointList() override;
99112

100113
boost::optional<TEndPoint> getQueryEndPoint() override;
@@ -108,8 +121,9 @@ class NodesSupplier : public INodesSupplier {
108121
int32_t thriftMaxFrameSize_;
109122
int32_t connectionTimeoutInMs_;
110123
bool useSSL_;
124+
std::string trustCertFilePath_;
111125
bool enableRPCCompression_;
112-
std::string version;
126+
std::string version_;
113127
std::string zoneId_;
114128

115129
std::mutex mutex_;

iotdb-client/client-cpp/src/main/Session.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ void Session::initNodesSupplier(const std::vector<std::string>& nodeUrls) {
763763
}
764764

765765
if (enableAutoFetch_) {
766-
nodesSupplier_ = NodesSupplier::create(endPoints, username_, password_);
766+
nodesSupplier_ = NodesSupplier::create(endPoints, username_, password_, useSSL_, trustCertFilePath_);
767767
}
768768
else {
769769
nodesSupplier_ = make_shared<StaticNodesSupplier>(endPoints);

iotdb-client/client-cpp/src/main/Session.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ class Session {
535535
private:
536536
std::string host_;
537537
int rpcPort_;
538+
bool useSSL_;
539+
std::string trustCertFilePath_;
538540
std::vector<string> nodeUrls_;
539541
std::string username_;
540542
std::string password_;
@@ -724,6 +726,8 @@ class Session {
724726
this->enableRedirection_ = builder->enableRedirections;
725727
this->connectTimeoutMs_ = builder->connectTimeoutMs;
726728
this->nodeUrls_ = builder->nodeUrls;
729+
this->useSSL_ = builder->useSSL;
730+
this->trustCertFilePath_ = builder->trustCertFilePath;
727731
initZoneId();
728732
initNodesSupplier(this->nodeUrls_);
729733
}

iotdb-client/client-cpp/src/main/SessionBuilder.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ class SessionBuilder : public AbstractSessionBuilder {
3434
return this;
3535
}
3636

37+
SessionBuilder* useSSL(bool useSSL) {
38+
AbstractSessionBuilder::useSSL = useSSL;
39+
return this;
40+
}
41+
42+
SessionBuilder* trustCertFilePath(const std::string &trustCertFilePath) {
43+
AbstractSessionBuilder::trustCertFilePath = trustCertFilePath;
44+
return this;
45+
}
46+
3747
SessionBuilder* username(const std::string &username) {
3848
AbstractSessionBuilder::username = username;
3949
return this;

iotdb-client/client-cpp/src/main/SessionConnection.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ SessionConnection::SessionConnection(Session* session_ptr, const TEndPoint& endp
5050
database(std::move(db)) {
5151
this->zoneId = zoneId.empty() ? getSystemDefaultZoneId() : zoneId;
5252
endPointList.push_back(endpoint);
53-
init(endPoint);
53+
init(endPoint, session->useSSL_, session->trustCertFilePath_);
5454
}
5555

5656
void SessionConnection::close() {
@@ -98,10 +98,18 @@ SessionConnection::~SessionConnection() {
9898
}
9999
}
100100

101-
void SessionConnection::init(const TEndPoint& endpoint) {
102-
shared_ptr<TSocket> socket(new TSocket(endpoint.ip, endpoint.port));
103-
transport = std::make_shared<TFramedTransport>(socket);
104-
socket->setConnTimeout(connectionTimeoutInMs);
101+
void SessionConnection::init(const TEndPoint& endpoint, bool useSSL, const std::string& trustCertFilePath) {
102+
if (useSSL) {
103+
socketFactory_->loadTrustedCertificates(trustCertFilePath.c_str());
104+
socketFactory_->authenticate(false);
105+
auto sslSocket = socketFactory_->createSocket(endPoint.ip, endPoint.port);
106+
sslSocket->setConnTimeout(connectionTimeoutInMs);
107+
transport = std::make_shared<TFramedTransport>(sslSocket);
108+
} else {
109+
auto socket = std::make_shared<TSocket>(endPoint.ip, endPoint.port);
110+
socket->setConnTimeout(connectionTimeoutInMs);
111+
transport = std::make_shared<TFramedTransport>(socket);
112+
}
105113
if (!transport->isOpen()) {
106114
try {
107115
transport->open();
@@ -341,7 +349,7 @@ bool SessionConnection::reconnect() {
341349
}
342350
tryHostNum++;
343351
try {
344-
init(this->endPoint);
352+
init(this->endPoint, this->session->useSSL_, this->session->trustCertFilePath_);
345353
reconnect = true;
346354
}
347355
catch (const IoTDBConnectionException& e) {

iotdb-client/client-cpp/src/main/SessionConnection.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <vector>
2424
#include <string>
2525
#include <thrift/transport/TTransport.h>
26+
#include <thrift/transport/TSSLSocket.h>
2627
#include "IClientRPCService.h"
2728
#include "common_types.h"
2829
#include "NodesSupplier.h"
@@ -50,7 +51,7 @@ class SessionConnection : public std::enable_shared_from_this<SessionConnection>
5051

5152
const TEndPoint& getEndPoint();
5253

53-
void init(const TEndPoint& endpoint);
54+
void init(const TEndPoint& endpoint, bool useSSL, const std::string& trustCertFilePath);
5455

5556
void insertStringRecord(const TSInsertStringRecordReq& request);
5657

@@ -179,6 +180,8 @@ class SessionConnection : public std::enable_shared_from_this<SessionConnection>
179180

180181
TSStatus deleteDataInternal(TSDeleteDataReq request);
181182

183+
std::shared_ptr<apache::thrift::transport::TSSLSocketFactory> socketFactory_ =
184+
std::make_shared<apache::thrift::transport::TSSLSocketFactory>();;
182185
std::shared_ptr<TTransport> transport;
183186
std::shared_ptr<IClientRPCServiceClient> client;
184187
Session* session;

0 commit comments

Comments
 (0)