Skip to content

Commit b1e64cf

Browse files
committed
refactor: Added correlationID to dbGetRaw
1 parent 8ecbfaf commit b1e64cf

File tree

10 files changed

+97
-58
lines changed

10 files changed

+97
-58
lines changed

common/include/common/IAuthenticationManager.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <string>
88

99
namespace SDMS {
10-
10+
struct LogContext;
1111
/**
1212
* Interface class for managing authenticating
1313
*
@@ -26,7 +26,7 @@ class IAuthenticationManager {
2626
* Increments the number of times that the key has been accessed, this is
2727
*useful information when deciding if a key should be purged.
2828
**/
29-
virtual void incrementKeyAccessCounter(const std::string &public_key) = 0;
29+
virtual void incrementKeyAccessCounter(const std::string &public_key, LogContext log_context) = 0;
3030

3131
/**
3232
* Will return true if the public key is known. This is also dependent on the
@@ -39,7 +39,7 @@ class IAuthenticationManager {
3939
* - SESSION
4040
* - PERSISTENT
4141
**/
42-
virtual bool hasKey(const std::string &pub_key) const = 0;
42+
virtual bool hasKey(const std::string &pub_key, LogContext log_context) const = 0;
4343

4444
/**
4545
* Will get the unique id or throw an error
@@ -49,7 +49,7 @@ class IAuthenticationManager {
4949
* - SESSION
5050
* - PERSISTENT - user or repo
5151
**/
52-
virtual std::string getUID(const std::string &pub_key) const = 0;
52+
virtual std::string getUID(const std::string &pub_key, LogContext log_context) const = 0;
5353

5454
/**
5555
* Purge keys if needed

common/source/operators/AuthenticationOperator.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
// Local public includes
66
#include "common/TraceException.hpp"
7+
#include "common/DynaLog.hpp"
78

89
// Standard includes
910
#include <any>
1011
#include <iostream>
12+
#include <boost/uuid/uuid.hpp>
13+
#include <boost/uuid/uuid_generators.hpp>
14+
#include <boost/uuid/uuid_io.hpp>
1115

1216
namespace SDMS {
1317

@@ -25,17 +29,22 @@ void AuthenticationOperator::execute(IMessage &message) {
2529
if (message.exists(MessageAttribute::KEY) == 0) {
2630
EXCEPT(1, "'KEY' attribute not defined.");
2731
}
32+
// 🔹 Generate correlation ID for this request
33+
boost::uuids::random_generator generator;
34+
boost::uuids::uuid uuid = generator();
2835

36+
SDMS::LogContext log_context;
37+
log_context.correlation_id = boost::uuids::to_string(uuid);
2938
m_authentication_manager->purge();
3039

3140
std::string key = std::get<std::string>(message.get(MessageAttribute::KEY));
3241

3342
std::string uid = "anon";
34-
if (m_authentication_manager->hasKey(key)) {
35-
m_authentication_manager->incrementKeyAccessCounter(key);
43+
if (m_authentication_manager->hasKey(key, log_context)) {
44+
m_authentication_manager->incrementKeyAccessCounter(key, log_context);
3645

3746
try {
38-
uid = m_authentication_manager->getUID(key);
47+
uid = m_authentication_manager->getUID(key, log_context);
3948
} catch (const std::exception& e) {
4049
// Log the exception to help diagnose authentication issues
4150
std::cerr << "[AuthenticationOperator] Failed to get UID for key: "

core/server/AuthMap.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ size_t AuthMap::size(const PublicKeyType pub_key_type) const {
168168
}
169169

170170
void AuthMap::incrementKeyAccessCounter(const PublicKeyType pub_key_type,
171-
const std::string &public_key) {
171+
const std::string &public_key,
172+
LogContext log_context) {
172173
if (pub_key_type == PublicKeyType::TRANSIENT) {
173174
lock_guard<mutex> lock(m_trans_clients_mtx);
174175
if (m_trans_auth_clients.count(public_key)) {
@@ -183,7 +184,7 @@ void AuthMap::incrementKeyAccessCounter(const PublicKeyType pub_key_type,
183184
}
184185

185186
bool AuthMap::hasKey(const PublicKeyType pub_key_type,
186-
const std::string &public_key) const {
187+
const std::string &public_key, LogContext log_context) const {
187188
if (pub_key_type == PublicKeyType::TRANSIENT) {
188189
lock_guard<mutex> lock(m_trans_clients_mtx);
189190
return m_trans_auth_clients.count(public_key) > 0;
@@ -203,7 +204,7 @@ bool AuthMap::hasKey(const PublicKeyType pub_key_type,
203204
try {
204205
DatabaseAPI db(m_db_url, m_db_user, m_db_pass);
205206
std::string uid;
206-
if (db.uidByPubKey(public_key, uid)) {
207+
if (db.uidByPubKey(public_key, uid, log_context)) {
207208
return true;
208209
}
209210
} catch (const std::exception& e) {
@@ -217,9 +218,10 @@ bool AuthMap::hasKey(const PublicKeyType pub_key_type,
217218
}
218219

219220
std::string AuthMap::getUID(const PublicKeyType pub_key_type,
220-
const std::string &public_key) const {
221+
const std::string &public_key,
222+
LogContext log_context) const {
221223

222-
std::string uid = getUIDSafe(pub_key_type, public_key);
224+
std::string uid = getUIDSafe(pub_key_type, public_key, log_context);
223225

224226
if (uid.empty()) {
225227
if (pub_key_type == PublicKeyType::TRANSIENT) {
@@ -238,7 +240,8 @@ std::string AuthMap::getUID(const PublicKeyType pub_key_type,
238240
}
239241

240242
std::string AuthMap::getUIDSafe(const PublicKeyType pub_key_type,
241-
const std::string &public_key) const {
243+
const std::string &public_key,
244+
LogContext log_context) const {
242245
if (pub_key_type == PublicKeyType::TRANSIENT) {
243246
lock_guard<mutex> lock(m_trans_clients_mtx);
244247
if (m_trans_auth_clients.count(public_key)) {
@@ -261,7 +264,7 @@ std::string AuthMap::getUIDSafe(const PublicKeyType pub_key_type,
261264
// Check database for user keys
262265
DatabaseAPI db(m_db_url, m_db_user, m_db_pass);
263266
std::string uid;
264-
if (db.uidByPubKey(public_key, uid)) {
267+
if (db.uidByPubKey(public_key, uid, log_context)) {
265268
return uid;
266269
}
267270
}

core/server/AuthMap.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
// Local common includes
1010
#include "common/IAuthenticationManager.hpp"
11+
#include "common/DynaLog.hpp"
1112

1213
// Standard includes
1314
#include <map>
@@ -113,13 +114,15 @@ class AuthMap {
113114
*does not exist. Best to call hasKey first.
114115
**/
115116
std::string getUID(const PublicKeyType pub_key_type,
116-
const std::string &public_key) const;
117+
const std::string &public_key,
118+
LogContext log_context) const;
117119

