Skip to content

Commit 515517d

Browse files
committed
Modify authenticator and initialconnectionhandler to handle authentication
Signed-off-by: Emelia Lei <[email protected]>
1 parent aa3ac48 commit 515517d

10 files changed

+166
-71
lines changed

src/groups/mqb/mqba/mqba_application.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <mqbscm_version.h>
2020
// MQB
21+
#include <mqba_authenticator.h>
2122
#include <mqba_configprovider.h>
2223
#include <mqba_dispatcher.h>
2324
#include <mqba_domainmanager.h>
@@ -316,6 +317,11 @@ int Application::start(bsl::ostream& errorDescription)
316317
}
317318

318319
// Start the transport manager
320+
bslma::ManagedPtr<mqbnet::Authenticator> authenticatorMp(
321+
new (*d_allocator_p)
322+
Authenticator(&d_blobSpPool, d_allocators.get("Authenticator")),
323+
d_allocator_p);
324+
319325
SessionNegotiator* sessionNegotiator = new (*d_allocator_p)
320326
SessionNegotiator(&d_bufferFactory,
321327
d_dispatcher_mp.get(),
@@ -339,6 +345,7 @@ int Application::start(bsl::ostream& errorDescription)
339345
bslma::ManagedPtr<mqbnet::InitialConnectionHandler>
340346
initialConnectionHandlerMp(
341347
new (*d_allocator_p) InitialConnectionHandler(
348+
authenticatorMp,
342349
negotiatorMp,
343350
d_allocators.get("InitialConnectionHandler")),
344351
d_allocator_p);

src/groups/mqb/mqba/mqba_authenticator.cpp

+11-20
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ int Authenticator::onAuthenticationRequest(
8282
response.lifetimeMs() = 10 * 60 * 1000;
8383
}
8484

85-
BALL_LOG_INFO << "send authn response " << authenticationResponse;
86-
8785
int rc = sendAuthenticationMessage(errorDescription,
8886
authenticationResponse,
8987
context);
@@ -95,7 +93,9 @@ int Authenticator::onAuthenticationResponse(
9593
bsl::ostream& errorDescription,
9694
const AuthenticationContextSp& context)
9795
{
98-
return 0;
96+
BALL_LOG_ERROR << "Not Implemented";
97+
98+
return -1;
9999
}
100100

101101
int Authenticator::sendAuthenticationMessage(
@@ -162,6 +162,8 @@ Authenticator::~Authenticator()
162162
}
163163

164164
int Authenticator::handleAuthenticationOnMsgType(
165+
bsl::ostream& errorDescription,
166+
bool* isContinueRead,
165167
const AuthenticationContextSp& context)
166168
{
167169
enum RcEnum {
@@ -176,33 +178,22 @@ int Authenticator::handleAuthenticationOnMsgType(
176178
switch (context->d_authenticationMessage.selectionId()) {
177179
case bmqp_ctrlmsg::AuthenticationMessage::
178180
SELECTION_ID_AUTHENTICATE_REQUEST: {
179-
BALL_LOG_INFO << "Received authn request: "
180-
<< context->d_authenticationMessage;
181181
rc = onAuthenticationRequest(errStream, context);
182182
} break; // BREAK
183183
case bmqp_ctrlmsg::AuthenticationMessage::
184184
SELECTION_ID_AUTHENTICATE_RESPONSE: {
185-
BALL_LOG_INFO << "Received authn response: "
186-
<< context->d_authenticationMessage;
185+
rc = onAuthenticationResponse(errStream, context);
187186
} break; // BREAK
188187
default: {
189-
errStream << "Invalid authentication message received (unknown type): "
190-
<< context->d_authenticationMessage;
191-
bsl::string error(errStream.str().data(), errStream.str().length());
192-
context->d_initialConnectionContext_p->initialConnectionCompleteCb()(
193-
rc_ERROR,
194-
error,
195-
bsl::shared_ptr<mqbnet::Session>());
188+
errorDescription
189+
<< "Invalid authentication message received (unknown type): "
190+
<< context->d_authenticationMessage;
196191
return rc_ERROR; // RETURN
197192
}
198193
}
199194

200-
if (rc != rc_SUCCESS) {
201-
bsl::string error(errStream.str().data(), errStream.str().length());
202-
context->d_initialConnectionContext_p->initialConnectionCompleteCb()(
203-
rc_ERROR,
204-
error,
205-
bsl::shared_ptr<mqbnet::Session>());
195+
if (rc == rc_SUCCESS) {
196+
*isContinueRead = true;
206197
}
207198

208199
return rc;

src/groups/mqb/mqba/mqba_authenticator.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ class Authenticator : public mqbnet::Authenticator {
154154
// MANIPULATORS
155155
// (virtual: mqbnet::Authenticator)
156156

157-
int handleAuthenticationOnMsgType(const AuthenticationContextSp& context)
157+
int handleAuthenticationOnMsgType(bsl::ostream& errorDescription,
158+
bool* isContinueRead,
159+
const AuthenticationContextSp& context)
158160
BSLS_KEYWORD_OVERRIDE;
159161

160162
/// Send out outbound authentication message or reverse connection request

src/groups/mqb/mqba/mqba_initialconnectionhandler.cpp

+87-31
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,29 @@ int InitialConnectionHandler::processBlob(
175175
rc_INVALID_NEGOTIATION_MESSAGE = -1,
176176
};
177177

178-
bsl::optional<bmqp_ctrlmsg::NegotiationMessage> negotiationMsg;
178+
bsl::optional<bmqp_ctrlmsg::AuthenticationMessage> authenticationMsg;
179+
bsl::optional<bmqp_ctrlmsg::NegotiationMessage> negotiationMsg;
179180

180181
int rc = decodeInitialConnectionMessage(errorDescription,
181182
blob,
183+
&authenticationMsg,
182184
&negotiationMsg);
183185

184-
if (rc != 0) {
186+
if (rc != rc_SUCCESS) {
185187
return (rc * 10) + rc_INVALID_NEGOTIATION_MESSAGE; // RETURN
186188
}
187189

188-
if (negotiationMsg.has_value()) {
190+
// Authentication or Negotiation based on the type of message received.
191+
if (authenticationMsg.has_value()) {
192+
context->authenticationContext()->d_authenticationMessage =
193+
authenticationMsg.value();
194+
195+
rc = d_authenticator_mp->handleAuthenticationOnMsgType(
196+
errorDescription,
197+
isContinueRead,
198+
context->authenticationContext());
199+
}
200+
else if (negotiationMsg.has_value()) {
189201
context->negotiationContext()->d_negotiationMessage =
190202
negotiationMsg.value();
191203

@@ -206,18 +218,21 @@ int InitialConnectionHandler::processBlob(
206218
}
207219

208220
int InitialConnectionHandler::decodeInitialConnectionMessage(
209-
bsl::ostream& errorDescription,
210-
const bdlbb::Blob& blob,
211-
bsl::optional<bmqp_ctrlmsg::NegotiationMessage>* message)
221+
bsl::ostream& errorDescription,
222+
const bdlbb::Blob& blob,
223+
bsl::optional<bmqp_ctrlmsg::AuthenticationMessage>* authenticationMsg,
224+
bsl::optional<bmqp_ctrlmsg::NegotiationMessage>* negotiationMsg)
212225
{
213-
BSLS_ASSERT(message);
226+
BSLS_ASSERT(authenticationMsg);
227+
BSLS_ASSERT(negotiationMsg);
214228

215229
enum RcEnum {
216230
// Value for the various RC error categories
217-
rc_SUCCESS = 0,
218-
rc_INVALID_MESSAGE = -1,
219-
rc_NOT_CONTROL_EVENT = -2,
220-
rc_INVALID_CONTROL_EVENT = -3
231+
rc_SUCCESS = 0,
232+
rc_INVALID_MESSAGE = -1,
233+
rc_INVALID_EVENT = -2,
234+
rc_INVALID_AUTHENTICATION_EVENT = -3,
235+
rc_INVALID_CONTROL_EVENT = -4
221236
};
222237

223238
bdlma::LocalSequentialAllocator<2048> localAllocator(d_allocator_p);
@@ -231,25 +246,41 @@ int InitialConnectionHandler::decodeInitialConnectionMessage(
231246
return rc_INVALID_MESSAGE; // RETURN
232247
}
233248

234-
if (!event.isControlEvent()) {
235-
errorDescription << "Invalid negotiation message received "
236-
<< "(packet is not a ControlEvent):\n"
237-
<< bmqu::BlobStartHexDumper(&blob);
238-
return rc_NOT_CONTROL_EVENT; // RETURN
239-
}
240-
241-
bmqp_ctrlmsg::NegotiationMessage negotiationMessage;
242-
243-
int rc = event.loadControlEvent(&negotiationMessage);
249+
bmqp_ctrlmsg::AuthenticationMessage authenticaionMessage;
250+
bmqp_ctrlmsg::NegotiationMessage negotiationMessage;
251+
252+
if (event.isAuthenticationEvent()) {
253+
const int rc = event.loadAuthenticationEvent(&authenticaionMessage);
254+
if (rc != 0) {
255+
BALL_LOG_ERROR
256+
<< "Invalid response from broker [reason: 'authentication "
257+
"event is not an AuthenticationMessage', rc: "
258+
<< rc << "]: " << event;
259+
return rc_INVALID_AUTHENTICATION_EVENT; // RETURN
260+
}
244261

245-
if (rc != 0) {
246-
errorDescription << "Invalid negotiation message received (failed "
247-
<< "decoding ControlEvent): [rc: " << rc << "]:\n"
248-
<< bmqu::BlobStartHexDumper(&blob);
249-
return rc_INVALID_CONTROL_EVENT; // RETURN
262+
*authenticationMsg = authenticaionMessage;
250263
}
264+
else if (event.isControlEvent()) {
265+
const int rc = event.loadControlEvent(&negotiationMessage);
266+
if (rc != 0) {
267+
BALL_LOG_ERROR
268+
<< "Invalid response from broker [reason: 'authentication "
269+
"event is not an AuthenticationMessage', rc: "
270+
<< rc << "]: " << event;
271+
272+
return rc_INVALID_CONTROL_EVENT; // RETURN
273+
}
251274

252-
*message = negotiationMessage;
275+
*negotiationMsg = negotiationMessage;
276+
}
277+
else {
278+
errorDescription
279+
<< "Invalid initial connection message received "
280+
<< "(packet is not an AuthenticationEvent or ControlEvent):\n"
281+
<< bmqu::BlobStartHexDumper(&blob);
282+
return rc_INVALID_EVENT; // RETURN
283+
}
253284

254285
return rc_SUCCESS;
255286
}
@@ -297,9 +328,11 @@ void InitialConnectionHandler::complete(
297328
}
298329

299330
InitialConnectionHandler::InitialConnectionHandler(
300-
bslma::ManagedPtr<mqbnet::Negotiator>& negotiator,
301-
bslma::Allocator* allocator)
302-
: d_negotiator_mp(negotiator)
331+
bslma::ManagedPtr<mqbnet::Authenticator>& authenticator,
332+
bslma::ManagedPtr<mqbnet::Negotiator>& negotiator,
333+
bslma::Allocator* allocator)
334+
: d_authenticator_mp(authenticator)
335+
, d_negotiator_mp(negotiator)
303336
, d_allocator_p(allocator)
304337
{
305338
}
@@ -308,9 +341,21 @@ InitialConnectionHandler::~InitialConnectionHandler()
308341
{
309342
}
310343

311-
void InitialConnectionHandler::handleInitialConnection(
344+
void InitialConnectionHandler::setupContext(
312345
const InitialConnectionContextSp& context)
313346
{
347+
// Create an AuthenticationContext for that connection
348+
bsl::shared_ptr<mqbnet::AuthenticationContext> authenticationContext;
349+
authenticationContext.createInplace(d_allocator_p);
350+
351+
authenticationContext->d_initialConnectionContext_p = context.get();
352+
authenticationContext->d_isReversed = false;
353+
authenticationContext->d_clusterName = "";
354+
authenticationContext->d_connectionType =
355+
mqbnet::ConnectionType::e_UNKNOWN;
356+
357+
context->setAuthenticationContext(authenticationContext);
358+
314359
// Create an NegotiationContext for that connection
315360
bsl::shared_ptr<mqbnet::NegotiationContext> negotiationContext;
316361
negotiationContext.createInplace(d_allocator_p);
@@ -321,7 +366,11 @@ void InitialConnectionHandler::handleInitialConnection(
321366
negotiationContext->d_connectionType = mqbnet::ConnectionType::e_UNKNOWN;
322367

323368
context->setNegotiationContext(negotiationContext);
369+
}
324370

371+
void InitialConnectionHandler::handleConnectionFlow(
372+
const InitialConnectionContextSp& context)
373+
{
325374
// Reading for inbound request or continue to read
326375
// after sending a request ourselves
327376

@@ -363,5 +412,12 @@ void InitialConnectionHandler::handleInitialConnection(
363412
guard.release();
364413
}
365414

415+
void InitialConnectionHandler::handleInitialConnection(
416+
const InitialConnectionContextSp& context)
417+
{
418+
setupContext(context);
419+
handleConnectionFlow(context);
420+
}
421+
366422
} // close package namespace
367423
} // close enterprise namespace

src/groups/mqb/mqba/mqba_initialconnectionhandler.h

+18-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <mqbnet_initialconnectionhandler.h>
2121

2222
// MQB
23+
#include <mqbnet_authenticator.h>
2324
#include <mqbnet_initialconnectioncontext.h>
2425
#include <mqbnet_negotiator.h>
2526

@@ -39,6 +40,7 @@ namespace mqba {
3940

4041
// FORWARD DECLARATION
4142
class SessionNegotiator;
43+
class Authenticator;
4244

4345
// ==============================
4446
// class InitialConnectionHandler
@@ -53,7 +55,10 @@ class InitialConnectionHandler : public mqbnet::InitialConnectionHandler {
5355
private:
5456
// DATA
5557

56-
/// Negotiator to use for converting a Channel to a Session
58+
/// Authenticator to use for authenticating a connection.
59+
bslma::ManagedPtr<mqbnet::Authenticator> d_authenticator_mp;
60+
61+
/// Negotiator to use for converting a Channel to a Session.
5762
bslma::ManagedPtr<mqbnet::Negotiator> d_negotiator_mp;
5863

5964
/// Allocator to use.
@@ -99,9 +104,10 @@ class InitialConnectionHandler : public mqbnet::InitialConnectionHandler {
99104
/// populate the specified `errorDescription` with a description of the
100105
/// error.
101106
int decodeInitialConnectionMessage(
102-
bsl::ostream& errorDescription,
103-
const bdlbb::Blob& blob,
104-
bsl::optional<bmqp_ctrlmsg::NegotiationMessage>* negotiationMsg);
107+
bsl::ostream& errorDescription,
108+
const bdlbb::Blob& blob,
109+
bsl::optional<bmqp_ctrlmsg::AuthenticationMessage>* authenticationMsg,
110+
bsl::optional<bmqp_ctrlmsg::NegotiationMessage>* negotiationMsg);
105111

106112
/// Schedule a read for the initial connection of the session of the
107113
/// specified `context`. Return a non-zero code on error and
@@ -118,11 +124,17 @@ class InitialConnectionHandler : public mqbnet::InitialConnectionHandler {
118124
const bsl::string& error,
119125
const bsl::shared_ptr<mqbnet::Session>& session);
120126

127+
void setupContext(const InitialConnectionContextSp& context);
128+
129+
void handleConnectionFlow(const InitialConnectionContextSp& context);
130+
121131
public:
122132
// CREATORS
123133

124-
InitialConnectionHandler(bslma::ManagedPtr<mqbnet::Negotiator>& negotiator,
125-
bslma::Allocator* allocator);
134+
InitialConnectionHandler(
135+
bslma::ManagedPtr<mqbnet::Authenticator>& authenticator,
136+
bslma::ManagedPtr<mqbnet::Negotiator>& negotiator,
137+
bslma::Allocator* allocator);
126138

127139
/// Destructor
128140
~InitialConnectionHandler() BSLS_KEYWORD_OVERRIDE;

src/groups/mqb/mqba/mqba_sessionnegotiator.h

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444

4545
// BDE
4646
#include <bdlbb_blob.h>
47-
#include <bdlcc_objectpool.h>
4847
#include <bdlcc_sharedobjectpool.h>
4948
#include <bsl_memory.h>
5049
#include <bsl_ostream.h>

src/groups/mqb/mqbnet/mqbnet_authenticator.h

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Authenticator {
4848
// MANIPULATORS
4949

5050
virtual int handleAuthenticationOnMsgType(
51+
bsl::ostream& errorDescription,
52+
bool* isContinueRead,
5153
const bsl::shared_ptr<AuthenticationContext>& context) = 0;
5254

5355
/// Send out outbound authentication message or reverse connection request

src/groups/mqb/mqbnet/mqbnet_initialconnectioncontext.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ InitialConnectionContext::setInitialConnectionCompleteCb(
6060
return *this;
6161
}
6262

63+
InitialConnectionContext& InitialConnectionContext::setAuthenticationContext(
64+
const bsl::shared_ptr<AuthenticationContext>& value)
65+
{
66+
d_authenticationCtxSp = value;
67+
return *this;
68+
}
69+
6370
InitialConnectionContext& InitialConnectionContext::setNegotiationContext(
6471
const bsl::shared_ptr<NegotiationContext>& value)
6572
{
@@ -94,6 +101,12 @@ InitialConnectionContext::initialConnectionCompleteCb() const
94101
return d_initialConnectionCompleteCb;
95102
}
96103

104+
const bsl::shared_ptr<AuthenticationContext>&
105+
InitialConnectionContext::authenticationContext() const
106+
{
107+
return d_authenticationCtxSp;
108+
}
109+
97110
const bsl::shared_ptr<NegotiationContext>&
98111
InitialConnectionContext::negotiationContext() const
99112
{

0 commit comments

Comments
 (0)