Skip to content

Commit 057a7c2

Browse files
committed
WIP: bmqbrkrcfg.json and authn timeout
1 parent 993f4ba commit 057a7c2

File tree

5 files changed

+134
-1
lines changed

5 files changed

+134
-1
lines changed

src/applications/bmqbrkr/etc/bmqbrkrcfg.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@
9292
},
9393
"bmqconfConfig": {
9494
"cacheTTLSeconds": 30
95+
},
96+
"plugins": {
97+
"libraries": ["/Users/wlei29/Workspace/blazingmq/build/blazingmq/src/plugins/"],
98+
"enabled": ["PassAuthenticator"]
99+
},
100+
"authentication": {
101+
"plugins": [
102+
{
103+
"name": "FailAuthenticator",
104+
"configs": []
105+
},
106+
{
107+
"name": "PassAuthenticator",
108+
"configs": []
109+
}
110+
],
111+
"fallbackPrincipal": "defaultFallbackPrincipal"
95112
}
96113
}
97114
}

src/groups/mqb/mqba/mqba_authenticator.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// limitations under the License.
1515

1616
// mqba_authenticator.h -*-C++-*-
17+
#include <bdlmt_timereventscheduler.h>
1718
#include <mqba_authenticator.h>
1819

1920
#include <mqbscm_version.h>
@@ -284,6 +285,14 @@ void Authenticator::authenticate(
284285
bsl::shared_ptr<mqbnet::Session>());
285286
}
286287
}
288+
289+
// Set off the timer
290+
if (result->lifetimeMs().has_value()) {
291+
bdlmt::TimerEventScheduler::Handle handle = d_scheduler.scheduleEvent(
292+
bsls::TimeInterval(result->lifetimeMs().value()),
293+
bdlf::BindUtil::bind(&Authenticator::timeout, this, channel));
294+
context->setAuthenticationTimerHandle(handle);
295+
}
287296
}
288297

289298
int Authenticator::reAuthenticateAsync(
@@ -398,6 +407,39 @@ void Authenticator::reAuthenticate(
398407
channel->close(status);
399408
}
400409
}
410+
411+
// Cancel the timer or reschedule
412+
{
413+
bslmt::LockGuard<bslmt::Mutex> guard(
414+
&context->authenticationTimerHandleMutex()); // MUTEX LOCKED
415+
416+
bdlmt::TimerEventScheduler::Handle handle =
417+
context->authenticationTimerHandle();
418+
419+
// If the authenticated channel has already timed out, simply return
420+
rc = d_scheduler.cancelEvent(handle);
421+
if (rc != 0) {
422+
return; // RETURN
423+
}
424+
425+
if (result->lifetimeMs().has_value()) {
426+
rc = d_scheduler.rescheduleEvent(
427+
handle,
428+
bsls::TimeInterval(result->lifetimeMs().value()));
429+
430+
if (rc != 0) {
431+
BALL_LOG_ERROR
432+
<< "Failed to reschedule authentication timer for '"
433+
<< channel->peerUri() << "' [rc: " << rc
434+
<< ", lifetime: " << result->lifetimeMs().value() << "]";
435+
bmqio::Status status(bmqio::StatusCategory::e_GENERIC_ERROR,
436+
"reAuthenticationError",
437+
rc,
438+
d_allocator_p);
439+
channel->close(status);
440+
}
441+
}
442+
}
401443
}
402444

403445
// CREATORS
@@ -411,6 +453,8 @@ Authenticator::Authenticator(
411453
100, // max threads
412454
bsls::TimeInterval(120).totalMilliseconds(), // idle time
413455
allocator)
456+
, d_scheduler(bdlmt::TimerEventScheduler(bsls::SystemClockType::e_MONOTONIC,
457+
allocator))
414458
, d_blobSpPool_p(blobSpPool)
415459
, d_allocator_p(allocator)
416460
{
@@ -432,6 +476,15 @@ int Authenticator::start(bsl::ostream& errorDescription)
432476
return rc; // RETURN
433477
}
434478

479+
rc = d_scheduler.start();
480+
if (rc != 0) {
481+
errorDescription << "Failed to start TimerEventScheduler for "
482+
"Authenticator [rc: "
483+
<< rc << "]";
484+
d_threadPool.stop();
485+
return rc; // RETURN
486+
}
487+
435488
return 0;
436489
}
437490

@@ -514,6 +567,18 @@ int Authenticator::handleReauthentication(
514567
return rc;
515568
}
516569

