merlimat closed pull request #1913: Link internal C++ authentication plugins 
inside client library
URL: https://github.com/apache/incubator-pulsar/pull/1913
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pulsar-client-cpp/include/pulsar/Authentication.h 
b/pulsar-client-cpp/include/pulsar/Authentication.h
index ea3536db4a..7656477604 100644
--- a/pulsar-client-cpp/include/pulsar/Authentication.h
+++ b/pulsar-client-cpp/include/pulsar/Authentication.h
@@ -68,9 +68,23 @@ class Authentication {
     friend class ClientConfiguration;
 };
 
+/**
+ * AuthFactory is used to create instances of Authentication class when
+ * configuring a Client instance. It loads the authentication from an
+ * external plugin.
+ *
+ * To use authentication methods that are internally supported, you should
+ * use `AuthTls::create("my-cert.pem", "my-private.key")` or similar.
+ */
 class AuthFactory {
    public:
     static AuthenticationPtr Disabled();
+
+    /**
+     * Create
+     * @param dynamicLibPath
+     * @return
+     */
     static AuthenticationPtr create(const std::string& dynamicLibPath);
     static AuthenticationPtr create(const std::string& dynamicLibPath, const 
std::string& authParamsString);
     static AuthenticationPtr create(const std::string& dynamicLibPath, 
ParamMap& params);
@@ -80,8 +94,39 @@ class AuthFactory {
     static std::vector<void*> loadedLibrariesHandles_;
     static void release_handles();
 };
+
+/**
+ * TLS implementation of Pulsar client authentication
+ */
+class AuthTls : public Authentication {
+   public:
+    AuthTls(AuthenticationDataPtr&);
+    ~AuthTls();
+    static AuthenticationPtr create(const std::string& certificatePath, const 
std::string& privateKeyPath);
+    const std::string getAuthMethodName() const;
+    Result getAuthData(AuthenticationDataPtr& authDataTls) const;
+
+   private:
+    AuthenticationDataPtr authDataTls_;
+};
+
+/**
+ * Athenz implementation of Pulsar client authentication
+ */
+class AuthAthenz : public Authentication {
+   public:
+    AuthAthenz(AuthenticationDataPtr&);
+    ~AuthAthenz();
+    static AuthenticationPtr create(ParamMap& params);
+    static AuthenticationPtr create(const std::string& authParamsString);
+    const std::string getAuthMethodName() const;
+    Result getAuthData(AuthenticationDataPtr& authDataAthenz) const;
+
+   private:
+    AuthenticationDataPtr authDataAthenz_;
+};
+
 }  // namespace pulsar
-// namespace pulsar
 
 #pragma GCC visibility pop
 
diff --git a/pulsar-client-cpp/include/pulsar/c/authentication.h 
b/pulsar-client-cpp/include/pulsar/c/authentication.h
index bd29d5f8f3..c3a4d32669 100644
--- a/pulsar-client-cpp/include/pulsar/c/authentication.h
+++ b/pulsar-client-cpp/include/pulsar/c/authentication.h
@@ -30,6 +30,11 @@ typedef struct _pulsar_authentication 
pulsar_authentication_t;
 pulsar_authentication_t *pulsar_authentication_create(const char 
*dynamicLibPath,
                                                       const char 
*authParamsString);
 
+pulsar_authentication_t *pulsar_authentication_tls_create(const char 
*certificatePath,
+                                                          const char 
*privateKeyPath);
+
+pulsar_authentication_t *pulsar_authentication_athenz_create(const char 
*authParamsString);
+
 void pulsar_authentication_free(pulsar_authentication_t *authentication);
 
 #pragma GCC visibility pop
