2017-10-13 03:06:35 +08:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using k8s.Exceptions;
|
|
|
|
|
using Org.BouncyCastle.Crypto;
|
|
|
|
|
using Org.BouncyCastle.Crypto.Parameters;
|
|
|
|
|
using Org.BouncyCastle.OpenSsl;
|
|
|
|
|
using Org.BouncyCastle.Pkcs;
|
|
|
|
|
using Org.BouncyCastle.Security;
|
|
|
|
|
using Org.BouncyCastle.X509;
|
2017-09-22 20:59:41 -07:00
|
|
|
|
2017-10-13 03:06:35 +08:00
|
|
|
namespace k8s
|
|
|
|
|
{
|
2017-09-22 20:59:41 -07:00
|
|
|
public static class Utils
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2017-10-13 03:06:35 +08:00
|
|
|
/// Encode string in base64 format.
|
2017-09-22 20:59:41 -07:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">string to be encoded.</param>
|
|
|
|
|
/// <returns>Encoded string.</returns>
|
|
|
|
|
public static string Base64Encode(string text)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-10-13 03:06:35 +08:00
|
|
|
/// Encode string in base64 format.
|
2017-09-22 20:59:41 -07:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">string to be encoded.</param>
|
|
|
|
|
/// <returns>Encoded string.</returns>
|
|
|
|
|
public static string Base64Decode(string text)
|
|
|
|
|
{
|
|
|
|
|
return Encoding.UTF8.GetString(Convert.FromBase64String(text));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-10-13 03:06:35 +08:00
|
|
|
/// Generates pfx from client configuration
|
2017-09-22 20:59:41 -07:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="config">Kuberentes Client Configuration</param>
|
|
|
|
|
/// <returns>Generated Pfx Path</returns>
|
2017-09-27 21:51:00 -07:00
|
|
|
public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
|
2017-09-22 20:59:41 -07:00
|
|
|
{
|
2017-10-13 03:06:35 +08:00
|
|
|
byte[] keyData = null;
|
|
|
|
|
byte[] certData = null;
|
2017-09-22 20:59:41 -07:00
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(config.ClientCertificateKey))
|
|
|
|
|
{
|
|
|
|
|
keyData = Convert.FromBase64String(config.ClientCertificateKey);
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(config.ClientKey))
|
|
|
|
|
{
|
|
|
|
|
keyData = File.ReadAllBytes(config.ClientKey);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 03:06:35 +08:00
|
|
|
if (keyData == null)
|
|
|
|
|
{
|
|
|
|
|
throw new KubeConfigException("certData is empty");
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 20:59:41 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(config.ClientCertificateData))
|
|
|
|
|
{
|
|
|
|
|
certData = Convert.FromBase64String(config.ClientCertificateData);
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(config.ClientCertificate))
|
|
|
|
|
{
|
|
|
|
|
certData = File.ReadAllBytes(config.ClientCertificate);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 03:06:35 +08:00
|
|
|
if (certData == null)
|
|
|
|
|
{
|
|
|
|
|
throw new KubeConfigException("certData is empty");
|
|
|
|
|
}
|
2017-09-22 20:59:41 -07:00
|
|
|
|
2017-10-13 03:06:35 +08:00
|
|
|
var cert = new X509CertificateParser().ReadCertificate(new MemoryStream(certData));
|
|
|
|
|
|
|
|
|
|
object obj;
|
2017-09-22 20:59:41 -07:00
|
|
|
using (var reader = new StreamReader(new MemoryStream(keyData)))
|
|
|
|
|
{
|
2017-10-13 03:06:35 +08:00
|
|
|
obj = new PemReader(reader).ReadObject();
|
|
|
|
|
var key = obj as AsymmetricCipherKeyPair;
|
|
|
|
|
if (key != null)
|
|
|
|
|
{
|
|
|
|
|
var cipherKey = key;
|
2017-09-27 21:51:00 -07:00
|
|
|
obj = cipherKey.Private;
|
|
|
|
|
}
|
2017-10-13 03:06:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var rsaKeyParams = (RsaPrivateCrtKeyParameters) obj;
|
|
|
|
|
|
|
|
|
|
var store = new Pkcs12StoreBuilder().Build();
|
|
|
|
|
store.SetKeyEntry("K8SKEY", new AsymmetricKeyEntry(rsaKeyParams), new[] {new X509CertificateEntry(cert)});
|
|
|
|
|
|
|
|
|
|
using (var pkcs = new MemoryStream())
|
|
|
|
|
{
|
2017-10-13 03:34:24 +08:00
|
|
|
store.Save(pkcs, new char[0], new SecureRandom());
|
2017-10-13 03:06:35 +08:00
|
|
|
return new X509Certificate2(pkcs.ToArray());
|
2017-09-22 20:59:41 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-13 03:06:35 +08:00
|
|
|
}
|