Skip to content

Commit 95f9480

Browse files
committed
delay the creation of AuthenticationContext
Signed-off-by: Emelia Lei <[email protected]>
1 parent ac15e95 commit 95f9480

5 files changed

+80
-54
lines changed

src/groups/mqb/mqba/mqba_authenticator.cpp

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
// MQB
2929
#include <mqbblp_clustercatalog.h>
3030
#include <mqbnet_authenticationcontext.h>
31+
#include <mqbnet_initialconnectioncontext.h>
3132

3233
// BMQ
3334
#include <bmqio_status.h>
@@ -57,26 +58,42 @@ const int k_AUTHENTICATION_READTIMEOUT = 3 * 60; // 3 minutes
5758
// -------------------
5859

5960
int Authenticator::onAuthenticationRequest(
60-
bsl::ostream& errorDescription,
61-
const AuthenticationContextSp& context)
61+
bsl::ostream& errorDescription,
62+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
63+
const InitialConnectionContextSp& context)
6264
{
6365
// PRECONDITIONS
64-
BSLS_ASSERT_SAFE(
65-
context->authenticationMessage().isAuthenticateRequestValue());
66-
BSLS_ASSERT_SAFE(context->initialConnectionContext()->isIncoming());
67-
BSLS_ASSERT_SAFE(!context->isReversed()); // not supported for now
66+
BSLS_ASSERT_SAFE(authenticationMsg.isAuthenticateRequestValue());
67+
BSLS_ASSERT_SAFE(context->isIncoming());
6868

6969
const bmqp_ctrlmsg::AuthenticateRequest& authenticateRequest =
70-
context->authenticationMessage().authenticateRequest();
70+
authenticationMsg.authenticateRequest();
7171

7272
BALL_LOG_DEBUG << "Received authentication message from '"
73-
<< context->initialConnectionContext()->channel()->peerUri()
73+
<< context->channel()->peerUri()
7474
<< "': " << authenticateRequest;
7575

7676
bmqp_ctrlmsg::AuthenticationMessage authenticationResponse;
7777
bmqp_ctrlmsg::AuthenticateResponse& response =
7878
authenticationResponse.makeAuthenticateResponse();
7979

80+
if (!context->authenticationContext()) {
81+
// Create an AuthenticationContext for that connection
82+
bsl::shared_ptr<mqbnet::AuthenticationContext> authenticationContext =
83+
bsl::allocate_shared<mqbnet::AuthenticationContext>(
84+
d_allocator_p,
85+
context.get(), // initialConnectionContext
86+
false, // isReversed
87+
State::e_AUTHENTICATING // state
88+
);
89+
context->setAuthenticationContext(authenticationContext);
90+
}
91+
else {
92+
context->authenticationContext()->testAndSwapState(
93+
State::e_AUTHENTICATED,
94+
State::e_AUTHENTICATING);
95+
}
96+
8097
// Always succeeds for now
8198
// TODO: For later implementation, plugins will perform authentication,
8299
// taking the `AuthenticationContext` and updates it with the
@@ -85,20 +102,20 @@ int Authenticator::onAuthenticationRequest(
85102
response.status().code() = 0;
86103
response.lifetimeMs() = 10 * 60 * 1000;
87104

88-
context->testAndSwapState(
89-
mqbnet::AuthenticationContext::State::e_AUTHENTICATING,
90-
mqbnet::AuthenticationContext::State::e_AUTHENTICATED);
105+
context->authenticationContext()->testAndSwapState(State::e_AUTHENTICATING,
106+
State::e_AUTHENTICATED);
91107

92108
int rc = sendAuthenticationMessage(errorDescription,
93109
authenticationResponse,
94-
context);
110+
context->authenticationContext());
95111

96112
return rc;
97113
}
98114

99115
int Authenticator::onAuthenticationResponse(
100-
bsl::ostream& errorDescription,
101-
const AuthenticationContextSp& context)
116+
bsl::ostream& errorDescription,
117+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
118+
const InitialConnectionContextSp& context)
102119
{
103120
BALL_LOG_ERROR << "Not Implemented";
104121

@@ -167,9 +184,11 @@ Authenticator::~Authenticator()
167184
// NOTHING: (required because of inheritance)
168185
}
169186

