Skip to content

[improve][client] Implement tls_client_auth for AuthOauth2 #575

Open
izumo27 wants to merge 3 commits into
apache:mainfrom
izumo27:add_tls-client-auth
Open

[improve][client] Implement tls_client_auth for AuthOauth2 #575
izumo27 wants to merge 3 commits into
apache:mainfrom
izumo27:add_tls-client-auth

Conversation

@izumo27
Copy link
Copy Markdown
Contributor

@izumo27 izumo27 commented May 11, 2026

Motivation

Currently, AuthOauth2 supports only client_secret_post.
This PR adds tls_client_auth.
cf. apache/pulsar#25538

Modifications

  • Allow configuration of tls_cert_file and tls_key_file, and make these two parameters required when using tls_client_auth.
  • Commonized the logic for setting the TLS context and getting tokens.

Verifying this change

(Please pick either of the following options)

This change added tests and can be verified as follows:

  • Added tests for tls_client_auth
  • Implement a mock server for E2E tests

Documentation

  • doc-required
    (Your PR needs to update docs and you will update later)

  • doc-not-needed
    (Please explain why)

  • doc
    (Your PR contains doc changes)

  • doc-complete
    (Docs have been already added)

Comment on lines +544 to +546
* For `tokenEndpointAuthMethod = "client_secret_post"` (default), the required parameter
* keys are “issuer_url”, “private_key”, and “audience”.
* Optional keys: `scope`, `tls_cert_file`, `tls_key_file`.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the RFC defines audience as an optional parameter, it remains required for client_secret_post to avoid a breaking change.

@BewareMyPower BewareMyPower added this to the 4.2.0 milestone May 11, 2026
@BewareMyPower BewareMyPower requested a review from Copilot May 11, 2026 10:14
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds OAuth2 tls_client_auth support to the C++ client, including mTLS client cert/key configuration, shared TLS/token retrieval logic, and new E2E-style tests using a local mock TLS server.

Changes:

  • Implement TlsClientAuthFlow and select OAuth2 flow by tokenEndpointAuthMethod.
  • Refactor token endpoint discovery and token fetching into shared helpers with TLS context handling.
  • Add mock TLS OAuth2 server + tests for success/failure paths; update public API docs.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 9 comments.

File Description
tests/AuthPluginTest.cc Adds a mock TLS OAuth2 server and new tests for tls_client_auth behavior.
lib/auth/AuthOauth2.h Introduces TlsClientAuthFlow and adds mTLS-related fields to flows.
lib/auth/AuthOauth2.cc Implements flow selection, shared well-known/token fetching, and TLS context creation.
include/pulsar/Authentication.h Documents tokenEndpointAuthMethod and the new tls_client_auth parameters.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/AuthPluginTest.cc
Comment on lines +574 to +576
const std::string response = "HTTP/1.1 200 OK\nContent-Type: " + responseContentType_ +
"\nContent-Length: " + std::to_string(responseBody_.size()) +
"\nConnection: close\n\n" + responseBody_;
Comment thread tests/AuthPluginTest.cc
Comment on lines +824 to +825
ASSERT_TRUE(wellKnownFuture.get());
ASSERT_TRUE(tokenFuture.get());
Comment thread tests/AuthPluginTest.cc
Comment on lines +829 to +830
wellKnownThread.join();
tokenThread.join();
Comment thread lib/auth/AuthOauth2.cc
Comment on lines +244 to +263
static std::unique_ptr<CurlWrapper::TlsContext> createTlsContext(const std::string& tlsTrustCertsFilePath,
const std::string& tlsCertFilePath,
const std::string& tlsKeyFilePath,
OAuth2TokenEndpointAuthMethod authMethod) {
if (tlsTrustCertsFilePath.empty() && tlsCertFilePath.empty() && tlsKeyFilePath.empty()) {
return nullptr;
}

void ClientCredentialFlow::initialize() {
if (issuerUrl_.empty()) {
LOG_ERROR("Failed to initialize ClientCredentialFlow: issuer_url is not set");
return;
auto tlsContext = std::unique_ptr<CurlWrapper::TlsContext>(new CurlWrapper::TlsContext);
if (!tlsTrustCertsFilePath.empty()) {
tlsContext->trustCertsFilePath = tlsTrustCertsFilePath;
}
if (!keyFile_.isValid()) {
return;
if (!tlsCertFilePath.empty() && !tlsKeyFilePath.empty()) {
tlsContext->certPath = tlsCertFilePath;
tlsContext->keyPath = tlsKeyFilePath;
} else if (authMethod == OAuth2TokenEndpointAuthMethod::TlsClientAuth) {
LOG_WARN("Ignore incomplete mTLS settings: both tls_cert_file and tls_key_file are required");
}
return tlsContext;
}
Comment thread lib/auth/AuthOauth2.cc
Comment on lines 338 to 341
if (!result.error.empty()) {
LOG_ERROR("Failed to get the well-known configuration " << issuerUrl_ << ": " << result.error);
return;
LOG_ERROR("Failed to get the well-known configuration " << issuerUrl << ": " << result.error);
return resultPtr;
}
Comment thread lib/auth/AuthOauth2.cc
Comment on lines +358 to +360
LOG_ERROR("Failed to parse json of Oauth2 response: "
<< e.what() << "\nInput Json = " << responseData
<< " passedin: " << options.postFields);
Comment thread lib/auth/AuthOauth2.cc
Comment on lines +377 to +378
LOG_ERROR("Response failed for issuerurl " << issuerUrl << ". response Code " << responseCode
<< " passedin: " << options.postFields);
Comment thread lib/auth/AuthOauth2.cc
Comment on lines +382 to +383
LOG_ERROR("Response failed for issuerurl " << issuerUrl << ". ErrorCode " << res << ": "
<< errorBuffer << " passedin: " << options.postFields);
Comment on lines 520 to 521
* "client_id": "d9ZyX97q1ef8Cr81WHVC4hFQ64vSlDK3",
* "client_secret": "on1uJ...k6F6R",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants