Skip to content

Commit 049ab63

Browse files
authored
[EXPORTER] OTLP GRPC mTLS support (open-telemetry#2120)
1 parent 313b04d commit 049ab63

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
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_SSL_MTLS_PREVIEW)
117+
target_compile_definitions(opentelemetry_api
118+
INTERFACE ENABLE_OTLP_GRPC_SSL_MTLS_PREVIEW)
119+
endif()
120+
116121
if(WITH_METRICS_EXEMPLAR_PREVIEW)
117122
target_compile_definitions(opentelemetry_api
118123
INTERFACE ENABLE_METRICS_EXEMPLAR_PREVIEW)

ci/do_ci.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ elif [[ "$1" == "cmake.exporter.otprotocol.test" ]]; then
258258
cmake -DCMAKE_BUILD_TYPE=Debug \
259259
-DWITH_OTLP_GRPC=ON \
260260
-DWITH_OTLP_HTTP=ON \
261+
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=ON \
261262
"${SRC_DIR}"
262263
grpc_cpp_plugin=`which grpc_cpp_plugin`
263264
proto_make_file="CMakeFiles/opentelemetry_proto.dir/build.make"

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ struct OtlpGrpcExporterOptions
2828
// ssl_credentials_cacert_as_string in-memory string representation of .pem file to be used for
2929
// SSL encryption.
3030
std::string ssl_credentials_cacert_as_string = GetOtlpDefaultSslCertificateString();
31+
32+
#ifdef ENABLE_OTLP_GRPC_SSL_MTLS_PREVIEW
33+
// At most one of ssl_client_key_* should be non-empty. If use_ssl_credentials, they will
34+
// be read to allow for mTLS.
35+
std::string ssl_client_key_path = GetOtlpDefaultTracesSslClientKeyPath();
36+
std::string ssl_client_key_string = GetOtlpDefaultTracesSslClientKeyString();
37+
38+
// At most one of ssl_client_cert_* should be non-empty. If use_ssl_credentials, they will
39+
// be read to allow for mTLS.
40+
std::string ssl_client_cert_path = GetOtlpDefaultTracesSslClientCertificatePath();
41+
std::string ssl_client_cert_string = GetOtlpDefaultTracesSslClientCertificateString();
42+
#endif
43+
3144
// Timeout for grpc deadline
3245
std::chrono::system_clock::duration timeout = GetOtlpDefaultTimeout();
3346
// Additional HTTP headers

exporters/otlp/src/otlp_grpc_client.cc

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ 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(const std::string &file_path,
40+
const std::string &contents)
41+
{
42+
if (!file_path.empty())
43+
{
44+
return GetFileContents(file_path.c_str());
45+
}
46+
return contents;
47+
}
48+
3749
} // namespace
3850

3951
std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcExporterOptions &options)
@@ -61,14 +73,14 @@ std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcExporte
6173
if (options.use_ssl_credentials)
6274
{
6375
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-
}
76+
ssl_opts.pem_root_certs = GetFileContentsOrInMemoryContents(
77+
options.ssl_credentials_cacert_path, options.ssl_credentials_cacert_as_string);
78+
#if ENABLE_OTLP_GRPC_SSL_MTLS_PREVIEW
79+
ssl_opts.pem_private_key = GetFileContentsOrInMemoryContents(options.ssl_client_key_path,
80+
options.ssl_client_key_string);
81+
ssl_opts.pem_cert_chain = GetFileContentsOrInMemoryContents(options.ssl_client_cert_path,
82+
options.ssl_client_cert_string);
83+
#endif
7284
channel =
7385
grpc::CreateCustomChannel(grpc_target, grpc::SslCredentials(ssl_opts), grpc_arguments);
7486
}

0 commit comments

Comments
 (0)