Skip to content

Commit efddcc1

Browse files
committed
- Refactor and rename connection_config_file_path to configuration_map.
- Remove duplicated and unused misc functions to extract values. - Made move explicit difference between the configuration map and the connection config file.
1 parent 2ce9e55 commit efddcc1

9 files changed

Lines changed: 88 additions & 187 deletions

src/common/dbpa_interface.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class DBPS_EXPORT DataBatchProtectionAgentInterface {
101101
// (e.g., as a serialized map/JSON field).
102102
virtual void init(
103103
std::string column_name,
104-
std::map<std::string, std::string> connection_config,
104+
std::map<std::string, std::string> configuration_map,
105105
std::string app_context,
106106
std::string column_key_id,
107107
Type::type datatype,
@@ -110,7 +110,7 @@ class DBPS_EXPORT DataBatchProtectionAgentInterface {
110110
std::optional<std::map<std::string, std::string>> column_encryption_metadata)
111111
{
112112
column_name_ = std::move(column_name);
113-
connection_config_ = std::move(connection_config);
113+
configuration_map_ = std::move(configuration_map);
114114
app_context_ = std::move(app_context);
115115
column_key_id_ = std::move(column_key_id);
116116
datatype_ = datatype;
@@ -150,7 +150,7 @@ class DBPS_EXPORT DataBatchProtectionAgentInterface {
150150

151151
protected:
152152
std::string column_name_;
153-
std::map<std::string, std::string> connection_config_;
153+
std::map<std::string, std::string> configuration_map_;
154154
std::string app_context_; // includes user_id
155155
std::optional<std::map<std::string, std::string>> column_encryption_metadata_;
156156

src/common/dbpa_local.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const std::map<std::string, std::string>& LocalDecryptionResult::error_fields()
111111

112112
void LocalDataBatchProtectionAgent::init(
113113
std::string column_name,
114-
std::map<std::string, std::string> connection_config,
114+
std::map<std::string, std::string> configuration_map,
115115
std::string app_context,
116116
std::string column_key_id,
117117
Type::type datatype,
@@ -126,7 +126,7 @@ void LocalDataBatchProtectionAgent::init(
126126
// Call the base class init to store the configuration
127127
DataBatchProtectionAgentInterface::init(
128128
std::move(column_name),
129-
std::move(connection_config),
129+
std::move(configuration_map),
130130
std::move(app_context),
131131
std::move(column_key_id),
132132
datatype,

src/common/dbpa_local.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class DBPS_EXPORT LocalDataBatchProtectionAgent : public DataBatchProtectionAgen
104104
// DataBatchProtectionAgentInterface implementation
105105
void init(
106106
std::string column_name,
107-
std::map<std::string, std::string> connection_config,
107+
std::map<std::string, std::string> configuration_map,
108108
std::string app_context,
109109
std::string column_key_id,
110110
Type::type datatype,

src/common/dbpa_local_test.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ class LocalDataBatchProtectionAgentTest : public ::testing::Test {
4141
TEST_F(LocalDataBatchProtectionAgentTest, SuccessfulEncryption) {
4242
LocalDataBatchProtectionAgent agent;
4343

44-
std::map<std::string, std::string> connection_config;
44+
std::map<std::string, std::string> configuration_map = {};
4545
std::string app_context = R"({"user_id": "test_user"})";
4646

47-
EXPECT_NO_THROW(agent.init("test_column", connection_config, app_context, "test_key",
47+
EXPECT_NO_THROW(agent.init("test_column", configuration_map, app_context, "test_key",
4848
Type::UNDEFINED, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt));
4949

5050
std::vector<uint8_t> test_data = {1, 2, 3, 4};
@@ -59,10 +59,10 @@ TEST_F(LocalDataBatchProtectionAgentTest, SuccessfulEncryption) {
5959
TEST_F(LocalDataBatchProtectionAgentTest, SuccessfulEncryptionCompressedDictionary) {
6060
LocalDataBatchProtectionAgent agent;
6161

62-
std::map<std::string, std::string> connection_config;
62+
std::map<std::string, std::string> configuration_map = {};
6363
std::string app_context = R"({"user_id": "test_user"})";
6464

65-
EXPECT_NO_THROW(agent.init("test_column", connection_config, app_context, "test_key",
65+
EXPECT_NO_THROW(agent.init("test_column", configuration_map, app_context, "test_key",
6666
Type::BYTE_ARRAY, std::nullopt, CompressionCodec::GZIP, std::nullopt));
6767

6868
// GZIP compressed data for strings "apple" and "banana"
@@ -84,10 +84,10 @@ TEST_F(LocalDataBatchProtectionAgentTest, SuccessfulEncryptionCompressedDictiona
8484
TEST_F(LocalDataBatchProtectionAgentTest, SuccessfulDecryption) {
8585
LocalDataBatchProtectionAgent agent;
8686

87-
std::map<std::string, std::string> connection_config;
87+
std::map<std::string, std::string> configuration_map = {};
8888
std::string app_context = R"({"user_id": "test_user"})";
8989

90-
EXPECT_NO_THROW(agent.init("test_column", connection_config, app_context, "test_key",
90+
EXPECT_NO_THROW(agent.init("test_column", configuration_map, app_context, "test_key",
9191
Type::UNDEFINED, std::nullopt, CompressionCodec::UNCOMPRESSED, DBPS_ENCRYPTION_METADATA));
9292

9393
std::vector<uint8_t> test_data = {1, 2, 3, 4};
@@ -103,10 +103,10 @@ TEST_F(LocalDataBatchProtectionAgentTest, SuccessfulDecryption) {
103103
TEST_F(LocalDataBatchProtectionAgentTest, RoundTripEncryptDecrypt) {
104104
LocalDataBatchProtectionAgent encrypt_agent;
105105

106-
std::map<std::string, std::string> connection_config;
106+
std::map<std::string, std::string> configuration_map = {};
107107
std::string app_context = R"({"user_id": "test_user"})";
108108

109-
EXPECT_NO_THROW(encrypt_agent.init("test_column", connection_config, app_context, "test_key",
109+
EXPECT_NO_THROW(encrypt_agent.init("test_column", configuration_map, app_context, "test_key",
110110
Type::UNDEFINED, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt));
111111

112112
// Original data to encrypt
@@ -132,7 +132,7 @@ TEST_F(LocalDataBatchProtectionAgentTest, RoundTripEncryptDecrypt) {
132132

133133
// Create a new agent for decryption with the encryption_metadata from the encryption result
134134
LocalDataBatchProtectionAgent decrypt_agent;
135-
EXPECT_NO_THROW(decrypt_agent.init("test_column", connection_config, app_context, "test_key",
135+
EXPECT_NO_THROW(decrypt_agent.init("test_column", configuration_map, app_context, "test_key",
136136
Type::UNDEFINED, std::nullopt, CompressionCodec::UNCOMPRESSED, encryption_metadata));
137137

138138
// Decrypt the ciphertext
@@ -183,21 +183,21 @@ TEST_F(LocalDataBatchProtectionAgentTest, DecryptWithoutInit) {
183183
TEST_F(LocalDataBatchProtectionAgentTest, MissingUserId) {
184184
LocalDataBatchProtectionAgent agent;
185185

186-
std::map<std::string, std::string> connection_config;
186+
std::map<std::string, std::string> configuration_map = {};
187187
std::string app_context = R"({"role": "admin"})";
188188

189-
EXPECT_THROW(agent.init("test_column", connection_config, app_context, "test_key",
189+
EXPECT_THROW(agent.init("test_column", configuration_map, app_context, "test_key",
190190
Type::BYTE_ARRAY, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt), DBPSException);
191191
}
192192

193193
// Test missing page_encoding in encoding_attributes
194194
TEST_F(LocalDataBatchProtectionAgentTest, MissingPageEncoding) {
195195
LocalDataBatchProtectionAgent agent;
196196

197-
std::map<std::string, std::string> connection_config;
197+
std::map<std::string, std::string> configuration_map = {};
198198
std::string app_context = R"({"user_id": "test_user"})";
199199

200-
EXPECT_NO_THROW(agent.init("test_column", connection_config, app_context, "test_key",
200+
EXPECT_NO_THROW(agent.init("test_column", configuration_map, app_context, "test_key",
201201
Type::BYTE_ARRAY, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt));
202202

203203
std::vector<uint8_t> test_data = {1, 2, 3, 4};

src/common/dbpa_remote.cpp

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "../client/dbps_api_client.h"
2020
#include "../client/httplib_pool_registry.h"
2121
#include "../client/httplib_pooled_client.h"
22+
#include "dbpa_utils.h"
2223
#include "enum_utils.h"
2324
#include <iostream>
2425

@@ -167,7 +168,7 @@ RemoteDataBatchProtectionAgent::RemoteDataBatchProtectionAgent(std::shared_ptr<H
167168

168169
void RemoteDataBatchProtectionAgent::init(
169170
std::string column_name,
170-
std::map<std::string, std::string> connection_config,
171+
std::map<std::string, std::string> configuration_map,
171172
std::string app_context,
172173
std::string column_key_id,
173174
Type::type datatype,
@@ -182,7 +183,7 @@ void RemoteDataBatchProtectionAgent::init(
182183
// Call the base class init to store the configuration
183184
DataBatchProtectionAgentInterface::init(
184185
std::move(column_name),
185-
std::move(connection_config),
186+
std::move(configuration_map),
186187
std::move(app_context),
187188
std::move(column_key_id),
188189
datatype,
@@ -212,7 +213,7 @@ void RemoteDataBatchProtectionAgent::init(
212213
// The API client constructor does not attempt a HTTP connection with the server. The first Get/Post call creates the HTTP connection.
213214
if (!api_client_) {
214215
// given that there is no API client, instantiate a new one with a new HTTP client.
215-
auto http_client = InstantiateHttpClient(); //uses connection_config_
216+
auto http_client = InstantiateHttpClient(); //uses configuration_map_
216217
api_client_ = std::make_unique<DBPSApiClient>(http_client);
217218
} else {
218219
std::cerr << "INFO: RemoteDataBatchProtectionAgent::init() - Using existing API client" << std::endl;
@@ -366,24 +367,24 @@ void RemoteDataBatchProtectionAgent::UpdateEncryptionMetadata(std::optional<std:
366367
column_encryption_metadata_ = std::move(encryption_metadata);
367368
}
368369

369-
std::optional<nlohmann::json> RemoteDataBatchProtectionAgent::LoadConnectionConfigFile(const std::map<std::string, std::string>& connection_config) const {
370-
const std::string error_trace = "ERROR: RemoteDataBatchProtectionAgent::LoadConnectionConfigFile() - ";
371-
auto config_file_it = connection_config.find(k_connection_config_key_);
372-
if (config_file_it == connection_config.end()) {
370+
std::optional<nlohmann::json> RemoteDataBatchProtectionAgent::LoadConfigFile(const std::string& config_file_key) const {
371+
const std::string error_trace = "ERROR: RemoteDataBatchProtectionAgent::LoadConfigFile() - ";
372+
auto config_file_it = configuration_map_.find(config_file_key);
373+
if (config_file_it == configuration_map_.end()) {
373374
std::cerr << error_trace
374-
<< "connection_config does not provide " << k_connection_config_key_ << std::endl;
375+
<< "configuration_map does not provide " << config_file_key << std::endl;
375376
return std::nullopt;
376377
}
377378
auto config_file_path = config_file_it->second;
378379
if (!std::filesystem::exists(config_file_path)) {
379-
std::cerr << error_trace << k_connection_config_key_ << " [" << config_file_path
380+
std::cerr << error_trace << config_file_key << " [" << config_file_path
380381
<< "] does not exist" << std::endl;
381382
return std::nullopt;
382383
}
383384

384385
std::ifstream config_file(config_file_path);
385386
if (!config_file.is_open()) {
386-
std::cerr << error_trace << k_connection_config_key_ << " [" << config_file_path
387+
std::cerr << error_trace << config_file_key << " [" << config_file_path
387388
<< "] could not be opened" << std::endl;
388389
return std::nullopt;
389390
}
@@ -393,7 +394,7 @@ std::optional<nlohmann::json> RemoteDataBatchProtectionAgent::LoadConnectionConf
393394
config_file.close();
394395

395396
if (config_file_contents.empty()) {
396-
std::cerr << error_trace << k_connection_config_key_ << " [" << config_file_path
397+
std::cerr << error_trace << config_file_key << " [" << config_file_path
397398
<< "] is empty" << std::endl;
398399
return std::nullopt;
399400
}
@@ -402,19 +403,19 @@ std::optional<nlohmann::json> RemoteDataBatchProtectionAgent::LoadConnectionConf
402403
auto json = nlohmann::json::parse(config_file_contents);
403404
return json;
404405
} catch (const nlohmann::json::exception& e) {
405-
std::cerr << error_trace << k_connection_config_key_ << " [" << config_file_path
406+
std::cerr << error_trace << config_file_key << " [" << config_file_path
406407
<< "] is not a valid JSON file: " << e.what() << std::endl;
407408
return std::nullopt;
408409
}
409410
}
410411

411412
std::shared_ptr<HttpClientInterface> RemoteDataBatchProtectionAgent::InstantiateHttpClient() {
412-
auto config_json_opt = LoadConnectionConfigFile(connection_config_);
413+
auto config_json_opt = LoadConfigFile(k_connection_config_key_);
413414
auto server_url_opt = config_json_opt ? ExtractServerUrl(*config_json_opt) : std::nullopt;
414415
if (!server_url_opt || server_url_opt->empty()) {
415-
std::cerr << "ERROR: RemoteDataBatchProtectionAgent::InstantiateHttpClient() - No server_url provided in connection_config." << std::endl;
416+
std::cerr << "ERROR: RemoteDataBatchProtectionAgent::InstantiateHttpClient() - No server_url provided in " << k_connection_config_key_ << "." << std::endl;
416417
initialized_ = "Agent not properly initialized - server_url missing";
417-
throw DBPSException("No server_url provided in connection_config");
418+
throw DBPSException("No server_url provided in " + k_connection_config_key_);
418419
}
419420
server_url_ = *server_url_opt;
420421
std::cerr << "INFO: RemoteDataBatchProtectionAgent::init() - server_url extracted: [" << server_url_ << "]" << std::endl;
@@ -475,7 +476,7 @@ static long long get_int_or_default(const nlohmann::json& config_json, const cha
475476
return val.get<long long>();
476477
}
477478

478-
//Extract pool config from connection_config. Expected format is a set of key-values pairs which are top level in the file
479+
// Extract pool config from connection_config json. Expected format is a set of key-values pairs which are top level in the file
479480
// there is no nesting
480481
HttplibPoolRegistry::PoolConfig RemoteDataBatchProtectionAgent::ExtractPoolConfig(const nlohmann::json& config_json) {
481482
HttplibPoolRegistry::PoolConfig pool_config;
@@ -511,35 +512,3 @@ std::size_t RemoteDataBatchProtectionAgent::ExtractNumWorkerThreads(const nlohma
511512
return static_cast<std::size_t>(
512513
get_int_or_default(config_json, "connection_pool.num_worker_threads", 0));
513514
}
514-
515-
std::optional<std::string> RemoteDataBatchProtectionAgent::ExtractUserId(const std::string& app_context) const {
516-
try {
517-
auto json = nlohmann::json::parse(app_context);
518-
if (json.contains("user_id") && json["user_id"].is_string()) {
519-
std::string user_id = json["user_id"];
520-
if (!user_id.empty()) {
521-
return user_id;
522-
}
523-
}
524-
} catch (const nlohmann::json::exception& e) {
525-
std::cerr << "ERROR: RemoteDataBatchProtectionAgent::ExtractUserId() - Failed to parse app_context JSON: " << e.what() << std::endl;
526-
}
527-
return std::nullopt;
528-
}
529-
530-
std::optional<Format::type> RemoteDataBatchProtectionAgent::ExtractPageEncoding(const std::map<std::string, std::string>& encoding_attributes) const {
531-
auto it = encoding_attributes.find("page_encoding");
532-
if (it != encoding_attributes.end()) {
533-
const std::string& encoding_str = it->second;
534-
auto format_opt = to_format_enum(encoding_str);
535-
if (format_opt.has_value()) {
536-
return format_opt.value();
537-
} else {
538-
std::cerr << "ERROR: RemoteDataBatchProtectionAgent::ExtractPageEncoding() - Unknown page_encoding: " << encoding_str << std::endl;
539-
return std::nullopt;
540-
}
541-
}
542-
// Return nullopt if page_encoding not found
543-
std::cerr << "ERROR: RemoteDataBatchProtectionAgent::ExtractPageEncoding() - page_encoding not found." << std::endl;
544-
return std::nullopt;
545-
}

src/common/dbpa_remote.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class DBPS_EXPORT RemoteDecryptionResult : public DecryptionResult {
9898
*/
9999
class DBPS_EXPORT RemoteDataBatchProtectionAgent : public DataBatchProtectionAgentInterface {
100100
public:
101-
// Constructor (default). Creates API_client during init() using server_url from connection_config
101+
// Constructor (default). Creates API_client during init() using server_url from the connection_config file.
102102
RemoteDataBatchProtectionAgent() = default;
103103

104104
// Constructor with HTTP client passed. Creates API_client immediately on the contructor.
@@ -107,7 +107,7 @@ class DBPS_EXPORT RemoteDataBatchProtectionAgent : public DataBatchProtectionAge
107107
// DataBatchProtectionAgentInterface implementation
108108
void init(
109109
std::string column_name,
110-
std::map<std::string, std::string> connection_config,
110+
std::map<std::string, std::string> configuration_map,
111111
std::string app_context,
112112
std::string column_key_id,
113113
Type::type datatype,
@@ -135,29 +135,26 @@ class DBPS_EXPORT RemoteDataBatchProtectionAgent : public DataBatchProtectionAge
135135
std::optional<std::string> initialized_;
136136
std::string server_url_;
137137
std::string user_id_;
138-
std::string k_connection_config_key_ = "connection_config_file_path";
138+
inline static const std::string k_connection_config_key_ = "connection_config_file_path";
139+
140+
// Helper methods for configuration parsing
139141

140-
// Extract pool config from connection_config
141-
// assumes all values in connection_config are optional, and will use default values if any not present.
142+
// Load and parse the config file in the configuration_map.
143+
std::optional<nlohmann::json> LoadConfigFile(const std::string& config_file_key) const;
144+
145+
// Extract pool config from the connection_config json. Values are optional, and will use default values if not present.
142146
HttplibPoolRegistry::PoolConfig ExtractPoolConfig(const nlohmann::json& config_json);
147+
143148
// Extract number of worker threads for pooled client; defaults to 0 (auto)
144149
std::size_t ExtractNumWorkerThreads(const nlohmann::json& config_json) const;
145150

146-
private:
147-
// Helper methods for configuration parsing
148-
149-
// Load and parse the connection config file specified in connection_config
150-
std::optional<nlohmann::json> LoadConnectionConfigFile(const std::map<std::string, std::string>& connection_config) const;
151+
// Extract server_url from parsed JSON config such as {"server_url": "http://localhost:8080"}
152+
std::optional<std::string> ExtractServerUrl(const nlohmann::json& config_json) const;
151153

154+
private:
152155
// Instantiate a new HTTP client using the connection config file
153156
std::shared_ptr<HttpClientInterface> InstantiateHttpClient();
154157

155-
// Extract server_url from parsed JSON config such as {"server_url": "http://localhost:8080"}
156-
std::optional<std::string> ExtractServerUrl(const nlohmann::json& config_json) const;
157-
158-
std::optional<std::string> ExtractUserId(const std::string& app_context) const;
159-
std::optional<Format::type> ExtractPageEncoding(const std::map<std::string, std::string>& encoding_attributes) const;
160-
161158
// Client instance
162159
std::unique_ptr<DBPSApiClient> api_client_;
163160
};

0 commit comments

Comments
 (0)