11using k8s . Exceptions ;
2+ #if ! NET5_0_OR_GREATER
23using Org . BouncyCastle . Crypto ;
34using Org . BouncyCastle . OpenSsl ;
45using Org . BouncyCastle . Pkcs ;
56using Org . BouncyCastle . Security ;
67using Org . BouncyCastle . X509 ;
8+ #endif
79using System ;
810using System . IO ;
911using System . Security . Cryptography . X509Certificates ;
12+ using System . Text ;
1013
1114namespace k8s
1215{
@@ -22,6 +25,9 @@ public static X509Certificate2Collection LoadPemFileCert(string file)
2225 var certCollection = new X509Certificate2Collection ( ) ;
2326 using ( var stream = FileUtils . FileSystem ( ) . File . OpenRead ( file ) )
2427 {
28+ #if NET5_0_OR_GREATER
29+ certCollection . ImportFromPem ( new StreamReader ( stream ) . ReadToEnd ( ) ) ;
30+ #else
2531 var certs = new X509CertificateParser ( ) . ReadCertificates ( stream ) ;
2632
2733 // Convert BouncyCastle X509Certificates to the .NET cryptography implementation and add
@@ -31,6 +37,7 @@ public static X509Certificate2Collection LoadPemFileCert(string file)
3137 {
3238 certCollection . Add ( new X509Certificate2 ( cert . GetEncoded ( ) ) ) ;
3339 }
40+ #endif
3441 }
3542
3643 return certCollection ;
@@ -48,6 +55,44 @@ public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
4855 throw new ArgumentNullException ( nameof ( config ) ) ;
4956 }
5057
58+ #if NET5_0_OR_GREATER
59+ string keyData = null ;
60+ string certData = null ;
61+
62+ if ( ! string . IsNullOrWhiteSpace ( config . ClientCertificateKeyData ) )
63+ {
64+ keyData = Encoding . UTF8 . GetString ( Convert . FromBase64String ( config . ClientCertificateKeyData ) ) ;
65+ }
66+
67+ if ( ! string . IsNullOrWhiteSpace ( config . ClientKeyFilePath ) )
68+ {
69+ keyData = File . ReadAllText ( config . ClientKeyFilePath ) ;
70+ }
71+
72+ if ( keyData == null )
73+ {
74+ throw new KubeConfigException ( "keyData is empty" ) ;
75+ }
76+
77+ if ( ! string . IsNullOrWhiteSpace ( config . ClientCertificateData ) )
78+ {
79+ certData = Encoding . UTF8 . GetString ( Convert . FromBase64String ( config . ClientCertificateData ) ) ;
80+ }
81+
82+ if ( ! string . IsNullOrWhiteSpace ( config . ClientCertificateFilePath ) )
83+ {
84+ certData = File . ReadAllText ( config . ClientCertificateFilePath ) ;
85+ }
86+
87+ if ( certData == null )
88+ {
89+ throw new KubeConfigException ( "certData is empty" ) ;
90+ }
91+
92+
93+ return X509Certificate2 . CreateFromPem ( certData , keyData ) ;
94+ #else
95+
5196 byte [ ] keyData = null ;
5297 byte [ ] certData = null ;
5398
@@ -121,6 +166,7 @@ public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
121166 return new X509Certificate2 ( pkcs . ToArray ( ) ) ;
122167 }
123168 }
169+ #endif
124170 }
125171
126172 /// <summary>
0 commit comments