Skip to content

Commit a33efc3

Browse files
committed
adding state to AuthenticationContext and delay creation
Signed-off-by: Emelia Lei <[email protected]>
1 parent 74f8f32 commit a33efc3

6 files changed

+122
-71
lines changed

src/groups/mqb/mqba/mqba_authenticator.cpp

Lines changed: 33 additions & 23 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,26 @@ 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+
AuthenticationContextSp* 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
68-
69-
const bmqp_ctrlmsg::AuthenticateRequest& authenticateRequest =
70-
context->authenticationMessage().authenticateRequest();
71-
72-
BALL_LOG_DEBUG << "Received authentication message from '"
73-
<< context->initialConnectionContext()->channel()->peerUri()
74-
<< "': " << authenticateRequest;
66+
BSLS_ASSERT_SAFE(authenticationMsg.isAuthenticateRequestValue());
7567

7668
bmqp_ctrlmsg::AuthenticationMessage authenticationResponse;
7769
bmqp_ctrlmsg::AuthenticateResponse& response =
7870
authenticationResponse.makeAuthenticateResponse();
7971

72+
// Create an AuthenticationContext for that connection
73+
bsl::shared_ptr<mqbnet::AuthenticationContext> authenticationContext =
74+
bsl::allocate_shared<mqbnet::AuthenticationContext>(
75+
d_allocator_p,
76+
authenticationMsg, // authenticationMessage
77+
false, // isReversed
78+
State::e_AUTHENTICATING // state
79+
);
80+
8081
// Always succeeds for now
8182
// TODO: For later implementation, plugins will perform authentication,
8283
// taking the `AuthenticationContext` and updates it with the
@@ -85,16 +86,23 @@ int Authenticator::onAuthenticationRequest(
8586
response.status().code() = 0;
8687
response.lifetimeMs() = 10 * 60 * 1000;
8788

89+
authenticationContext->state().testAndSwap(
90+
mqbnet::AuthenticationContext::State::e_AUTHENTICATING,
91+
mqbnet::AuthenticationContext::State::e_AUTHENTICATED);
92+
8893
int rc = sendAuthenticationMessage(errorDescription,
8994
authenticationResponse,
90-
context);
95+
authenticationContext);
96+
97+
*context = authenticationContext;
9198

9299
return rc;
93100
}
94101

95102
int Authenticator::onAuthenticationResponse(
96-
bsl::ostream& errorDescription,
97-
const AuthenticationContextSp& context)
103+
bsl::ostream& errorDescription,
104+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
105+
AuthenticationContextSp* context)
98106
{
99107
BALL_LOG_ERROR << "Not Implemented";
100108

@@ -163,9 +171,11 @@ Authenticator::~Authenticator()
163171
// NOTHING: (required because of inheritance)
164172
}
165173

