2018-09-27 10:50:39 -07:00
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Security;
|
2021-03-11 09:32:22 -08:00
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Reflection;
|
2021-03-18 09:17:19 -07:00
|
|
|
using System.Runtime.InteropServices;
|
2018-09-27 10:50:39 -07:00
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2021-10-11 12:55:02 -07:00
|
|
|
using System.Threading;
|
2018-09-27 10:50:39 -07:00
|
|
|
using k8s.Exceptions;
|
2022-02-25 13:33:23 -08:00
|
|
|
using k8s.Autorest;
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
namespace k8s
|
|
|
|
|
{
|
|
|
|
|
public partial class Kubernetes
|
|
|
|
|
{
|
2021-10-11 12:55:02 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Timeout of REST calls to Kubernetes server
|
|
|
|
|
/// Does not apply to watch related api
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>timeout</value>
|
|
|
|
|
public TimeSpan HttpClientTimeout { get; set; } = TimeSpan.FromSeconds(100);
|
|
|
|
|
|
2018-09-27 10:50:39 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="Kubernetes" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name='config'>
|
2019-09-23 21:33:25 -07:00
|
|
|
/// The kube config to use.
|
2018-09-27 10:50:39 -07:00
|
|
|
/// </param>
|
|
|
|
|
/// <param name="handlers">
|
|
|
|
|
/// Optional. The delegating handlers to add to the http client pipeline.
|
|
|
|
|
/// </param>
|
2019-09-23 21:33:25 -07:00
|
|
|
public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler[] handlers)
|
2018-09-27 10:50:39 -07:00
|
|
|
{
|
2020-04-16 10:54:47 -07:00
|
|
|
Initialize();
|
2019-09-23 21:33:25 -07:00
|
|
|
ValidateConfig(config);
|
|
|
|
|
CaCerts = config.SslCaCerts;
|
|
|
|
|
SkipTlsVerify = config.SkipTlsVerify;
|
2021-04-02 14:52:12 -07:00
|
|
|
CreateHttpClient(handlers, config);
|
2019-09-23 21:33:25 -07:00
|
|
|
InitializeFromConfig(config);
|
2021-10-11 12:55:02 -07:00
|
|
|
HttpClientTimeout = config.HttpClientTimeout;
|
2019-09-23 21:33:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ValidateConfig(KubernetesClientConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
if (config == null)
|
|
|
|
|
{
|
|
|
|
|
throw new KubeConfigException("KubeConfig must be provided");
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-27 10:50:39 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(config.Host))
|
|
|
|
|
{
|
|
|
|
|
throw new KubeConfigException("Host url must be set");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
BaseUri = new Uri(config.Host);
|
|
|
|
|
}
|
|
|
|
|
catch (UriFormatException e)
|
|
|
|
|
{
|
|
|
|
|
throw new KubeConfigException("Bad host url", e);
|
|
|
|
|
}
|
2019-09-23 21:33:25 -07:00
|
|
|
}
|
2018-09-27 10:50:39 -07:00
|
|
|
|
2019-09-23 21:33:25 -07:00
|
|
|
private void InitializeFromConfig(KubernetesClientConfiguration config)
|
|
|
|
|
{
|
2018-09-27 10:50:39 -07:00
|
|
|
if (BaseUri.Scheme == "https")
|
|
|
|
|
{
|
|
|
|
|
if (config.SkipTlsVerify)
|
|
|
|
|
{
|
2022-02-25 13:33:23 -08:00
|
|
|
#if NET5_0_OR_GREATER
|
|
|
|
|
HttpClientHandler.SslOptions.RemoteCertificateValidationCallback =
|
|
|
|
|
#else
|
2018-09-27 10:50:39 -07:00
|
|
|
HttpClientHandler.ServerCertificateCustomValidationCallback =
|
2022-02-25 13:33:23 -08:00
|
|
|
#endif
|
2018-09-27 10:50:39 -07:00
|
|
|
(sender, certificate, chain, sslPolicyErrors) => true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-03-11 06:39:28 -07:00
|
|
|
if (CaCerts == null)
|
2018-09-27 10:50:39 -07:00
|
|
|
{
|
2019-09-23 21:33:25 -07:00
|
|
|
throw new KubeConfigException("A CA must be set when SkipTlsVerify === false");
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
2019-09-23 21:33:25 -07:00
|
|
|
|
2022-02-25 13:33:23 -08:00
|
|
|
#if NET5_0_OR_GREATER
|
|
|
|
|
HttpClientHandler.SslOptions.RemoteCertificateValidationCallback =
|
|
|
|
|
#else
|
2020-04-23 11:40:06 -07:00
|
|
|
HttpClientHandler.ServerCertificateCustomValidationCallback =
|
2022-02-25 13:33:23 -08:00
|
|
|
#endif
|
2020-04-23 11:40:06 -07:00
|
|
|
(sender, certificate, chain, sslPolicyErrors) =>
|
|
|
|
|
{
|
2020-11-22 14:52:09 -08:00
|
|
|
return CertificateValidationCallBack(sender, CaCerts, certificate, chain,
|
2020-04-23 11:40:06 -07:00
|
|
|
sslPolicyErrors);
|
|
|
|
|
};
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-06 23:28:17 -06:00
|
|
|
// set credentails for the kubernetes client
|
|
|
|
|
SetCredentials(config);
|
2018-09-27 10:50:39 -07:00
|
|
|
|
2022-02-25 13:33:23 -08:00
|
|
|
var clientCert = CertUtils.GetClientCert(config);
|
|
|
|
|
if (clientCert != null)
|
2020-04-23 11:40:06 -07:00
|
|
|
{
|
2022-02-25 13:33:23 -08:00
|
|
|
#if NET5_0_OR_GREATER
|
|
|
|
|
HttpClientHandler.SslOptions.ClientCertificates.Add(clientCert);
|
|
|
|
|
#else
|
|
|
|
|
HttpClientHandler.ClientCertificates.Add(clientCert);
|
|
|
|
|
#endif
|
2020-04-23 11:40:06 -07:00
|
|
|
}
|
2020-04-16 10:54:47 -07:00
|
|
|
}
|
|
|
|
|
|
2022-02-25 13:33:23 -08:00
|
|
|
private X509Certificate2Collection CaCerts { get; }
|
2018-09-27 10:50:39 -07:00
|
|
|
|
2022-02-25 13:33:23 -08:00
|
|
|
private X509Certificate2 ClientCert { get; }
|
2018-09-27 10:50:39 -07:00
|
|
|
|
2022-02-25 13:33:23 -08:00
|
|
|
private bool SkipTlsVerify { get; }
|
2018-09-27 10:50:39 -07:00
|
|
|
|
2020-04-16 10:54:47 -07:00
|
|
|
// NOTE: this method replicates the logic that the base ServiceClient uses except that it doesn't insert the RetryDelegatingHandler
|
|
|
|
|
// and it does insert the WatcherDelegatingHandler. we don't want the RetryDelegatingHandler because it has a very broad definition
|
|
|
|
|
// of what requests have failed. it considers everything outside 2xx to be failed, including 1xx (e.g. 101 Switching Protocols) and
|
|
|
|
|
// 3xx. in particular, this prevents upgraded connections and certain generic/custom requests from working.
|
2021-03-18 09:17:19 -07:00
|
|
|
private void CreateHttpClient(DelegatingHandler[] handlers, KubernetesClientConfiguration config)
|
2020-04-16 10:54:47 -07:00
|
|
|
{
|
2022-02-08 08:13:24 +00:00
|
|
|
#if NET5_0_OR_GREATER
|
2022-02-25 13:33:23 -08:00
|
|
|
FirstMessageHandler = HttpClientHandler = new SocketsHttpHandler
|
2021-03-11 09:32:22 -08:00
|
|
|
{
|
2022-02-25 13:33:23 -08:00
|
|
|
KeepAlivePingPolicy = HttpKeepAlivePingPolicy.WithActiveRequests,
|
|
|
|
|
KeepAlivePingDelay = TimeSpan.FromMinutes(3),
|
|
|
|
|
KeepAlivePingTimeout = TimeSpan.FromSeconds(30),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
HttpClientHandler.SslOptions.ClientCertificates = new X509Certificate2Collection();
|
|
|
|
|
#else
|
|
|
|
|
FirstMessageHandler = HttpClientHandler = new HttpClientHandler();
|
2021-03-11 09:32:22 -08:00
|
|
|
#endif
|
|
|
|
|
|
2022-02-25 13:33:23 -08:00
|
|
|
if (handlers != null)
|
2020-04-16 10:54:47 -07:00
|
|
|
{
|
|
|
|
|
for (int i = handlers.Length - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
DelegatingHandler handler = handlers[i];
|
2020-04-23 11:40:06 -07:00
|
|
|
while (handler.InnerHandler is DelegatingHandler d)
|
|
|
|
|
{
|
|
|
|
|
handler = d;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-16 10:54:47 -07:00
|
|
|
handler.InnerHandler = FirstMessageHandler;
|
|
|
|
|
FirstMessageHandler = handlers[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-23 11:40:06 -07:00
|
|
|
|
2021-10-11 12:55:02 -07:00
|
|
|
HttpClient = new HttpClient(FirstMessageHandler, false)
|
|
|
|
|
{
|
|
|
|
|
Timeout = Timeout.InfiniteTimeSpan,
|
|
|
|
|
};
|
2020-04-16 10:54:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-20 15:51:58 +02:00
|
|
|
|
|
|
|
|
|
2018-09-27 10:50:39 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Set credentials for the Client
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="config">k8s client configuration</param>
|
2020-04-20 09:39:40 -07:00
|
|
|
private void SetCredentials(KubernetesClientConfiguration config) => Credentials = CreateCredentials(config);
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// SSl Cert Validation Callback
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">sender</param>
|
2020-11-22 14:52:09 -08:00
|
|
|
/// <param name="caCerts">client ca</param>
|
2018-09-27 10:50:39 -07:00
|
|
|
/// <param name="certificate">client certificate</param>
|
|
|
|
|
/// <param name="chain">chain</param>
|
|
|
|
|
/// <param name="sslPolicyErrors">ssl policy errors</param>
|
|
|
|
|
/// <returns>true if valid cert</returns>
|
|
|
|
|
public static bool CertificateValidationCallBack(
|
|
|
|
|
object sender,
|
2019-03-11 06:39:28 -07:00
|
|
|
X509Certificate2Collection caCerts,
|
2018-09-27 10:50:39 -07:00
|
|
|
X509Certificate certificate,
|
|
|
|
|
X509Chain chain,
|
|
|
|
|
SslPolicyErrors sslPolicyErrors)
|
|
|
|
|
{
|
2020-11-22 14:52:09 -08:00
|
|
|
if (caCerts == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(caCerts));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (chain == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(chain));
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-27 10:50:39 -07:00
|
|
|
// If the certificate is a valid, signed certificate, return true.
|
|
|
|
|
if (sslPolicyErrors == SslPolicyErrors.None)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there are errors in the certificate chain, look at each error to determine the cause.
|
|
|
|
|
if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) != 0)
|
|
|
|
|
{
|
|
|
|
|
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
|
|
|
|
|
|
2019-03-11 06:39:28 -07:00
|
|
|
// Added our trusted certificates to the chain
|
|
|
|
|
//
|
|
|
|
|
chain.ChainPolicy.ExtraStore.AddRange(caCerts);
|
|
|
|
|
|
2018-09-27 10:50:39 -07:00
|
|
|
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
|
2018-09-10 04:00:45 +01:00
|
|
|
var isValid = chain.Build((X509Certificate2)certificate);
|
|
|
|
|
|
2019-03-11 06:39:28 -07:00
|
|
|
var isTrusted = false;
|
|
|
|
|
|
2018-09-27 10:50:39 -07:00
|
|
|
var rootCert = chain.ChainElements[chain.ChainElements.Count - 1].Certificate;
|
|
|
|
|
|
2019-03-11 06:39:28 -07:00
|
|
|
// Make sure that one of our trusted certs exists in the chain provided by the server.
|
|
|
|
|
//
|
|
|
|
|
foreach (var cert in caCerts)
|
|
|
|
|
{
|
|
|
|
|
if (rootCert.RawData.SequenceEqual(cert.RawData))
|
|
|
|
|
{
|
|
|
|
|
isTrusted = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isValid && isTrusted;
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// In all other cases, return false.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-04-20 09:39:40 -07:00
|
|
|
|
2020-11-22 14:52:09 -08:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates <see cref="ServiceClientCredentials"/> based on the given config, or returns null if no such credentials are needed.
|
2020-04-20 09:39:40 -07:00
|
|
|
/// </summary>
|
2020-11-22 14:52:09 -08:00
|
|
|
/// <param name="config">kubenetes config object</param>
|
|
|
|
|
/// <returns>instance of <see cref="ServiceClientCredentials"/></returns>
|
2020-04-20 09:39:40 -07:00
|
|
|
public static ServiceClientCredentials CreateCredentials(KubernetesClientConfiguration config)
|
|
|
|
|
{
|
2020-04-23 11:40:06 -07:00
|
|
|
if (config == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(config));
|
|
|
|
|
}
|
2020-10-23 08:31:57 -07:00
|
|
|
|
2020-04-28 18:34:25 -04:00
|
|
|
if (config.TokenProvider != null)
|
|
|
|
|
{
|
|
|
|
|
return new TokenCredentials(config.TokenProvider);
|
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrEmpty(config.AccessToken))
|
2020-04-20 09:39:40 -07:00
|
|
|
{
|
|
|
|
|
return new TokenCredentials(config.AccessToken);
|
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrEmpty(config.Username))
|
|
|
|
|
{
|
|
|
|
|
return new BasicAuthenticationCredentials() { UserName = config.Username, Password = config.Password };
|
|
|
|
|
}
|
2020-04-23 11:40:06 -07:00
|
|
|
|
2020-04-20 09:39:40 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
|
|
|
|
}
|