2017-09-14 10:47:41 -07:00
|
|
|
namespace k8s
|
2017-06-17 14:11:52 -07:00
|
|
|
{
|
2017-08-09 16:49:45 -07:00
|
|
|
using k8s.Exceptions;
|
2017-06-17 14:11:52 -07:00
|
|
|
using System;
|
2017-08-09 16:49:45 -07:00
|
|
|
using System.ComponentModel;
|
2017-06-17 14:11:52 -07:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2017-06-28 13:05:20 +02:00
|
|
|
using System.Runtime.InteropServices;
|
2017-06-17 14:11:52 -07:00
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
public static class Utils
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Encode string in base64 format.
|
|
|
|
|
/// </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>
|
|
|
|
|
/// Encode string in base64 format.
|
|
|
|
|
/// </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>
|
|
|
|
|
/// Generates pfx from client configuration
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="config">Kuberentes Client Configuration</param>
|
|
|
|
|
/// <returns>Generated Pfx Path</returns>
|
|
|
|
|
/// TODO: kabhishek8260 Remplace the method with X509 Certificate with private key(in dotnet 2.0)
|
|
|
|
|
public static async Task<string> GeneratePfxAsync(KubernetesClientConfiguration config)
|
|
|
|
|
{
|
2017-06-28 13:05:20 +02:00
|
|
|
var userHomeDir = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
|
|
|
|
|
Environment.GetEnvironmentVariable("USERPROFILE") :
|
|
|
|
|
Environment.GetEnvironmentVariable("HOME");
|
|
|
|
|
|
2017-06-17 14:11:52 -07:00
|
|
|
var certDirPath = Path.Combine(userHomeDir, ".k8scerts");
|
|
|
|
|
Directory.CreateDirectory(certDirPath);
|
|
|
|
|
|
2017-07-23 20:45:09 -07:00
|
|
|
var keyFilePath = "";
|
|
|
|
|
var certFilePath = "";
|
|
|
|
|
|
2017-06-17 14:11:52 -07:00
|
|
|
var filePrefix = config.CurrentContext;
|
|
|
|
|
var pfxFilePath = Path.Combine(certDirPath, filePrefix + "pfx");
|
2017-08-09 16:49:45 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(config.ClientCertificateKey))
|
|
|
|
|
{
|
2017-07-23 20:45:09 -07:00
|
|
|
keyFilePath = Path.Combine(certDirPath, filePrefix + "key");
|
|
|
|
|
using (FileStream fs = File.Create(keyFilePath))
|
|
|
|
|
{
|
|
|
|
|
byte[] info = Convert.FromBase64String(config.ClientCertificateKey);
|
|
|
|
|
await fs.WriteAsync(info, 0, info.Length).ConfigureAwait(false);
|
|
|
|
|
}
|
2017-06-17 14:11:52 -07:00
|
|
|
}
|
2017-08-09 16:49:45 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(config.ClientKey))
|
|
|
|
|
{
|
2017-07-23 20:45:09 -07:00
|
|
|
keyFilePath = config.ClientKey;
|
2017-06-17 14:11:52 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-09 16:49:45 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(config.ClientCertificateData))
|
|
|
|
|
{
|
2017-07-23 20:45:09 -07:00
|
|
|
certFilePath = Path.Combine(certDirPath, filePrefix + "cert");
|
2017-08-09 16:49:45 -07:00
|
|
|
|
2017-07-23 20:45:09 -07:00
|
|
|
using (FileStream fs = File.Create(certFilePath))
|
|
|
|
|
{
|
|
|
|
|
byte[] info = Convert.FromBase64String(config.ClientCertificateData);
|
|
|
|
|
await fs.WriteAsync(info, 0, info.Length).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-09 16:49:45 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(config.ClientCertificate))
|
|
|
|
|
{
|
2017-07-23 20:45:09 -07:00
|
|
|
certFilePath = config.ClientCertificate;
|
|
|
|
|
}
|
2017-08-09 16:49:45 -07:00
|
|
|
|
|
|
|
|
var processStartInfo = new ProcessStartInfo
|
2017-06-17 14:11:52 -07:00
|
|
|
{
|
2017-06-28 13:05:20 +02:00
|
|
|
FileName = @"openssl",
|
|
|
|
|
Arguments = $"pkcs12 -export -out {pfxFilePath} -inkey {keyFilePath} -in {certFilePath} -passout pass:",
|
2017-06-17 14:11:52 -07:00
|
|
|
CreateNoWindow = true,
|
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
RedirectStandardOutput = true
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-09 16:49:45 -07:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (Process process = Process.Start(processStartInfo))
|
|
|
|
|
{
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
if (process.ExitCode != 0)
|
|
|
|
|
{
|
|
|
|
|
throw new KubernetesClientException($"Failed to generate pfx file with openssl. ExitCode = {process.ExitCode}.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Win32Exception e)
|
2017-06-17 14:11:52 -07:00
|
|
|
{
|
2017-08-09 16:49:45 -07:00
|
|
|
throw new KubernetesClientException("Failed to generate pfx file with openssl.", e);
|
2017-06-17 14:11:52 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-09 16:49:45 -07:00
|
|
|
return pfxFilePath;
|
2017-06-17 14:11:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|