166-
int Authenticator::handleAuthentication(bsl::ostream& errorDescription,
167-
bool* isContinueRead,
168-
const AuthenticationContextSp& context)
174+
int Authenticator::handleAuthentication(
175+
bsl::ostream& errorDescription,
176+
AuthenticationContextSp* context,
177+
bool* isContinueRead,
178+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg)
169179
{
170180
enum RcEnum {
171181
// Value for the various RC error categories
@@ -176,19 +186,19 @@ int Authenticator::handleAuthentication(bsl::ostream& errorDescription,
176186
bmqu::MemOutStream errStream;
177187
int rc = rc_SUCCESS;
178188

179-
switch (context->authenticationMessage().selectionId()) {
189+
switch (authenticationMsg.selectionId()) {
180190
case bmqp_ctrlmsg::AuthenticationMessage::
181191
SELECTION_ID_AUTHENTICATE_REQUEST: {
182-
rc = onAuthenticationRequest(errStream, context);
192+
rc = onAuthenticationRequest(errStream, authenticationMsg, context);
183193
} break; // BREAK
184194
case bmqp_ctrlmsg::AuthenticationMessage::
185195
SELECTION_ID_AUTHENTICATE_RESPONSE: {
186-
rc = onAuthenticationResponse(errStream, context);
196+
rc = onAuthenticationResponse(errStream, authenticationMsg, context);
187197
} break; // BREAK
188198
default: {
189199
errorDescription
190200
<< "Invalid authentication message received (unknown type): "
191-
<< context->authenticationMessage();
201+
<< authenticationMsg;
192202
return rc_ERROR; // RETURN
193203
}
194204
}

src/groups/mqb/mqba/mqba_authenticator.h

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class Authenticator : public mqbnet::Authenticator {
7878
bdlcc::ObjectPoolFunctors::RemoveAll<bdlbb::Blob> >
7979
BlobSpPool;
8080

81+
typedef mqbnet::AuthenticationContext::State State;
82+
8183
private:
8284
typedef bsl::shared_ptr<mqbnet::AuthenticationContext>
8385
AuthenticationContextSp;
@@ -100,22 +102,29 @@ class Authenticator : public mqbnet::Authenticator {
100102
private:
101103
// PRIVATE MANIPULATORS
102104

103-
/// Invoked when received a `AuthenticationRequest` authentication message
104-
/// 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);
112-
113-
/// Invoked when received a `AuthenticationResponse` authentication message
114-
/// with the specified `context`. Returns 0 on success, or return a
115-
/// non-zero code and populate the specified `errorDescription` with
116-
/// a description of the error on failure.
117-
int onAuthenticationResponse(bsl::ostream& errorDescription,
118-
const AuthenticationContextSp& context);
105+
/// Handles an incoming `AuthenticationRequest` message by authenticating
106+
/// using the specified `AuthenticationMessage`. On success, creates an
107+
/// `AuthenticationContext` and stores it in `context`. The behavior of
108+
/// this function is undefined unless `authenticationMsg` is an
109+
/// `AuthenticationRequest`.
110+
/// Returns 0 on success; otherwise, returns a non-zero error code and
111+
/// populates `errorDescription` with details of the failure.
112+
int onAuthenticationRequest(
113+
bsl::ostream& errorDescription,
114+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
115+
AuthenticationContextSp* context);
116+
117+
/// Handles an incoming `AuthenticationResponse` message by authenticating
118+
/// using the specified `AuthenticationMessage`. On success, creates an
119+
/// `AuthenticationContext` and stores it in `context`. The behavior of
120+
/// this function is undefined unless `authenticationMsg` is an
121+
/// `AuthenticationResponse`.
122+
/// Returns 0 on success; otherwise, returns a non-zero error code and
123+
/// populates `errorDescription` with details of the failure.
124+
int onAuthenticationResponse(
125+
bsl::ostream& errorDescription,
126+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg,
127+
AuthenticationContextSp* 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,22 @@ 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. Create an
167+
/// AuthenticationContext and store into `context`.
168+
/// Return 0 on success, or a non-zero error code and populate the
169+
/// specified `errorDescription` with a description of the error otherwise.
170+
int handleAuthentication(bsl::ostream& errorDescription,
171+
AuthenticationContextSp* context,
172+
bool* isContinueRead,
173+
const bmqp_ctrlmsg::AuthenticationMessage&
174+
authenticationMsg) BSLS_KEYWORD_OVERRIDE;
159175

160176
/// Send out outbound authentication message or reverse connection request
161177
/// with the specified `context`.
178+
/// Return 0 on success, or a non-zero error code and populate the
179+
/// specified `errorDescription` with a description of the error otherwise.
162180
int authenticationOutboundOrReverse(const AuthenticationContextSp& context)
163181
BSLS_KEYWORD_OVERRIDE;
164182
};

src/groups/mqb/mqba/mqba_initialconnectionhandler.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <bdlb_scopeexit.h>
3838
#include <bdlf_bind.h>
3939
#include <bdlma_localsequentialallocator.h>
40+
#include <bsl_memory.h>
4041
#include <bsls_timeinterval.h>
4142

4243
namespace BloombergLP {
@@ -191,13 +192,22 @@ int InitialConnectionHandler::processBlob(
191192

192193
// Authentication or Negotiation based on the type of message received.
193194
if (authenticationMsg.has_value()) {
194-
context->authenticationContext()->setAuthenticationMessage(
195-
authenticationMsg.value());
195+
BALL_LOG_DEBUG << "Received authentication message from '"
196+
<< context->channel()->peerUri()
197+
<< "': " << authenticationMsg.value();
198+
199+
bsl::shared_ptr<mqbnet::AuthenticationContext> authenticationContext;
196200

197201
rc = d_authenticator_mp->handleAuthentication(
198202
errorDescription,
203+
&authenticationContext,
199204
isContinueRead,
200-
context->authenticationContext());
205+
authenticationMsg.value());
206+
207+
if (rc == rc_SUCCESS) {
208+
authenticationContext->setInitialConnectionContext(context.get());
209+
context->setAuthenticationContext(authenticationContext);
210+
}
201211
}
202212
else if (negotiationMsg.has_value()) {
203213
context->negotiationContext()->d_negotiationMessage =
@@ -344,15 +354,6 @@ InitialConnectionHandler::~InitialConnectionHandler()
344354
void InitialConnectionHandler::setupContext(
345355
const InitialConnectionContextSp& context)
346356
{
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-
356357
// Create an NegotiationContext for that connection
357358
bsl::shared_ptr<mqbnet::NegotiationContext> negotiationContext;
358359
negotiationContext.createInplace(d_allocator_p);

src/groups/mqb/mqbnet/mqbnet_authenticationcontext.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
// MQB
2121
#include <mqbnet_initialconnectioncontext.h>
2222

23+
// BDE
24+
#include <bsls_atomic.h>
25+
2326
namespace BloombergLP {
2427
namespace mqbnet {
2528

@@ -28,12 +31,13 @@ namespace mqbnet {
2831
// ---------------------------
2932

3033
AuthenticationContext::AuthenticationContext(
31-
InitialConnectionContext* initialConnectionContext,
32-
bool isReversed,
33-
ConnectionType::Enum connectionType,
34-
bslma::Allocator* basicAllocator)
35-
: d_initialConnectionContext_p(initialConnectionContext)
36-
, d_authenticationMessage(basicAllocator)
34+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMessage,
35+
bool isReversed,
36+
State state,
37+
ConnectionType::Enum connectionType)
38+
: d_initialConnectionContext_p(nullptr)
39+
, d_authenticationMessage(authenticationMessage)
40+
, d_state(state)
3741
, d_isReversed(isReversed)
3842
, d_connectionType(connectionType)
3943
{
@@ -54,6 +58,11 @@ AuthenticationContext& AuthenticationContext::setAuthenticationMessage(
5458
return *this;
5559
}
5660

61+
bsls::AtomicInt& AuthenticationContext::state()
62+
{
63+
return d_state;
64+
}
65+
5766
AuthenticationContext& AuthenticationContext::setIsReversed(bool value)
5867
{
5968
d_isReversed = value;

src/groups/mqb/mqbnet/mqbnet_authenticationcontext.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,18 @@ namespace mqbnet {
4040

4141
/// VST for the context associated with an connection being authenticated.
4242
class AuthenticationContext {
43+
public:
44+
// TYPES
45+
enum State {
46+
e_AUTHENTICATING = 0,
47+
e_AUTHENTICATED,
48+
};
49+
4350
private:
4451
// DATA
4552
InitialConnectionContext* d_initialConnectionContext_p;
4653
bmqp_ctrlmsg::AuthenticationMessage d_authenticationMessage;
54+
bsls::AtomicInt d_state;
4755
bool d_isReversed;
4856
ConnectionType::Enum d_connectionType;
4957

@@ -61,10 +69,10 @@ class AuthenticationContext {
6169
bslma::UsesBslmaAllocator)
6270
// CREATORS
6371
AuthenticationContext(
64-
InitialConnectionContext* initialConnectionContext,
65-
bool isReversed,
66-
ConnectionType::Enum connectionType = ConnectionType::e_UNKNOWN,
67-
bslma::Allocator* basicAllocator = 0);
72+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMessage,
73+
bool isReversed,
74+
State state,
75+
ConnectionType::Enum connectionType = ConnectionType::e_UNKNOWN);
6876

6977
// MANIPULATORS
7078
AuthenticationContext&
@@ -74,6 +82,8 @@ class AuthenticationContext {
7482
AuthenticationContext& setIsReversed(bool value);
7583
AuthenticationContext& setConnectionType(ConnectionType::Enum value);
7684

85+
bsls::AtomicInt& state();
86+
7787
// ACCESSORS
7888
InitialConnectionContext* initialConnectionContext() const;
7989
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMessage() const;

src/groups/mqb/mqbnet/mqbnet_authenticator.h

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

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

3031
// BDE
31-
#include <bsl_iostream.h>
3232
#include <bsl_memory.h>
33+
#include <bsl_ostream.h>
3334

3435
namespace BloombergLP {
3536

@@ -50,14 +51,16 @@ 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-
/// continue reading instead of finishing authentication.
54+
/// `authenticationMsg`. Set `isContinueRead` to true if we want to
55+
/// continue reading instead of finishing authentication. Create an
56+
/// AuthenticationContext and store into `context`.
5557
/// Return 0 on success, or a non-zero error code and populate the
5658
/// specified `errorDescription` with a description of the error otherwise.
5759
virtual int handleAuthentication(
58-
bsl::ostream& errorDescription,
59-
bool* isContinueRead,
60-
const bsl::shared_ptr<AuthenticationContext>& context) = 0;
60+
bsl::ostream& errorDescription,
61+
bsl::shared_ptr<AuthenticationContext>* context,
62+
bool* isContinueRead,
63+
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMsg) = 0;
6164

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

0 commit comments

Comments
 (0)