Skip to content

Commit d06bced

Browse files
authored
Support session with CPP conditional compilation for SSL. (#17080)
* Support session with CPP conditional compilation for SSL. * remove unused asan
1 parent 0b25ea2 commit d06bced

File tree

8 files changed

+222
-103
lines changed

8 files changed

+222
-103
lines changed

example/client-cpp-example/src/CMakeLists.txt

Lines changed: 103 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,23 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/thrift/include)
2828
# Add cpp-client include directory
2929
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/client/include)
3030

31-
FIND_PACKAGE(OpenSSL REQUIRED)
32-
IF(OpenSSL_FOUND)
33-
MESSAGE(STATUS "OpenSSL found: ${OPENSSL_VERSION}")
34-
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
31+
# =========================
32+
# SSL option (default OFF)
33+
# =========================
34+
option(WITH_SSL "Build with SSL support" OFF)
35+
36+
IF(WITH_SSL)
37+
FIND_PACKAGE(OpenSSL REQUIRED)
38+
IF(OpenSSL_FOUND)
39+
MESSAGE(STATUS "OpenSSL found: ${OPENSSL_VERSION}")
40+
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
41+
ADD_DEFINITIONS(-DWITH_SSL=1)
42+
ELSE()
43+
MESSAGE(FATAL_ERROR "OpenSSL not found, but WITH_SSL is enabled")
44+
ENDIF()
3545
ELSE()
36-
MESSAGE(FATAL_ERROR "OpenSSL not found")
46+
MESSAGE(STATUS "Building without SSL support")
47+
ADD_DEFINITIONS(-DWITH_SSL=0)
3748
ENDIF()
3849

3950
FIND_PACKAGE(Boost REQUIRED)
@@ -50,53 +61,91 @@ ADD_EXECUTABLE(TableModelSessionExample TableModelSessionExample.cpp)
5061
ADD_EXECUTABLE(MultiSvrNodeClient MultiSvrNodeClient.cpp)
5162

5263
IF(MSVC)
53-
TARGET_LINK_LIBRARIES(SessionExample
54-
iotdb_session
55-
"${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib"
56-
OpenSSL::SSL
57-
OpenSSL::Crypto
58-
)
59-
TARGET_LINK_LIBRARIES(AlignedTimeseriesSessionExample
60-
iotdb_session
61-
"${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib"
62-
OpenSSL::SSL
63-
OpenSSL::Crypto
64-
)
65-
TARGET_LINK_LIBRARIES(TableModelSessionExample
66-
iotdb_session
67-
"${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib"
68-
OpenSSL::SSL
69-
OpenSSL::Crypto
70-
)
71-
TARGET_LINK_LIBRARIES(MultiSvrNodeClient
72-
iotdb_session
73-
"${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib"
74-
OpenSSL::SSL
75-
OpenSSL::Crypto
76-
)
64+
IF(WITH_SSL)
65+
TARGET_LINK_LIBRARIES(SessionExample
66+
iotdb_session
67+
"${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib"
68+
OpenSSL::SSL
69+
OpenSSL::Crypto
70+
)
71+
TARGET_LINK_LIBRARIES(AlignedTimeseriesSessionExample
72+
iotdb_session
73+
"${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib"
74+
OpenSSL::SSL
75+
OpenSSL::Crypto
76+
)
77+
TARGET_LINK_LIBRARIES(TableModelSessionExample
78+
iotdb_session
79+
"${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib"
80+
OpenSSL::SSL
81+
OpenSSL::Crypto
82+
)
83+
TARGET_LINK_LIBRARIES(MultiSvrNodeClient
84+
iotdb_session
85+
"${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib"
86+
OpenSSL::SSL
87+
OpenSSL::Crypto
88+
)
89+
ELSE()
90+
TARGET_LINK_LIBRARIES(SessionExample
91+
iotdb_session
92+
"${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib"
93+
)
94+
TARGET_LINK_LIBRARIES(AlignedTimeseriesSessionExample
95+
iotdb_session
96+
"${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib"
97+
)
98+
TARGET_LINK_LIBRARIES(TableModelSessionExample
99+
iotdb_session
100+
"${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib"
101+
)
102+
TARGET_LINK_LIBRARIES(MultiSvrNodeClient
103+
iotdb_session
104+
"${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib"
105+
)
106+
ENDIF()
77107
ELSE()
78-
TARGET_LINK_LIBRARIES(SessionExample
79-
iotdb_session
80-
pthread
81-
OpenSSL::SSL
82-
OpenSSL::Crypto
83-
)
84-
TARGET_LINK_LIBRARIES(AlignedTimeseriesSessionExample
85-
iotdb_session
86-
pthread
87-
OpenSSL::SSL
88-
OpenSSL::Crypto
89-
)
90-
TARGET_LINK_LIBRARIES(TableModelSessionExample
91-
iotdb_session
92-
pthread
93-
OpenSSL::SSL
94-
OpenSSL::Crypto
95-
)
96-
TARGET_LINK_LIBRARIES(MultiSvrNodeClient
97-
iotdb_session
98-
pthread
99-
OpenSSL::SSL
100-
OpenSSL::Crypto
101-
)
102-
ENDIF()
108+
IF(WITH_SSL)
109+
TARGET_LINK_LIBRARIES(SessionExample
110+
iotdb_session
111+
pthread
112+
OpenSSL::SSL
113+
OpenSSL::Crypto
114+
)
115+
TARGET_LINK_LIBRARIES(AlignedTimeseriesSessionExample
116+
iotdb_session
117+
pthread
118+
OpenSSL::SSL
119+
OpenSSL::Crypto
120+
)
121+
TARGET_LINK_LIBRARIES(TableModelSessionExample
122+
iotdb_session
123+
pthread
124+
OpenSSL::SSL
125+
OpenSSL::Crypto
126+
)
127+
TARGET_LINK_LIBRARIES(MultiSvrNodeClient
128+
iotdb_session
129+
pthread
130+
OpenSSL::SSL
131+
OpenSSL::Crypto
132+
)
133+
ELSE()
134+
TARGET_LINK_LIBRARIES(SessionExample
135+
iotdb_session
136+
pthread
137+
)
138+
TARGET_LINK_LIBRARIES(AlignedTimeseriesSessionExample
139+
iotdb_session
140+
pthread
141+
)
142+
TARGET_LINK_LIBRARIES(TableModelSessionExample
143+
iotdb_session
144+
pthread
145+
)
146+
TARGET_LINK_LIBRARIES(MultiSvrNodeClient
147+
iotdb_session
148+
pthread
149+
)
150+
ENDIF()
151+
ENDIF()

