Skip to content

Commit 3a30033

Browse files
BrandonSharpk8s-ci-robot
authored andcommitted
Prevent NullReferenceException when using HttpClient to initialize Kubernetes client. Fixes #335 (#336)
* Preventing null ref exception when initializing with HttpClient and adding support for generating a default client handler from a config object * Updating example to make use of config-based client handler generation
1 parent 3c3c6ca commit 3a30033

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

examples/httpClientFactory/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public static async Task Main(string[] args)
3131
return new Kubernetes(
3232
serviceProvider.GetRequiredService<KubernetesClientConfiguration>(),
3333
httpClient);
34-
});
34+
})
35+
.ConfigurePrimaryHttpMessageHandler(config.CreateDefaultHttpClientHandler);
3536

3637
// Add the class that uses the client
3738
services.AddHostedService<PodListHostedService>();

src/KubernetesClient/Kubernetes.ConfigInit.cs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Kubernetes(KubernetesClientConfiguration config, HttpClient httpClient, b
4343
ValidateConfig(config);
4444
CaCerts = config.SslCaCerts;
4545
SkipTlsVerify = config.SkipTlsVerify;
46-
InitializeFromConfig(config);
46+
SetCredentials(config);
4747
}
4848

4949
/// <summary>
@@ -151,8 +151,9 @@ private void InitializeFromConfig(KubernetesClientConfiguration config)
151151
}
152152
}
153153

154-
// set credentails for the kubernernet client
155-
SetCredentials(config, HttpClientHandler);
154+
// set credentails for the kubernetes client
155+
SetCredentials(config);
156+
config.AddCertificates(HttpClientHandler);
156157
}
157158

158159
private X509Certificate2Collection CaCerts { get; }
@@ -195,9 +196,7 @@ partial void CustomInitialize()
195196
/// Set credentials for the Client
196197
/// </summary>
197198
/// <param name="config">k8s client configuration</param>
198-
/// <param name="handler">http client handler for the rest client</param>
199-
/// <returns>Task</returns>
200-
private void SetCredentials(KubernetesClientConfiguration config, HttpClientHandler handler)
199+
private void SetCredentials(KubernetesClientConfiguration config)
201200
{
202201
// set the Credentails for token based auth
203202
if (!string.IsNullOrWhiteSpace(config.AccessToken))
@@ -212,25 +211,6 @@ private void SetCredentials(KubernetesClientConfiguration config, HttpClientHand
212211
Password = config.Password
213212
};
214213
}
215-
216-
#if XAMARINIOS1_0 || MONOANDROID8_1
217-
// handle.ClientCertificates is not implemented in Xamarin.
218-
return;
219-
#endif
220-
221-
if ((!string.IsNullOrWhiteSpace(config.ClientCertificateData) ||
222-
!string.IsNullOrWhiteSpace(config.ClientCertificateFilePath)) &&
223-
(!string.IsNullOrWhiteSpace(config.ClientCertificateKeyData) ||
224-
!string.IsNullOrWhiteSpace(config.ClientKeyFilePath)))
225-
{
226-
var cert = CertUtils.GeneratePfx(config);
227-
228-
#if NET452
229-
((WebRequestHandler) handler).ClientCertificates.Add(cert);
230-
#else
231-
handler.ClientCertificates.Add(cert);
232-
#endif
233-
}
234214
}
235215

236216
/// <summary>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Net.Http;
3+
4+
namespace k8s
5+
{
6+
public partial class KubernetesClientConfiguration {
7+
public HttpClientHandler CreateDefaultHttpClientHandler() {
8+
var httpClientHandler = new HttpClientHandler();
9+
10+
#if !NET452
11+
var uriScheme = new Uri(this.Host).Scheme;
12+
13+
if(uriScheme == "https")
14+
{
15+
if(this.SkipTlsVerify)
16+
{
17+
httpClientHandler.ServerCertificateCustomValidationCallback =
18+
(sender, certificate, chain, sslPolicyErrors) => true;
19+
}
20+
else
21+
{
22+
httpClientHandler.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
23+
{
24+
return Kubernetes.CertificateValidationCallBack(sender, this.SslCaCerts, certificate, chain, sslPolicyErrors);
25+
};
26+
}
27+
}
28+
#endif
29+
30+
AddCertificates(httpClientHandler);
31+
32+
return httpClientHandler;
33+
}
34+
35+
public void AddCertificates(HttpClientHandler handler) {
36+
#if XAMARINIOS1_0 || MONOANDROID8_1
37+
// handle.ClientCertificates is not implemented in Xamarin.
38+
return;
39+
#endif
40+
41+
if ((!string.IsNullOrWhiteSpace(this.ClientCertificateData) ||
42+
!string.IsNullOrWhiteSpace(this.ClientCertificateFilePath)) &&
43+
(!string.IsNullOrWhiteSpace(this.ClientCertificateKeyData) ||
44+
!string.IsNullOrWhiteSpace(this.ClientKeyFilePath)))
45+
{
46+
var cert = CertUtils.GeneratePfx(this);
47+
48+
#if NET452
49+
((WebRequestHandler) handler).ClientCertificates.Add(cert);
50+
#else
51+
handler.ClientCertificates.Add(cert);
52+
#endif
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)