570+
void Authenticator::timeout(const AuthenticationContextSp& context,
571+
const bsl::shared_ptr<bmqio::Channel>& channel)
572+
{
573+
bmqio::Status status(bmqio::StatusCategory::e_TIMEOUT,
574+
"authenticationTimeout",
575+
-1,
576+
d_allocator_p);
577+
context->setAuthenticationTimerHandle(
578+
bdlmt::TimerEventScheduler::Handle());
579+
channel->close(status);
580+
}
581+
517582
int Authenticator::authenticationOutboundOrReverse(
518583
const AuthenticationContextSp& context)
519584
{

src/groups/mqb/mqba/mqba_authenticator.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <bdlbb_blob.h>
4848
#include <bdlcc_sharedobjectpool.h>
4949
#include <bdlmt_threadpool.h>
50+
#include <bdlmt_timereventscheduler.h>
5051
#include <bsl_memory.h>
5152
#include <bsl_ostream.h>
5253
#include <bslma_allocator.h>
@@ -98,6 +99,8 @@ class Authenticator : public mqbnet::Authenticator {
9899

99100
bdlmt::ThreadPool d_threadPool;
100101

102+
bdlmt::TimerEventScheduler d_scheduler;
103+
101104
BlobSpPool* d_blobSpPool_p;
102105

103106
/// Allocator to use.
@@ -193,6 +196,9 @@ class Authenticator : public mqbnet::Authenticator {
193196
const AuthenticationContextSp& context,
194197
const bsl::shared_ptr<bmqio::Channel>& channel);
195198

199+
void timeout(const AuthenticationContextSp& context,
200+
const bsl::shared_ptr<bmqio::Channel>& channel);
201+
196202
public:
197203
// TRAITS
198204
BSLMF_NESTED_TRAIT_DECLARATION(Authenticator, bslma::UsesBslmaAllocator)

src/groups/mqb/mqbnet/mqbnet_authenticationcontext.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ AuthenticationContext& AuthenticationContext::setAuthenticationResult(
5959
return *this;
6060
}
6161

62+
AuthenticationContext& AuthenticationContext::setAuthenticationTimerHandle(
63+
bdlmt::TimerEventScheduler::Handle value)
64+
{
65+
d_authenticationTimerHandle = value;
66+
return *this;
67+
}
68+
69+
AuthenticationContext&
70+
AuthenticationContext::setAuthenticationTimerHandleLocked(
71+
bdlmt::TimerEventScheduler::Handle value)
72+
{
73+
bslmt::LockGuard<bslmt::Mutex> guard(&d_timerHandleMutex); // MUTEX LOCKED
74+
75+
d_authenticationTimerHandle = value;
76+
return *this;
77+
}
78+
6279
AuthenticationContext& AuthenticationContext::setInitialConnectionContext(
6380
InitialConnectionContext* value)
6481
{
@@ -113,6 +130,17 @@ AuthenticationContext::authenticationResult() const
113130
return d_authenticationResultSp;
114131
}
115132

133+
bdlmt::TimerEventScheduler::Handle
134+
AuthenticationContext::authenticationTimerHandle() const
135+
{
136+
return d_authenticationTimerHandle;
137+
}
138+
139+
bslmt::Mutex& AuthenticationContext::authenticationTimerHandleMutex()
140+
{
141+
return d_timerHandleMutex;
142+
}
143+
116144
InitialConnectionContext*
117145
AuthenticationContext::initialConnectionContext() const
118146
{

src/groups/mqb/mqbnet/mqbnet_authenticationcontext.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
///
2424

2525
// MQB
26-
#include "bmqp_protocol.h"
2726
#include <mqbnet_initialconnectioncontext.h>
2827

2928
// BMQ
3029
#include <bmqp_ctrlmsg_messages.h>
30+
#include <bmqp_protocol.h>
3131

3232
// BDE
33+
#include <bdlmt_timereventscheduler.h>
3334
#include <bslmt_mutex.h>
3435
#include <bsls_atomic.h>
3536

@@ -63,6 +64,12 @@ class AuthenticationContext {
6364
/// during re-authentication.
6465
bsl::shared_ptr<mqbplug::AuthenticationResult> d_authenticationResultSp;
6566

67+
/// The timer handle for the authentication timeout.
68+
bdlmt::TimerEventScheduler::Handle d_authenticationTimerHandle;
69+
70+
/// The mutex to protect the AuthenticationTimerHandle and
71+
bslmt::Mutex d_timerHandleMutex;
72+
6673
/// The mutex to protect the AuthenticationResult.
6774
mutable bslmt::Mutex d_mutex;
6875

@@ -105,6 +112,10 @@ class AuthenticationContext {
105112
AuthenticationContext& setAuthenticationResult(
106113
const bsl::shared_ptr<mqbplug::AuthenticationResult>& value);
107114
AuthenticationContext&
115+
setAuthenticationTimerHandle(bdlmt::TimerEventScheduler::Handle value);
116+
AuthenticationContext& setAuthenticationTimerHandleLocked(
117+
bdlmt::TimerEventScheduler::Handle value);
118+
AuthenticationContext&
108119
setInitialConnectionContext(InitialConnectionContext* value);
109120
AuthenticationContext&
110121
setAuthenticationMessage(const bmqp_ctrlmsg::AuthenticationMessage& value);
@@ -124,6 +135,12 @@ class AuthenticationContext {
124135
const bsl::shared_ptr<mqbplug::AuthenticationResult>&
125136
authenticationResult() const;
126137

138+
/// This function holds a mutex lock while accessing the
139+
/// `d_authenticationTimerHandle` to ensure thread safety.
140+
bdlmt::TimerEventScheduler::Handle authenticationTimerHandle() const;
141+
142+
bslmt::Mutex& authenticationTimerHandleMutex();
143+
127144
InitialConnectionContext* initialConnectionContext() const;
128145
const bmqp_ctrlmsg::AuthenticationMessage& authenticationMessage() const;
129146
bmqp::EncodingType::Enum authenticationEncodingType() const;

0 commit comments

Comments
 (0)