118120
/**
119121
* Safe version that returns empty string if key not found
120122
**/
121123
std::string getUIDSafe(const PublicKeyType pub_key_type,
122-
const std::string &public_key) const;
124+
const std::string &public_key,
125+
LogContext log_context) const;
123126

124127
/**
125128
* Will return the number of keys of the provided type. Does not currently
@@ -128,7 +131,8 @@ class AuthMap {
128131
size_t size(const PublicKeyType pub_key_type) const;
129132

130133
bool hasKey(const PublicKeyType pub_key_type,
131-
const std::string &public_key) const;
134+
const std::string &public_key,
135+
LogContext log_context) const;
132136

133137
/***********************************************************************************
134138
* Manipulators
@@ -138,7 +142,8 @@ class AuthMap {
138142
* Increase the recorded times the the public key has been accessed by one.
139143
**/
140144
void incrementKeyAccessCounter(const PublicKeyType pub_key_type,
141-
const std::string &public_key);
145+
const std::string &public_key,
146+
LogContext log_context);
142147

143148
/**
144149
* Adds the key to the AuthMap object

core/server/AuthenticationManager.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// Common includes
66
#include "common/TraceException.hpp"
7+
#include "common/DynaLog.hpp"
78

89
// Standard includes
910
#include <iostream>
@@ -69,46 +70,47 @@ void AuthenticationManager::purge(const PublicKeyType pub_key_type) {
6970
}
7071

7172
void AuthenticationManager::incrementKeyAccessCounter(
72-
const std::string &public_key) {
73+
const std::string &public_key,
74+
LogContext log_context) {
7375
std::lock_guard<std::mutex> lock(m_lock);
74-
if (m_auth_mapper.hasKey(PublicKeyType::TRANSIENT, public_key)) {
76+
if (m_auth_mapper.hasKey(PublicKeyType::TRANSIENT, public_key, log_context)) {
7577
m_auth_mapper.incrementKeyAccessCounter(PublicKeyType::TRANSIENT,
76-
public_key);
77-
} else if (m_auth_mapper.hasKey(PublicKeyType::SESSION, public_key)) {
78-
m_auth_mapper.incrementKeyAccessCounter(PublicKeyType::SESSION, public_key);
78+
public_key, log_context);
79+
} else if (m_auth_mapper.hasKey(PublicKeyType::SESSION, public_key, log_context)) {
80+
m_auth_mapper.incrementKeyAccessCounter(PublicKeyType::SESSION, public_key, log_context);
7981
}
8082
// Ignore persistent cases because counter does nothing for them
8183
}
8284

83-
bool AuthenticationManager::hasKey(const std::string &public_key) const {
85+
bool AuthenticationManager::hasKey(const std::string &public_key, LogContext log_context) const {
8486
std::lock_guard<std::mutex> lock(m_lock);
8587

86-
if (m_auth_mapper.hasKey(PublicKeyType::TRANSIENT, public_key)) {
88+
if (m_auth_mapper.hasKey(PublicKeyType::TRANSIENT, public_key, log_context)) {
8789
return true;
8890
}
8991

90-
if (m_auth_mapper.hasKey(PublicKeyType::SESSION, public_key)) {
92+
if (m_auth_mapper.hasKey(PublicKeyType::SESSION, public_key, log_context)) {
9193
return true;
9294
}
9395

94-
if (m_auth_mapper.hasKey(PublicKeyType::PERSISTENT, public_key)) {
96+
if (m_auth_mapper.hasKey(PublicKeyType::PERSISTENT, public_key, log_context)) {
9597
return true;
9698
}
9799

98100
return false;
99101
}
100102

101-
std::string AuthenticationManager::getUID(const std::string &public_key) const {
103+
std::string AuthenticationManager::getUID(const std::string &public_key, LogContext log_context) const {
102104
std::lock_guard<std::mutex> lock(m_lock);
103105

104-
if (m_auth_mapper.hasKey(PublicKeyType::TRANSIENT, public_key)) {
105-
return m_auth_mapper.getUID(PublicKeyType::TRANSIENT, public_key);
106+
if (m_auth_mapper.hasKey(PublicKeyType::TRANSIENT, public_key, log_context)) {
107+
return m_auth_mapper.getUID(PublicKeyType::TRANSIENT, public_key, log_context);
106108
}
107-
if (m_auth_mapper.hasKey(PublicKeyType::SESSION, public_key)) {
108-
return m_auth_mapper.getUID(PublicKeyType::SESSION, public_key);
109+
if (m_auth_mapper.hasKey(PublicKeyType::SESSION, public_key, log_context)) {
110+
return m_auth_mapper.getUID(PublicKeyType::SESSION, public_key, log_context);
109111
}
110-
if (m_auth_mapper.hasKey(PublicKeyType::PERSISTENT, public_key)) {
111-
return m_auth_mapper.getUID(PublicKeyType::PERSISTENT, public_key);
112+
if (m_auth_mapper.hasKey(PublicKeyType::PERSISTENT, public_key, log_context)) {
113+
return m_auth_mapper.getUID(PublicKeyType::PERSISTENT, public_key, log_context);
112114
}
113115

114116
EXCEPT(1, "Unrecognized public_key during execution of getUID.");
@@ -122,9 +124,11 @@ void AuthenticationManager::addKey(const PublicKeyType &pub_key_type,
122124
}
123125

124126
bool AuthenticationManager::hasKey(const PublicKeyType &pub_key_type,
125-
const std::string &public_key) const {
127+
const std::string &public_key,
128+
LogContext log_context) const {
126129
std::lock_guard<std::mutex> lock(m_lock);
127-
return m_auth_mapper.hasKey(pub_key_type, public_key);
130+
131+
return m_auth_mapper.hasKey(pub_key_type, public_key, log_context);
128132
}
129133

130134
void AuthenticationManager::migrateKey(const PublicKeyType &from_type,
@@ -150,21 +154,21 @@ void AuthenticationManager::clearAllNonPersistentKeys() {
150154
m_auth_mapper.clearAllNonPersistentKeys();
151155
}
152156

153-
std::string AuthenticationManager::getUIDSafe(const std::string &public_key) const {
157+
std::string AuthenticationManager::getUIDSafe(const std::string &public_key, LogContext log_context) const {
154158
std::lock_guard<std::mutex> lock(m_lock);
155159

156160
// Try each key type in order
157-
std::string uid = m_auth_mapper.getUIDSafe(PublicKeyType::TRANSIENT, public_key);
161+
std::string uid = m_auth_mapper.getUIDSafe(PublicKeyType::TRANSIENT, public_key, log_context);
158162
if (!uid.empty()) {
159163
return uid;
160164
}
161165

162-
uid = m_auth_mapper.getUIDSafe(PublicKeyType::SESSION, public_key);
166+
uid = m_auth_mapper.getUIDSafe(PublicKeyType::SESSION, public_key, log_context);
163167
if (!uid.empty()) {
164168
return uid;
165169
}
166170

167-
uid = m_auth_mapper.getUIDSafe(PublicKeyType::PERSISTENT, public_key);
171+
uid = m_auth_mapper.getUIDSafe(PublicKeyType::PERSISTENT, public_key, log_context);
168172
if (!uid.empty()) {
169173
return uid;
170174
}

core/server/AuthenticationManager.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class AuthenticationManager : public IAuthenticationManager {
5454
*allotted purge time frame. If the count is above one then the session key
5555
*not be purged.
5656
**/
57-
virtual void incrementKeyAccessCounter(const std::string &public_key) final;
57+
virtual void incrementKeyAccessCounter(const std::string &public_key, LogContext log_context) final;
5858

