-
Notifications
You must be signed in to change notification settings - Fork 307
Update certificate validation to use custom trust store for .NET 5.0+ #1653
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
Changes from 1 commit
65d84f0
33b5ef2
3739514
27a95f0
f21f787
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
using System; | ||
using System.Security.Cryptography; | ||
using System.Net.Security; | ||
using System.Security.Cryptography.X509Certificates; | ||
using Xunit; | ||
|
@@ -6,11 +8,66 @@ | |
{ | ||
public class CertificateValidationTests | ||
{ | ||
[Fact] | ||
public void ShouldRejectCertFromDifferentCA() | ||
{ | ||
// Load our "trusted" Kubernetes CA | ||
var trustedCaCert = CertUtils.LoadPemFileCert("assets/ca.crt"); | ||
|
||
// Generate a completely different CA and server cert in memory | ||
var differentCA = CreateSelfSignedCA("CN=Different CA"); | ||
var untrustedServerCert = CreateServerCert(differentCA, "CN=fake-server.com"); | ||
|
||
var chain = new X509Chain(); | ||
|
||
// Pre-populate the chain like SSL validation would do | ||
// This will likely succeed because we allow unknown CAs in the validation | ||
chain.Build(untrustedServerCert); | ||
|
||
var errors = SslPolicyErrors.RemoteCertificateChainErrors; | ||
|
||
var result = Kubernetes.CertificateValidationCallBack(this, trustedCaCert, untrustedServerCert, chain, errors); | ||
|
||
// This SHOULD be false because the server cert wasn't signed by our trusted CA | ||
// But the current K8s validation logic might incorrectly return true | ||
Assert.False(result, "Should reject certificates not signed by trusted CA"); | ||
|
||
// Cleanup | ||
differentCA.Dispose(); | ||
untrustedServerCert.Dispose(); | ||
tg123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
// Helper methods to create test certificates | ||
private static X509Certificate2 CreateSelfSignedCA(string subject) | ||
{ | ||
using (var rsa = RSA.Create(2048)) | ||
{ | ||
var req = new CertificateRequest(subject, rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); | ||
req.CertificateExtensions.Add(new X509BasicConstraintsExtension(true, false, 0, true)); | ||
req.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.KeyCertSign | X509KeyUsageFlags.CrlSign, true)); | ||
|
||
return req.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(365)); | ||
} | ||
} | ||
|
||
private static X509Certificate2 CreateServerCert(X509Certificate2 issuerCA, string subject) | ||
{ | ||
using (var rsa = RSA.Create(2048)) | ||
{ | ||
var req = new CertificateRequest(subject, rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); | ||
req.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, true)); | ||
req.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.KeyEncipherment, true)); | ||
req.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(new OidCollection { new Oid("1.3.6.1.5.5.7.3.1") }, true)); | ||
|
||
return req.Create(issuerCA, DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(90), new byte[] { 1, 2, 3, 4 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The serial number byte array Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
} | ||
} | ||
|
||
[Fact] | ||
public void ValidCert() | ||
{ | ||
var caCert = CertUtils.LoadPemFileCert("assets/ca.crt"); | ||
var testCert = new X509Certificate2("assets/ca.crt"); | ||
Check warning on line 70 in tests/KubernetesClient.Tests/CertificateValidationTests.cs
|
||
var chain = new X509Chain(); | ||
var errors = SslPolicyErrors.RemoteCertificateChainErrors; | ||
|
||
|
@@ -23,7 +80,7 @@ | |
public void InvalidCert() | ||
{ | ||
var caCert = CertUtils.LoadPemFileCert("assets/ca.crt"); | ||
var testCert = new X509Certificate2("assets/ca2.crt"); | ||
Check warning on line 83 in tests/KubernetesClient.Tests/CertificateValidationTests.cs
|
||
var chain = new X509Chain(); | ||
var errors = SslPolicyErrors.RemoteCertificateChainErrors; | ||
|
||
|
@@ -52,7 +109,7 @@ | |
public void InvalidBundleCert() | ||
{ | ||
var caCert = CertUtils.LoadPemFileCert("assets/ca-bundle.crt"); | ||
var testCert = new X509Certificate2("assets/ca2.crt"); | ||
Check warning on line 112 in tests/KubernetesClient.Tests/CertificateValidationTests.cs
|
||
var chain = new X509Chain(); | ||
var errors = SslPolicyErrors.RemoteCertificateChainErrors; | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.