-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbps_api_client.cpp
More file actions
343 lines (288 loc) · 14.1 KB
/
Copy pathdbps_api_client.cpp
File metadata and controls
343 lines (288 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// Project includes
#include "dbps_api_client.h"
#include "httplib_client.h"
// Standard library includes
#include <sstream>
#include <stdexcept>
#include <chrono>
using namespace dbps::external;
using namespace dbps::enum_utils;
// Generate a simple unique reference ID using timestamp
// TODO: Potentially not-unique if concurrent calls are made on the same millisecond.
// Can use atomic counters but may not be an issue to justify the complexity.
std::string GenerateReferenceId() {
auto now = std::chrono::system_clock::now();
auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()).count();
return std::to_string(timestamp);
}
// Auxiliary function to check if HTTP status code indicates success
bool IsHttpSuccess(int status_code) {
return status_code >= 200 && status_code < 300;
}
// ApiResponse method implementations
void ApiResponse::SetHttpStatusCode(int code) { http_status_code_ = code; }
void ApiResponse::SetApiClientError(const std::string& error) { api_client_error_ = error; }
void ApiResponse::SetRawResponse(const std::string& raw_response) { raw_response_ = raw_response; }
bool ApiResponse::HasHttpStatusCode() const { return http_status_code_.has_value(); }
bool ApiResponse::HasApiClientError() const { return api_client_error_.has_value(); }
bool ApiResponse::HasRawResponse() const { return raw_response_.has_value(); }
int ApiResponse::GetHttpStatusCode() const { return http_status_code_.value(); }
const std::string& ApiResponse::GetApiClientError() const { return api_client_error_.value(); }
const std::string& ApiResponse::GetRawResponse() const { return raw_response_.value(); }
bool ApiResponse::Success() const {
return !HasApiClientError() && HasJsonResponse() && GetJsonResponse().IsValid() &&
HasHttpStatusCode() && IsHttpSuccess(GetHttpStatusCode());
}
std::string ApiResponse::ErrorMessage() const {
if (HasApiClientError()) return "API client error: " + GetApiClientError();
if (!HasHttpStatusCode()) return "No HTTP status code";
if (!IsHttpSuccess(GetHttpStatusCode())) return "Non-2xx HTTP status code: " + std::to_string(GetHttpStatusCode());
if (!HasJsonResponse()) return "No JSON response";
if (!GetJsonResponse().IsValid()) return "Invalid JSON response";
return "Successful call";
}
std::map<std::string, std::string> ApiResponse::ErrorFields() const {
std::map<std::string, std::string> fields;
if (HasJsonRequest()) {
fields["request_string"] = GetJsonRequest().ToJson();
fields["request_validation"] = GetJsonRequest().GetValidationError();
fields["request_is_valid"] = GetJsonRequest().IsValid() ? "true" : "false";
}
if (HasRawResponse()) {
fields["response_string"] = GetRawResponse();
}
if (HasJsonResponse()) {
fields["response_validation"] = GetJsonResponse().GetValidationError();
fields["response_is_valid"] = GetJsonResponse().IsValid() ? "true" : "false";
}
if (HasHttpStatusCode()) {
fields["http_status_code"] = std::to_string(GetHttpStatusCode());
}
if (HasApiClientError()) {
fields["api_client_error"] = GetApiClientError();
}
return fields;
}
// EncryptApiResponse method implementations
void EncryptApiResponse::SetJsonResponse(const EncryptJsonResponse& response) {
if (response.IsValid()) {
decoded_ciphertext_ = response.encrypted_value_;
} else {
decoded_ciphertext_.reset();
}
encrypt_response_ = response;
}
const EncryptJsonResponse& EncryptApiResponse::GetJsonResponse() const { return encrypt_response_.value(); }
bool EncryptApiResponse::HasJsonResponse() const { return encrypt_response_.has_value(); }
span<const uint8_t> EncryptApiResponse::GetResponseCiphertext() const {
// Adding the null check for safety, but in regular flows, the method is not called unless various checks pass.
if (!decoded_ciphertext_.has_value()) {
std::cerr << "ERROR: GetResponseCiphertext() called but decoded_ciphertext_ is not available" << std::endl;
return span<const uint8_t>();
}
return span<const uint8_t>(decoded_ciphertext_.value());
}
const EncryptJsonResponse& EncryptApiResponse::GetResponseAttributes() const {
return GetJsonResponse();
}
void EncryptApiResponse::SetJsonRequest(const EncryptJsonRequest& request) { json_request_ = request; }
bool EncryptApiResponse::HasJsonRequest() const { return json_request_.has_value(); }
const JsonRequest& EncryptApiResponse::GetJsonRequest() const { return json_request_.value(); }
// DecryptApiResponse method implementations
void DecryptApiResponse::SetJsonResponse(const DecryptJsonResponse& response) {
if (response.IsValid()) {
decoded_plaintext_ = response.decrypted_value_;
} else {
decoded_plaintext_.reset();
}
decrypt_response_ = response;
}
const DecryptJsonResponse& DecryptApiResponse::GetJsonResponse() const { return decrypt_response_.value(); }
bool DecryptApiResponse::HasJsonResponse() const { return decrypt_response_.has_value(); }
span<const uint8_t> DecryptApiResponse::GetResponsePlaintext() const {
// Adding the null check for safety, but in regular flows, the method is not called unless various checks pass.
if (!decoded_plaintext_.has_value()) {
std::cerr << "ERROR: GetResponsePlaintext() called but decoded_plaintext_ is not available" << std::endl;
return span<const uint8_t>();
}
return span<const uint8_t>(decoded_plaintext_.value());
}
const DecryptJsonResponse& DecryptApiResponse::GetResponseAttributes() const {
return GetJsonResponse();
}
void DecryptApiResponse::SetJsonRequest(const DecryptJsonRequest& request) { json_request_ = request; }
bool DecryptApiResponse::HasJsonRequest() const { return json_request_.has_value(); }
const JsonRequest& DecryptApiResponse::GetJsonRequest() const { return json_request_.value(); }
DBPSApiClient::DBPSApiClient(std::shared_ptr<HttpClientBase> http_client)
: http_client_(std::move(http_client)) {
}
std::string DBPSApiClient::HealthCheck() {
auto response = http_client_->Get("/healthz", false);
if (!response.error_message.empty()) {
return "Error: " + response.error_message;
}
if (!IsHttpSuccess(response.status_code)) {
return "Health check failed with status: " + std::to_string(response.status_code);
}
return response.result;
}
EncryptApiResponse DBPSApiClient::Encrypt(
span<const uint8_t> plaintext,
const std::string& column_name,
Type::type datatype,
const std::optional<int>& datatype_length,
CompressionCodec::type compression,
Format::type format,
const std::map<std::string, std::string>& encoding_attributes,
CompressionCodec::type encrypted_compression,
const std::string& key_id,
const std::string& user_id,
const std::string& application_context
) {
EncryptJsonRequest json_request;
json_request.column_name_ = column_name;
json_request.datatype_ = datatype;
json_request.datatype_length_ = datatype_length;
json_request.compression_ = compression;
json_request.format_ = format;
json_request.encoding_attributes_ = encoding_attributes;
json_request.encrypted_compression_ = encrypted_compression;
json_request.key_id_ = key_id;
json_request.user_id_ = user_id;
json_request.application_context_ = application_context;
json_request.reference_id_ = GenerateReferenceId();
EncryptApiResponse api_response;
try {
// Set the binary plaintext data directly (base64 conversion handled on json request functions)
json_request.value_ = std::vector<uint8_t>(plaintext.begin(), plaintext.end());
// Set the complete request after all fields are populated
api_response.SetJsonRequest(json_request);
// Check if the request is valid
if (!json_request.IsValid()) {
api_response.SetApiClientError("Invalid encrypt request");
return api_response;
}
// Make the POST request
auto http_response = http_client_->Post("/encrypt", json_request.ToJson());
api_response.SetHttpStatusCode(http_response.status_code);
// Check if the HTTP response has an error and include the server response body when available
if (!http_response.error_message.empty() || !IsHttpSuccess(http_response.status_code)) {
std::string error_msg = "HTTP POST request failed for /encrypt: [" + std::to_string(http_response.status_code) + "] [" + http_response.error_message + "]";
if (!http_response.result.empty()) {
error_msg += " Server response: " + http_response.result;
}
api_response.SetApiClientError(error_msg);
api_response.SetRawResponse(http_response.result);
return api_response;
}
// Create an EncryptJsonResponse and parse since the HTTP response level succeeded.
EncryptJsonResponse json_response;
json_response.Parse(http_response.result);
api_response.SetJsonResponse(json_response);
// Check if the response is valid
if (!json_response.IsValid()) {
api_response.SetApiClientError("Invalid JSON encrypt response");
api_response.SetRawResponse(http_response.result);
return api_response;
}
// Check if the decoded ciphertext is empty
if (api_response.GetResponseCiphertext().empty()) {
api_response.SetApiClientError("Decoded ciphertext response is empty");
api_response.SetRawResponse(http_response.result);
return api_response;
}
} catch (const std::exception& e) {
api_response.SetApiClientError("API client encrypt unexpected error: " + std::string(e.what()));
}
// Finally return the API response if all is good.
return api_response;
}
DecryptApiResponse DBPSApiClient::Decrypt(
span<const uint8_t> ciphertext,
const std::string& column_name,
Type::type datatype,
const std::optional<int>& datatype_length,
CompressionCodec::type compression,
Format::type format,
const std::map<std::string, std::string>& encoding_attributes,
CompressionCodec::type encrypted_compression,
const std::string& key_id,
const std::string& user_id,
const std::string& application_context,
const std::map<std::string, std::string>& encryption_metadata
) {
DecryptJsonRequest json_request;
json_request.column_name_ = column_name;
json_request.datatype_ = datatype;
json_request.datatype_length_ = datatype_length;
json_request.compression_ = compression;
json_request.format_ = format;
json_request.encoding_attributes_ = encoding_attributes;
json_request.encrypted_compression_ = encrypted_compression;
json_request.key_id_ = key_id;
json_request.user_id_ = user_id;
json_request.application_context_ = application_context;
json_request.encryption_metadata_ = encryption_metadata;
json_request.reference_id_ = GenerateReferenceId();
DecryptApiResponse api_response;
try {
// Set the binary ciphertext data directly (base64 conversion handled on json request functions)
json_request.encrypted_value_ = std::vector<uint8_t>(ciphertext.begin(), ciphertext.end());
// Set the complete request after all fields are populated
api_response.SetJsonRequest(json_request);
// Check if the request is valid
if (!json_request.IsValid()) {
api_response.SetApiClientError("Invalid decrypt request");
return api_response;
}
// Make the POST request
auto http_response = http_client_->Post("/decrypt", json_request.ToJson());
api_response.SetHttpStatusCode(http_response.status_code);
// Check if the HTTP response has an error and include the server response body when available
if (!http_response.error_message.empty() || !IsHttpSuccess(http_response.status_code)) {
std::string error_msg = "HTTP POST request failed for /decrypt: [" + std::to_string(http_response.status_code) + "] [" + http_response.error_message + "]";
if (!http_response.result.empty()) {
error_msg += " Server response: " + http_response.result;
}
api_response.SetApiClientError(error_msg);
api_response.SetRawResponse(http_response.result);
return api_response;
}
// Create a DecryptJsonResponse and parse since the HTTP response level succeeded.
DecryptJsonResponse json_response;
json_response.Parse(http_response.result);
api_response.SetJsonResponse(json_response);
// Check if the response is valid
if (!json_response.IsValid()) {
api_response.SetApiClientError("Invalid JSON decrypt response");
api_response.SetRawResponse(http_response.result);
return api_response;
}
// Check if the decoded plaintext is empty
if (api_response.GetResponsePlaintext().empty()) {
api_response.SetApiClientError("Decoded plaintext response is empty");
api_response.SetRawResponse(http_response.result);
return api_response;
}
} catch (const std::exception& e) {
api_response.SetApiClientError("API client decrypt unexpected error: " + std::string(e.what()));
}
return api_response;
}