Skip to content

Commit 19573f7

Browse files
committed
Refactor certificate loading to improve compatibility with .NET 9.0
1 parent 6ef98a6 commit 19573f7

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,22 +305,24 @@ private void SetClusterDetails(K8SConfiguration k8SConfig, Context activeContext
305305

306306
if (uri.Scheme == "https")
307307
{
308-
string nullPassword = null;
309308
if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthorityData))
310309
{
311-
// This null password is to change the constructor to fix this KB:
312-
// https://support.microsoft.com/en-us/topic/kb5025823-change-in-how-net-applications-import-x-509-certificates-bf81c936-af2b-446e-9f7a-016f4713b46b
313310
var data = clusterDetails.ClusterEndpoint.CertificateAuthorityData;
314311
#if NET9_0_OR_GREATER
315-
SslCaCerts = X509CertificateLoader.LoadPkcs12Collection(Convert.FromBase64String(data), nullPassword);
312+
SslCaCerts = new X509Certificate2Collection(X509CertificateLoader.LoadCertificate(Convert.FromBase64String(data)));
316313
#else
314+
string nullPassword = null;
315+
// This null password is to change the constructor to fix this KB:
316+
// https://support.microsoft.com/en-us/topic/kb5025823-change-in-how-net-applications-import-x-509-certificates-bf81c936-af2b-446e-9f7a-016f4713b46b
317317
SslCaCerts = new X509Certificate2Collection(new X509Certificate2(Convert.FromBase64String(data), nullPassword));
318318
#endif
319319
}
320320
else if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthority))
321321
{
322322
#if NET9_0_OR_GREATER
323-
SslCaCerts = X509CertificateLoader.LoadPkcs12CollectionFromFile(GetFullPath(k8SConfig, clusterDetails.ClusterEndpoint.CertificateAuthority), nullPassword);
323+
SslCaCerts = new X509Certificate2Collection(X509CertificateLoader.LoadCertificateFromFile(GetFullPath(
324+
k8SConfig,
325+
clusterDetails.ClusterEndpoint.CertificateAuthority)));
324326
#else
325327
SslCaCerts = new X509Certificate2Collection(new X509Certificate2(GetFullPath(
326328
k8SConfig,

0 commit comments

Comments
 (0)