Skip to content

Commit

Permalink
Updated CertificateValidationCallBack to build the CaCert bundle with…
Browse files Browse the repository at this point in the history
… respect to the rootChain certs to verify that they are correct. (kubernetes-client#860)

* Updated CertificateValidationCallBack to build the CaCert bundle with respect to the rootChain certs to verify that they are correct.

* Added a test cases and assets for when all the certs files are in the same file.

* Removed the hardcoded first cert as the chosen root cert
  • Loading branch information
KLazarov authored May 24, 2022
1 parent d8e2236 commit 877587e
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/KubernetesClient/Kubernetes.ConfigInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,11 @@ public static bool CertificateValidationCallBack(

var isTrusted = false;

var rootCert = chain.ChainElements[chain.ChainElements.Count - 1].Certificate;

// Make sure that one of our trusted certs exists in the chain provided by the server.
//
foreach (var cert in caCerts)
{
if (rootCert.RawData.SequenceEqual(cert.RawData))
if (chain.Build(cert))
{
isTrusted = true;
break;
Expand Down
27 changes: 27 additions & 0 deletions tests/KubernetesClient.Tests/CertificateValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,32 @@ public void InvalidBundleCert()

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);
}
}
}
56 changes: 56 additions & 0 deletions tests/KubernetesClient.Tests/assets/ca-bundle-correct.crt
Original file line number Diff line number Diff line change
@@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BE2AX0wdpY0UeXsNDyzUf/2WF
fHY4DnuBdeVdHtl1yPlXmQkMoQM=
-----END CERTIFICATE-----
56 changes: 56 additions & 0 deletions tests/KubernetesClient.Tests/assets/ca-bundle-incorrect.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
-----BEGIN CERTIFICATE-----
MIIC3zCCAcegAwIBAgIQWNOfSGBRn4EUcsj7E1UN8zANBgkqhkiG9w0BAQsFADAZ
MRcwFQYDVQQKEw5EYXZpZCBPcmJlbGlhbjAeFw0xODA2MDgxMjI2MDBaFw0yMTA1
MjMxMjI2MDBaMBkxFzAVBgNVBAoTDkRhdmlkIE9yYmVsaWFuMIIBIjANBgkqhkiG
9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnXGK1ZHqF4fhO3WOtlo5kqVYHHYTasNmzbQh
MJ0IHiFrCVNi6apohleHi0IlzVFCQY5+yab2Lz7J2qcadRVWLlfhskMx4hbSD+eX
H9MDcnV1k4AyFz+9I+dL4rb5DPcK9vNQF0KXtdpaq4qVs+IoRR4Ck00yvzLmOMTs
YvFVjW6XgKPR+y89y8iykW2puiJ/y6DLKlP+2HDGGEI07C+4Tkxps6uRkPz6ySVb
6mhJ6P/+8WmuMc0Ur1kNgA0GEUTFYlRNuF0nNjBvncGBUwOWAUNbsYQgElaqXJKe
XZ6M44+oBvRsCsnf7j3hfKti4u/Qy9nDejJ/15R6I6A5JdYOxwIDAQABoyMwITAO
BgNVHQ8BAf8EBAMCAqwwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOC
AQEAU2Rp4T7iWomEsCC8nrQPXh/6AlVnfb/vhC7aCq+g6CF+LvksfM3Uj+JLQ5rM
QNavSXowqe11vNb1Qu7LcQT5ff76XEoK0dKA8uMs60wUkHttfPzXM522rdv+i8EF
QwVirN85W5i2q669MQ2BeJ37gQ6vQAOLvHXTuspDo1qrfT3zkeGiLEXRM4k4d6OT
BnZNYvfdTTZX7OlvHfw5hdcRtoOTBmTA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