Skip to content

Commit 3c92c0a

Browse files
committed
agents: add root certs API and use it in OTLPAgent
Also, make all our existing agents code to use it. Ideally having an API like this being exposed from the `node::crypto` namespace (crypto_context specifically) would be ideal, but for the time being this should do. PR-URL: #340 Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent f6e6e7a commit 3c92c0a

9 files changed

Lines changed: 68 additions & 19 deletions

File tree

agents/grpc/src/grpc_agent.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "nsolid/nsolid_api.h"
66
#include "nsolid/nsolid_util.h"
77
#include "../../otlp/src/otlp_common.h"
8+
#include "../../src/root_certs.h"
89
#include "../../src/span_collector.h"
910
#include "absl/log/initialize.h"
1011
#include "opentelemetry/sdk/metrics/data/metric_data.h"
@@ -73,10 +74,6 @@ const size_t GRPC_MAX_SIZE = 4L * 1024 * 1024; // 4GB
7374
const int PUB_KEY_SIZE = 40;
7475
const int CONSOLE_ID_SIZE = 36;
7576

76-
static const char* const root_certs[] = {
77-
#include "node_root_certs.h" // NOLINT(build/include_order)
78-
};
79-
8077
template <typename... Args>
8178
inline void Debug(Args&&... args) {
8279
per_process::Debug(DebugCategory::NSOLID_GRPC_AGENT,
@@ -457,8 +454,8 @@ GrpcAgent::GrpcAgent(): hooks_init_(false),
457454

458455
if (custom_certs_.empty()) {
459456
Debug("Using default certs\n");
460-
for (size_t i = 0; i < sizeof(root_certs) / sizeof(root_certs[0]); i++) {
461-
cacert_ += root_certs[i];
457+
for (size_t i = 0; i < GetRootCertsCount(); i++) {
458+
cacert_ += GetRootCerts()[i];
462459
cacert_ += "\n";
463460
}
464461
}

agents/otlp/src/otlp_agent.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "opentelemetry/exporters/otlp/otlp_http_exporter.h"
1919
#include "opentelemetry/ext/http/client/curl/http_client_curl.h"
2020
#include "opentelemetry/trace/propagation/detail/hex.h"
21+
#include "../../src/root_certs.h"
2122
#include "../../src/span_collector.h"
2223

2324
using ThreadMetricsStor = node::nsolid::ThreadMetrics::MetricsStor;
@@ -96,6 +97,11 @@ OTLPAgent::OTLPAgent(): ready_(false),
9697
ASSERT_EQ(0, uv_mutex_init(&start_lock_));
9798
ASSERT_EQ(0, exit_lock_.init(true));
9899
is_running_ = true;
100+
101+
for (size_t i = 0; i < GetRootCertsCount(); i++) {
102+
cacert_ += GetRootCerts()[i];
103+
cacert_ += "\n";
104+
}
99105
}
100106

101107

@@ -538,7 +544,7 @@ void OTLPAgent::config_otlp_endpoint(const json& config) {
538544
}
539545

540546
metrics_exporter_.reset(
541-
new OTLPMetrics(&loop_, GetScope()));
547+
new OTLPMetrics(&loop_, GetScope(), cacert_));
542548
return;
543549
}
544550

@@ -563,7 +569,7 @@ void OTLPAgent::config_otlp_endpoint(const json& config) {
563569
}
564570

565571
metrics_exporter_.reset(
566-
new OTLPMetrics(&loop_, url, "", is_http, GetScope()));
572+
new OTLPMetrics(&loop_, url, "", is_http, GetScope(), cacert_));
567573
}
568574

569575

@@ -582,12 +588,14 @@ void OTLPAgent::setup_trace_otlp_exporter(
582588
exporter::otlp::OtlpHttpExporterOptions& opts) {
583589
opts.content_type = exporter::otlp::HttpRequestContentType::kBinary;
584590
opts.console_debug = true;
591+
opts.ssl_ca_cert_string = cacert_;
585592
otlp_exporter_.reset(new exporter::otlp::OtlpHttpExporter(opts));
586593
}
587594

588595

589596
void OTLPAgent::setup_trace_grpc_otlp_exporter(
590597
exporter::otlp::OtlpGrpcExporterOptions& opts) {
598+
opts.ssl_credentials_cacert_as_string = cacert_;
591599
otlp_exporter_.reset(new exporter::otlp::OtlpGrpcExporter(opts));
592600
}
593601

agents/otlp/src/otlp_agent.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ class OTLPAgent {
149149
nsuv::ns_async config_msg_;
150150
TSQueue<nlohmann::json> config_msg_q_;
151151
nlohmann::json config_;
152+
153+
std::string cacert_;
152154
};
153155

154156
} // namespace otlp

agents/otlp/src/otlp_metrics.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,18 @@ static std::vector<std::string> discarded_metrics = {
6060
};
6161

