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
…1261)

* Use the system certificate store if no certificates are specified.

* Don't use ServerCertificateCustomValidationCallback when no CA is set
  • Loading branch information
marcusbooyah authored Apr 18, 2023
1 parent 142fd14 commit 729b10c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/KubernetesClient/Kubernetes.ConfigInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,19 @@ private void InitializeFromConfig(KubernetesClientConfiguration config)
}
else
{
if (CaCerts == null)
if (CaCerts != null)
{
throw new KubeConfigException("A CA must be set when SkipTlsVerify === false");
}

#if NET5_0_OR_GREATER
HttpClientHandler.SslOptions.RemoteCertificateValidationCallback =
HttpClientHandler.SslOptions.RemoteCertificateValidationCallback =
#else
HttpClientHandler.ServerCertificateCustomValidationCallback =
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 729b10c

Please sign in to comment.