Skip to content

Commit 5e8d11f

Browse files
committed
Allows the DaprClient to work with mTLS enabled grpc endpoint when https endpoint is provided (#263)
(cherry picked from commit e0a3d93)
1 parent b4079df commit 5e8d11f

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Dapr.Client/DaprClientBuilder.cs

+25-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
namespace Dapr.Client
77
{
88
using System;
9+
using System.Net.Http;
910
using System.Text.Json;
11+
using System.Threading.Tasks;
12+
using Grpc.Core;
1013
using Grpc.Net.Client;
1114

1215
/// <summary>
@@ -65,8 +68,29 @@ public DaprClient Build()
6568
// Set correct switch to make insecure gRPC service calls. This switch must be set before creating the GrpcChannel.
6669
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
6770
}
71+
else
72+
{
73+
// Workaround to allow with mTLS enabled Dapr grpc endpoint. The behavior will be fixed in 0.6.0 Dapr runtime.
74+
if (this.gRPCChannelOptions == null)
75+
{
76+
var httpClientHandler = new HttpClientHandler();
77+
78+
// validate server cert.
79+
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) =>
80+
{
81+
return true;
82+
};
6883

69-
var channel = GrpcChannel.ForAddress(this.daprEndpoint, this.gRPCChannelOptions ?? new GrpcChannelOptions());
84+
var httpClient = new HttpClient(httpClientHandler);
85+
this.gRPCChannelOptions = new GrpcChannelOptions
86+
{
87+
HttpClient = httpClient,
88+
DisposeHttpClient = true
89+
};
90+
}
91+
}
92+
93+
var channel = GrpcChannel.ForAddress(this.daprEndpoint, this.gRPCChannelOptions ?? new GrpcChannelOptions());
7094
return new DaprClientGrpc(channel, this.jsonSerializerOptions);
7195
}
7296

0 commit comments

Comments
 (0)