5959
/**
6060
* This will purge all keys of a particular type that have expired.
@@ -79,15 +79,15 @@ class AuthenticationManager : public IAuthenticationManager {
7979
* - SESSION
8080
* - PERSISTENT
8181
**/
82-
virtual bool hasKey(const std::string &pub_key) const final;
82+
virtual bool hasKey(const std::string &pub_key, LogContext log_context) const final;
8383

8484
void addKey(const PublicKeyType &pub_key_type, const std::string &public_key,
8585
const std::string &uid);
8686

8787
/**
8888
* Check if a specific key exists in a specific map type
8989
**/
90-
bool hasKey(const PublicKeyType &pub_key_type, const std::string &public_key) const;
90+
bool hasKey(const PublicKeyType &pub_key_type, const std::string &public_key, LogContext log_context) const;
9191

9292
/**
9393
* Migrate a key from one type to another
@@ -121,13 +121,13 @@ class AuthenticationManager : public IAuthenticationManager {
121121
* - SESSION
122122
* - PERSISTENT
123123
**/
124-
virtual std::string getUID(const std::string &pub_key) const final;
124+
virtual std::string getUID(const std::string &pub_key, LogContext log_context) const final;
125125

126126
/**
127127
* Safe version that returns empty string if key not found
128128
* instead of throwing an exception
129129
**/
130-
std::string getUIDSafe(const std::string &pub_key) const;
130+
std::string getUIDSafe(const std::string &pub_key, LogContext log_context) const;
131131
};
132132