iotdb-client/client-cpp/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<cmake.root.dir>${project.build.directory}/dependency/cmake/</cmake.root.dir>
3939
<thrift.exec.absolute.path>${project.build.directory}/thrift/bin/${thrift.executable}</thrift.exec.absolute.path>
4040
<maven.test.skip>${ctest.skip.tests}</maven.test.skip>
41+
<with.ssl>false</with.ssl>
4142
</properties>
4243
<dependencies>
4344
<dependency>
@@ -217,6 +218,7 @@
217218
<targetPath>${project.build.directory}/build/main</targetPath>
218219
<options>
219220
<option>-DBOOST_INCLUDEDIR=${boost.include.dir}</option>
221+
<option>-DWITH_SSL=${with.ssl}</option>
220222
</options>
221223
</configuration>
222224
</execution>
@@ -233,6 +235,7 @@
233235
<targetPath>${project.build.directory}/build/test</targetPath>
234236
<options>
235237
<option>-DBOOST_INCLUDEDIR=${boost.include.dir}</option>
238+
<option>-DWITH_SSL=${with.ssl}</option>
236239
</options>
237240
</configuration>
238241
</execution>

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

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,23 @@ 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})
29+
# =========================
30+
# SSL option (default OFF)
31+
# =========================
32+
option(WITH_SSL "Build with SSL support" OFF)
33+
34+
IF(WITH_SSL)
35+
FIND_PACKAGE(OpenSSL REQUIRED)
36+
IF(OpenSSL_FOUND)
37+
MESSAGE(STATUS "OpenSSL found: ${OPENSSL_VERSION}")
38+
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
39+
ADD_DEFINITIONS(-DWITH_SSL=1)
40+
ELSE()
41+
MESSAGE(FATAL_ERROR "OpenSSL not found, but WITH_SSL is enabled")
42+
ENDIF()
3443
ELSE()
35-
MESSAGE(FATAL_ERROR "OpenSSL not found")
44+
MESSAGE(STATUS "Building without SSL support")
45+
ADD_DEFINITIONS(-DWITH_SSL=0)
3646
ENDIF()
3747

3848
# Add Boost include path for MacOS
@@ -50,11 +60,6 @@ ELSE()
5060
SET(THRIFT_STATIC_LIB "${CMAKE_SOURCE_DIR}/../../thrift/lib/libthrift.a")
5161
ENDIF()
5262

