Skip to content

Commit 729b10c

Browse files
authored
Use the system certificate store if no certificates are specified. (#1261)
* Use the system certificate store if no certificates are specified. * Don't use ServerCertificateCustomValidationCallback when no CA is set
1 parent 142fd14 commit 729b10c

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

src/KubernetesClient/Kubernetes.ConfigInit.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,19 @@ private void InitializeFromConfig(KubernetesClientConfiguration config)
7272
}
7373
else
7474
{
75-
if (CaCerts == null)
75+
if (CaCerts != null)
7676
{
77-
throw new KubeConfigException("A CA must be set when SkipTlsVerify === false");
78-
}
79-
8077
#if NET5_0_OR_GREATER
81-
HttpClientHandler.SslOptions.RemoteCertificateValidationCallback =
78+
HttpClientHandler.SslOptions.RemoteCertificateValidationCallback =
8279
#else
83-
HttpClientHandler.ServerCertificateCustomValidationCallback =
80+
HttpClientHandler.ServerCertificateCustomValidationCallback =
8481
#endif
85-
(sender, certificate, chain, sslPolicyErrors) =>
86-
{
87-
return CertificateValidationCallBack(sender, CaCerts, certificate, chain,
88-
sslPolicyErrors);
89-
};
82+
(sender, certificate, chain, sslPolicyErrors) =>
83+
{
84+
return CertificateValidationCallBack(sender, CaCerts, certificate, chain,
85+
sslPolicyErrors);
86+
};
87+
}
9088
}
9189
}
9290

tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ public void CheckClusterTlsSkipCorrectness()
138138
Assert.True(cfg.SkipTlsVerify);
139139
}
140140

141+
/// <summary>
142+
/// Checks that a KubeConfigException is not thrown when no certificate-authority-data is set and user do not require tls
143+
/// skip
144+
/// </summary>
145+
[Fact]
146+
public void CheckClusterTlsNoSkipCorrectness()
147+
{
148+
var fi = new FileInfo("assets/kubeconfig.tls-no-skip.yml");
149+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi);
150+
Assert.NotNull(cfg.Host);
151+
Assert.Null(cfg.SslCaCerts);
152+
Assert.False(cfg.SkipTlsVerify);
153+
}
154+
141155
/// <summary>
142156
/// Checks that a KubeConfigException is thrown when the cluster defined in clusters and contexts do not match
143157
/// </summary>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
2+
# WARNING: File includes minor fixes
3+
---
4+
current-context: federal-context
5+
apiVersion: v1
6+
clusters:
7+
- cluster:
8+
insecure-skip-tls-verify: false
9+
server: https://horse.org:443
10+
name: horse-cluster
11+
contexts:
12+
- context:
13+
cluster: horse-cluster
14+
namespace: chisel-ns
15+
user: green-user
16+
name: federal-context
17+
kind: Config
18+
users:
19+
- name: green-user
20+
user:
21+
password: secret
22+
username: admin

0 commit comments

Comments
 (0)