133133
} // namespace Core

core/server/Condition.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44

55
// Standard includes
66
#include <iostream>
7+
#include <boost/uuid/uuid.hpp>
8+
#include <boost/uuid/uuid_generators.hpp>
9+
#include <boost/uuid/uuid_io.hpp>
710

811
namespace SDMS {
912
namespace Core {
1013

1114
void Promote::enforce(AuthMap &auth_map, const std::string &public_key) {
1215
if (auth_map.hasKeyType(m_promote_from, public_key)) {
1316
size_t access_count = auth_map.getAccessCount(m_promote_from, public_key);
17+
boost::uuids::random_generator generator;
18+
boost::uuids::uuid uuid = generator();
19+
20+
LogContext log_context;
21+
log_context.correlation_id = boost::uuids::to_string(uuid);
1422
if (access_count >= m_transient_to_session_count_threshold) {
1523
// Convert transient key to session key if has been accessed more than the
1624
// threshold
17-
std::string uid = auth_map.getUID(m_promote_from, public_key);
25+
std::string uid = auth_map.getUID(m_promote_from, public_key, log_context);
1826
auth_map.addKey(m_promote_to, public_key, uid);
1927
}
2028
// Remove expired short lived transient key

core/server/Config.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ void Config::loadRepositoryConfig(AuthenticationManager &auth_manager,
7878
DL_TRACE(log_context, "Registering repo " << r.id());
7979

8080
// Check for duplicate keys across different maps
81-
bool in_transient = auth_manager.hasKey(PublicKeyType::TRANSIENT, r.pub_key());
82-
bool in_session = auth_manager.hasKey(PublicKeyType::SESSION, r.pub_key());
81+
bool in_transient = auth_manager.hasKey(PublicKeyType::TRANSIENT, r.pub_key(), log_context);
82+
bool in_session = auth_manager.hasKey(PublicKeyType::SESSION, r.pub_key(), log_context);
8383

8484
if (in_transient && in_session) {
8585
// Key exists in both maps - this is an inconsistent state
@@ -114,7 +114,7 @@ void Config::loadRepositoryConfig(AuthenticationManager &auth_manager,
114114
DL_TRACE(log_context, "Validating repository keys after loading");
115115
for (const auto& repo_pair : m_repos) {
116116
const RepoData& repo = repo_pair.second;
117-
if (auth_manager.hasKey(PublicKeyType::PERSISTENT, repo.pub_key())) {
117+
if (auth_manager.hasKey(PublicKeyType::PERSISTENT, repo.pub_key(), log_context)) {
118118
DL_TRACE(log_context, "Key for " << repo.id() << " verified in PERSISTENT map");
119119
} else {
120120
DL_ERROR(log_context, "KEY MISSING! Repository " << repo.id()

0 commit comments

Comments
 (0)