Skip to content

Commit 33eea88

Browse files
authored
Improve cpp-client-lib: provide another libpulsarwithdeps.a in dep/rpm (#6458)
Fix #6439 We shouldn't static link libssl in libpulsar.a, as this is a security red flag. we should just use whatever the libssl the system provides. Because if there is a security problem in libssl, all the machines can just update their own libssl library without rebuilding libpulsar.a. As suggested, this change not change the old behavior, and mainly provides 2 other additional pulsar cpp client library in deb/rpm, and add related docs of how to use 4 libs in doc. The additional 2 libs: - pulsarSharedNossl (libpulsarnossl.so), similar to pulsarShared(libpulsar.so), with no ssl statically linked. - pulsarStaticWithDeps(libpulsarwithdeps.a), similar to pulsarStatic(libpulsar.a), and archived in the dependencies libraries of `libboost_regex`, `libboost_system`, `libcurl`, `libprotobuf`, `libzstd` and `libz` statically. Passed 4 libs rpm/deb build, install, and compile with a pulsar-client example code. * also add libpulsarwithdeps.a together with libpulsar.a into cpp client release * add documentation for libpulsarwithdeps.a, add g++ build examples * add pulsarSharedNossl target to build libpulsarnossl.so * update doc * verify 4 libs in rpm/deb build, installed, use all good
1 parent 1517d19 commit 33eea88

File tree

7 files changed

+118
-29
lines changed

7 files changed

+118
-29
lines changed

pulsar-client-cpp/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@ endif(NOT LOG_CATEGORY_NAME)
7373

7474
add_definitions(-DLOG_CATEGORY_NAME=${LOG_CATEGORY_NAME} -DBUILDING_PULSAR -DBOOST_ALL_NO_LIB -DBOOST_ALLOW_DEPRECATED_HEADERS)
7575

76+
### This part is to find and keep SSL dynamic libs in RECORD_OPENSSL_SSL_LIBRARY and RECORD_OPENSSL_CRYPTO_LIBRARY
77+
### After find the libs, will unset related cache, and will not affact another same call to find_package.
78+
if (APPLE)
79+
set(OPENSSL_INCLUDE_DIR /usr/local/opt/openssl/include/)
80+
set(OPENSSL_ROOT_DIR /usr/local/opt/openssl/)
81+
endif ()
82+
83+
set(OPENSSL_ROOT_DIR /usr/lib64/)
84+
set(OPENSSL_USE_STATIC_LIBS FALSE)
85+
find_package(OpenSSL REQUIRED)
86+
set(RECORD_OPENSSL_SSL_LIBRARY ${OPENSSL_SSL_LIBRARY})
87+
set(RECORD_OPENSSL_CRYPTO_LIBRARY ${OPENSSL_CRYPTO_LIBRARY})
88+
89+
unset(OPENSSL_FOUND CACHE)
90+
unset(OPENSSL_INCLUDE_DIR CACHE)
91+
unset(OPENSSL_CRYPTO_LIBRARY CACHE)
92+
unset(OPENSSL_CRYPTO_LIBRARIES CACHE)
93+
unset(OPENSSL_SSL_LIBRARY CACHE)
94+
unset(OPENSSL_SSL_LIBRARIES CACHE)
95+
unset(OPENSSL_LIBRARIES CACHE)
96+
unset(OPENSSL_VERSION CACHE)
97+
7698
if (LINK_STATIC)
7799
find_library(ZLIB_LIBRARIES REQUIRED NAMES libz.a z zlib)
78100
find_library(Protobuf_LITE_LIBRARIES NAMES libprotobuf-lite.a libprotobuf-lite)

pulsar-client-cpp/lib/CMakeLists.txt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ add_library(pulsarShared SHARED ${PULSAR_SOURCES})
6161
set_target_properties(pulsarShared PROPERTIES OUTPUT_NAME ${LIB_NAME_SHARED} VERSION ${LIBRARY_VERSION})
6262
target_link_libraries(pulsarShared ${COMMON_LIBS} ${CMAKE_DL_LIBS})
6363

64+
65+
### pulsarSharedNossl not static link ssl, it could avoid rebuild libpulsar when ssl lib need update.
66+
### pulsarSharedNossl is build under condition LINK_STATIC=ON, we should replace static ssl libs with dynamic libs.
67+
SET(COMMON_LIBS_NOSSL ${COMMON_LIBS})
68+
if (NOT ${RECORD_OPENSSL_SSL_LIBRARY} MATCHES ".+\\.a$")
69+
LIST(REMOVE_ITEM COMMON_LIBS_NOSSL ${OPENSSL_SSL_LIBRARY})
70+
LIST(APPEND COMMON_LIBS_NOSSL ${RECORD_OPENSSL_SSL_LIBRARY})
71+
endif ()
72+
if (NOT ${RECORD_OPENSSL_CRYPTO_LIBRARY} MATCHES ".+\\.a$")
73+
LIST(REMOVE_ITEM COMMON_LIBS_NOSSL ${OPENSSL_CRYPTO_LIBRARY})
74+
LIST(APPEND COMMON_LIBS_NOSSL ${RECORD_OPENSSL_CRYPTO_LIBRARY})
75+
endif ()
76+
77+
add_library(pulsarSharedNossl SHARED ${PULSAR_SOURCES})
78+
set_target_properties(pulsarSharedNossl PROPERTIES OUTPUT_NAME ${LIB_NAME_SHARED}nossl VERSION ${LIBRARY_VERSION})
79+
target_link_libraries(pulsarSharedNossl ${COMMON_LIBS_NOSSL} ${CMAKE_DL_LIBS})
80+
6481
add_library(pulsarStatic STATIC ${PULSAR_SOURCES})
6582
set_target_properties(pulsarStatic PROPERTIES OUTPUT_NAME ${LIB_NAME} VERSION ${LIBRARY_VERSION})
6683
target_compile_definitions(pulsarStatic PRIVATE PULSAR_STATIC)
@@ -72,7 +89,7 @@ if (MSVC)
7289
endif()
7390

7491
# When linking statically, install a libpulsar.a that contains all the
75-
# required dependencies
92+
# required dependencies except ssl
7693
if (LINK_STATIC)
7794
if (MSVC)
7895

@@ -98,21 +115,20 @@ if (LINK_STATIC)
98115
set_target_properties(pulsarStaticWithDeps PROPERTIES STATIC_LIBRARY_FLAGS_DEBUG ${DEBUG_STATIC_LIBS} STATIC_LIBRARY_FLAGS_RELEASE ${STATIC_LIBS} OUTPUT_NAME ${LIB_NAME}WithDeps VERSION ${LIBRARY_VERSION})
99116
install(TARGETS pulsarStaticWithDeps DESTINATION lib)
100117
else()
101-
# Build a list of the requird .a libs to merge
118+
# Build a list of the requird .a libs (except ssl) to merge
102119
SET(STATIC_LIBS "")
103120
foreach (LIB IN LISTS COMMON_LIBS)
104-
if (${LIB} MATCHES ".+\\.a$")
121+
if (${LIB} MATCHES ".+\\.a$" AND NOT ${LIB} MATCHES ${OPENSSL_SSL_LIBRARY} AND NOT ${LIB} MATCHES ${OPENSSL_CRYPTO_LIBRARY})
105122
set(STATIC_LIBS "${STATIC_LIBS} ${LIB}")
106123
endif()
107124
endforeach()
108125

109126
add_custom_target(pulsarStaticWithDeps
110127
ALL
111128
BYPRODUCTS merged-library
112-
COMMAND ./build-support/merge_archives.sh libpulsar.a $<TARGET_FILE:pulsarStatic> ${STATIC_LIBS}
129+
COMMAND ./build-support/merge_archives.sh libpulsar.a $<TARGET_FILE:pulsarStatic> ${STATIC_LIBS} && mv merged-library/libpulsar.a lib/libpulsarwithdeps.a
113130
DEPENDS pulsarStatic
114131
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
115-
install(FILES ../merged-library/libpulsar.a DESTINATION lib)
116132
endif(MSVC)
117133
else()
118134
# Install regular libpulsar.a
@@ -122,4 +138,5 @@ endif(LINK_STATIC)
122138

123139
install(TARGETS pulsarStatic DESTINATION lib)
124140
install(TARGETS pulsarShared RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
141+
install(TARGETS pulsarSharedNossl RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
125142
install(DIRECTORY "../include/pulsar" DESTINATION include)

pulsar-client-cpp/pkg/deb/Dockerfile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ RUN curl -O -L https://github.com/madler/zlib/archive/v1.2.11.tar.gz && \
6464
make && make install && \
6565
rm -rf /v1.2.11.tar.gz /zlib-1.2.11
6666

67-
RUN curl -O -L https://github.com/openssl/openssl/archive/OpenSSL_1_1_0j.tar.gz && \
68-
tar xvfz OpenSSL_1_1_0j.tar.gz && \
69-
cd openssl-OpenSSL_1_1_0j/ && \
70-
./Configure -fPIC --prefix=/usr/local/ssl/ no-shared linux-x86_64 && \
71-
make && make install && \
72-
rm -rf /OpenSSL_1_1_0j.tar.gz /openssl-OpenSSL_1_1_0j
73-
7467
# Zstandard
7568
RUN curl -O -L https://github.com/facebook/zstd/releases/download/v1.3.7/zstd-1.3.7.tar.gz && \
7669
tar xvfz zstd-1.3.7.tar.gz && \
@@ -79,6 +72,13 @@ RUN curl -O -L https://github.com/facebook/zstd/releases/download/v1.3.7/zstd-1.
7972
make install && \
8073
rm -rf /zstd-1.3.7 /zstd-1.3.7.tar.gz
8174

75+
RUN curl -O -L https://github.com/openssl/openssl/archive/OpenSSL_1_1_0j.tar.gz && \
76+
tar xvfz OpenSSL_1_1_0j.tar.gz && \
77+
cd openssl-OpenSSL_1_1_0j/ && \
78+
./Configure -fPIC --prefix=/usr/local/ssl/ linux-x86_64 && \
79+
make && make install && \
80+
rm -rf /OpenSSL_1_1_0j.tar.gz /openssl-OpenSSL_1_1_0j
81+
8282
# LibCurl
8383
RUN curl -O -L https://github.com/curl/curl/releases/download/curl-7_61_0/curl-7.61.0.tar.gz && \
8484
tar xvfz curl-7.61.0.tar.gz && \

pulsar-client-cpp/pkg/deb/build-deb.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ tar xfz $SRC_ROOT_DIR/distribution/server/target/apache-pulsar-$POM_VERSION-src.
3838
pushd $CPP_DIR
3939

4040
cmake . -DBUILD_TESTS=OFF -DLINK_STATIC=ON
41-
make pulsarShared pulsarStatic -j 3
41+
make pulsarShared pulsarSharedNossl pulsarStatic pulsarStaticWithDeps -j 3
4242
popd
4343

4444
DEST_DIR=apache-pulsar-client
@@ -68,11 +68,17 @@ mkdir -p $DEVEL_DEST_DIR/usr/include
6868
mkdir -p $DEST_DIR/usr/share/doc/pulsar-client-$VERSION
6969
mkdir -p $DEVEL_DEST_DIR/usr/share/doc/pulsar-client-dev-$VERSION
7070

71+
ls $CPP_DIR/lib/libpulsar*
72+
7173
cp -ar $CPP_DIR/include/pulsar $DEVEL_DEST_DIR/usr/include/
7274
cp $CPP_DIR/lib/libpulsar.a $DEVEL_DEST_DIR/usr/lib
75+
cp $CPP_DIR/lib/libpulsarwithdeps.a $DEVEL_DEST_DIR/usr/lib
7376
cp $CPP_DIR/lib/libpulsar.so.$POM_VERSION $DEST_DIR/usr/lib
77+
cp $CPP_DIR/lib/libpulsarnossl.so.$POM_VERSION $DEST_DIR/usr/lib
78+
7479
pushd $DEST_DIR/usr/lib
7580
ln -s libpulsar.so.$POM_VERSION libpulsar.so
81+
ln -s libpulsarnossl.so.$POM_VERSION libpulsarnossl.so
7682
popd
7783

7884
cp $ROOT_DIR/NOTICE $DEST_DIR/usr/share/doc/pulsar-client-$VERSION

pulsar-client-cpp/pkg/rpm/Dockerfile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,18 @@ RUN curl -O -L https://github.com/madler/zlib/archive/v1.2.11.tar.gz && \
6464
make && make install && \
6565
rm -rf /v1.2.11.tar.gz /zlib-1.2.11
6666

67+
# Zstandard
68+
RUN curl -O -L https://github.com/facebook/zstd/releases/download/v1.3.7/zstd-1.3.7.tar.gz && \
69+
tar xvfz zstd-1.3.7.tar.gz && \
70+
cd zstd-1.3.7 && \
71+
CFLAGS="-fPIC -O3" make -j8 && \
72+
make install && \
73+
rm -rf /zstd-1.3.7 /zstd-1.3.7.tar.gz
74+
6775
RUN curl -O -L https://github.com/openssl/openssl/archive/OpenSSL_1_1_0j.tar.gz && \
6876
tar xvfz OpenSSL_1_1_0j.tar.gz && \
6977
cd openssl-OpenSSL_1_1_0j/ && \
70-
./Configure -fPIC --prefix=/usr/local/ssl/ no-shared linux-x86_64 && \
78+
./Configure -fPIC --prefix=/usr/local/ssl/ linux-x86_64 && \
7179
make && make install && \
7280
rm -rf /OpenSSL_1_1_0j.tar.gz /openssl-OpenSSL_1_1_0j
7381

@@ -79,12 +87,4 @@ RUN curl -O -L https://github.com/curl/curl/releases/download/curl-7_61_0/curl-
7987
make && make install && \
8088
rm -rf /curl-7.61.0.tar.gz /curl-7.61.0
8189

82-
# Zstandard
83-
RUN curl -O -L https://github.com/facebook/zstd/releases/download/v1.3.7/zstd-1.3.7.tar.gz && \
84-
tar xvfz zstd-1.3.7.tar.gz && \
85-
cd zstd-1.3.7 && \
86-
CFLAGS="-fPIC -O3" make -j8 && \
87-
make install && \
88-
rm -rf /zstd-1.3.7 /zstd-1.3.7.tar.gz
89-
9090
ENV OPENSSL_ROOT_DIR /usr/local/ssl/

pulsar-client-cpp/pkg/rpm/SPECS/pulsar-client.spec

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Version: %{version}
3030
Release: %{release}
3131
Source: apache-pulsar-%{pom_version}-src.tar.gz
3232
Prefix: /usr
33+
AutoReq: no
3334

3435
%package devel
3536
Summary: Apache Pulsar client library
@@ -53,7 +54,7 @@ static library.
5354
%build
5455
cd pulsar-client-cpp
5556
cmake . -DBUILD_TESTS=OFF -DLINK_STATIC=ON -DBUILD_PYTHON_WRAPPER=OFF
56-
make pulsarShared pulsarStatic -j 3
57+
make pulsarShared pulsarSharedNossl pulsarStatic pulsarStaticWithDeps -j 3
5758

5859
%install
5960
cd pulsar-client-cpp
@@ -65,7 +66,9 @@ mkdir -p $INCLUDE_DIR $LIB_DIR $DOC_DIR $DOC_DEVEL_DIR
6566

6667
cp -ar include/pulsar $INCLUDE_DIR
6768
cp lib/libpulsar.a $LIB_DIR
69+
cp lib/libpulsarwithdeps.a $LIB_DIR
6870
cp lib/libpulsar.so.%{pom_version} $LIB_DIR
71+
cp lib/libpulsarnossl.so.%{pom_version} $LIB_DIR
6972

7073
# Copy LICENSE files
7174
cp ../NOTICE $DOC_DIR
@@ -75,15 +78,19 @@ cp $DOC_DIR/* $DOC_DEVEL_DIR/
7578

7679
cd $LIB_DIR
7780
ln -s libpulsar.so.%{pom_version} libpulsar.so
81+
ln -s libpulsarnossl.so.%{pom_version} libpulsarnossl.so
7882

7983
%files
8084
%defattr(-,root,root)
8185
/usr/lib/libpulsar.so
8286
/usr/lib/libpulsar.so.%{pom_version}
87+
/usr/lib/libpulsarnossl.so
88+
/usr/lib/libpulsarnossl.so.%{pom_version}
8389
/usr/share/doc/pulsar-client-%{version}
8490

8591
%files devel
8692
%defattr(-,root,root)
8793
/usr/lib/libpulsar.a
94+
/usr/lib/libpulsarwithdeps.a
8895
/usr/include/pulsar
8996
/usr/share/doc/pulsar-client-devel-%{version}

site2/docs/client-libraries-cpp.md

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,37 @@ Pulsar C++ client is supported on **Linux** and **MacOS** platforms.
1818

1919
> Since 2.1.0 release, Pulsar ships pre-built RPM and Debian packages. You can download and install those packages directly.
2020
21+
Four kind of libraries `libpulsar.so` / `libpulsarnossl.so` / `libpulsar.a` / `libpulsarwithdeps.a` are included in your `/usr/lib` after rpm/deb download and install.
22+
By default, they are build under code path `${PULSAR_HOME}/pulsar-client-cpp`, using command
23+
`cmake . -DBUILD_TESTS=OFF -DLINK_STATIC=ON && make pulsarShared pulsarSharedNossl pulsarStatic pulsarStaticWithDeps -j 3`
24+
These libraries rely on some other libraries, if you want to get detailed version of dependencies libraries, please reference [these](https://github.com/apache/pulsar/blob/master/pulsar-client-cpp/pkg/rpm/Dockerfile) [files](https://github.com/apache/pulsar/blob/master/pulsar-client-cpp/pkg/deb/Dockerfile).
25+
26+
1. `libpulsar.so` is the Shared library, it contains statically linked `boost` and `openssl`, and will also dynamically link all other needed libraries.
27+
The command the when use this pulsar library is like this:
28+
```bash
29+
g++ --std=c++11 PulsarTest.cpp -o test /usr/lib/libpulsar.so -I/usr/local/ssl/include
30+
```
31+
32+
2. `libpulsarnossl.so` is the Shared library that similar to `libpulsar.so` except that the library `openssl` and `crypto` are dynamically linked.
33+
The command the when use this pulsar library is like this:
34+
```bash
35+
g++ --std=c++11 PulsarTest.cpp -o test /usr/lib/libpulsarnossl.so -lssl -lcrypto -I/usr/local/ssl/include -L/usr/local/ssl/lib
36+
```
37+
38+
3. `libpulsar.a` is the Static library, it need to load some dependencies library when using it.
39+
The command the when use this pulsar library is like this:
40+
```bash
41+
g++ --std=c++11 PulsarTest.cpp -o test /usr/lib/libpulsar.a -lssl -lcrypto -ldl -lpthread -I/usr/local/ssl/include -L/usr/local/ssl/lib -lboost_system -lboost_regex -lcurl -lprotobuf -lzstd -lz
42+
```
43+
44+
4. `libpulsarwithdeps.a` is the Static library, base on `libpulsar.a`, and archived in the dependencies libraries of `libboost_regex`, `libboost_system`, `libcurl`, `libprotobuf`, `libzstd` and `libz`,
45+
The command the when use this pulsar library is like this:
46+
```bash
47+
g++ --std=c++11 PulsarTest.cpp -o test /usr/lib/libpulsarwithdeps.a -lssl -lcrypto -ldl -lpthread -I/usr/local/ssl/include -L/usr/local/ssl/lib
48+
```
49+
`libpulsarwithdeps.a` does not include library openssl related libraries: `libssl` and `libcrypto`, because these 2 library is related to security,
50+
by using user local system provided version is more reasonable, and more easy for user to handling security issue and library upgrade.
51+
2152
### Install RPM
2253

2354
1. Download a RPM package from the links in the table.
@@ -33,6 +64,9 @@ Pulsar C++ client is supported on **Linux** and **MacOS** platforms.
3364
```bash
3465
$ rpm -ivh apache-pulsar-client*.rpm
3566
```
67+
68+
After install, Pulsar libraries will be placed under `/usr/lib`.
69+
3670
### Install Debian
3771

3872
1. Download a Debian package from the links in the table.
@@ -47,12 +81,15 @@ $ rpm -ivh apache-pulsar-client*.rpm
4781
```bash
4882
$ apt install ./apache-pulsar-client*.deb
4983
```
84+
85+
After install, Pulsar libraries will be placed under `/usr/lib`.
86+
5087
### Build
5188

5289
> If you want to build RPM and Debian packages from the latest master, follow the instructions below. All the instructions are run at the root directory of your cloned Pulsar repository.
5390
5491
There are recipes that build RPM and Debian packages containing a
55-
statically linked `libpulsar.so` / `libpulsar.a` with all the required
92+
statically linked `libpulsar.so` / `libpulsarnossl.so` / `libpulsar.a` / `libpulsarwithdeps.a` with all the required
5693
dependencies.
5794

5895
To build the C++ library packages, build the Java packages first.
@@ -71,8 +108,8 @@ This builds the RPM inside a Docker container and it leaves the RPMs in `pulsar-
71108

72109
| Package name | Content |
73110
|-----|-----|
74-
| pulsar-client | Shared library `libpulsar.so` |
75-
| pulsar-client-devel | Static library `libpulsar.a` and C++ and C headers |
111+
| pulsar-client | Shared library `libpulsar.so` and `libpulsarnossl.so` |
112+
| pulsar-client-devel | Static library `libpulsar.a`, `libpulsarwithdeps.a`and C++ and C headers |
76113
| pulsar-client-debuginfo | Debug symbols for `libpulsar.so` |
77114

78115
#### Debian
@@ -87,8 +124,8 @@ Debian packages are created at `pulsar-client-cpp/pkg/deb/BUILD/DEB/`.
87124

88125
| Package name | Content |
89126
|-----|-----|
90-
| pulsar-client | Shared library `libpulsar.so` |
91-
| pulsar-client-dev | Static library `libpulsar.a` and C++ and C headers |
127+
| pulsar-client | Shared library `libpulsar.so` and `libpulsarnossl.so` |
128+
| pulsar-client-dev | Static library `libpulsar.a`, `libpulsarwithdeps.a` and C++ and C headers |
92129

93130
## MacOS
94131

@@ -180,4 +217,4 @@ config.setAuth(pulsar::AuthTls::create(
180217
Client client("pulsar+ssl://my-broker.com:6651", config);
181218
```
182219
183-
For complete examples, refer to [C++ client examples](https://github.com/apache/pulsar/tree/master/pulsar-client-cpp/examples).
220+
For complete examples, refer to [C++ client examples](https://github.com/apache/pulsar/tree/master/pulsar-client-cpp/examples).

0 commit comments

Comments
 (0)