Skip to content

Commit aa3ac48

Browse files
committed
modify authenticator
Signed-off-by: Emelia Lei <[email protected]>
1 parent 6620831 commit aa3ac48

File tree

4 files changed

+144
-16
lines changed

4 files changed

+144
-16
lines changed

src/groups/mqb/mqba/mqba_authenticator.cpp

+127-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,48 @@ int Authenticator::onAuthenticationRequest(
4747
bsl::ostream& errorDescription,
4848
const AuthenticationContextSp& context)
4949
{
50-
return 0;
50+
// PRECONDITIONS
51+
BSLS_ASSERT_SAFE(
52+
context->d_authenticationMessage.isAuthenticateRequestValue());
53+
BSLS_ASSERT_SAFE(context->d_initialConnectionContext_p->isIncoming() ||
54+
context->d_isReversed);
55+
56+
bmqp_ctrlmsg::AuthenticateRequest& authenticateRequest =
57+
context->d_authenticationMessage.authenticateRequest();
58+
59+
BALL_LOG_DEBUG
60+
<< "Received authentication message from '"
61+
<< context->d_initialConnectionContext_p->channel()->peerUri()
62+
<< "': " << authenticateRequest;
63+
64+
bmqp_ctrlmsg::AuthenticationMessage authenticationResponse;
65+
bmqp_ctrlmsg::AuthenticateResponse& response =
66+
authenticationResponse.makeAuthenticateResponse();
67+
68+
// TODO: authenticate
69+
if (authenticateRequest.mechanism() == "") {
70+
BALL_LOG_ERROR << "Error on authentication";
71+
72+
bmqu::MemOutStream os;
73+
os << "Mechanism is unspecified";
74+
response.status().category() =
75+
bmqp_ctrlmsg::StatusCategory::E_NOT_SUPPORTED;
76+
response.status().message() = os.str();
77+
response.status().code() = -1;
78+
}
79+
else {
80+
response.status().category() = bmqp_ctrlmsg::StatusCategory::E_SUCCESS;
81+
response.status().code() = 0;
82+
response.lifetimeMs() = 10 * 60 * 1000;
83+
}
84+
85+
BALL_LOG_INFO << "send authn response " << authenticationResponse;
86+
87+
int rc = sendAuthenticationMessage(errorDescription,
88+
authenticationResponse,
89+
context);
90+
91+
return rc;
5192
}
5293

