Skip to content

Commit 27cba3e

Browse files
Fix tlsTrustCertsFilePath config is not applied for OAuth2 (#364)
### Motivation #313 has reverted the fix of #190, which applies the `tlsTrustCertsFilePath` config for OAuth2 authentication. The macOS pre-built libraries are affected most because the bundled CA path is empty. ### Modification Apply the `tlsTrustCertsFilePath` for OAuth2.
1 parent 24ab12c commit 27cba3e

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

lib/ClientConfiguration.cc

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <stdexcept>
2020

2121
#include "ClientConfigurationImpl.h"
22+
#include "auth/AuthOauth2.h"
2223

2324
namespace pulsar {
2425

lib/ClientConnection.cc

+10-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "PulsarApi.pb.h"
3434
#include "ResultUtils.h"
3535
#include "Url.h"
36+
#include "auth/AuthOauth2.h"
3637
#include "auth/InitialAuthData.h"
3738
#include "checksum/ChecksumProvider.h"
3839

@@ -193,6 +194,14 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
193194
return;
194195
}
195196

197+
auto oauth2Auth = std::dynamic_pointer_cast<AuthOauth2>(authentication_);
198+
if (oauth2Auth) {
199+
// Configure the TLS trust certs file for Oauth2
200+
auto authData = std::dynamic_pointer_cast<AuthenticationDataProvider>(
201+
std::make_shared<InitialAuthData>(clientConfiguration.getTlsTrustCertsFilePath()));
202+
oauth2Auth->getAuthData(authData);
203+
}
204+
196205
if (clientConfiguration.isUseTls()) {
197206
#if BOOST_VERSION >= 105400
198207
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12_client);
@@ -223,8 +232,7 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
223232
std::string tlsCertificates = clientConfiguration.getTlsCertificateFilePath();
224233
std::string tlsPrivateKey = clientConfiguration.getTlsPrivateKeyFilePath();
225234

226-
auto authData = std::dynamic_pointer_cast<AuthenticationDataProvider>(
227-
std::make_shared<InitialAuthData>(clientConfiguration.getTlsTrustCertsFilePath()));
235+
AuthenticationDataPtr authData;
228236
if (authentication_->getAuthData(authData) == ResultOk && authData->hasDataForTls()) {
229237
tlsCertificates = authData->getTlsCertificates();
230238
tlsPrivateKey = authData->getTlsPrivateKey();

lib/auth/AuthOauth2.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,13 @@ Oauth2TokenResultPtr ClientCredentialFlow::authenticate() {
342342

343343
CurlWrapper::Options options;
344344
options.postFields = std::move(postData);
345-
auto result =
346-
curl.get(tokenEndPoint_, "Content-Type: application/x-www-form-urlencoded", options, nullptr);
345+
std::unique_ptr<CurlWrapper::TlsContext> tlsContext;
346+
if (!tlsTrustCertsFilePath_.empty()) {
347+
tlsContext.reset(new CurlWrapper::TlsContext);
348+
tlsContext->trustCertsFilePath = tlsTrustCertsFilePath_;
349+
}
350+
auto result = curl.get(tokenEndPoint_, "Content-Type: application/x-www-form-urlencoded", options,
351+
tlsContext.get());
347352
if (!result.error.empty()) {
348353
LOG_ERROR("Failed to get the well-known configuration " << issuerUrl_ << ": " << result.error);
349354
return resultPtr;

run-unit-tests.sh

+8-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ docker compose -f tests/oauth2/docker-compose.yml up -d
3535
# Wait until the namespace is created, currently there is no good way to check it
3636
# because it's hard to configure OAuth2 authentication via CLI.
3737
sleep 15
38-
$CMAKE_BUILD_DIRECTORY/tests/Oauth2Test
38+
$CMAKE_BUILD_DIRECTORY/tests/Oauth2Test --gtest_filter='-*testTlsTrustFilePath'
39+
if [[ -f /etc/ssl/certs/ca-certificates.crt ]]; then
40+
sudo mv /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/my-cert.crt
41+
fi
42+
$CMAKE_BUILD_DIRECTORY/tests/Oauth2Test --gtest_filter='*testTlsTrustFilePath'
43+
if [[ -f /etc/ssl/certs/my-cert.crt ]]; then
44+
sudo mv /etc/ssl/certs/my-cert.crt /etc/ssl/certs/ca-certificates.crt
45+
fi
3946
docker compose -f tests/oauth2/docker-compose.yml down
4047

4148
# Run BrokerMetadata tests

tests/oauth2/Oauth2Test.cc

+21
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <boost/property_tree/json_parser.hpp>
2424
#include <boost/property_tree/ptree.hpp>
25+
#include <fstream>
2526

2627
#include "lib/Base64Utils.h"
2728

@@ -64,6 +65,26 @@ TEST(Oauth2Test, testWrongUrl) {
6465
ASSERT_EQ(ResultAuthenticationError, testCreateProducer("my-protocol:" + gKeyPath));
6566
}
6667

68+
TEST(Oauth2Test, testTlsTrustFilePath) {
69+
const auto caPath = "/etc/ssl/certs/my-cert.crt";
70+
std::ifstream fin{caPath};
71+
if (!fin) { // Skip this test if the CA cert is not prepared
72+
return;
73+
}
74+
fin.close();
75+
76+
ClientConfiguration conf;
77+
conf.setTlsTrustCertsFilePath(caPath);
78+
auto params = gCommonParams;
79+
params["private_key"] = "file://" + gKeyPath;
80+
conf.setAuth(AuthOauth2::create(params));
81+
82+
Client client{"pulsar://localhost:6650", conf};
83+
Producer producer;
84+
ASSERT_EQ(ResultOk, client.createProducer("oauth2-test", producer));
85+
client.close();
86+
}
87+
6788
int main(int argc, char* argv[]) {
6889
std::cout << "Load Oauth2 configs from " << gKeyPath << "..." << std::endl;
6990
boost::property_tree::ptree root;

0 commit comments

Comments
 (0)