53-
IF(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" AND NOT MSVC)
54-
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
55-
add_link_options(-fsanitize=address)
56-
ENDIF()
57-
5863
# Add the generated source files to the sources for the library.
5964
AUX_SOURCE_DIRECTORY(./generated-sources-cpp SESSION_SRCS)
6065
IF(MSVC)
@@ -63,10 +68,20 @@ ELSE()
6368
ADD_LIBRARY(iotdb_session SHARED ${SESSION_SRCS})
6469
ENDIF()
6570

66-
# Link with Thrift static library
67-
target_link_libraries(iotdb_session
68-
PUBLIC
69-
OpenSSL::SSL
70-
OpenSSL::Crypto
71-
${THRIFT_STATIC_LIB}
72-
)
71+
# =========================
72+
# Link libraries (SSL optional)
73+
# =========================
74+
IF(WITH_SSL)
75+
target_link_libraries(iotdb_session
76+
PUBLIC
77+
OpenSSL::SSL
78+
OpenSSL::Crypto
79+
${THRIFT_STATIC_LIB}
80+
)
81+
ELSE()
82+
target_link_libraries(iotdb_session
83+
PUBLIC
84+
${THRIFT_STATIC_LIB}
85+
)
86+
ENDIF()
87+

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,17 @@ SessionConnection::~SessionConnection() {
100100

101101
void SessionConnection::init(const TEndPoint& endpoint, bool useSSL, const std::string& trustCertFilePath) {
102102
if (useSSL) {
103+
#if WITH_SSL
103104
socketFactory_->loadTrustedCertificates(trustCertFilePath.c_str());
104105
socketFactory_->authenticate(false);
105106
auto sslSocket = socketFactory_->createSocket(endPoint.ip, endPoint.port);
106107
sslSocket->setConnTimeout(connectionTimeoutInMs);
107108
transport = std::make_shared<TFramedTransport>(sslSocket);
109+
#else
110+
throw IoTDBException("SSL/TLS support is not enabled in this build. "
111+
"Please rebuild with -DWITH_SSL=ON flag "
112+
"or use non-SSL connection.");
113+
#endif
108114
} else {
109115
auto socket = std::make_shared<TSocket>(endPoint.ip, endPoint.port);
110116
socket->setConnTimeout(connectionTimeoutInMs);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
#include <vector>
2424
#include <string>
2525
#include <thrift/transport/TTransport.h>
26+
#if WITH_SSL
2627
#include <thrift/transport/TSSLSocket.h>
28+
#endif
29+
2730
#include "IClientRPCService.h"
2831
#include "common_types.h"
2932
#include "NodesSupplier.h"
@@ -179,9 +182,10 @@ class SessionConnection : public std::enable_shared_from_this<SessionConnection>
179182
TSStatus insertTabletsInternal(TSInsertTabletsReq request);
180183

181184
TSStatus deleteDataInternal(TSDeleteDataReq request);
182-
185+
#if WITH_SSL
183186
std::shared_ptr<apache::thrift::transport::TSSLSocketFactory> socketFactory_ =
184187
std::make_shared<apache::thrift::transport::TSSLSocketFactory>();
188+
#endif
185189
std::shared_ptr<TTransport> transport;
186190
std::shared_ptr<IClientRPCServiceClient> client;
187191
Session* session;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,17 @@ void ThriftConnection::init(const std::string& username,
6969
const std::string& zoneId,
7070
const std::string& version) {
7171
if (useSSL) {
72+
#if WITH_SSL
7273
socketFactory_->loadTrustedCertificates(trustCertFilePath.c_str());
7374
socketFactory_->authenticate(false);
7475
auto sslSocket = socketFactory_->createSocket(endPoint_.ip, endPoint_.port);
7576
sslSocket->setConnTimeout(connectionTimeoutInMs_);
7677
transport_ = std::make_shared<TFramedTransport>(sslSocket);
78+
#else
79+
throw IoTDBException("SSL/TLS support is not enabled in this build. "
80+
"Please rebuild with -DWITH_SSL=ON flag "
81+
"or use non-SSL connection.");
82+
#endif
7783
} else {
7884
auto socket = std::make_shared<TSocket>(endPoint_.ip, endPoint_.port);
7985
socket->setConnTimeout(connectionTimeoutInMs_);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
#define IOTDB_THRIFTCONNECTION_H
2121

2222
#include <memory>
23+
#if WITH_SSL
2324
#include <thrift/transport/TSSLSocket.h>
25+
#endif
2426
#include "IClientRPCService.h"
2527

2628
class SessionDataSet;
@@ -60,8 +62,10 @@ class ThriftConnection {
6062
int connectionTimeoutInMs_;
6163
int fetchSize_;
6264

65+
#if WITH_SSL
6366
std::shared_ptr<apache::thrift::transport::TSSLSocketFactory> socketFactory_ =
6467
std::make_shared<apache::thrift::transport::TSSLSocketFactory>();
68+
#endif
6569
std::shared_ptr<apache::thrift::transport::TTransport> transport_;
6670
std::shared_ptr<IClientRPCServiceClient> client_;
6771
int64_t sessionId_{};

0 commit comments

Comments
 (0)