Skip to content

[fix] Use ClientConfiguration::getTlsTrustCertsFilePath for the OAuth2 flow #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/ClientConnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "ProducerImpl.h"
#include "PulsarApi.pb.h"
#include "Url.h"
#include "auth/InitialAuthData.h"
#include "checksum/ChecksumProvider.h"

DECLARE_LOG_OBJECT()
Expand Down Expand Up @@ -225,7 +226,8 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
std::string tlsCertificates = clientConfiguration.getTlsCertificateFilePath();
std::string tlsPrivateKey = clientConfiguration.getTlsPrivateKeyFilePath();

AuthenticationDataPtr authData;
auto authData = std::dynamic_pointer_cast<AuthenticationDataProvider>(
std::make_shared<InitialAuthData>(clientConfiguration.getTlsTrustCertsFilePath()));
if (authentication_->getAuthData(authData) == ResultOk && authData->hasDataForTls()) {
tlsCertificates = authData->getTlsCertificates();
tlsPrivateKey = authData->getTlsPrivateKey();
Expand Down
18 changes: 18 additions & 0 deletions lib/auth/AuthOauth2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <sstream>
#include <stdexcept>

#include "InitialAuthData.h"
#include "lib/LogUtils.h"
DECLARE_LOG_OBJECT()

Expand Down Expand Up @@ -191,6 +192,10 @@ void ClientCredentialFlow::initialize() {
char errorBuffer[CURL_ERROR_SIZE];
curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errorBuffer);

if (!tlsTrustCertsFilePath_.empty()) {
curl_easy_setopt(handle, CURLOPT_CAINFO, tlsTrustCertsFilePath_.c_str());
}

// Make get call to server
res = curl_easy_perform(handle);

Expand Down Expand Up @@ -317,6 +322,10 @@ Oauth2TokenResultPtr ClientCredentialFlow::authenticate() {
char errorBuffer[CURL_ERROR_SIZE];
curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errorBuffer);

if (!tlsTrustCertsFilePath_.empty()) {
curl_easy_setopt(handle, CURLOPT_CAINFO, tlsTrustCertsFilePath_.c_str());
}

// Make get call to server
res = curl_easy_perform(handle);

Expand Down Expand Up @@ -401,6 +410,15 @@ AuthenticationPtr AuthOauth2::create(ParamMap& params) { return AuthenticationPt
const std::string AuthOauth2::getAuthMethodName() const { return "token"; }

Result AuthOauth2::getAuthData(AuthenticationDataPtr& authDataContent) {
auto initialAuthData = std::dynamic_pointer_cast<InitialAuthData>(authDataContent);
if (initialAuthData) {
auto flowPtr = std::dynamic_pointer_cast<ClientCredentialFlow>(flowPtr_);
if (!flowPtr_) {
throw std::invalid_argument("AuthOauth2::flowPtr_ is not a ClientCredentialFlow");
}
flowPtr->setTlsTrustCertsFilePath(initialAuthData->tlsTrustCertsFilePath_);
}

if (cachedTokenPtr_ == nullptr || cachedTokenPtr_->isExpired()) {
try {
cachedTokenPtr_ = CachedTokenPtr(new Oauth2CachedToken(flowPtr_->authenticate()));
Expand Down
5 changes: 5 additions & 0 deletions lib/auth/AuthOauth2.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,17 @@ class ClientCredentialFlow : public Oauth2Flow {
ParamMap generateParamMap() const;
std::string getTokenEndPoint() const;

void setTlsTrustCertsFilePath(const std::string& tlsTrustCertsFilePath) {
tlsTrustCertsFilePath_ = tlsTrustCertsFilePath;
}

private:
std::string tokenEndPoint_;
const std::string issuerUrl_;
const KeyFile keyFile_;
const std::string audience_;
const std::string scope_;
std::string tlsTrustCertsFilePath_;
std::once_flag initializeOnce_;
};

Expand Down
39 changes: 39 additions & 0 deletions lib/auth/InitialAuthData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#pragma once

#include <pulsar/Authentication.h>

namespace pulsar {

class ClientConfiguration;

struct InitialAuthData : public AuthenticationDataProvider {
const std::string tlsTrustCertsFilePath_;

InitialAuthData(const std::string& tlsTrustCertsFilePath)
: tlsTrustCertsFilePath_(tlsTrustCertsFilePath) {}

bool hasDataForHttp() override { return false; }
std::string getHttpHeaders() override { return ""; }
bool hasDataFromCommand() override { return false; }
std::string getCommandData() override { return ""; }
};

} // namespace pulsar