diff --git a/pulsar-client-cpp/lib/CMakeLists.txt 
b/pulsar-client-cpp/lib/CMakeLists.txt
index 74dbc08dcd..1d3862b21f 100644
--- a/pulsar-client-cpp/lib/CMakeLists.txt
+++ b/pulsar-client-cpp/lib/CMakeLists.txt
@@ -42,7 +42,8 @@ endif(NOT LIBRARY_VERSION)
 set_target_properties(pulsarStatic PROPERTIES OUTPUT_NAME ${LIB_NAME} VERSION 
${LIBRARY_VERSION})
 set_target_properties(pulsarShared PROPERTIES OUTPUT_NAME ${LIB_NAME} VERSION 
${LIBRARY_VERSION})
 
-target_link_libraries(pulsarStatic ${COMMON_LIBS})
-target_link_libraries(pulsarShared ${COMMON_LIBS})
-
 add_subdirectory(auth)
+
+target_link_libraries(pulsarStatic ${COMMON_LIBS} authTls authAthenz 
ztsClientStatic)
+target_link_libraries(pulsarShared ${COMMON_LIBS} authTls authAthenz 
ztsClientStatic)
+
diff --git a/pulsar-client-cpp/lib/auth/AuthAthenz.cc 
b/pulsar-client-cpp/lib/auth/AuthAthenz.cc
index fcbf888a1b..b5efa82524 100644
--- a/pulsar-client-cpp/lib/auth/AuthAthenz.cc
+++ b/pulsar-client-cpp/lib/auth/AuthAthenz.cc
@@ -100,8 +100,4 @@ extern "C" Authentication* create(const std::string& 
authParamsString) {
     return new AuthAthenz(authDataAthenz);
 }
 
-extern "C" Authentication* createFromMap(ParamMap& params) {
-    AuthenticationDataPtr authDataAthenz = AuthenticationDataPtr(new 
AuthDataAthenz(params));
-    return new AuthAthenz(authDataAthenz);
-}
 }  // namespace pulsar
diff --git a/pulsar-client-cpp/lib/auth/AuthAthenz.h 
b/pulsar-client-cpp/lib/auth/AuthAthenz.h
index 1ec933eb6d..7add5fc4d6 100644
--- a/pulsar-client-cpp/lib/auth/AuthAthenz.h
+++ b/pulsar-client-cpp/lib/auth/AuthAthenz.h
@@ -21,8 +21,6 @@
 
 #include <pulsar/Authentication.h>
 #include <lib/auth/athenz/ZTSClient.h>
-#include <lib/LogUtils.h>
-#include <iostream>
 #include <string>
 
 namespace pulsar {
@@ -40,17 +38,5 @@ class AuthDataAthenz : public AuthenticationDataProvider {
     boost::shared_ptr<ZTSClient> ztsClient_;
 };
 
-class AuthAthenz : public Authentication {
-   public:
-    AuthAthenz(AuthenticationDataPtr&);
-    ~AuthAthenz();
-    static AuthenticationPtr create(ParamMap& params);
-    static AuthenticationPtr create(const std::string& authParamsString);
-    const std::string getAuthMethodName() const;
-    Result getAuthData(AuthenticationDataPtr& authDataAthenz) const;
-
-   private:
-    AuthenticationDataPtr authDataAthenz_;
-};
 }  // namespace pulsar
 #endif /* PULSAR_AUTH_ATHENZ_H_ */
