Skip to content

Commit 56646ae

Browse files
authored
enable websocket on netstandard (#899)
* enable websocket on netstandard * happy build
1 parent ef90174 commit 56646ae

5 files changed

+52
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Net.Security;
2+
using System.Security.Cryptography.X509Certificates;
3+
4+
namespace k8s;
5+
6+
public partial class Kubernetes
7+
{
8+
partial void BeforeRequest()
9+
{
10+
System.Net.ServicePointManager.ServerCertificateValidationCallback += ServerCertificateValidationCallback;
11+
}
12+
13+
partial void AfterRequest()
14+
{
15+
System.Net.ServicePointManager.ServerCertificateValidationCallback -= ServerCertificateValidationCallback;
16+
}
17+
18+
private bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain,
19+
SslPolicyErrors sslPolicyErrors)
20+
{
21+
if (SkipTlsVerify)
22+
{
23+
return true;
24+
}
25+
26+
return CertificateValidationCallBack(sender, CaCerts, certificate, chain, sslPolicyErrors);
27+
}
28+
}

src/KubernetesClient.Classic/KubernetesClient.Classic.csproj

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
<Compile Include="..\KubernetesClient\KubernetesClientConfiguration.cs" />
2929
<Compile Include="..\KubernetesClient\KubernetesException.cs" />
3030

31+
<Compile Include="..\KubernetesClient\ChannelIndex.cs" />
32+
<Compile Include="..\KubernetesClient\IStreamDemuxer.cs" />
33+
<Compile Include="..\KubernetesClient\ByteBuffer.cs" />
34+
<Compile Include="..\KubernetesClient\StreamDemuxer.cs" />
35+
<Compile Include="..\KubernetesClient\MuxedStream.cs" />
36+
<Compile Include="..\KubernetesClient\StreamType.cs" />
37+
<Compile Include="..\KubernetesClient\IKubernetes.WebSocket.cs" />
38+
<Compile Include="..\KubernetesClient\Kubernetes.WebSocket.cs" />
39+
<Compile Include="..\KubernetesClient\WebSocketBuilder.cs" />
40+
<Compile Include="..\KubernetesClient\WebSocketProtocol.cs" />
41+
<Compile Include="..\KubernetesClient\Utilities.cs" />
42+
3143
<Compile Include="..\KubernetesClient\Exceptions\KubeConfigException.cs" />
3244
<Compile Include="..\KubernetesClient\Exceptions\KubernetesClientException.cs" />
3345

src/KubernetesClient/Kubernetes.ConfigInit.cs

-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler
2323
Initialize();
2424
ValidateConfig(config);
2525
CaCerts = config.SslCaCerts;
26-
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
2726
SkipTlsVerify = config.SkipTlsVerify;
28-
#endif
2927
CreateHttpClient(handlers, config);
3028
InitializeFromConfig(config);
3129
HttpClientTimeout = config.HttpClientTimeout;
@@ -102,11 +100,9 @@ private void InitializeFromConfig(KubernetesClientConfiguration config)
102100

103101
private X509Certificate2Collection CaCerts { get; }
104102

105-
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
106103
private X509Certificate2 ClientCert { get; }
107104

108105
private bool SkipTlsVerify { get; }
109-
#endif
110106

111107
// NOTE: this method replicates the logic that the base ServiceClient uses except that it doesn't insert the RetryDelegatingHandler
112108
// and it does insert the WatcherDelegatingHandler. we don't want the RetryDelegatingHandler because it has a very broad definition

src/KubernetesClient/Kubernetes.WebSocket.cs

+8
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ public Task<WebSocket> WebSocketNamespacedPodAttachAsync(string name, string @na
210210
cancellationToken);
211211
}
212212

213+
partial void BeforeRequest();
214+
partial void AfterRequest();
215+
213216
protected async Task<WebSocket> StreamConnectAsync(Uri uri, string webSocketSubProtocol = null, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default)
214217
{
215218
if (uri == null)
@@ -280,6 +283,7 @@ protected async Task<WebSocket> StreamConnectAsync(Uri uri, string webSocketSubP
280283
WebSocket webSocket = null;
281284
try
282285
{
286+
BeforeRequest();
283287
webSocket = await webSocketBuilder.BuildAndConnectAsync(uri, CancellationToken.None)
284288
.ConfigureAwait(false);
285289
}
@@ -335,6 +339,10 @@ protected async Task<WebSocket> StreamConnectAsync(Uri uri, string webSocketSubP
335339
{
336340
throw;
337341
}
342+
finally
343+
{
344+
AfterRequest();
345+
}
338346

339347
return webSocket;
340348
}

src/KubernetesClient/WebSocketBuilder.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,23 @@ public virtual WebSocketBuilder AddClientCertificate(X509Certificate2 certificat
3636

3737
public WebSocketBuilder ExpectServerCertificate(X509Certificate2Collection serverCertificate)
3838
{
39+
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
3940
Options.RemoteCertificateValidationCallback
4041
= (sender, certificate, chain, sslPolicyErrors) =>
4142
{
4243
return Kubernetes.CertificateValidationCallBack(sender, serverCertificate, certificate, chain, sslPolicyErrors);
4344
};
44-
45+
#endif
4546
return this;
4647
}
4748

4849
public WebSocketBuilder SkipServerCertificateValidation()
4950
{
51+
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
5052
Options.RemoteCertificateValidationCallback
5153
= (sender, certificate, chain, sslPolicyErrors) => true;
5254

55+
#endif
5356
return this;
5457
}
5558

0 commit comments

Comments
 (0)