@@ -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
3949std::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