Skip to content

Commit 1c6612b

Browse files
committed
Enhance certificate handling for .NET 9.0 compatibility in CertUtils and KubernetesClientConfiguration
1 parent f572a5a commit 1c6612b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/KubernetesClient/CertUtils.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,20 @@ public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
8080

8181
if (config.ClientCertificateKeyStoreFlags.HasValue)
8282
{
83+
#if NET9_0_OR_GREATER
84+
X509CertificateLoader.LoadPkcs12(cert.Export(X509ContentType.Pkcs12), nullPassword, config.ClientCertificateKeyStoreFlags.Value);
85+
#else
8386
cert = new X509Certificate2(cert.Export(X509ContentType.Pkcs12), nullPassword, config.ClientCertificateKeyStoreFlags.Value);
87+
#endif
88+
8489
}
8590
else
8691
{
92+
#if NET9_0_OR_GREATER
93+
X509CertificateLoader.LoadPkcs12(cert.Export(X509ContentType.Pkcs12), nullPassword);
94+
#else
8795
cert = new X509Certificate2(cert.Export(X509ContentType.Pkcs12), nullPassword);
96+
#endif
8897
}
8998
}
9099

src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,27 @@ private void SetClusterDetails(K8SConfiguration k8SConfig, Context activeContext
305305

306306
if (uri.Scheme == "https")
307307
{
308+
string nullPassword = null;
308309
if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthorityData))
309310
{
310311
// This null password is to change the constructor to fix this KB:
311312
// https://support.microsoft.com/en-us/topic/kb5025823-change-in-how-net-applications-import-x-509-certificates-bf81c936-af2b-446e-9f7a-016f4713b46b
312-
string nullPassword = null;
313313
var data = clusterDetails.ClusterEndpoint.CertificateAuthorityData;
314+
#if NET9_0_OR_GREATER
315+
SslCaCerts = X509CertificateLoader.LoadPkcs12Collection(Convert.FromBase64String(data), nullPassword);
316+
#else
314317
SslCaCerts = new X509Certificate2Collection(new X509Certificate2(Convert.FromBase64String(data), nullPassword));
318+
#endif
315319
}
316320
else if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthority))
317321
{
322+
#if NET9_0_OR_GREATER
323+
SslCaCerts = X509CertificateLoader.LoadPkcs12CollectionFromFile(GetFullPath(k8SConfig, clusterDetails.ClusterEndpoint.CertificateAuthority), nullPassword);
324+
#else
318325
SslCaCerts = new X509Certificate2Collection(new X509Certificate2(GetFullPath(
319326
k8SConfig,
320327
clusterDetails.ClusterEndpoint.CertificateAuthority)));
328+
#endif
321329
}
322330
}
323331
}

0 commit comments

Comments
 (0)