namespace k8s
{
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using k8s.Exceptions;
using k8s.KubeConfigModels;
using YamlDotNet.Serialization;
using System.Runtime.InteropServices;
///
/// Represents a set of kubernetes client configuration settings
///
public class KubernetesClientConfiguration
{
///
/// Initializes a new instance of the class.
///
/// kubeconfig file info
/// Context to use from kube config
public KubernetesClientConfiguration(FileInfo kubeconfig = null, string currentContext = null)
{
var k8SConfig = this.LoadKubeConfig(kubeconfig ?? new FileInfo(KubeConfigDefaultLocation));
this.Initialize(k8SConfig, currentContext);
}
///
/// kubeconfig Default Location
///
private static readonly string KubeConfigDefaultLocation = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), @".kube\config") :
Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".kube/config");
///
/// Gets CurrentContext
///
public string CurrentContext { get; private set; }
///
/// Gets Host
///
public string Host { get; private set; }
///
/// Gets SslCaCert
///
public X509Certificate2 SslCaCert { get; private set; }
///
/// Gets ClientCertificateData
///
public string ClientCertificateData { get; private set; }
///
/// Gets ClientCertificate Key
///
public string ClientCertificateKey { get; private set; }
///
/// Gets ClientCertificate filename
///
public string ClientCertificate { get; private set; }
///
/// Gets ClientCertificate Key filename
///
public string ClientKey { get; private set; }
///
/// Gets a value indicating whether to skip ssl server cert validation
///
public bool SkipTlsVerify { get; private set; }
///
/// Gets or sets the HTTP user agent.
///
/// Http user agent.
public string UserAgent { get; set; }
///
/// Gets or sets the username (HTTP basic authentication).
///
/// The username.
public string Username { get; set; }
///
/// Gets or sets the password (HTTP basic authentication).
///
/// The password.
public string Password { get; set; }
///
/// Gets or sets the access token for OAuth2 authentication.
///
/// The access token.
public string AccessToken { get; set; }
///
/// Validates and Intializes Client Configuration
///
/// Kubernetes Configuration
/// Current Context
private void Initialize(K8SConfiguration k8SConfig, string currentContext = null)
{
if (k8SConfig.Contexts == null)
{
throw new KubeConfigException("No contexts found in kubeconfig");
}
if (k8SConfig.Clusters == null)
{
throw new KubeConfigException($"No clusters found in kubeconfig");
}
if (k8SConfig.Users == null)
{
throw new KubeConfigException($"No users found in kubeconfig");
}
// current context
currentContext = currentContext ?? k8SConfig.CurrentContext;
Context activeContext = k8SConfig.Contexts.FirstOrDefault(c => c.Name.Equals(currentContext, StringComparison.OrdinalIgnoreCase));
if (activeContext == null)
{
throw new KubeConfigException($"CurrentContext: {currentContext} not found in contexts in kubeconfig");
}
this.CurrentContext = activeContext.Name;
// cluster
var clusterDetails = k8SConfig.Clusters.FirstOrDefault(c => c.Name.Equals(activeContext.ContextDetails.Cluster, StringComparison.OrdinalIgnoreCase));
if (clusterDetails?.ClusterEndpoint == null)
{
throw new KubeConfigException($"Cluster not found for context {activeContext} in kubeconfig");
}
if (string.IsNullOrWhiteSpace(clusterDetails.ClusterEndpoint.Server))
{
throw new KubeConfigException($"Server not found for current-context {activeContext} in kubeconfig");
}
if (!clusterDetails.ClusterEndpoint.SkipTlsVerify &&
string.IsNullOrWhiteSpace(clusterDetails.ClusterEndpoint.CertificateAuthorityData) &&
string.IsNullOrWhiteSpace(clusterDetails.ClusterEndpoint.CertificateAuthority))
{
throw new KubeConfigException($"neither certificate-authority-data nor certificate-authority not found for current-context :{activeContext} in kubeconfig");
}
this.Host = clusterDetails.ClusterEndpoint.Server;
if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthorityData))
{
string data = clusterDetails.ClusterEndpoint.CertificateAuthorityData;
this.SslCaCert = new X509Certificate2(System.Text.Encoding.UTF8.GetBytes(Utils.Base64Decode(data)));
}
else if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthority))
{
this.SslCaCert = new X509Certificate2(clusterDetails.ClusterEndpoint.CertificateAuthority);
}
this.SkipTlsVerify = clusterDetails.ClusterEndpoint.SkipTlsVerify;
// user
this.SetUserDetails(k8SConfig, activeContext);
}
private void SetUserDetails(K8SConfiguration k8SConfig, Context activeContext)
{
var userDetails = k8SConfig.Users.FirstOrDefault(c => c.Name.Equals(activeContext.ContextDetails.User, StringComparison.OrdinalIgnoreCase));
if (userDetails == null)
{
throw new KubeConfigException("User not found for context {activeContext.Name} in kubeconfig");
}
if (userDetails.UserCredentials == null)
{
throw new KubeConfigException($"User credentials not found for user: {userDetails.Name} in kubeconfig");
}
var userCredentialsFound = false;
// Basic and bearer tokens are mutually exclusive
if (!string.IsNullOrWhiteSpace(userDetails.UserCredentials.Token))
{
this.AccessToken = userDetails.UserCredentials.Token;
userCredentialsFound = true;
}
else if (!string.IsNullOrWhiteSpace(userDetails.UserCredentials.UserName) &&
!string.IsNullOrWhiteSpace(userDetails.UserCredentials.Password))
{
this.Username = userDetails.UserCredentials.UserName;
this.Password = userDetails.UserCredentials.Password;
userCredentialsFound = true;
}
// Token and cert based auth can co-exist
if (!string.IsNullOrWhiteSpace(userDetails.UserCredentials.ClientCertificateData) &&
!string.IsNullOrWhiteSpace(userDetails.UserCredentials.ClientKeyData))
{
this.ClientCertificateData = userDetails.UserCredentials.ClientCertificateData;
this.ClientCertificateKey = userDetails.UserCredentials.ClientKeyData;
userCredentialsFound = true;
}
if (!string.IsNullOrWhiteSpace(userDetails.UserCredentials.ClientCertificate) &&
!string.IsNullOrWhiteSpace(userDetails.UserCredentials.ClientKey)) {
this.ClientCertificate = userDetails.UserCredentials.ClientCertificate;
this.ClientKey = userDetails.UserCredentials.ClientKey;
userCredentialsFound = true;
}
if (!userCredentialsFound)
{
throw new KubeConfigException($"User: {userDetails.Name} does not have appropriate auth credentials in kubeconfig");
}
}
///
/// Loads Kube Config
///
/// Kube config file contents
/// Instance of the class
private K8SConfiguration LoadKubeConfig(FileInfo kubeconfig)
{
if (!kubeconfig.Exists)
{
throw new KubeConfigException($"kubeconfig file not found at {kubeconfig.FullName}");
}
var kubeconfigContent = File.ReadAllText(kubeconfig.FullName);
var deserializeBuilder = new DeserializerBuilder();
var deserializer = deserializeBuilder.Build();
return deserializer.Deserialize(kubeconfigContent);
}
}
}