Skip to content

Commit e150837

Browse files
authored
Dispose certificates in Kubernetes.Dispose() (#1191)
* Dispose certs created by Kuberentes * Update tests
1 parent f0b93e0 commit e150837

File tree

7 files changed

+39
-34
lines changed

7 files changed

+39
-34
lines changed

src/KubernetesClient/Kubernetes.ConfigInit.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,27 @@ private void InitializeFromConfig(KubernetesClientConfiguration config)
9090
// set credentails for the kubernetes client
9191
SetCredentials(config);
9292

93-
var clientCert = CertUtils.GetClientCert(config);
94-
if (clientCert != null)
93+
ClientCert = CertUtils.GetClientCert(config);
94+
if (ClientCert != null)
9595
{
9696
#if NET5_0_OR_GREATER
97-
HttpClientHandler.SslOptions.ClientCertificates.Add(clientCert);
97+
HttpClientHandler.SslOptions.ClientCertificates.Add(ClientCert);
9898

9999
// TODO this is workaround for net7.0, remove it when the issue is fixed
100100
// seems the client certificate is cached and cannot be updated
101101
HttpClientHandler.SslOptions.LocalCertificateSelectionCallback = (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) =>
102102
{
103-
return clientCert;
103+
return ClientCert;
104104
};
105105
#else
106-
HttpClientHandler.ClientCertificates.Add(clientCert);
106+
HttpClientHandler.ClientCertificates.Add(ClientCert);
107107
#endif
108108
}
109109
}
110110

111111
private X509Certificate2Collection CaCerts { get; }
112112

113-
private X509Certificate2 ClientCert { get; }
113+
private X509Certificate2 ClientCert { get; set; }
114114

115115
private bool SkipTlsVerify { get; }
116116

src/KubernetesClient/Kubernetes.WebSocket.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,6 @@ protected async Task<WebSocket> StreamConnectAsync(Uri uri, string webSocketSubP
239239
}
240240