diff --git a/pulsar-client-cpp/lib/auth/AuthTls.cc 
b/pulsar-client-cpp/lib/auth/AuthTls.cc
index f594a47645..d449a39173 100644
--- a/pulsar-client-cpp/lib/auth/AuthTls.cc
+++ b/pulsar-client-cpp/lib/auth/AuthTls.cc
@@ -19,16 +19,16 @@
 #include <lib/auth/AuthTls.h>
 
 namespace pulsar {
-AuthDataTls::AuthDataTls(ParamMap& params) {
-    tlsCertificates_ = params["tlsCertFile"];
-    tlsPrivateKey_ = params["tlsKeyFile"];
+AuthDataTls::AuthDataTls(const std::string& certificatePath, const 
std::string& privateKeyPath) {
+    tlsCertificate_ = certificatePath;
+    tlsPrivateKey_ = privateKeyPath;
 }
 
 AuthDataTls::~AuthDataTls() {}
 
 bool AuthDataTls::hasDataForTls() { return true; }
 
-std::string AuthDataTls::getTlsCertificates() { return tlsCertificates_; }
+std::string AuthDataTls::getTlsCertificates() { return tlsCertificate_; }
 
 std::string AuthDataTls::getTlsPrivateKey() { return tlsPrivateKey_; }
 
@@ -36,8 +36,9 @@ AuthTls::AuthTls(AuthenticationDataPtr& authDataTls) { 
authDataTls_ = authDataTl
 
 AuthTls::~AuthTls() {}
 
-AuthenticationPtr AuthTls::create(ParamMap& params) {
-    AuthenticationDataPtr authDataTls = AuthenticationDataPtr(new 
AuthDataTls(params));
+AuthenticationPtr AuthTls::create(const std::string& certificatePath, const 
std::string& privateKeyPath) {
+    AuthenticationDataPtr authDataTls =
+        AuthenticationDataPtr(new AuthDataTls(certificatePath, 
privateKeyPath));
     return AuthenticationPtr(new AuthTls(authDataTls));
 }
 
@@ -48,8 +49,4 @@ Result AuthTls::getAuthData(AuthenticationDataPtr& 
authDataContent) const {
     return ResultOk;
 }
 
-extern "C" Authentication* createFromMap(ParamMap& params) {
-    AuthenticationDataPtr authDataTls = AuthenticationDataPtr(new 
AuthDataTls(params));
-    return new AuthTls(authDataTls);
-}
 }  // namespace pulsar
diff --git a/pulsar-client-cpp/lib/auth/AuthTls.h 
b/pulsar-client-cpp/lib/auth/AuthTls.h
index 8e5dc2fb0a..e9b711d9dc 100644
--- a/pulsar-client-cpp/lib/auth/AuthTls.h
+++ b/pulsar-client-cpp/lib/auth/AuthTls.h
@@ -16,39 +16,26 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifndef PULSAR_AUTH_TLS_H_
-#define PULSAR_AUTH_TLS_H_
+
+#pragma once
 
 #include <pulsar/Authentication.h>
-#include <lib/LogUtils.h>
-#include <iostream>
 #include <string>
 
 namespace pulsar {
 
 class AuthDataTls : public AuthenticationDataProvider {
    public:
-    AuthDataTls(ParamMap& params);
+    AuthDataTls(const std::string& certificatePath, const std::string& 
privateKeyPath);
     ~AuthDataTls();
+
     bool hasDataForTls();
     std::string getTlsCertificates();
     std::string getTlsPrivateKey();
 
    private:
-    std::string tlsCertificates_;
+    std::string tlsCertificate_;
     std::string tlsPrivateKey_;
 };
 
-class AuthTls : public Authentication {
-   public:
-    AuthTls(AuthenticationDataPtr&);
-    ~AuthTls();
-    static AuthenticationPtr create(ParamMap& params);
-    const std::string getAuthMethodName() const;
-    Result getAuthData(AuthenticationDataPtr& authDataTls) const;
-
-   private:
-    AuthenticationDataPtr authDataTls_;
-};
 }  // namespace pulsar
-#endif /* PULSAR_AUTH_TLS_H_ */
diff --git a/pulsar-client-cpp/lib/auth/CMakeLists.txt 
b/pulsar-client-cpp/lib/auth/CMakeLists.txt
index 1a6b4f0652..1fbf92f00b 100644
--- a/pulsar-client-cpp/lib/auth/CMakeLists.txt
+++ b/pulsar-client-cpp/lib/auth/CMakeLists.txt
@@ -17,12 +17,7 @@
 # under the License.
 #
 
-add_library(authTls MODULE AuthTls.cc)
-target_link_libraries(authTls ${CLIENT_LIBS})
-set_target_properties(authTls PROPERTIES OUTPUT_NAME authtls)
-
-add_library(authAthenz MODULE AuthAthenz.cc)
-target_link_libraries(authAthenz ${CLIENT_LIBS} ztsClient)
-set_target_properties(authAthenz PROPERTIES OUTPUT_NAME authathenz)
+add_library(authTls STATIC AuthTls.cc)
+add_library(authAthenz STATIC AuthAthenz.cc)
 
 add_subdirectory(athenz)
diff --git a/pulsar-client-cpp/lib/auth/athenz/CMakeLists.txt 
b/pulsar-client-cpp/lib/auth/athenz/CMakeLists.txt
index 05bc2d9c6d..a2335cc057 100644
--- a/pulsar-client-cpp/lib/auth/athenz/CMakeLists.txt
+++ b/pulsar-client-cpp/lib/auth/athenz/CMakeLists.txt
@@ -17,5 +17,4 @@
 # under the License.
 #
 
-add_library(ztsClient SHARED ZTSClient.cc)
-target_link_libraries(ztsClient ${CLIENT_LIBS})
+add_library(ztsClientStatic STATIC ZTSClient.cc)
diff --git a/pulsar-client-cpp/lib/c/c_Authentication.cc 
b/pulsar-client-cpp/lib/c/c_Authentication.cc
index 5cd2a64e00..c03f239feb 100644
--- a/pulsar-client-cpp/lib/c/c_Authentication.cc
+++ b/pulsar-client-cpp/lib/c/c_Authentication.cc
@@ -30,4 +30,17 @@ pulsar_authentication_t *pulsar_authentication_create(const 
char *dynamicLibPath
     return authentication;
 }
 
-void pulsar_authentication_free(pulsar_authentication_t *authentication) { 
delete authentication; }
\ No newline at end of file
+void pulsar_authentication_free(pulsar_authentication_t *authentication) { 
delete authentication; }
+
+pulsar_authentication_t *pulsar_authentication_tls_create(const char 
*certificatePath,
+                                                          const char 
*privateKeyPath) {
+    pulsar_authentication_t *authentication = new pulsar_authentication_t;
+    authentication->auth = pulsar::AuthTls::create(certificatePath, 
privateKeyPath);
+    return authentication;
+}
+
+pulsar_authentication_t *pulsar_authentication_athenz_create(const char 
*authParamsString) {
+    pulsar_authentication_t *authentication = new pulsar_authentication_t;
+    authentication->auth = pulsar::AuthAthenz::create(authParamsString);
+    return authentication;
+}
\ No newline at end of file
diff --git a/pulsar-client-cpp/python/CMakeLists.txt 
b/pulsar-client-cpp/python/CMakeLists.txt
index a4ff679236..9a79ea3060 100644
--- a/pulsar-client-cpp/python/CMakeLists.txt
+++ b/pulsar-client-cpp/python/CMakeLists.txt
@@ -30,9 +30,11 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
   set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS 
"${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS} -undefined dynamic_lookup")
 endif()
 
+set(PYTHON_WRAPPER_LIBS ${Boost_PYTHON_LIBRARY} ${Boost_PYTHON3_LIBRARY} 
authTls authAthenz)
+
 if (APPLE)
-    target_link_libraries(_pulsar -Wl,-all_load pulsarStatic 
${Boost_PYTHON_LIBRARY} ${Boost_PYTHON3_LIBRARY})
+    target_link_libraries(_pulsar -Wl,-all_load pulsarStatic 
${PYTHON_WRAPPER_LIBS})
 else ()
     set (CMAKE_SHARED_LINKER_FLAGS " -static-libgcc  -static-libstdc++")
-    target_link_libraries(_pulsar pulsarStatic ${Boost_PYTHON_LIBRARY} 
${Boost_PYTHON3_LIBRARY})
+    target_link_libraries(_pulsar pulsarStatic ${PYTHON_WRAPPER_LIBS})
 endif ()
diff --git a/pulsar-client-cpp/tests/AuthPluginTest.cc 
b/pulsar-client-cpp/tests/AuthPluginTest.cc
index 1892178a76..0f7b3c7e3e 100644
--- a/pulsar-client-cpp/tests/AuthPluginTest.cc
+++ b/pulsar-client-cpp/tests/AuthPluginTest.cc
@@ -42,28 +42,24 @@ static void sendCallBackTls(Result r, const Message& msg) {
     LOG_DEBUG("Received publish acknowledgement for " << 
msg.getDataAsString());
 }
 
-TEST(AuthPluginTest, testCreate) {
-    pulsar::AuthenticationDataPtr data;
+TEST(AuthPluginTest, testTls) {
+    ClientConfiguration config = ClientConfiguration();
+    config.setUseTls(true);
+    
config.setTlsTrustCertsFilePath("../../pulsar-broker/src/test/resources/authentication/tls/cacert.pem");
+    config.setTlsAllowInsecureConnection(false);
+    AuthenticationPtr auth =
+        
pulsar::AuthTls::create("../../pulsar-broker/src/test/resources/authentication/tls/client-cert.pem",
+                                
"../../pulsar-broker/src/test/resources/authentication/tls/client-key.pem");
 
-    pulsar::AuthenticationPtr auth = 
pulsar::AuthFactory::create("../lib/auth/libauthtls.so");
     ASSERT_TRUE(auth != NULL);
     ASSERT_EQ(auth->getAuthMethodName(), "tls");
+
+    pulsar::AuthenticationDataPtr data;
     ASSERT_EQ(auth->getAuthData(data), pulsar::ResultOk);
     ASSERT_EQ(data->getCommandData(), "none");
     ASSERT_EQ(data->hasDataForTls(), true);
     ASSERT_EQ(auth.use_count(), 1);
-}
 
-TEST(AuthPluginTest, testTls) {
-    ClientConfiguration config = ClientConfiguration();
-    config.setUseTls(true);
-    std::string certfile = 
"../../pulsar-broker/src/test/resources/authentication/tls/cacert.pem";
-    std::string params =
-        
"tlsCertFile:../../pulsar-broker/src/test/resources/authentication/tls/client-cert.pem,tlsKeyFile:../"
-        
"../pulsar-broker/src/test/resources/authentication/tls/client-key.pem";
-    config.setTlsTrustCertsFilePath(certfile);
-    config.setTlsAllowInsecureConnection(false);
-    AuthenticationPtr auth = 
pulsar::AuthFactory::create("../lib/auth/libauthtls.so", params);
     config.setAuth(auth);
     Client client(lookupUrlTls, config);
 
@@ -164,7 +160,7 @@ TEST(AuthPluginTest, testAthenz) {
         "privateKey": 
"file:../../pulsar-broker/src/test/resources/authentication/tls/client-key.pem",
         "ztsUrl": "http://localhost:9999";
     })";
-    pulsar::AuthenticationPtr auth = 
pulsar::AuthFactory::create("../lib/auth/libauthathenz.so", params);
+    pulsar::AuthenticationPtr auth = pulsar::AuthAthenz::create(params);
     ASSERT_EQ(auth->getAuthMethodName(), "athenz");
     ASSERT_EQ(auth->getAuthData(data), pulsar::ResultOk);
     ASSERT_EQ(data->hasDataForHttp(), true);
diff --git a/pulsar-client-cpp/tests/CMakeLists.txt 
b/pulsar-client-cpp/tests/CMakeLists.txt
index 3f27ffed14..598162738a 100644
--- a/pulsar-client-cpp/tests/CMakeLists.txt
+++ b/pulsar-client-cpp/tests/CMakeLists.txt
@@ -25,4 +25,4 @@ add_executable(main ${TEST_SOURCES})
 
 target_include_directories(main PRIVATE ${CMAKE_SOURCE_DIR}/lib)
 
-target_link_libraries(main ${CLIENT_LIBS} ${GMOCK_LIBRARY_PATH} ztsClient)
+target_link_libraries(main ${CLIENT_LIBS} ${GMOCK_LIBRARY_PATH})


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to