Files
csharp/tests/KubernetesClient.Tests/CertificateValidationTests.cs

162 lines
6.2 KiB
C#
Raw Normal View History

using System;
using System.Security.Cryptography;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Xunit;
namespace k8s.Tests
{
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
using (var differentCA = CreateSelfSignedCA("CN=Different CA"))
using (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();
}
// 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 });
}
}
[Fact]
public void ValidCert()
{
var caCert = CertUtils.LoadPemFileCert("assets/ca.crt");
generate base on 1.34 (#1654) * Implement code changes to enhance functionality and improve performance * chore: update version to 18.0 in version.json * fix: correct initialization of opblackList in PluralHelper * chore: update SDK version to 18.0 in README.md * Fixdocfx (#82) * fix: update file references and clean up validation comments in models * chore: add symlink to CONTRIBUTING.md for easier access * fix: update documentation to include full type names for WebSocket and Predicate * fix: include CONTRIBUTING.md in docfx.json build content * refactor: update IKubernetes interface and template for consistency * feat: add Microsoft.CodeAnalysis.CSharp package and improve source normalization in generator context * chore: update package versions in Directory.Packages.props for compatibility and improvements * chore: update Microsoft.VisualStudio.SlnGen and Nerdbank.GitVersioning package versions for compatibility * Implement code changes to enhance functionality and improve performance * chore: update version to 18.0 in version.json * fix: correct initialization of opblackList in PluralHelper * chore: update SDK version to 18.0 in README.md * refactor: update IKubernetes interface and template for consistency * feat: add Microsoft.CodeAnalysis.CSharp package and improve source normalization in generator context * chore: update package versions in Directory.Packages.props for compatibility and improvements * chore: update Microsoft.VisualStudio.SlnGen and Nerdbank.GitVersioning package versions for compatibility * chore: downgrade xunit.runner.visualstudio and Xunit.StaFact package versions for compatibility * chore: update package versions in Directory.Packages.props for compatibility and improvements * style: format code for consistency and readability * feat: update certificate loading logic for .NET 9 compatibility * fix: update certificate loading method for .NET 9 compatibility
2025-09-22 14:18:16 -07:00
#if NET9_0_OR_GREATER
var testCert = X509CertificateLoader.LoadCertificateFromFile("assets/ca.crt");
#else
var testCert = new X509Certificate2("assets/ca.crt");
generate base on 1.34 (#1654) * Implement code changes to enhance functionality and improve performance * chore: update version to 18.0 in version.json * fix: correct initialization of opblackList in PluralHelper * chore: update SDK version to 18.0 in README.md * Fixdocfx (#82) * fix: update file references and clean up validation comments in models * chore: add symlink to CONTRIBUTING.md for easier access * fix: update documentation to include full type names for WebSocket and Predicate * fix: include CONTRIBUTING.md in docfx.json build content * refactor: update IKubernetes interface and template for consistency * feat: add Microsoft.CodeAnalysis.CSharp package and improve source normalization in generator context * chore: update package versions in Directory.Packages.props for compatibility and improvements * chore: update Microsoft.VisualStudio.SlnGen and Nerdbank.GitVersioning package versions for compatibility * Implement code changes to enhance functionality and improve performance * chore: update version to 18.0 in version.json * fix: correct initialization of opblackList in PluralHelper * chore: update SDK version to 18.0 in README.md * refactor: update IKubernetes interface and template for consistency * feat: add Microsoft.CodeAnalysis.CSharp package and improve source normalization in generator context * chore: update package versions in Directory.Packages.props for compatibility and improvements * chore: update Microsoft.VisualStudio.SlnGen and Nerdbank.GitVersioning package versions for compatibility * chore: downgrade xunit.runner.visualstudio and Xunit.StaFact package versions for compatibility * chore: update package versions in Directory.Packages.props for compatibility and improvements * style: format code for consistency and readability * feat: update certificate loading logic for .NET 9 compatibility * fix: update certificate loading method for .NET 9 compatibility
2025-09-22 14:18:16 -07:00
#endif
var chain = new X509Chain();
var errors = SslPolicyErrors.RemoteCertificateChainErrors;
var result = Kubernetes.CertificateValidationCallBack(this, caCert, testCert, chain, errors);
Assert.True(result);
}
[Fact]
public void InvalidCert()
{
var caCert = CertUtils.LoadPemFileCert("assets/ca.crt");
generate base on 1.34 (#1654) * Implement code changes to enhance functionality and improve performance * chore: update version to 18.0 in version.json * fix: correct initialization of opblackList in PluralHelper * chore: update SDK version to 18.0 in README.md * Fixdocfx (#82) * fix: update file references and clean up validation comments in models * chore: add symlink to CONTRIBUTING.md for easier access * fix: update documentation to include full type names for WebSocket and Predicate * fix: include CONTRIBUTING.md in docfx.json build content * refactor: update IKubernetes interface and template for consistency * feat: add Microsoft.CodeAnalysis.CSharp package and improve source normalization in generator context * chore: update package versions in Directory.Packages.props for compatibility and improvements * chore: update Microsoft.VisualStudio.SlnGen and Nerdbank.GitVersioning package versions for compatibility * Implement code changes to enhance functionality and improve performance * chore: update version to 18.0 in version.json * fix: correct initialization of opblackList in PluralHelper * chore: update SDK version to 18.0 in README.md * refactor: update IKubernetes interface and template for consistency * feat: add Microsoft.CodeAnalysis.CSharp package and improve source normalization in generator context * chore: update package versions in Directory.Packages.props for compatibility and improvements * chore: update Microsoft.VisualStudio.SlnGen and Nerdbank.GitVersioning package versions for compatibility * chore: downgrade xunit.runner.visualstudio and Xunit.StaFact package versions for compatibility * chore: update package versions in Directory.Packages.props for compatibility and improvements * style: format code for consistency and readability * feat: update certificate loading logic for .NET 9 compatibility * fix: update certificate loading method for .NET 9 compatibility
2025-09-22 14:18:16 -07:00
#if NET9_0_OR_GREATER
var testCert = X509CertificateLoader.LoadCertificateFromFile("assets/ca2.crt");
#else
var testCert = new X509Certificate2("assets/ca2.crt");
generate base on 1.34 (#1654) * Implement code changes to enhance functionality and improve performance * chore: update version to 18.0 in version.json * fix: correct initialization of opblackList in PluralHelper * chore: update SDK version to 18.0 in README.md * Fixdocfx (#82) * fix: update file references and clean up validation comments in models * chore: add symlink to CONTRIBUTING.md for easier access * fix: update documentation to include full type names for WebSocket and Predicate * fix: include CONTRIBUTING.md in docfx.json build content * refactor: update IKubernetes interface and template for consistency * feat: add Microsoft.CodeAnalysis.CSharp package and improve source normalization in generator context * chore: update package versions in Directory.Packages.props for compatibility and improvements * chore: update Microsoft.VisualStudio.SlnGen and Nerdbank.GitVersioning package versions for compatibility * Implement code changes to enhance functionality and improve performance * chore: update version to 18.0 in version.json * fix: correct initialization of opblackList in PluralHelper * chore: update SDK version to 18.0 in README.md * refactor: update IKubernetes interface and template for consistency * feat: add Microsoft.CodeAnalysis.CSharp package and improve source normalization in generator context * chore: update package versions in Directory.Packages.props for compatibility and improvements * chore: update Microsoft.VisualStudio.SlnGen and Nerdbank.GitVersioning package versions for compatibility * chore: downgrade xunit.runner.visualstudio and Xunit.StaFact package versions for compatibility * chore: update package versions in Directory.Packages.props for compatibility and improvements * style: format code for consistency and readability * feat: update certificate loading logic for .NET 9 compatibility * fix: update certificate loading method for .NET 9 compatibility
2025-09-22 14:18:16 -07:00
#endif
var chain = new X509Chain();
var errors = SslPolicyErrors.RemoteCertificateChainErrors;
var result = Kubernetes.CertificateValidationCallBack(this, caCert, testCert, chain, errors);
Assert.False(result);
}
[Fact]
public void ValidBundleCert()
{
var caCert = CertUtils.LoadPemFileCert("assets/ca-bundle.crt");
// Load the intermediate cert
//
var testCert = caCert[0];
var chain = new X509Chain();
var errors = SslPolicyErrors.RemoteCertificateChainErrors;
var result = Kubernetes.CertificateValidationCallBack(this, caCert, testCert, chain, errors);
Assert.True(result);
}
[Fact]
public void InvalidBundleCert()
{
var caCert = CertUtils.LoadPemFileCert("assets/ca-bundle.crt");
generate base on 1.34 (#1654) * Implement code changes to enhance functionality and improve performance * chore: update version to 18.0 in version.json * fix: correct initialization of opblackList in PluralHelper * chore: update SDK version to 18.0 in README.md * Fixdocfx (#82) * fix: update file references and clean up validation comments in models * chore: add symlink to CONTRIBUTING.md for easier access * fix: update documentation to include full type names for WebSocket and Predicate * fix: include CONTRIBUTING.md in docfx.json build content * refactor: update IKubernetes interface and template for consistency * feat: add Microsoft.CodeAnalysis.CSharp package and improve source normalization in generator context * chore: update package versions in Directory.Packages.props for compatibility and improvements * chore: update Microsoft.VisualStudio.SlnGen and Nerdbank.GitVersioning package versions for compatibility * Implement code changes to enhance functionality and improve performance * chore: update version to 18.0 in version.json * fix: correct initialization of opblackList in PluralHelper * chore: update SDK version to 18.0 in README.md * refactor: update IKubernetes interface and template for consistency * feat: add Microsoft.CodeAnalysis.CSharp package and improve source normalization in generator context * chore: update package versions in Directory.Packages.props for compatibility and improvements * chore: update Microsoft.VisualStudio.SlnGen and Nerdbank.GitVersioning package versions for compatibility * chore: downgrade xunit.runner.visualstudio and Xunit.StaFact package versions for compatibility * chore: update package versions in Directory.Packages.props for compatibility and improvements * style: format code for consistency and readability * feat: update certificate loading logic for .NET 9 compatibility * fix: update certificate loading method for .NET 9 compatibility
2025-09-22 14:18:16 -07:00
#if NET9_0_OR_GREATER
var testCert = X509CertificateLoader.LoadCertificateFromFile("assets/ca2.crt");
#else
var testCert = new X509Certificate2("assets/ca2.crt");
generate base on 1.34 (#1654) * Implement code changes to enhance functionality and improve performance * chore: update version to 18.0 in version.json * fix: correct initialization of opblackList in PluralHelper * chore: update SDK version to 18.0 in README.md * Fixdocfx (#82) * fix: update file references and clean up validation comments in models * chore: add symlink to CONTRIBUTING.md for easier access * fix: update documentation to include full type names for WebSocket and Predicate * fix: include CONTRIBUTING.md in docfx.json build content * refactor: update IKubernetes interface and template for consistency * feat: add Microsoft.CodeAnalysis.CSharp package and improve source normalization in generator context * chore: update package versions in Directory.Packages.props for compatibility and improvements * chore: update Microsoft.VisualStudio.SlnGen and Nerdbank.GitVersioning package versions for compatibility * Implement code changes to enhance functionality and improve performance * chore: update version to 18.0 in version.json * fix: correct initialization of opblackList in PluralHelper * chore: update SDK version to 18.0 in README.md * refactor: update IKubernetes interface and template for consistency * feat: add Microsoft.CodeAnalysis.CSharp package and improve source normalization in generator context * chore: update package versions in Directory.Packages.props for compatibility and improvements * chore: update Microsoft.VisualStudio.SlnGen and Nerdbank.GitVersioning package versions for compatibility * chore: downgrade xunit.runner.visualstudio and Xunit.StaFact package versions for compatibility * chore: update package versions in Directory.Packages.props for compatibility and improvements * style: format code for consistency and readability * feat: update certificate loading logic for .NET 9 compatibility * fix: update certificate loading method for .NET 9 compatibility
2025-09-22 14:18:16 -07:00
#endif
var chain = new X509Chain();
var errors = SslPolicyErrors.RemoteCertificateChainErrors;
var result = Kubernetes.CertificateValidationCallBack(this, caCert, testCert, chain, errors);
Assert.False(result);
}
[Fact]
public void ValidBundleWithMultipleCerts()
{
var caCert = CertUtils.LoadPemFileCert("assets/ca-bundle-correct.crt");
var testCert = caCert[0];
var chain = new X509Chain();
var errors = SslPolicyErrors.RemoteCertificateChainErrors;
var result = Kubernetes.CertificateValidationCallBack(this, caCert, testCert, chain, errors);
Assert.True(result);
}
[Fact]
public void InvalidBundleWithMultipleCerts()
{
var caCert = CertUtils.LoadPemFileCert("assets/ca-bundle-incorrect.crt");
var testCert = caCert[0];
var chain = new X509Chain();
var errors = SslPolicyErrors.RemoteCertificateChainErrors;
var result = Kubernetes.CertificateValidationCallBack(this, caCert, testCert, chain, errors);
Assert.False(result);
}
}
}