Skip to content
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

[C++] PIP-55: Refresh Authentication Credentials #7070

Merged
merged 1 commit into from
May 28, 2020
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
18 changes: 18 additions & 0 deletions pulsar-client-cpp/lib/ClientConnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,15 @@ void ClientConnection::handleSentPulsarConnect(const boost::system::error_code&
readNextCommand();
}

void ClientConnection::handleSentAuthResponse(const boost::system::error_code& err,
const SharedBuffer& buffer) {
if (err) {
LOG_WARN(cnxString_ << "Failed to send auth response: " << err.message());
close();
return;
}
}

/*
* Async method to establish TCP connection with broker
*
Expand Down Expand Up @@ -1026,6 +1035,15 @@ void ClientConnection::handleIncomingCommand() {
break;
}

case BaseCommand::AUTH_CHALLENGE: {
LOG_DEBUG(cnxString_ << "Received auth challenge from broker");

SharedBuffer buffer = Commands::newAuthResponse(authentication_);
asyncWrite(buffer.const_asio_buffer(),
std::bind(&ClientConnection::handleSentAuthResponse, shared_from_this(),
std::placeholders::_1, buffer));
}

case BaseCommand::ACTIVE_CONSUMER_CHANGE: {
LOG_DEBUG(cnxString_ << "Received notification about active consumer changes");
// ignore this message for now.
Expand Down
1 change: 1 addition & 0 deletions pulsar-client-cpp/lib/ClientConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
void handleHandshake(const boost::system::error_code& err);

void handleSentPulsarConnect(const boost::system::error_code& err, const SharedBuffer& buffer);
void handleSentAuthResponse(const boost::system::error_code& err, const SharedBuffer& buffer);

void readNextCommand();

Expand Down
20 changes: 20 additions & 0 deletions pulsar-client-cpp/lib/Commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ SharedBuffer Commands::newConnect(const AuthenticationPtr& authentication, const
connect->set_client_version(_PULSAR_VERSION_);
connect->set_auth_method_name(authentication->getAuthMethodName());
connect->set_protocol_version(ProtocolVersion_MAX);

FeatureFlags* flags = connect->mutable_feature_flags();
flags->set_supports_auth_refresh(true);
if (connectingThroughProxy) {
Url logicalAddressUrl;
Url::parse(logicalAddress, logicalAddressUrl);
Expand All @@ -228,6 +231,23 @@ SharedBuffer Commands::newConnect(const AuthenticationPtr& authentication, const
return writeMessageWithSize(cmd);
}

SharedBuffer Commands::newAuthResponse(const AuthenticationPtr& authentication) {
BaseCommand cmd;
cmd.set_type(BaseCommand::AUTH_RESPONSE);
CommandAuthResponse* authResponse = cmd.mutable_authresponse();
authResponse->set_client_version(_PULSAR_VERSION_);

AuthData* authData = authResponse->mutable_response();
authData->set_auth_method_name(authentication->getAuthMethodName());

AuthenticationDataPtr authDataContent;
if (authentication->getAuthData(authDataContent) == ResultOk && authDataContent->hasDataFromCommand()) {
authData->set_auth_data(authDataContent->getCommandData());
}

return writeMessageWithSize(cmd);
}

SharedBuffer Commands::newSubscribe(const std::string& topic, const std::string& subscription,
uint64_t consumerId, uint64_t requestId, CommandSubscribe_SubType subType,
const std::string& consumerName, SubscriptionMode subscriptionMode,
Expand Down
2 changes: 2 additions & 0 deletions pulsar-client-cpp/lib/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class Commands {
static SharedBuffer newConnect(const AuthenticationPtr& authentication, const std::string& logicalAddress,
bool connectingThroughProxy);

static SharedBuffer newAuthResponse(const AuthenticationPtr& authentication);

static SharedBuffer newPartitionMetadataRequest(const std::string& topic, uint64_t requestId);

static SharedBuffer newLookup(const std::string& topic, const bool authoritative, uint64_t requestId);
Expand Down