Skip to content

Commit 73d8e99

Browse files
authored
Add missing client cert (#729)
* Updated GitVersioning package to fix issue with loading native libgit lib. Added check for missing HttpClientHandler * fixed type * HttpClientHandler is null when trying to get client certificates for web socket connection. Added direct configuration of client cert instead of via HttpClientHandler * fixed indentation warning * re-added certs from httpclienthandler if present * Updated GitVersioning package to fix issue with loading native libgit lib. Added check for missing HttpClientHandler * fixed type * HttpClientHandler is null when trying to get client certificates for web socket connection. Added direct configuration of client cert instead of via HttpClientHandler * fixed indentation warning * re-added certs from httpclienthandler if present * merged duplicate code * reverted package changes
1 parent 2d8915d commit 73d8e99

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

src/KubernetesClient/CertUtils.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,28 @@ public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
122122
}
123123
}
124124
}
125+
126+
/// <summary>
127+
/// Retrieves Client Certificate PFX from configuration
128+
/// </summary>
129+
/// <param name="config">Kubernetes Client Configuration</param>
130+
/// <returns>Client certificate PFX</returns>
131+
public static X509Certificate2 GetClientCert(KubernetesClientConfiguration config)
132+
{
133+
if (config == null)
134+
{
135+
throw new ArgumentNullException(nameof(config));
136+
}
137+
138+
if ((!string.IsNullOrWhiteSpace(config.ClientCertificateData) ||
139+
!string.IsNullOrWhiteSpace(config.ClientCertificateFilePath)) &&
140+
(!string.IsNullOrWhiteSpace(config.ClientCertificateKeyData) ||
141+
!string.IsNullOrWhiteSpace(config.ClientKeyFilePath)))
142+
{
143+
return GeneratePfx(config);
144+
}
145+
146+
return null;
147+
}
125148
}
126149
}

src/KubernetesClient/Kubernetes.ConfigInit.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public Kubernetes(KubernetesClientConfiguration config, HttpClient httpClient, b
5757
ValidateConfig(config);
5858
CaCerts = config.SslCaCerts;
5959
SkipTlsVerify = config.SkipTlsVerify;
60+
ClientCert = CertUtils.GetClientCert(config);
6061
SetCredentials(config);
6162
}
6263

@@ -133,7 +134,7 @@ private void InitializeFromConfig(KubernetesClientConfiguration config)
133134
}
134135

135136
private X509Certificate2Collection CaCerts { get; }
136-
137+
private X509Certificate2 ClientCert { get; }
137138
private bool SkipTlsVerify { get; }
138139

139140
partial void CustomInitialize()
@@ -262,6 +263,8 @@ private void CreateHttpClient(DelegatingHandler[] handlers, KubernetesClientConf
262263
};
263264
}
264265

266+
267+
265268
/// <summary>
266269
/// Set credentials for the Client
267270
/// </summary>

src/KubernetesClient/Kubernetes.WebSocket.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,17 @@ protected async Task<WebSocket> StreamConnectAsync(Uri uri, string invocationId
295295
}
296296

297297
// Set Credentials
298-
foreach (var cert in this.HttpClientHandler.ClientCertificates.OfType<X509Certificate2>())
298+
if (this.ClientCert != null)
299299
{
300-
webSocketBuilder.AddClientCertificate(cert);
300+
webSocketBuilder.AddClientCertificate(this.ClientCert);
301+
}
302+
303+
if (this.HttpClientHandler != null)
304+
{
305+
foreach (var cert in this.HttpClientHandler.ClientCertificates.OfType<X509Certificate2>())
306+
{
307+
webSocketBuilder.AddClientCertificate(cert);
308+
}
301309
}
302310

303311
if (Credentials != null)

src/KubernetesClient/KubernetesClientConfiguration.HttpClientHandler.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,10 @@ public void AddCertificates(HttpClientHandler handler)
4141
throw new ArgumentNullException(nameof(handler));
4242
}
4343

44-
if ((!string.IsNullOrWhiteSpace(ClientCertificateData) ||
45-
!string.IsNullOrWhiteSpace(ClientCertificateFilePath)) &&
46-
(!string.IsNullOrWhiteSpace(ClientCertificateKeyData) ||
47-
!string.IsNullOrWhiteSpace(ClientKeyFilePath)))
44+
var clientCert = CertUtils.GetClientCert(this);
45+
if (clientCert != null)
4846
{
49-
var cert = CertUtils.GeneratePfx(this);
50-
51-
handler.ClientCertificates.Add(cert);
47+
handler.ClientCertificates.Add(clientCert);
5248
}
5349
}
5450
}

0 commit comments

Comments
 (0)