Skip to content

Commit d8da943

Browse files
authored
Support TLS Server Name overrides in kubeconfig (#1282)
The client should support tls-server-name just like client-go and kubectl. See kubernetes/kubernetes#88769
1 parent ceddcfc commit d8da943

File tree

7 files changed

+53
-0
lines changed

7 files changed

+53
-0
lines changed

src/KubernetesClient.Models/KubeConfigModels/ClusterEndpoint.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public class ClusterEndpoint
2525
[YamlMember(Alias = "server")]
2626
public string Server { get; set; }
2727

28+
/// <summary>
29+
/// Gets or sets a value to override the TLS server name.
30+
/// </summary>
31+
[YamlMember(Alias = "tls-server-name", ApplyNamingConventions = false)]
32+
public string TlsServerName { get; set; }
33+
2834
/// <summary>
2935
/// Gets or sets a value indicating whether to skip the validity check for the server's certificate.
3036
/// This will make your HTTPS connections insecure.

src/KubernetesClient/Kubernetes.ConfigInit.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler
2626
ValidateConfig(config);
2727
CaCerts = config.SslCaCerts;
2828
SkipTlsVerify = config.SkipTlsVerify;
29+
TlsServerName = config.TlsServerName;
2930
CreateHttpClient(handlers, config);
3031
InitializeFromConfig(config);
3132
HttpClientTimeout = config.HttpClientTimeout;
@@ -115,6 +116,8 @@ private void InitializeFromConfig(KubernetesClientConfiguration config)
115116

116117
private bool SkipTlsVerify { get; }
117118

119+
private string TlsServerName { get; }
120+
118121
// NOTE: this method replicates the logic that the base ServiceClient uses except that it doesn't insert the RetryDelegatingHandler
119122
// and it does insert the WatcherDelegatingHandler. we don't want the RetryDelegatingHandler because it has a very broad definition
120123
// of what requests have failed. it considers everything outside 2xx to be failed, including 1xx (e.g. 101 Switching Protocols) and

src/KubernetesClient/Kubernetes.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ protected virtual async Task<HttpResponseMessage> SendRequestRaw(string requestC
149149
await Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
150150
}
151151

152+
if (!string.IsNullOrWhiteSpace(TlsServerName))
153+
{
154+
httpRequest.Headers.Host = TlsServerName;
155+
}
156+
152157
// Send Request
153158
cancellationToken.ThrowIfCancellationRequested();
154159
var httpResponse = await HttpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ private void SetClusterDetails(K8SConfiguration k8SConfig, Context activeContext
267267

268268
Host = clusterDetails.ClusterEndpoint.Server;
269269
SkipTlsVerify = clusterDetails.ClusterEndpoint.SkipTlsVerify;
270+
TlsServerName = clusterDetails.ClusterEndpoint.TlsServerName;
270271

271272
if (!Uri.TryCreate(Host, UriKind.Absolute, out var uri))
272273
{

src/KubernetesClient/KubernetesClientConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public partial class KubernetesClientConfiguration
5656
/// </summary>
5757
public bool SkipTlsVerify { get; set; }
5858

59+
/// <summary>
60+
/// Option to override the TLS server name
61+
/// </summary>
62+
public string TlsServerName { get; set; }
63+
5964
/// <summary>
6065
/// Gets or sets the HTTP user agent.
6166
/// </summary>

tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,17 @@ public void SmartSkipTlsVerify()
341341
Assert.Equal("http://horse.org", cfg.Host);
342342
}
343343

344+
/// <summary>
345+
/// Make sure that TlsServerName is present
346+
/// </summary>
347+
[Fact]
348+
public void TlsServerName()
349+
{
350+
var fi = new FileInfo("assets/kubeconfig.tls-servername.yml");
351+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi);
352+
Assert.Equal("pony", cfg.TlsServerName);
353+
}
354+
344355
/// <summary>
345356
/// Checks config could work well when current-context is not set but masterUrl is set. #issue 24
346357
/// </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+
server: https://horse.org:443
9+
tls-server-name: pony
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)