forked from kubernetes-client/csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KubernetesClientConfiguration.HttpClientHandler.cs
63 lines (54 loc) · 1.94 KB
/
KubernetesClientConfiguration.HttpClientHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Net.Http;
namespace k8s
{
public partial class KubernetesClientConfiguration
{
public HttpClientHandler CreateDefaultHttpClientHandler()
{
var httpClientHandler = new HttpClientHandler();
#if !NET452
var uriScheme = new Uri(Host).Scheme;
if (uriScheme == "https")
{
if (SkipTlsVerify)
{
httpClientHandler.ServerCertificateCustomValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => true;
}
else
{
httpClientHandler.ServerCertificateCustomValidationCallback =
(sender, certificate, chain, sslPolicyErrors) =>
{
return Kubernetes.CertificateValidationCallBack(sender, SslCaCerts, certificate, chain,
sslPolicyErrors);
};
}
}
#endif
AddCertificates(httpClientHandler);
return httpClientHandler;
}
public void AddCertificates(HttpClientHandler handler)
{
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
if ((!string.IsNullOrWhiteSpace(ClientCertificateData) ||
!string.IsNullOrWhiteSpace(ClientCertificateFilePath)) &&
(!string.IsNullOrWhiteSpace(ClientCertificateKeyData) ||
!string.IsNullOrWhiteSpace(ClientKeyFilePath)))
{
var cert = CertUtils.GeneratePfx(this);
#if NET452
((WebRequestHandler)handler).ClientCertificates.Add(cert);
#else
handler.ClientCertificates.Add(cert);
#endif
}
}
public static DelegatingHandler CreateWatchHandler() => new WatcherDelegatingHandler();
}
}