241241
// Set Credentials
242-
if (this.ClientCert != null)
243-
{
244-
webSocketBuilder.AddClientCertificate(this.ClientCert);
245-
}
246-
247242
if (this.HttpClientHandler != null)
248243
{
249244
#if NET5_0_OR_GREATER

src/KubernetesClient/Kubernetes.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,27 @@ public void Dispose()
202202
/// <param name="disposing">True to release both managed and unmanaged resources; false to releases only unmanaged resources.</param>
203203
protected virtual void Dispose(bool disposing)
204204
{
205-
if (!_disposed)
205+
if (disposing && !_disposed)
206206
{
207207
_disposed = true;
208208

209209
// Dispose the client
210210
HttpClient?.Dispose();
211+
212+
// Dispose the certificates
213+
if (CaCerts is not null)
214+
{
215+
foreach (var caCert in CaCerts)
216+
{
217+
caCert.Dispose();
218+
}
219+
220+
CaCerts.Clear();
221+
}
222+
223+
224+
ClientCert?.Dispose();
225+
211226
HttpClient = null;
212227
FirstMessageHandler = null;
213228
HttpClientHandler = null;

tests/E2E.Tests/MinikubeTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void SimpleTest()
2828
var namespaceParameter = "default";
2929
var podName = "k8scsharp-e2e-pod";
3030

31-
var client = CreateClient();
31+
using var client = CreateClient();
3232

3333
void Cleanup()
3434
{
@@ -79,7 +79,7 @@ public void PatchTest()
7979
var namespaceParameter = "default";
8080
var podName = "k8scsharp-e2e-patch-pod";
8181

82-
var client = CreateClient();
82+
using var client = CreateClient();
8383

8484
void Cleanup()
8585
{
@@ -183,7 +183,7 @@ void Cleanup()
183183
[MinikubeFact]
184184
public async Task WatcherIntegrationTest()
185185
{
186-
var kubernetes = CreateClient();
186+
using var kubernetes = CreateClient();
187187

188188
var job = await kubernetes.BatchV1.CreateNamespacedJobAsync(
189189
new V1Job()
@@ -251,7 +251,7 @@ public async Task WatcherIntegrationTest()
251251
[MinikubeFact]
252252
public void LeaderIntegrationTest()
253253
{
254-
var client = CreateClient();
254+
using var client = CreateClient();
255255
var namespaceParameter = "default";
256256

257257
void Cleanup()
@@ -350,7 +350,7 @@ public async Task LogStreamTestAsync()
350350
var namespaceParameter = "default";
351351
var podName = "k8scsharp-e2e-logstream-pod";
352352

353-
var client = CreateClient();
353+
using var client = CreateClient();
354354

355355
void Cleanup()
356356
{
@@ -446,7 +446,7 @@ async Task<V1Pod> Pod()
446446
[MinikubeFact]
447447
public async Task DatetimeFieldTest()
448448
{
449-
var kubernetes = CreateClient();
449+
using var kubernetes = CreateClient();
450450

451451
await kubernetes.CoreV1.CreateNamespacedEventAsync(
452452
new Corev1Event(
@@ -478,7 +478,7 @@ public async void GenericTest()
478478
var namespaceParameter = "default";
479479
var podName = "k8scsharp-e2e-generic-pod";
480480

481-
var client = CreateClient();
481+
using var client = CreateClient();
482482
var genericPods = new GenericClient(client, "", "v1", "pods");
483483

484484
void Cleanup()
@@ -590,7 +590,7 @@ public async Task CopyToPodTestAsync()
590590
var namespaceParameter = "default";
591591
var podName = "k8scsharp-e2e-cp-pod";
592592

593-
var client = CreateClient();
593+
using var client = CreateClient();
594594

595595
async Task<int> CopyFileToPodAsync(string name, string @namespace, string container, Stream inputFileStream, string destinationFilePath, CancellationToken cancellationToken = default(CancellationToken))
596596
{

tests/Kubectl.Tests/KubectlTests.Version.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using k8s.E2E;
2+
using k8s.kubectl.beta;
23
using System.Text.Json;
34
using Xunit;
45

@@ -9,7 +10,8 @@ public partial class KubectlTests
910
[MinikubeFact]
1011
public void Version()
1112
{
12-
var client = CreateClient();
13+
using var kubernetes = MinikubeTests.CreateClient();
14+
var client = new Kubectl(kubernetes);
1315
var version = client.Version();
1416
var serverobj = version.ServerVersion;
1517

tests/Kubectl.Tests/KubectlTests.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
using k8s.E2E;
2-
using k8s.kubectl.beta;
31
using System.Diagnostics;
42

53
namespace k8s.kubectl.Tests;
64

75
public partial class KubectlTests
86
{
9-
private Kubectl CreateClient()
10-
{
11-
return new Kubectl(MinikubeTests.CreateClient());
12-
}
13-
147
private string RunKubectl(string args)
158
{
169
var p = new Process

tests/KubernetesClient.Tests/CertUtilsTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void LoadFromFiles()
2929
useRelativePaths: false);
3030

3131
// Just validate that this doesn't throw and private key is non-null
32-
var cert = CertUtils.GeneratePfx(cfg);
32+
using var cert = CertUtils.GeneratePfx(cfg);
3333
Assert.NotNull(cert.GetRSAPrivateKey());
3434
}
3535

@@ -44,7 +44,7 @@ public void LoadFromFilesRelativePath()
4444
"federal-context");
4545

4646
// Just validate that this doesn't throw and private key is non-null
47-
var cert = CertUtils.GeneratePfx(cfg);
47+
using var cert = CertUtils.GeneratePfx(cfg);
4848
Assert.NotNull(cert.GetRSAPrivateKey());
4949
}
5050

@@ -58,7 +58,7 @@ public void LoadFromInlineData()
5858
useRelativePaths: false);
5959

6060
// Just validate that this doesn't throw and private key is non-null
61-
var cert = CertUtils.GeneratePfx(cfg);
61+
using var cert = CertUtils.GeneratePfx(cfg);
6262
Assert.NotNull(cert.GetRSAPrivateKey());
6363
}
6464

@@ -73,7 +73,7 @@ public void LoadFromInlineDataRelativePath()
7373
"victorian-context");
7474

7575
// Just validate that this doesn't throw and private key is non-null
76-
var cert = CertUtils.GeneratePfx(cfg);
76+
using var cert = CertUtils.GeneratePfx(cfg);
7777
Assert.NotNull(cert.GetRSAPrivateKey());
7878
}
7979

@@ -85,8 +85,8 @@ public void LoadPemWithMultiCert()
8585
{
8686
var certCollection = CertUtils.LoadPemFileCert("assets/ca-bundle.crt");
8787

88-
var intermediateCert = new X509Certificate2("assets/ca-bundle-intermediate.crt");
89-
var rootCert = new X509Certificate2("assets/ca-bundle-root.crt");
88+
using var intermediateCert = new X509Certificate2("assets/ca-bundle-intermediate.crt");
89+
using var rootCert = new X509Certificate2("assets/ca-bundle-root.crt");
9090

9191
Assert.Equal(2, certCollection.Count);
9292

0 commit comments

Comments
 (0)