-
Notifications
You must be signed in to change notification settings - Fork 310
Add ServerCertificateCustomValidationCallback for granular TLS validation control #1710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
d1fd6c8
0308f53
990bbdd
fe6d939
d6f2169
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -259,12 +259,22 @@ protected async Task<WebSocket> StreamConnectAsync(Uri uri, string webSocketSubP | |
| } | ||
| } | ||
|
|
||
| if (this.CaCerts != null) | ||
| // Custom validation callback takes precedence | ||
| if (this.ServerCertificateCustomValidationCallback != null) | ||
| { | ||
| webSocketBuilder.SetServerCertificateCustomValidationCallback( | ||
| (sender, certificate, chain, sslPolicyErrors) => | ||
| { | ||
| // Convert to the expected signature (with HttpRequestMessage as first parameter) | ||
| var cert = certificate as X509Certificate2 ?? new X509Certificate2(certificate); | ||
|
||
| return this.ServerCertificateCustomValidationCallback(null, cert, chain, sslPolicyErrors); | ||
| }); | ||
| } | ||
| else if (this.CaCerts != null) | ||
| { | ||
| webSocketBuilder.ExpectServerCertificate(this.CaCerts); | ||
| } | ||
|
|
||
| if (this.SkipTlsVerify) | ||
| else if (this.SkipTlsVerify) | ||
| { | ||
| webSocketBuilder.SkipServerCertificateValidation(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||
| using k8s.Authentication; | ||||||
| using System.Net.Http; | ||||||
| using System.Net.Security; | ||||||
| using System.Security.Cryptography.X509Certificates; | ||||||
|
|
||||||
| namespace k8s | ||||||
|
|
@@ -56,6 +57,34 @@ public partial class KubernetesClientConfiguration | |||||
| /// </summary> | ||||||
| public bool SkipTlsVerify { get; set; } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Gets or sets a custom server certificate validation callback. | ||||||
| /// This allows fine-grained control over certificate validation, such as disabling | ||||||
| /// revocation checks while maintaining other security validations. | ||||||
| /// Takes precedence over <see cref="SkipTlsVerify"/> when set. | ||||||
| /// </summary> | ||||||
| /// <remarks> | ||||||
| /// <para> | ||||||
| /// The callback signature is: <c>Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool></c>. | ||||||
| /// Note that the HttpRequestMessage parameter may be null in some contexts (e.g., WebSocket connections). | ||||||
| /// </para> | ||||||
| /// <para> | ||||||
| /// Example usage to disable revocation checking: | ||||||
| /// </para> | ||||||
| /// <code> | ||||||
| /// config.ServerCertificateCustomValidationCallback = (request, cert, chain, errors) => | ||||||
| /// { | ||||||
| /// if (errors == SslPolicyErrors.None) | ||||||
| /// return true; | ||||||
| /// | ||||||
| /// // Disable revocation checking | ||||||
| /// chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; | ||||||
| /// return chain.Build((X509Certificate2)cert); | ||||||
|
||||||
| /// return chain.Build((X509Certificate2)cert); | |
| /// return chain.Build(cert); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,140 @@ | ||||||
| using System.Net.Security; | ||||||
| using System.Security.Cryptography.X509Certificates; | ||||||
| using Xunit; | ||||||
|
|
||||||
| namespace k8s.Tests | ||||||
| { | ||||||
| public class CustomCertificateValidationTests | ||||||
| { | ||||||
| [Fact] | ||||||
| public void CustomValidationCallbackShouldBeUsedWhenSet() | ||||||
| { | ||||||
| // Arrange | ||||||
| var config = new KubernetesClientConfiguration | ||||||
| { | ||||||
| Host = "https://test.example.com", | ||||||
| ServerCertificateCustomValidationCallback = (request, cert, chain, errors) => | ||||||
| { | ||||||
| return true; | ||||||
| }, | ||||||
| }; | ||||||
|
|
||||||
| // Act | ||||||
| var client = new Kubernetes(config); | ||||||
|
||||||
|
|
||||||
| // Assert - verify the callback was set | ||||||
| Assert.NotNull(config.ServerCertificateCustomValidationCallback); | ||||||
| } | ||||||
|
|
||||||
| [Fact] | ||||||
| public void CustomValidationCallbackTakesPrecedenceOverSkipTlsVerify() | ||||||
| { | ||||||
| // Arrange | ||||||
| var config = new KubernetesClientConfiguration | ||||||
| { | ||||||
| Host = "https://test.example.com", | ||||||
| SkipTlsVerify = true, | ||||||
| ServerCertificateCustomValidationCallback = (request, cert, chain, errors) => | ||||||
| { | ||||||
| return false; // Custom callback returns false | ||||||
| }, | ||||||
| }; | ||||||
|
|
||||||
| // Act | ||||||
| var client = new Kubernetes(config); | ||||||
|
||||||
|
|
||||||
| // Assert - The custom callback should be set, not the skip all validation | ||||||
| Assert.NotNull(config.ServerCertificateCustomValidationCallback); | ||||||
| Assert.True(config.SkipTlsVerify); // SkipTlsVerify should still be true in config | ||||||
| } | ||||||
|
|
||||||
| [Fact] | ||||||
| public void CustomValidationCallbackCanDisableRevocationCheck() | ||||||
| { | ||||||
| // Arrange | ||||||
| var config = new KubernetesClientConfiguration | ||||||
| { | ||||||
| Host = "https://test.example.com", | ||||||
| ServerCertificateCustomValidationCallback = (request, cert, chain, errors) => | ||||||
| { | ||||||
| // Example: Disable revocation checking | ||||||
| if (errors == SslPolicyErrors.None) | ||||||
| { | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| // Disable revocation checking | ||||||
| chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; | ||||||
| return chain.Build((X509Certificate2)cert); | ||||||
|
||||||
| return chain.Build((X509Certificate2)cert); | |
| return chain.Build(cert); |
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assignment to client is useless, since its value is never read.
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assignment to client is useless, since its value is never read.
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assignment to client is useless, since its value is never read.
Copilot
AI
Dec 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assignment to client is useless, since its value is never read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating a new X509Certificate2 from an existing certificate can lead to resource leaks since the newly created certificate is not disposed. Consider using a cast with null-coalescing instead: 'var cert = certificate as X509Certificate2 ?? throw new ArgumentException("Certificate must be X509Certificate2");' or document that callers should ensure proper disposal if needed.