Skip to content

Commit

Permalink
Use the system certificate store if no certificates are specified.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusbooyah committed Apr 11, 2023
1 parent 2af57ca commit f2be6f6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/KubernetesClient/Kubernetes.ConfigInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,33 @@ private void InitializeFromConfig(KubernetesClientConfiguration config)
{
if (CaCerts == null)
{
throw new KubeConfigException("A CA must be set when SkipTlsVerify === false");
var store = new X509Store(
StoreName.CertificateAuthority,
StoreLocation.CurrentUser);
#if NET5_0_OR_GREATER
HttpClientHandler.SslOptions.RemoteCertificateValidationCallback =
#else
HttpClientHandler.ServerCertificateCustomValidationCallback =
#endif
(sender, certificate, chain, sslPolicyErrors) =>
{
return CertificateValidationCallBack(sender, store.Certificates, certificate, chain,
sslPolicyErrors);
};
}

else
{
#if NET5_0_OR_GREATER
HttpClientHandler.SslOptions.RemoteCertificateValidationCallback =
HttpClientHandler.SslOptions.RemoteCertificateValidationCallback =
#else
HttpClientHandler.ServerCertificateCustomValidationCallback =
#endif
(sender, certificate, chain, sslPolicyErrors) =>
{
return CertificateValidationCallBack(sender, CaCerts, certificate, chain,
sslPolicyErrors);
};
(sender, certificate, chain, sslPolicyErrors) =>
{
return CertificateValidationCallBack(sender, CaCerts, certificate, chain,
sslPolicyErrors);
};
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ public void CheckClusterTlsSkipCorrectness()
Assert.True(cfg.SkipTlsVerify);
}

/// <summary>
/// Checks that a KubeConfigException is not thrown when no certificate-authority-data is set and user do not require tls
/// skip
/// </summary>
[Fact]
public void CheckClusterTlsNoSkipCorrectness()
{
var fi = new FileInfo("assets/kubeconfig.tls-no-skip.yml");
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi);
Assert.NotNull(cfg.Host);
Assert.Null(cfg.SslCaCerts);
Assert.False(cfg.SkipTlsVerify);
}

/// <summary>
/// Checks that a KubeConfigException is thrown when the cluster defined in clusters and contexts do not match
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions tests/KubernetesClient.Tests/assets/kubeconfig.tls-no-skip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
# WARNING: File includes minor fixes
---
current-context: federal-context
apiVersion: v1
clusters:
- cluster:
insecure-skip-tls-verify: false
server: https://horse.org:443
name: horse-cluster
contexts:
- context:
cluster: horse-cluster
namespace: chisel-ns
user: green-user
name: federal-context
kind: Config
users:
- name: green-user
user:
password: secret
username: admin

0 comments on commit f2be6f6

Please sign in to comment.