6262
OTLPMetrics::OTLPMetrics(uv_loop_t* loop,
63-
InstrumentationScope* scope):
63+
InstrumentationScope* scope,
64+
const std::string& cacert):
6465
scope_(scope) {
6566
const std::string prot = GetOtlpDefaultHttpMetricsProtocol();
6667
if (prot == "grpc") {
6768
OtlpGrpcMetricExporterOptions opts;
69+
opts.ssl_credentials_cacert_as_string = cacert;
6870
otlp_metric_exporter_ = std::make_unique<OtlpGrpcMetricExporter>(opts);
6971
} else {
7072
OtlpHttpMetricExporterOptions opts;
7173
opts.console_debug = true;
74+
opts.ssl_ca_cert_string = cacert;
7275
otlp_metric_exporter_ = std::make_unique<OtlpHttpMetricExporter>(opts);
7376
}
7477
}
@@ -77,7 +80,8 @@ OTLPMetrics::OTLPMetrics(uv_loop_t* loop,
7780
const std::string& url,
7881
const std::string& key,
7982
bool is_http,
80-
InstrumentationScope* scope):
83+
InstrumentationScope* scope,
84+
const std::string& cacert):
8185
scope_(scope),
8286
key_(key),
8387
url_(url) {
@@ -86,10 +90,12 @@ OTLPMetrics::OTLPMetrics(uv_loop_t* loop,
8690
opts.url = url + "/v1/metrics";
8791
opts.content_type = HttpRequestContentType::kBinary;
8892
opts.console_debug = true;
93+
opts.ssl_ca_cert_string = cacert;
8994
otlp_metric_exporter_ = std::make_unique<OtlpHttpMetricExporter>(opts);
9095
} else {
9196
OtlpGrpcMetricExporterOptions opts;
9297
opts.endpoint = url + "/v1/metrics";
98+
opts.ssl_credentials_cacert_as_string = cacert;
9399
otlp_metric_exporter_ = std::make_unique<OtlpGrpcMetricExporter>(opts);
94100
}
95101
}

agents/otlp/src/otlp_metrics.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ class OTLPMetrics final: public MetricsExporter {
3131
public:
3232
explicit OTLPMetrics(
3333
uv_loop_t* loop,
34-
OPENTELEMETRY_NAMESPACE::sdk::instrumentationscope::InstrumentationScope*);
34+
OPENTELEMETRY_NAMESPACE::sdk::instrumentationscope::InstrumentationScope*,
35+
const std::string& cacert);
3536
explicit OTLPMetrics(
3637
uv_loop_t* loop,
3738
const std::string& url,
3839
const std::string& key,
3940
bool is_http,
40-
OPENTELEMETRY_NAMESPACE::sdk::instrumentationscope::InstrumentationScope*);
41+
OPENTELEMETRY_NAMESPACE::sdk::instrumentationscope::InstrumentationScope*,
42+
const std::string& cacert);
4143

4244
virtual ~OTLPMetrics();
4345

agents/src/http_client.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "http_client.h"
2-
#include "util.h"
2+
#include "root_certs.h"
33

44
#include <queue>
55

@@ -8,10 +8,6 @@
88
namespace node {
99
namespace nsolid {
1010

11-
static const char* const root_certs[] = {
12-
#include "node_root_certs.h" // NOLINT(build/include_order)
13-
};
14-
1511
CurlContext::CurlContext(HttpClient* http_client, curl_socket_t sockfd):
1612
http_client_(http_client),
1713
poll_handle_(new nsuv::ns_poll),
@@ -43,8 +39,8 @@ HttpClient::HttpClient(uv_loop_t* loop): loop_(loop),
4339
CURLMOPT_TIMERFUNCTION,
4440
start_timeout_));
4541
ASSERT_EQ(0, curl_multi_setopt(curl_handle_, CURLMOPT_TIMERDATA, this));
46-
for (size_t i = 0; i < node::arraysize(root_certs); i++) {
47-
cacert_ += root_certs[i];
42+
for (size_t i = 0; i < GetRootCertsCount(); i++) {
43+
cacert_ += GetRootCerts()[i];
4844
cacert_ += "\n";
4945
}
5046
}

agents/src/root_certs.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "root_certs.h"
2+
3+
namespace node {
4+
namespace nsolid {
5+
6+
static const char* const root_certs[] = {
7+
#include "node_root_certs.h"
8+
};
9+
10+
const char* const* GetRootCerts() {
11+
return root_certs;
12+
}
13+
14+
size_t GetRootCertsCount() {
15+
return sizeof(root_certs) / sizeof(root_certs[0]);
16+
}
17+
18+
} // namespace nsolid
19+
} // namespace node

agents/src/root_certs.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef AGENTS_SRC_ROOT_CERTS_H_
2+
#define AGENTS_SRC_ROOT_CERTS_H_
3+
4+
#include <cstddef>
5+
6+
namespace node {
7+
namespace nsolid {
8+
9+
// Accessor for the root certificates array
10+
const char* const* GetRootCerts();
11+
// Accessor for the number of root certificates
12+
size_t GetRootCertsCount();
13+
14+
} // namespace nsolid
15+
} // namespace node
16+
17+
#endif // AGENTS_SRC_ROOT_CERTS_H_

node.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@
395395
'agents/src/http_client.h',
396396
'agents/src/profile_collector.cc',
397397
'agents/src/profile_collector.h',
398+
'agents/src/root_certs.cc',
399+
'agents/src/root_certs.h',
398400
'agents/src/span_collector.cc',
399401
'agents/src/span_collector.h',
400402
'agents/grpc/src/asset_stream.cc',

0 commit comments

Comments
 (0)