Skip to content

Commit 5d1b60f

Browse files
Merge pull request #33 from tg123/netstandard13
change lib target to netstandard13
2 parents fea8fdd + 758eaea commit 5d1b60f

File tree

3 files changed

+50
-35
lines changed

3 files changed

+50
-35
lines changed

src/KubernetesClient.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>netcoreapp2.0</TargetFramework>
3+
<TargetFramework>netstandard1.3</TargetFramework>
4+
<RootNamespace>k8s</RootNamespace>
45
</PropertyGroup>
56
<ItemGroup>
67
<Compile Remove="GlobalSuppressions.cs" />

src/Utils.cs

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
namespace k8s
2-
{
3-
using System;
4-
using System.Diagnostics;
5-
using System.Globalization;
6-
using System.IO;
7-
using System.Runtime.InteropServices;
8-
using System.Security.Cryptography;
9-
using System.Security.Cryptography.X509Certificates;
10-
using System.Text;
11-
using System.Threading.Tasks;
12-
13-
using Org.BouncyCastle.Crypto;
14-
using Org.BouncyCastle.Crypto.Parameters;
15-
using Org.BouncyCastle.Security;
16-
using Org.BouncyCastle.OpenSsl;
1+
using System;
2+
using System.IO;
3+
using System.Security.Cryptography.X509Certificates;
4+
using System.Text;
5+
using k8s.Exceptions;
6+
using Org.BouncyCastle.Crypto;
7+
using Org.BouncyCastle.Crypto.Parameters;
8+
using Org.BouncyCastle.OpenSsl;
9+
using Org.BouncyCastle.Pkcs;
10+
using Org.BouncyCastle.Security;
11+
using Org.BouncyCastle.X509;
1712

13+
namespace k8s
14+
{
1815
public static class Utils
1916
{
2017
/// <summary>
21-
/// Encode string in base64 format.
18+
/// Encode string in base64 format.
2219
/// </summary>
2320
/// <param name="text">string to be encoded.</param>
2421
/// <returns>Encoded string.</returns>
@@ -28,7 +25,7 @@ public static string Base64Encode(string text)
2825
}
2926

3027
/// <summary>
31-
/// Encode string in base64 format.
28+
/// Encode string in base64 format.
3229
/// </summary>
3330
/// <param name="text">string to be encoded.</param>
3431
/// <returns>Encoded string.</returns>
@@ -38,16 +35,15 @@ public static string Base64Decode(string text)
3835
}
3936

4037
/// <summary>
41-
/// Generates pfx from client configuration
38+
/// Generates pfx from client configuration
4239
/// </summary>
4340
/// <param name="config">Kuberentes Client Configuration</param>
4441
/// <returns>Generated Pfx Path</returns>
4542
public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
4643
{
47-
var keyData = new byte[]{};
48-
var certData = new byte[]{};
44+
byte[] keyData = null;
45+
byte[] certData = null;
4946

50-
var filePrefix = config.CurrentContext;
5147
if (!string.IsNullOrWhiteSpace(config.ClientCertificateKey))
5248
{
5349
keyData = Convert.FromBase64String(config.ClientCertificateKey);
@@ -57,6 +53,11 @@ public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
5753
keyData = File.ReadAllBytes(config.ClientKey);
5854
}
5955

56+
if (keyData == null)
57+
{
58+
throw new KubeConfigException("certData is empty");
59+
}
60+
6061
if (!string.IsNullOrWhiteSpace(config.ClientCertificateData))
6162
{
6263
certData = Convert.FromBase64String(config.ClientCertificateData);
@@ -66,23 +67,35 @@ public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
6667
certData = File.ReadAllBytes(config.ClientCertificate);
6768
}
6869

69-
var cert = new X509Certificate2(certData);
70-
return addPrivateKey(cert, keyData);
71-
}
70+
if (certData == null)
71+
{
72+
throw new KubeConfigException("certData is empty");
73+
}
7274

73-
public static X509Certificate2 addPrivateKey(X509Certificate2 cert, byte[] keyData)
74-
{
75+
var cert = new X509CertificateParser().ReadCertificate(new MemoryStream(certData));
76+
77+
object obj;
7578
using (var reader = new StreamReader(new MemoryStream(keyData)))
7679
{
77-
var obj = new PemReader(reader).ReadObject();
78-
if (obj is AsymmetricCipherKeyPair) {
79-
var cipherKey = (AsymmetricCipherKeyPair)obj;
80+
obj = new PemReader(reader).ReadObject();
81+
var key = obj as AsymmetricCipherKeyPair;
82+
if (key != null)
83+
{
84+
var cipherKey = key;
8085
obj = cipherKey.Private;
8186
}
82-
var rsaKeyParams = (RsaPrivateCrtKeyParameters)obj;
83-
var rsaKey = RSA.Create(DotNetUtilities.ToRSAParameters(rsaKeyParams));
84-
return cert.CopyWithPrivateKey(rsaKey);
87+
}
88+
89+
var rsaKeyParams = (RsaPrivateCrtKeyParameters) obj;
90+
91+
var store = new Pkcs12StoreBuilder().Build();
92+
store.SetKeyEntry("K8SKEY", new AsymmetricKeyEntry(rsaKeyParams), new[] {new X509CertificateEntry(cert)});
93+
94+
using (var pkcs = new MemoryStream())
95+
{
96+
store.Save(pkcs, new char[0], new SecureRandom());
97+
return new X509Certificate2(pkcs.ToArray());
8598
}
8699
}
87100
}
88-
}
101+
}

tests/tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<TargetFramework>netcoreapp2.0</TargetFramework>
44
<IsPackable>false</IsPackable>
5+
<RootNamespace>k8s.tests</RootNamespace>
56
</PropertyGroup>
67
<ItemGroup>
78
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />

0 commit comments

Comments
 (0)