Skip to content

Commit 6c1659a

Browse files
Nikita Vakulap-pautov
authored andcommitted
Support export via TLS (fix #12).
1 parent da2e4eb commit 6c1659a

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

src/batch_exporter.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ class BatchExporter {
111111
int attrSize{0};
112112
};
113113

114-
BatchExporter(StrView target,
114+
BatchExporter(StrView target, bool ssl, const std::string& trustedCert,
115115
size_t batchSize, size_t batchCount,
116116
const std::map<StrView, StrView>& resourceAttrs) :
117-
batchSize(batchSize), client(std::string(target))
117+
batchSize(batchSize), client(std::string(target), ssl, trustedCert)
118118
{
119119
free.reserve(batchCount);
120120
while (batchCount-- > 0) {

src/http_module.cpp

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "trace_context.hpp"
77
#include "batch_exporter.hpp"
88

9+
#include <fstream>
10+
911
extern ngx_module_t gHttpModule;
1012

1113
namespace {
@@ -26,6 +28,8 @@ struct MainConfBase {
2628

2729
struct MainConf : MainConfBase {
2830
std::map<StrView, StrView> resourceAttrs;
31+
bool ssl;
32+
std::string trustedCert;
2933
};
3034

3135
struct SpanAttr {
@@ -44,6 +48,7 @@ struct LocationConf {
4448
char* setExporter(ngx_conf_t* cf, ngx_command_t* cmd, void* conf);
4549
char* addResourceAttr(ngx_conf_t* cf, ngx_command_t* cmd, void* conf);
4650
char* addSpanAttr(ngx_conf_t* cf, ngx_command_t* cmd, void* conf);
51+
char* setTrustedCertificate(ngx_conf_t* cf, ngx_command_t* cmd, void* conf);
4752

4853
namespace Propagation {
4954

@@ -111,6 +116,10 @@ ngx_command_t gExporterCommands[] = {
111116
0,
112117
offsetof(MainConfBase, endpoint) },
113118

119+
{ ngx_string("trusted_certificate"),
120+
NGX_CONF_TAKE1,
121+
setTrustedCertificate },
122+
114123
{ ngx_string("interval"),
115124
NGX_CONF_TAKE1,
116125
ngx_conf_set_msec_slot,
@@ -569,6 +578,8 @@ ngx_int_t initWorkerProcess(ngx_cycle_t* cycle)
569578
try {
570579
gExporter.reset(new BatchExporter(
571580
toStrView(mcf->endpoint),
581+
mcf->ssl,
582+
mcf->trustedCert,
572583
mcf->batchSize,
573584
mcf->batchCount,
574585
mcf->resourceAttrs));
@@ -671,9 +682,7 @@ char* setExporter(ngx_conf_t* cf, ngx_command_t* cmd, void* conf)
671682
}
672683

673684
if (iremovePrefix(&mcf->endpoint, "https://")) {
674-
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
675-
"\"otel_exporter\" doesn't support \"https\" endpoints");
676-
return (char*)NGX_CONF_ERROR;
685+
mcf->ssl = true;
677686
} else {
678687
iremovePrefix(&mcf->endpoint, "http://");
679688
}
@@ -702,6 +711,36 @@ char* addResourceAttr(ngx_conf_t* cf, ngx_command_t* cmd, void* conf)
702711
return NGX_CONF_OK;
703712
}
704713

714+
char* setTrustedCertificate(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) {
715+
auto path = ((ngx_str_t*)cf->args->elts)[1];
716+
auto mcf = getMainConf(cf);
717+
718+
if (ngx_get_full_name(cf->pool, &cf->cycle->conf_prefix, &path) != NGX_OK) {
719+
return (char*)NGX_CONF_ERROR;
720+
}
721+
722+
try {
723+
std::ifstream file{(const char*)path.data, std::ios::binary};
724+
if (!file.is_open()) {
725+
ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
726+
"failed to open \"%V\"", &path);
727+
return (char*)NGX_CONF_ERROR;
728+
}
729+
file.exceptions(std::ios::failbit | std::ios::badbit);
730+
file.seekg(0, std::ios::end);
731+
size_t size = file.tellg();
732+
mcf->trustedCert.resize(size);
733+
file.seekg(0);
734+
file.read(&mcf->trustedCert[0], mcf->trustedCert.size());
735+
} catch (const std::exception& e) {
736+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
737+
"failed to read \"%V\": %s", &path, e.what());
738+
return (char*)NGX_CONF_ERROR;
739+
}
740+
741+
return NGX_CONF_OK;
742+
}
743+
705744
void* createMainConf(ngx_conf_t* cf)
706745
{
707746
auto cln = ngx_pool_cleanup_add(cf->pool, sizeof(MainConf));

src/trace_service_client.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ class TraceServiceClient {
1717
typedef std::function<void (Request, Response, grpc::Status)>
1818
ResponseCb;
1919

20-
TraceServiceClient(const std::string& target)
20+
TraceServiceClient(const std::string& target, bool ssl,
21+
const std::string& trustedCert)
2122
{
22-
auto channel = grpc::CreateChannel(
23-
target, grpc::InsecureChannelCredentials());
23+
std::shared_ptr<grpc::ChannelCredentials> creds;
24+
if (ssl) {
25+
grpc::SslCredentialsOptions options;
26+
options.pem_root_certs = trustedCert;
27+
creds = grpc::SslCredentials(options);
28+
} else {
29+
creds = grpc::InsecureChannelCredentials();
30+
}
31+
auto channel = grpc::CreateChannel(target, creds);
2432
channel->GetState(true); // trigger 'connecting' state
2533

2634
stub = TraceService::NewStub(channel);

0 commit comments

Comments
 (0)