From 56646aeed3ee289146be9efda4d8da8756b8ac69 Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Tue, 28 Jun 2022 15:17:35 -0700 Subject: [PATCH] enable websocket on netstandard (#899) * enable websocket on netstandard * happy build --- .../Kubernetes.Websocket.Netstandard.cs | 28 +++++++++++++++++++ .../KubernetesClient.Classic.csproj | 12 ++++++++ src/KubernetesClient/Kubernetes.ConfigInit.cs | 4 --- src/KubernetesClient/Kubernetes.WebSocket.cs | 8 ++++++ src/KubernetesClient/WebSocketBuilder.cs | 5 +++- 5 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 src/KubernetesClient.Classic/Kubernetes.Websocket.Netstandard.cs diff --git a/src/KubernetesClient.Classic/Kubernetes.Websocket.Netstandard.cs b/src/KubernetesClient.Classic/Kubernetes.Websocket.Netstandard.cs new file mode 100644 index 00000000..5af6facc --- /dev/null +++ b/src/KubernetesClient.Classic/Kubernetes.Websocket.Netstandard.cs @@ -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); + } +} diff --git a/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj b/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj index b4041cd4..cb4ea4e0 100644 --- a/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj +++ b/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj @@ -28,6 +28,18 @@ + + + + + + + + + + + + diff --git a/src/KubernetesClient/Kubernetes.ConfigInit.cs b/src/KubernetesClient/Kubernetes.ConfigInit.cs index fa9ee76d..ee155e28 100644 --- a/src/KubernetesClient/Kubernetes.ConfigInit.cs +++ b/src/KubernetesClient/Kubernetes.ConfigInit.cs @@ -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; @@ -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 diff --git a/src/KubernetesClient/Kubernetes.WebSocket.cs b/src/KubernetesClient/Kubernetes.WebSocket.cs index 4114c4de..abcae21f 100644 --- a/src/KubernetesClient/Kubernetes.WebSocket.cs +++ b/src/KubernetesClient/Kubernetes.WebSocket.cs @@ -210,6 +210,9 @@ public Task WebSocketNamespacedPodAttachAsync(string name, string @na cancellationToken); } + partial void BeforeRequest(); + partial void AfterRequest(); + protected async Task StreamConnectAsync(Uri uri, string webSocketSubProtocol = null, Dictionary> customHeaders = null, CancellationToken cancellationToken = default) { if (uri == null) @@ -280,6 +283,7 @@ protected async Task StreamConnectAsync(Uri uri, string webSocketSubP WebSocket webSocket = null; try { + BeforeRequest(); webSocket = await webSocketBuilder.BuildAndConnectAsync(uri, CancellationToken.None) .ConfigureAwait(false); } @@ -335,6 +339,10 @@ protected async Task StreamConnectAsync(Uri uri, string webSocketSubP { throw; } + finally + { + AfterRequest(); + } return webSocket; } diff --git a/src/KubernetesClient/WebSocketBuilder.cs b/src/KubernetesClient/WebSocketBuilder.cs index a4809a21..053acf29 100644 --- a/src/KubernetesClient/WebSocketBuilder.cs +++ b/src/KubernetesClient/WebSocketBuilder.cs @@ -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; }