Skip to content

make BuildConfigFromConfigFile less sensitive #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/KubeConfigModels/K8SConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace k8s.KubeConfigModels
namespace k8s.KubeConfigModels
{
using System.Collections.Generic;
using YamlDotNet.Serialization;
Expand All @@ -9,7 +9,7 @@
public class K8SConfiguration
{
[YamlMember(Alias = "preferences")]
public IDictionary<string, object> preferences{ get; set; }
public IDictionary<string, object> Preferences{ get; set; }

[YamlMember(Alias = "apiVersion")]
public string ApiVersion { get; set; }
Expand All @@ -21,12 +21,12 @@ public class K8SConfiguration
public string CurrentContext { get; set; }

[YamlMember(Alias = "contexts")]
public IEnumerable<Context> Contexts { get; set; }
public IEnumerable<Context> Contexts { get; set; } = new Context[0];

[YamlMember(Alias = "clusters")]
public IEnumerable<Cluster> Clusters { get; set; }
public IEnumerable<Cluster> Clusters { get; set; } = new Cluster[0];

[YamlMember(Alias = "users")]
public IEnumerable<User> Users { get; set; }
public IEnumerable<User> Users { get; set; } = new User[0];
}
}
108 changes: 71 additions & 37 deletions src/Kubernetes.Auth.cs → src/Kubernetes.ConfigInit.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using k8s.Exceptions;
using k8s.Models;
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();
if (string.IsNullOrWhiteSpace(config.Host))
{
throw new KubeConfigException("Host url must be set");
}

this.CaCert = config.SslCaCert;
this.BaseUri = new Uri(config.Host);
try
{
BaseUri = new Uri(config.Host);
}
catch (UriFormatException e)
{
throw new KubeConfigException("Bad host url", e);
}

var handler = new HttpClientHandler();
CaCert = config.SslCaCert;

if (BaseUri.Scheme == "https")
{
if (config.SkipTlsVerify)
{
handler.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
HttpClientHandler.ServerCertificateCustomValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => true;
}
else
{
Expand All @@ -41,21 +52,47 @@ public Kubernetes(KubernetesClientConfiguration config)
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; }

partial void CustomInitialize()
{
AppendDelegatingHandler<WatcherDelegatingHandler>();
DeserializationSettings.Converters.Add(new V1Status.V1StatusObjectViewConverter());
}

private X509Certificate2 CaCert { get; set; }
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>
Expand Down Expand Up @@ -88,7 +125,7 @@ private void SetCredentials(KubernetesClientConfiguration config, HttpClientHand
}

/// <summary>
/// SSl Cert Validation Callback
/// SSl Cert Validation Callback
/// </summary>
/// <param name="sender">sender</param>
/// <param name="certificate">client certificate</param>
Expand All @@ -97,10 +134,10 @@ private void SetCredentials(KubernetesClientConfiguration config, HttpClientHand
/// <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)
Expand All @@ -114,16 +151,13 @@ private bool CertificateValidationCallBack(
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;
}
}
}
Loading