5394
int Authenticator::onAuthenticationResponse(
@@ -62,7 +103,41 @@ int Authenticator::sendAuthenticationMessage(
62103
const bmqp_ctrlmsg::AuthenticationMessage& message,
63104
const AuthenticationContextSp& context)
64105
{
65-
return 0;
106+
enum RcEnum {
107+
// Value for the various RC error categories
108+
rc_SUCCESS = 0,
109+
rc_BUILD_FAILURE = -1,
110+
rc_WRITE_FAILURE = -2
111+
};
112+
113+
bmqp::EncodingType::Enum encodingType = bmqp::EncodingType::e_BER;
114+
115+
// TODO: why do we create a local allocator?
116+
bdlma::LocalSequentialAllocator<2048> localAllocator(d_allocator_p);
117+
118+
bmqp::SchemaEventBuilder builder(d_blobSpPool_p,
119+
encodingType,
120+
&localAllocator);
121+
122+
int rc = builder.setMessage(message, bmqp::EventType::e_AUTHENTICATION);
123+
if (rc != 0) {
124+
errorDescription << "Failed building AuthenticationMessage "
125+
<< "[rc: " << rc << ", message: " << message << "]";
126+
return rc_BUILD_FAILURE; // RETURN
127+
}
128+
129+
// Send response event
130+
bmqio::Status status;
131+
context->d_initialConnectionContext_p->channel()->write(&status,
132+
*builder.blob());
133+
if (!status) {
134+
errorDescription << "Failed sending AuthenticationMessage "
135+
<< "[status: " << status << ", message: " << message
136+
<< "]";
137+
return rc_WRITE_FAILURE; // RETURN
138+
}
139+
140+
return rc_SUCCESS;
66141
}
67142

68143
void Authenticator::initiateOutboundAuthentication(
@@ -71,8 +146,10 @@ void Authenticator::initiateOutboundAuthentication(
71146
}
72147

73148
// CREATORS
74-
Authenticator::Authenticator(bslma::Allocator* allocator)
149+
Authenticator::Authenticator(BlobSpPool* blobSpPool,
150+
bslma::Allocator* allocator)
75151
: d_allocator_p(allocator)
152+
, d_blobSpPool_p(blobSpPool)
76153
, d_clusterCatalog_p(0)
77154
{
78155
// NOTHING
@@ -84,6 +161,53 @@ Authenticator::~Authenticator()
84161
// NOTHING: (required because of inheritance)
85162
}
86163

164+
int Authenticator::handleAuthenticationOnMsgType(
165+
const AuthenticationContextSp& context)
166+
{
167+
enum RcEnum {
168+
// Value for the various RC error categories
169+
rc_SUCCESS = 0,
170+
rc_ERROR = -1,
171+
};
172+
173+
bmqu::MemOutStream errStream;
174+
int rc = rc_SUCCESS;
175+
176+
switch (context->d_authenticationMessage.selectionId()) {
177+
case bmqp_ctrlmsg::AuthenticationMessage::
178+
SELECTION_ID_AUTHENTICATE_REQUEST: {
179+
BALL_LOG_INFO << "Received authn request: "
180+
<< context->d_authenticationMessage;
181+
rc = onAuthenticationRequest(errStream, context);
182+
} break; // BREAK
183+
case bmqp_ctrlmsg::AuthenticationMessage::
184+
SELECTION_ID_AUTHENTICATE_RESPONSE: {
185+
BALL_LOG_INFO << "Received authn response: "
186+
<< context->d_authenticationMessage;
187+
} break; // BREAK
188+
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>());
196+
return rc_ERROR; // RETURN
197+
}
198+
}
199+
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>());
206+
}
207+
208+
return rc;
209+
}
210+
87211
int Authenticator::authenticationOutboundOrReverse(
88212
const AuthenticationContextSp& context)
89213
{

src/groups/mqb/mqba/mqba_authenticator.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
// BDE
4141
#include <bdlbb_blob.h>
42+
#include <bdlcc_sharedobjectpool.h>
4243
#include <bsl_memory.h>
4344
#include <bsl_ostream.h>
4445
#include <bslma_allocator.h>
@@ -66,6 +67,13 @@ class Authenticator : public mqbnet::Authenticator {
6667
public:
6768
// TYPES
6869

70+
/// Type of a pool of shared pointers to blob
71+
typedef bdlcc::SharedObjectPool<
72+
bdlbb::Blob,
73+
bdlcc::ObjectPoolFunctors::DefaultCreator,
74+
bdlcc::ObjectPoolFunctors::RemoveAll<bdlbb::Blob> >
75+
BlobSpPool;
76+
6977
private:
7078
typedef bsl::shared_ptr<mqbnet::AuthenticationContext>
7179
AuthenticationContextSp;
@@ -76,6 +84,8 @@ class Authenticator : public mqbnet::Authenticator {
7684
/// Allocator to use.
7785
bslma::Allocator* d_allocator_p;
7886

87+
BlobSpPool* d_blobSpPool_p;
88+
7989
/// Cluster catalog to query for cluster information.
8090
mqbblp::ClusterCatalog* d_clusterCatalog_p;
8191

@@ -130,7 +140,7 @@ class Authenticator : public mqbnet::Authenticator {
130140
/// `bufferFactory`, `dispatcher`, `statContext`, `scheduler` and
131141
/// `blobSpPool` to inject in the negotiated sessions. Use the
132142
/// specified `allocator` for all memory allocations.
133-
Authenticator(bslma::Allocator* allocator);
143+
Authenticator(BlobSpPool* blobSpPool, bslma::Allocator* allocator);
134144

135145
/// Destructor
136146
~Authenticator() BSLS_KEYWORD_OVERRIDE;
@@ -144,6 +154,9 @@ class Authenticator : public mqbnet::Authenticator {
144154
// MANIPULATORS
145155
// (virtual: mqbnet::Authenticator)
146156

157+
int handleAuthenticationOnMsgType(const AuthenticationContextSp& context)
158+
BSLS_KEYWORD_OVERRIDE;
159+
147160
/// Send out outbound authentication message or reverse connection request
148161
/// with the specified `context`.
149162
int authenticationOutboundOrReverse(const AuthenticationContextSp& context)

src/groups/mqb/mqbnet/mqbnet_authenticationcontext.h

-12
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@
3232
namespace BloombergLP {
3333
namespace mqbnet {
3434

35-
struct ConnectionType {
36-
// Enum representing the type of session being authenticated, from that
37-
// side of the connection's point of view.
38-
enum Enum {
39-
e_UNKNOWN,
40-
e_CLUSTER_PROXY, // Reverse connection proxy -> broker
41-
e_CLUSTER_MEMBER, // Cluster node -> cluster node
42-
e_CLIENT, // Either SDK or Proxy -> Proxy or cluster node
43-
e_ADMIN
44-
};
45-
};
46-
4735
// ===========================
4836
// class AuthenticationContext
4937
// ===========================

src/groups/mqb/mqbnet/mqbnet_authenticator.h

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class Authenticator {
4747

4848
// MANIPULATORS
4949

50+
virtual int handleAuthenticationOnMsgType(
51+
const bsl::shared_ptr<AuthenticationContext>& context) = 0;
52+
5053
/// Send out outbound authentication message or reverse connection request
5154
/// with the specified `context`.
5255
virtual int authenticationOutboundOrReverse(

0 commit comments

Comments
 (0)