170-
int Authenticator::handleAuthentication(bsl::ostream& errorDescription,
171-
bool* isContinueRead,
172-
const AuthenticationContextSp& context)
187+
int Authenticator::handleAuthentication(
188+
bsl::ostream& errorDescription,
189+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
190+
bool* isContinueRead,
191+
const InitialConnectionContextSp& context)
173192
{
174193
enum RcEnum {
175194
// Value for the various RC error categories
@@ -180,19 +199,19 @@ int Authenticator::handleAuthentication(bsl::ostream& errorDescription,
180199
bmqu::MemOutStream errStream;
181200
int rc = rc_SUCCESS;
182201

183-
switch (context->authenticationMessage().selectionId()) {
202+
switch (authenticationMsg.selectionId()) {
184203
case bmqp_ctrlmsg::AuthenticationMessage::
185204
SELECTION_ID_AUTHENTICATE_REQUEST: {
186-
rc = onAuthenticationRequest(errStream, context);
205+
rc = onAuthenticationRequest(errStream, authenticationMsg, context);
187206
} break; // BREAK
188207
case bmqp_ctrlmsg::AuthenticationMessage::
189208
SELECTION_ID_AUTHENTICATE_RESPONSE: {
190-
rc = onAuthenticationResponse(errStream, context);
209+
rc = onAuthenticationResponse(errStream, authenticationMsg, context);
191210
} break; // BREAK
192211
default: {
193212
errorDescription
194213
<< "Invalid authentication message received (unknown type): "
195-
<< context->authenticationMessage();
214+
<< authenticationMsg;
196215
return rc_ERROR; // RETURN
197216
}
198217
}

src/groups/mqb/mqba/mqba_authenticator.h

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <mqbconfm_messages.h>
3636
#include <mqbnet_authenticationcontext.h>
3737
#include <mqbnet_authenticator.h>
38+
#include <mqbnet_initialconnectioncontext.h>
3839

3940
// BMQ
4041
#include <bmqio_channel.h>
@@ -78,10 +79,15 @@ class Authenticator : public mqbnet::Authenticator {
7879
bdlcc::ObjectPoolFunctors::RemoveAll<bdlbb::Blob> >
7980
BlobSpPool;
8081

82+
typedef mqbnet::AuthenticationContext::State State;
83+
8184
private:
8285
typedef bsl::shared_ptr<mqbnet::AuthenticationContext>
8386
AuthenticationContextSp;
8487

88+
typedef bsl::shared_ptr<mqbnet::InitialConnectionContext>
89+
InitialConnectionContextSp;
90+
8591
private:
8692
// DATA
8793

@@ -102,20 +108,23 @@ class Authenticator : public mqbnet::Authenticator {
102108

103109
/// Invoked when received a `AuthenticationRequest` authentication message
104110
/// with the specified `context`. The behavior of this function is
105-
/// undefined unless `d_authenticationMessage` in the `context` is an
106-
/// `AuthenticateRequest` and this request is incoming or reversed
107-
/// connection. Returns 0 on success, or return a non-zero code and
108-
/// populate the specified `errorDescription` with a description of the
109-
/// error on failure.
110-
int onAuthenticationRequest(bsl::ostream& errorDescription,
111-
const AuthenticationContextSp& context);
111+
/// undefined unless `authenticationMsg` is an `AuthenticateRequest` and
112+
/// this request is an incoming connection. Returns 0 on success,
113+
/// or return a non-zero code and populate the specified `errorDescription`
114+
/// with a description of the error on failure.
115+
int onAuthenticationRequest(
116+
bsl::ostream& errorDescription,
117+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
118+
const InitialConnectionContextSp& context);
112119

113120
/// Invoked when received a `AuthenticationResponse` authentication message
114121
/// with the specified `context`. Returns 0 on success, or return a
115122
/// non-zero code and populate the specified `errorDescription` with
116123
/// a description of the error on failure.
117-
int onAuthenticationResponse(bsl::ostream& errorDescription,
118-
const AuthenticationContextSp& context);
124+
int onAuthenticationResponse(
125+
bsl::ostream& errorDescription,
126+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
127+
const InitialConnectionContextSp& context);
119128

120129
/// Send the specified `message` to the peer associated with the
121130
/// specified `context` and return 0 on success, or return a non-zero
@@ -152,13 +161,21 @@ class Authenticator : public mqbnet::Authenticator {
152161
// MANIPULATORS
153162
// (virtual: mqbnet::Authenticator)
154163

155-
int handleAuthentication(bsl::ostream& errorDescription,
156-
bool* isContinueRead,
157-
const AuthenticationContextSp& context)
158-
BSLS_KEYWORD_OVERRIDE;
164+
/// Authenticate the connection based on the type of AuthenticationMessage
165+
/// `authenticationMsg`. Set `isContinueRead` to true if we want to
166+
/// continue reading instead of finishing authentication.
167+
/// Return 0 on success, or a non-zero error code and populate the
168+
/// specified `errorDescription` with a description of the error otherwise.
169+
int handleAuthentication(
170+
bsl::ostream& errorDescription,
171+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
172+
bool* isContinueRead,
173+
const InitialConnectionContextSp& context) BSLS_KEYWORD_OVERRIDE;
159174

160175
/// Send out outbound authentication message or reverse connection request
161176
/// with the specified `context`.
177+
/// Return 0 on success, or a non-zero error code and populate the
178+
/// specified `errorDescription` with a description of the error otherwise.
162179
int authenticationOutboundOrReverse(const AuthenticationContextSp& context)
163180
BSLS_KEYWORD_OVERRIDE;
164181
};

src/groups/mqb/mqba/mqba_initialconnectionhandler.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,11 @@ int InitialConnectionHandler::processBlob(
191191

192192
// Authentication or Negotiation based on the type of message received.
193193
if (authenticationMsg.has_value()) {
194-
context->authenticationContext()->setAuthenticationMessage(
195-
authenticationMsg.value());
196-
197194
rc = d_authenticator_mp->handleAuthentication(
198195
errorDescription,
196+
authenticationMsg.value(),
199197
isContinueRead,
200-
context->authenticationContext());
198+
context);
201199
}
202200
else if (negotiationMsg.has_value()) {
203201
context->negotiationContext()->d_negotiationMessage =
@@ -344,15 +342,6 @@ InitialConnectionHandler::~InitialConnectionHandler()
344342
void InitialConnectionHandler::setupContext(
345343
const InitialConnectionContextSp& context)
346344
{
347-
// Create an AuthenticationContext for that connection
348-
bsl::shared_ptr<mqbnet::AuthenticationContext> authenticationContext =
349-
bsl::allocate_shared<mqbnet::AuthenticationContext>(
350-
d_allocator_p,
351-
context.get(), // initialConnectionContext
352-
false // isReversed
353-
);
354-
context->setAuthenticationContext(authenticationContext);
355-
356345
// Create an NegotiationContext for that connection
357346
bsl::shared_ptr<mqbnet::NegotiationContext> negotiationContext;
358347
negotiationContext.createInplace(d_allocator_p);

src/groups/mqb/mqbnet/mqbnet_authenticationcontext.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ class AuthenticationContext {
4343
public:
4444
// TYPES
4545
enum State {
46-
e_IDLE = 0,
47-
e_AUTHENTICATING,
46+
e_AUTHENTICATING = 0,
4847
e_AUTHENTICATED,
4948
};
5049

@@ -72,7 +71,7 @@ class AuthenticationContext {
7271
AuthenticationContext(
7372
InitialConnectionContext* initialConnectionContext,
7473
bool isReversed,
75-
State state = State::e_IDLE,
74+
State state,
7675
ConnectionType::Enum connectionType = ConnectionType::e_UNKNOWN,
7776
bslma::Allocator* basicAllocator = 0);
7877

src/groups/mqb/mqbnet/mqbnet_authenticator.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
/// (re)authenticates a connection with a BlazingMQ client or another bmqbrkr.
2626

2727
// MQB
28+
#include "mqbnet_initialconnectioncontext.h"
2829
#include <mqbnet_authenticationcontext.h>
2930

3031
// BDE
@@ -50,14 +51,15 @@ class Authenticator {
5051
// MANIPULATORS
5152

5253
/// Authenticate the connection based on the type of AuthenticationMessage
53-
/// in the specified `context`. Set `isContinueRead` to true if we want to
54+
/// `authenticationMsg`. Set `isContinueRead` to true if we want to
5455
/// continue reading instead of finishing authentication.
5556
/// Return 0 on success, or a non-zero error code and populate the
5657
/// specified `errorDescription` with a description of the error otherwise.
5758
virtual int handleAuthentication(
58-
bsl::ostream& errorDescription,
59-
bool* isContinueRead,
60-
const bsl::shared_ptr<AuthenticationContext>& context) = 0;
59+
bsl::ostream& errorDescription,
60+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
61+
bool* isContinueRead,
62+
const bsl::shared_ptr<InitialConnectionContext>& context) = 0;
6163

6264
/// Send out outbound authentication message or reverse connection request
6365
/// with the specified `context`.

0 commit comments

Comments
 (0)