Skip to content
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

Add bundle certificate support #253

Merged
merged 7 commits into from
Mar 11, 2019

Conversation

etchang
Copy link
Contributor

@etchang etchang commented Mar 7, 2019

Add support for cluster CA bundle certificates.

This changes the client to load potentially multiple certificates from the cluster's ca.crt file. When the client goes to validate an SSL connection, it will add these certs to the certificate chain provided by the server's response and validate that it can rebuild the chain. It will then make sure that the last certificate in the chain matches one of the certificates loaded from ca.crt to ensure the chain can be #trusted.

Fixes #234

@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 7, 2019
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Mar 7, 2019
/// <returns>x509 instance.</returns>
public static X509Certificate2 LoadPemFileCert(string file)
/// <returns>List of x509 instances.</returns>
public static IList<X509Certificate2> LoadPemFileCert(string file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be X509Certificate2Collection

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/KubernetesClient/CertUtils.cs Show resolved Hide resolved
//
var r = new Regex("-----BEGIN CERTIFICATE-----(.*?)-----END CERTIFICATE-----");

var matches = r.Matches(certdata);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to be new X509CertificateParser().ReadCertificates instead of building it ourselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely better. Only caveat is that we need to do some additional steps to convert the bouncy x509 certificates to .Net crypt x509 certs to store them in an X509Certificate2Collection, but that is trivial.

//
foreach (var caCert in caCerts)
{
chain.ChainPolicy.ExtraStore.Add(caCert);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AddRange

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/KubernetesClient/Kubernetes.ConfigInit.cs Show resolved Hide resolved
/// Checks
/// </summary>
[Fact]
public void LoadPemWithMultiCert()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is to ensure no regression when loading bundle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the test to make sure the certificates we've loaded match the original certificates we meant to load. Since the implementation of LoadPemFileCert has changed this test is a bit different than what you originally wrote. Let me know if there are any other preferred way to test this as the CertificateValidationTests now also include tests that verify that we can properly build certificates using the bundle.

Copy link
Member

@tg123 tg123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some comments overall LGTM

@@ -152,7 +154,7 @@ public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler
}
#endif

private X509Certificate2 CaCert { get; }
private X509Certificate2Collection CaCert { get; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private X509Certificate2Collection CaCert { get; }
private X509Certificate2Collection CaCerts { get; }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this across the project

@@ -58,7 +58,9 @@ public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler
throw new KubeConfigException("a CA must be set when SkipTlsVerify === false");
}

using (System.IO.MemoryStream certStream = new System.IO.MemoryStream(config.SslCaCert.RawData))
var caCert = CaCert[CaCert.Count - 1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could it support collection here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with the Java libraries, but I changed it to at least add all the certs in the bundle to the trusted certificate list.

@brendandburns
Copy link
Contributor

@tg123 I defer to your review, LGTM it when you think it's ready.

@tg123
Copy link
Member

tg123 commented Mar 11, 2019

/LGTM

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 11, 2019
@brendandburns
Copy link
Contributor

/lgtm
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brendandburns, etchang

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 11, 2019
@k8s-ci-robot k8s-ci-robot merged commit 9bdaf13 into kubernetes-client:master Mar 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants