Skip to content

Commit 355def3

Browse files
committed
Add support for client-side cert/key to gRPC exports.
TODO: Add tests. I have not run or written any tests, other than verifying it worked once in my production system.
1 parent cfcda57 commit 355def3

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

api/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ if(WITH_OTLP_HTTP_SSL_PREVIEW)
113113
endif()
114114
endif()
115115

116+
if (WITH_OTLP_GRPC_MTLS_PREVIEW)
117+
target_compile_definitions(opentelemetry_api
118+
INTERFACE ENABLE_OTLP_GRPC_MTLS_PREVIEW)
119+
endif()
120+
116121
if(WITH_METRICS_EXEMPLAR_PREVIEW)
117122
target_compile_definitions(opentelemetry_api
118123
INTERFACE ENABLE_METRICS_EXEMPLAR_PREVIEW)

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#pragma once
55

6+
#include "absl/types/optional.h"
7+
#include "absl/types/variant.h"
68
#include "opentelemetry/exporters/otlp/otlp_environment.h"
79

810
#include <memory>
@@ -28,6 +30,19 @@ struct OtlpGrpcExporterOptions
2830
// ssl_credentials_cacert_as_string in-memory string representation of .pem file to be used for
2931
// SSL encryption.
3032
std::string ssl_credentials_cacert_as_string = GetOtlpDefaultSslCertificateString();
33+
34+
#ifdef ENABLE_OTLP_GRPC_MTLS_PREVIEW
35+
// At most one of ssl_client_key_* should be non-empty. If use_ssl_credentials, they will
36+
// be read to allow for mTLS.
37+
std::string ssl_client_key_path = GetOtlpDefaultTracesSslClientKeyPath();
38+
std::string ssl_client_key_string = GetOtlpDefaultTracesSslClientKeyString();
39+
40+
// At most one of ssl_client_cert_* should be non-empty. If use_ssl_credentials, they will
41+
// be read to allow for mTLS.
42+
std::string ssl_client_cert_path = GetOtlpDefaultTracesSslClientCertificatePath();
43+
std::string ssl_client_cert_string = GetOtlpDefaultTracesSslClientCertificateString();
44+
#endif
45+
3146
// Timeout for grpc deadline
3247
std::chrono::system_clock::duration timeout = GetOtlpDefaultTimeout();
3348
// Additional HTTP headers

exporters/otlp/src/otlp_grpc_client.cc

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ static std::string GetFileContents(const char *fpath)
3434
finstream.close();
3535
return contents;
3636
}
37+
38+
// If the file path is non-empty, returns the contents of the file. Otherwise returns contents.
39+
static std::string GetFileContentsOrInMemoryContents(
40+
const std::string& file_path, const std::string& contents) {
41+
if (!file_path.empty()) {
42+
return GetFileContents(file_path.c_str());
43+
}
44+
return contents;
45+
}
46+
3747
} // namespace
3848

3949
std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcExporterOptions &options)
@@ -58,17 +68,20 @@ std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcExporte
5868
grpc::ChannelArguments grpc_arguments;
5969
grpc_arguments.SetUserAgentPrefix(options.user_agent);
6070

61-
if (options.use_ssl_credentials)
62-
{
63-
grpc::SslCredentialsOptions ssl_opts;
64-
if (options.ssl_credentials_cacert_path.empty())
65-
{
66-
ssl_opts.pem_root_certs = options.ssl_credentials_cacert_as_string;
67-
}
68-
else
69-
{
70-
ssl_opts.pem_root_certs = GetFileContents((options.ssl_credentials_cacert_path).c_str());
71-
}
71+
if (options.use_ssl_credentials) {
72+
grpc::SslCredentialsOptions ssl_opts = {
73+
.pem_root_certs = GetFileContentsOrInMemoryContents(
74+
options.ssl_credentials_cacert_path,
75+
options.ssl_credentials_cacert_as_string),
76+
#if ENABLE_OTLP_GRPC_MTLS_PREVIEW
77+
.pem_private_key = GetFileContentsOrInMemoryContents(
78+
options.ssl_client_key_path,
79+
options.ssl_client_key_string),
80+
.pem_cert_chain = GetFileContentsOrInMemoryContents(
81+
options.ssl_client_cert_path,
82+
options.ssl_client_cert_string)
83+
#endif
84+
};
7285
channel =
7386
grpc::CreateCustomChannel(grpc_target, grpc::SslCredentials(ssl_opts), grpc_arguments);
7487
}

0 commit comments

Comments
 (0)