watcher handler will auto patch to all ctor path
This commit is contained in:
@@ -1,38 +1,36 @@
|
||||
using k8s.Models;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net.Http;
|
||||
using System.Net.Security;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using k8s.Exceptions;
|
||||
using Microsoft.Rest;
|
||||
|
||||
namespace k8s
|
||||
{
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net.Http;
|
||||
using System.Net.Security;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Threading.Tasks;
|
||||
using k8s.Exceptions;
|
||||
using Microsoft.Rest;
|
||||
|
||||
public partial class Kubernetes : ServiceClient<Kubernetes>, IKubernetes
|
||||
public partial class Kubernetes
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Kubernetes"/> class.
|
||||
/// Initializes a new instance of the <see cref="Kubernetes" /> class.
|
||||
/// </summary>
|
||||
/// <param name='config'>
|
||||
/// Optional. The delegating handlers to add to the http client pipeline.
|
||||
/// Optional. The delegating handlers to add to the http client pipeline.
|
||||
/// </param>
|
||||
public Kubernetes(KubernetesClientConfiguration config)
|
||||
/// <param name="handlers">
|
||||
/// Optional. The delegating handlers to add to the http client pipeline.
|
||||
/// </param>
|
||||
public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler[] handlers) : this(handlers)
|
||||
{
|
||||
this.Initialize();
|
||||
|
||||
this.CaCert = config.SslCaCert;
|
||||
this.BaseUri = new Uri(config.Host);
|
||||
|
||||
var handler = new HttpClientHandler();
|
||||
CaCert = config.SslCaCert;
|
||||
BaseUri = new Uri(config.Host);
|
||||
|
||||
if (BaseUri.Scheme == "https")
|
||||
{
|
||||
if (config.SkipTlsVerify)
|
||||
{
|
||||
handler.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
||||
HttpClientHandler.ServerCertificateCustomValidationCallback =
|
||||
(sender, certificate, chain, sslPolicyErrors) => true;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -41,21 +39,47 @@ namespace k8s
|
||||
throw new KubeConfigException("a CA must be set when SkipTlsVerify === false");
|
||||
}
|
||||
|
||||
handler.ServerCertificateCustomValidationCallback = CertificateValidationCallBack;
|
||||
HttpClientHandler.ServerCertificateCustomValidationCallback = CertificateValidationCallBack;
|
||||
}
|
||||
}
|
||||
|
||||
// set credentails for the kubernernet client
|
||||
this.SetCredentials(config, handler);
|
||||
this.InitializeHttpClient(handler, new DelegatingHandler[]{new WatcherDelegatingHandler()});
|
||||
|
||||
DeserializationSettings.Converters.Add(new V1Status.V1StatusObjectViewConverter());
|
||||
SetCredentials(config, HttpClientHandler);
|
||||
}
|
||||
|
||||
private X509Certificate2 CaCert { get; set; }
|
||||
private X509Certificate2 CaCert { get; }
|
||||
|
||||
partial void CustomInitialize()
|
||||
{
|
||||
AppendDelegatingHandler<WatcherDelegatingHandler>();
|
||||
DeserializationSettings.Converters.Add(new V1Status.V1StatusObjectViewConverter());
|
||||
}
|
||||
|
||||
private void AppendDelegatingHandler<T>() where T : DelegatingHandler, new()
|
||||
{
|
||||
var cur = FirstMessageHandler as DelegatingHandler;
|
||||
|
||||
while (cur != null)
|
||||
{
|
||||
var next = cur.InnerHandler as DelegatingHandler;
|
||||
|
||||
if (next == null)
|
||||
{
|
||||
// last one
|
||||
// append watcher handler between to last handler
|
||||
cur.InnerHandler = new T
|
||||
{
|
||||
InnerHandler = cur.InnerHandler
|
||||
};
|
||||
break;
|
||||
}
|
||||
|
||||
cur = next;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set credentials for the Client
|
||||
/// Set credentials for the Client
|
||||
/// </summary>
|
||||
/// <param name="config">k8s client configuration</param>
|
||||
/// <param name="handler">http client handler for the rest client</param>
|
||||
@@ -88,7 +112,7 @@ namespace k8s
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SSl Cert Validation Callback
|
||||
/// SSl Cert Validation Callback
|
||||
/// </summary>
|
||||
/// <param name="sender">sender</param>
|
||||
/// <param name="certificate">client certificate</param>
|
||||
@@ -97,10 +121,10 @@ namespace k8s
|
||||
/// <returns>true if valid cert</returns>
|
||||
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", Justification = "Unused by design")]
|
||||
private bool CertificateValidationCallBack(
|
||||
object sender,
|
||||
X509Certificate certificate,
|
||||
X509Chain chain,
|
||||
SslPolicyErrors sslPolicyErrors)
|
||||
object sender,
|
||||
X509Certificate certificate,
|
||||
X509Chain chain,
|
||||
SslPolicyErrors sslPolicyErrors)
|
||||
{
|
||||
// If the certificate is a valid, signed certificate, return true.
|
||||
if (sslPolicyErrors == SslPolicyErrors.None)
|
||||
@@ -114,16 +138,13 @@ namespace k8s
|
||||
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
|
||||
|
||||
// add all your extra certificate chain
|
||||
chain.ChainPolicy.ExtraStore.Add(this.CaCert);
|
||||
chain.ChainPolicy.ExtraStore.Add(CaCert);
|
||||
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
|
||||
var isValid = chain.Build((X509Certificate2)certificate);
|
||||
var isValid = chain.Build((X509Certificate2) certificate);
|
||||
return isValid;
|
||||
}
|
||||
else
|
||||
{
|
||||
// In all other cases, return false.
|
||||
return false;
|
||||
}
|
||||
// In all other cases, return false.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user