Skip to content

Commit

Permalink
enable websocket on netstandard (kubernetes-client#899)
Browse files Browse the repository at this point in the history
* enable websocket on netstandard

* happy build
  • Loading branch information
tg123 authored Jun 28, 2022
1 parent ef90174 commit 56646ae
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/KubernetesClient.Classic/Kubernetes.Websocket.Netstandard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace k8s;

public partial class Kubernetes
{
partial void BeforeRequest()
{
System.Net.ServicePointManager.ServerCertificateValidationCallback += ServerCertificateValidationCallback;
}

partial void AfterRequest()
{
System.Net.ServicePointManager.ServerCertificateValidationCallback -= ServerCertificateValidationCallback;
}

private bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (SkipTlsVerify)
{
return true;
}

return CertificateValidationCallBack(sender, CaCerts, certificate, chain, sslPolicyErrors);
}
}
12 changes: 12 additions & 0 deletions src/KubernetesClient.Classic/KubernetesClient.Classic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@
<Compile Include="..\KubernetesClient\KubernetesClientConfiguration.cs" />
<Compile Include="..\KubernetesClient\KubernetesException.cs" />

<Compile Include="..\KubernetesClient\ChannelIndex.cs" />
<Compile Include="..\KubernetesClient\IStreamDemuxer.cs" />
<Compile Include="..\KubernetesClient\ByteBuffer.cs" />
<Compile Include="..\KubernetesClient\StreamDemuxer.cs" />
<Compile Include="..\KubernetesClient\MuxedStream.cs" />
<Compile Include="..\KubernetesClient\StreamType.cs" />
<Compile Include="..\KubernetesClient\IKubernetes.WebSocket.cs" />
<Compile Include="..\KubernetesClient\Kubernetes.WebSocket.cs" />
<Compile Include="..\KubernetesClient\WebSocketBuilder.cs" />
<Compile Include="..\KubernetesClient\WebSocketProtocol.cs" />
<Compile Include="..\KubernetesClient\Utilities.cs" />

<Compile Include="..\KubernetesClient\Exceptions\KubeConfigException.cs" />
<Compile Include="..\KubernetesClient\Exceptions\KubernetesClientException.cs" />

Expand Down
4 changes: 0 additions & 4 deletions src/KubernetesClient/Kubernetes.ConfigInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler
Initialize();
ValidateConfig(config);
CaCerts = config.SslCaCerts;
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
SkipTlsVerify = config.SkipTlsVerify;
#endif
CreateHttpClient(handlers, config);
InitializeFromConfig(config);
HttpClientTimeout = config.HttpClientTimeout;
Expand Down Expand Up @@ -102,11 +100,9 @@ private void InitializeFromConfig(KubernetesClientConfiguration config)

private X509Certificate2Collection CaCerts { get; }

#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
private X509Certificate2 ClientCert { get; }

private bool SkipTlsVerify { get; }
#endif

// NOTE: this method replicates the logic that the base ServiceClient uses except that it doesn't insert the RetryDelegatingHandler
// and it does insert the WatcherDelegatingHandler. we don't want the RetryDelegatingHandler because it has a very broad definition
Expand Down
8 changes: 8 additions & 0 deletions src/KubernetesClient/Kubernetes.WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ public Task<WebSocket> WebSocketNamespacedPodAttachAsync(string name, string @na
cancellationToken);
}

partial void BeforeRequest();
partial void AfterRequest();

protected async Task<WebSocket> StreamConnectAsync(Uri uri, string webSocketSubProtocol = null, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default)
{
if (uri == null)
Expand Down Expand Up @@ -280,6 +283,7 @@ protected async Task<WebSocket> StreamConnectAsync(Uri uri, string webSocketSubP
WebSocket webSocket = null;
try
{
BeforeRequest();
webSocket = await webSocketBuilder.BuildAndConnectAsync(uri, CancellationToken.None)
.ConfigureAwait(false);
}
Expand Down Expand Up @@ -335,6 +339,10 @@ protected async Task<WebSocket> StreamConnectAsync(Uri uri, string webSocketSubP
{
throw;
}
finally
{
AfterRequest();
}

return webSocket;
}
Expand Down
5 changes: 4 additions & 1 deletion src/KubernetesClient/WebSocketBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,23 @@ public virtual WebSocketBuilder AddClientCertificate(X509Certificate2 certificat

public WebSocketBuilder ExpectServerCertificate(X509Certificate2Collection serverCertificate)
{
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
Options.RemoteCertificateValidationCallback
= (sender, certificate, chain, sslPolicyErrors) =>
{
return Kubernetes.CertificateValidationCallBack(sender, serverCertificate, certificate, chain, sslPolicyErrors);
};

#endif
return this;
}

public WebSocketBuilder SkipServerCertificateValidation()
{
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
Options.RemoteCertificateValidationCallback
= (sender, certificate, chain, sslPolicyErrors) => true;

#endif
return this;
}

Expand Down

0 comments on commit 56646ae

Please sign in to comment.