I want to make a call to Kubernetes API from .NET Core app outside the cluster.
I have an HttpClient with an HttpClientHandler where I set this callback to ignore invalid (untrusted) certificates and it works:
handler.ServerCertificateCustomValidationCallback +=
(message, certificate, chain, errors) => true;
But in my kubeconfig from kubectl I have this:
...
clusters:
- cluster:
certificate-authority-data: SOME_AUTHORITY_DATA
server: https://myserver.io:443
...
How can I validate server certificate using that certificate-authority-data in my application?
private static byte[] s_issuingCABytes = { ... };
handler.ServerCertificateCustomValidationCallback +=
(message, certificate, chain, errors) =>
{
const SslPolicyErrors Mask =
#if CA_IS_TRUSTED
~SslPolicyErrors.None;
#else
~SslPolicyErrors.RemoteCertificateChainErrors;
#endif
// If a cert is not present, or it didn't match the host.
// (And if the CA should have been root trusted anyways, also checks that)
if ((errors & Mask) != SslPolicyErrors.None)
{
return false;
}
foreach (X509ChainElement element in chain.ChainElements)
{
if (element.Certificate.RawData.SequenceEqual(s_issuingCABytes))
{
// The expected certificate was found, huzzah!
return true;
}
}
// The expected cert was not in the chain.
return false;
};