1
1
using k8s . Exceptions ;
2
+ #if ! NET5_0_OR_GREATER
2
3
using Org . BouncyCastle . Crypto ;
3
4
using Org . BouncyCastle . OpenSsl ;
4
5
using Org . BouncyCastle . Pkcs ;
5
6
using Org . BouncyCastle . Security ;
6
7
using Org . BouncyCastle . X509 ;
8
+ #endif
7
9
using System ;
8
10
using System . IO ;
9
11
using System . Security . Cryptography . X509Certificates ;
12
+ using System . Text ;
10
13
11
14
namespace k8s
12
15
{
@@ -22,6 +25,9 @@ public static X509Certificate2Collection LoadPemFileCert(string file)
22
25
var certCollection = new X509Certificate2Collection ( ) ;
23
26
using ( var stream = FileUtils . FileSystem ( ) . File . OpenRead ( file ) )
24
27
{
28
+ #if NET5_0_OR_GREATER
29
+ certCollection . ImportFromPem ( new StreamReader ( stream ) . ReadToEnd ( ) ) ;
30
+ #else
25
31
var certs = new X509CertificateParser ( ) . ReadCertificates ( stream ) ;
26
32
27
33
// Convert BouncyCastle X509Certificates to the .NET cryptography implementation and add
@@ -31,6 +37,7 @@ public static X509Certificate2Collection LoadPemFileCert(string file)
31
37
{
32
38
certCollection . Add ( new X509Certificate2 ( cert . GetEncoded ( ) ) ) ;
33
39
}
40
+ #endif
34
41
}
35
42
36
43
return certCollection ;
@@ -48,6 +55,44 @@ public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
48
55
throw new ArgumentNullException ( nameof ( config ) ) ;
49
56
}
50
57
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
+
51
96
byte [ ] keyData = null ;
52
97
byte [ ] certData = null ;
53
98
@@ -121,6 +166,7 @@ public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
121
166
return new X509Certificate2 ( pkcs . ToArray ( ) ) ;
122
167
}
123
168
}
169
+ #endif
124
170
}
125
171
126
172
/// <summary>
0 commit comments