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

kubeadm: Validate only the first cert entry when external ca mode is used #123102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

astundzia
Copy link

@astundzia astundzia commented Feb 2, 2024

/sig cluster-lifecycle

What type of PR is this?

/kind bug

What this PR does / why we need it:

We are deploying kubernetes using kubeadm using an external ca cert file / external ca mode. This external ca cert file contains multiple certificates. When kubeadm runs in external CA mode, kubeadm runs validation logic that compares the entire existing ca certificate file ( /etc/kubernetes/pki/ca.crt) to the newly rendered 'dummy' kubeconfig that only reads in the first certificate from ca.crt. The bytes are then compared & fails the check since they are different -- 1 contains 1+N certificates, the other only contains 1 certificate.

The ca.crt used within validation is read here:
https://github.com/kubernetes/kubernetes/blob/master/cmd/kubeadm/app/util/pkiutil/pki_helpers.go#L292

The original ca.crt is read here:
https://github.com/kubernetes/kubernetes/blob/master/cmd/kubeadm/app/phases/kubeconfig/kubeconfig.go#L257

The check is happening here:
https://github.com/kubernetes/kubernetes/blob/master/cmd/kubeadm/app/phases/kubeconfig/kubeconfig.go#L268

Which issue(s) this PR fixes:

Fixes kubernetes/kubeadm#3011

Special notes for your reviewer:

Does this PR introduce a user-facing change?

YES

kubeadm: allow to validate "ca.crt" files that contain multiple certificates.

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. kind/bug Categorizes issue or PR as related to a bug. labels Feb 2, 2024
Copy link

linux-foundation-easycla bot commented Feb 2, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: astundzia / name: Alex Stundzia (074a980)

@k8s-ci-robot k8s-ci-robot added needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Feb 2, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @astundzia!

It looks like this is your first PR to kubernetes/kubernetes 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kubernetes has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Feb 2, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @astundzia. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: astundzia
Once this PR has been reviewed and has the lgtm label, please assign sataqiu for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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

@astundzia
Copy link
Author

Working on adding the necessary EasyCLA auth with my org

@astundzia
Copy link
Author

Tested on kubedm 1.23.X and master.

@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 Feb 3, 2024
@astundzia
Copy link
Author

EasyCLA completed

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Feb 3, 2024
@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 Feb 3, 2024
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 3, 2024
@neolit123
Copy link
Member

/triage accepted
/priority backlog

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. priority/backlog Higher priority than priority/awaiting-more-evidence. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Feb 5, 2024
@astundzia
Copy link
Author

Hey, thanks for the comments. I will address the comments & add a unit test this week. Thank you!

@neolit123
Copy link
Member

/release-note-edit

kubeadm: allow to validate "ca.crt" files that contain multiple certificates.

@neolit123
Copy link
Member

Hey, thanks for the comments. I will address the comments & add a unit test this week. Thank you!

@astundzia any updates?

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 17, 2024
@astundzia
Copy link
Author

Added unit test, addressed comments. Please review when you get a chance. Thanks!

Copy link
Member

@neolit123 neolit123 left a comment

Choose a reason for hiding this comment

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

added some more minor comments.
please keep the commit squashed to one.

a question that i have:

i recall discussions that taking the first cert from a bundle maybe is arbitrary under certain conditions, we are already doing that in at least another location in the code base. but would it make more sense to compare entire bundles i.e. bundleCurrentCert1 == bundleExpCert1, undleCurrentCert2 == bundleExpCert12, ...

what do official specs say about that?

i don't think we should apply the change like that, but asking out of curiosity.
comparing only the first cert ensure we are comparing the CA root, supposedly, while intermediates follow and they can be perhaps different count between the current and expected.

cmd/kubeadm/app/phases/kubeconfig/kubeconfig_test.go Outdated Show resolved Hide resolved
cmd/kubeadm/app/phases/kubeconfig/kubeconfig_test.go Outdated Show resolved Hide resolved
cmd/kubeadm/app/phases/kubeconfig/kubeconfig_test.go Outdated Show resolved Hide resolved
cmd/kubeadm/app/util/certs/util.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Feb 17, 2024
@astundzia
Copy link
Author

astundzia commented Feb 17, 2024

Hey, I will address comments and squash the commits. I added some additional tests to cover this scenario.

The Root Certificate Authority (Root CA) is the fundamental element of the trust model. In terms of certificate validation, it's possible to create a Certificate Authority (CA) bundle that doesn't include the Root CA. As long as the CA bundle and the leaf certificates can trace back to a common trust anchor (a CA), the certificate chain should be valid.

However, you're correct that I should compare more than just the first certificate. Let's consider this (convoluted) scenario:

We have a Certificate Authority chain that includes the Root CA, Intermediate CA1, and Intermediate CA2. At some point, we issue Intermediate CA3 from Intermediate CA1. In this case, we can have a ca.crt that includes Intermediate CA1 and Intermediate CA2. The trust chain must include all certificates tracing back to a common trust anchor, which in this case is Intermediate CA1. Certificates issued from Intermediate CA3 should validate against a bundle containing any of the following combinations: {Root CA, Intermediate CA1, Intermediate CA2}, {Root CA, Intermediate CA1}, or {Intermediate CA1}, as long as the trust chain can be resolved.

In this scenario, it would be beneficial to compare all the current ca.crt certificates against the first expected CA certificate. As long as there's a match between the first expected certificate & any of the certs in the current ca bundle, a trust chain should be established.

@astundzia
Copy link
Author

/retest

@astundzia
Copy link
Author

/retest

1 similar comment
@astundzia
Copy link
Author

/retest

Copy link
Member

@neolit123 neolit123 left a comment

Choose a reason for hiding this comment

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

thanks for the detailed explanation!
yes, i think that the extended validation against all current CAs makes sense.

the logic and new tests SGTM.
i did another pass over the code, found some minor prior nits and also some new ones.

i think i might request another reviewer to have a quick look, in case we missed something.

}
if !trustAnchorFound {
return errors.Errorf("a kubeconfig file %q exists already but could not find trust anchor", kubeConfigFilePath)
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
return errors.Errorf("a kubeconfig file %q exists already but could not find trust anchor", kubeConfigFilePath)
return errors.Errorf("a kubeconfig file %q exists but does not contain a common trust anchor CA in its current context's cluster. Total CA certificates found: %d", kubeConfigFilePath, len(currentCACerts))

trying to formulate an error message that is sufficiently descriptive for users, so that they can resolve potential problems without logging a ticket for us.

my idea is to cover a couple of potential mistakes:

  • not having the right context / cluster in the kubeconfig
  • not having the right CA embedded for a given cluster in the context / cluster.

please adjust if you think it can be worded better.

// Only use the first certificate in the expected CA cert list
expectedCACert := expectedCACerts[0]

// find a common trust anchor
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
// find a common trust anchor
// Find a common trust anchor

},
}

// creates CA, write to pkiDir and remove ca.key to get into external CA condition
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
// creates CA, write to pkiDir and remove ca.key to get into external CA condition
// Creates CA, write to pkiDir and remove ca.key to get into external CA mode

that's how we call it (mode) across docs
also capitalization

clusterName := "myOrg1"

// Create a kube config and assign the CA data to the CA bundle, issued from root CA
multipleCAConfigRootCAIssuer := setupdKubeConfigWithClientAuth(t, caCert, caKey, "https://1.2.3.4:1234", "test-cluster", clusterName)
Copy link
Member

Choose a reason for hiding this comment

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

a good opportunity to fix this typo?

Suggested change
multipleCAConfigRootCAIssuer := setupdKubeConfigWithClientAuth(t, caCert, caKey, "https://1.2.3.4:1234", "test-cluster", clusterName)
multipleCAConfigRootCAIssuer := setupKubeConfigWithClientAuth(t, caCert, caKey, "https://1.2.3.4:1234", "test-cluster", clusterName)

// creates CA, write to pkiDir and remove ca.key to get into external CA condition
caCert, caKey := certstestutil.SetupCertificateAuthority(t)

// create a config with a CA cert containing multiple certificates
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
// create a config with a CA cert containing multiple certificates
// Create a config with a CA cert containing multiple certificates

multipleCAConfigIntermediateCA2Issuer := setupdKubeConfigWithClientAuth(t, intermediateCACert2, intermediateCAKey2, "https://1.2.3.4:1234", "test-cluster", clusterName)
multipleCAConfigIntermediateCA2Issuer.Clusters[clusterName].CertificateAuthorityData = caCertBytes

// create a config with a CA cert containing multiple certificates, omitting the root CA
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
// create a config with a CA cert containing multiple certificates, omitting the root CA
// Create a config with a CA cert containing multiple certificates, omitting the root CA

trying to use consistent capitalization of comments within a function (or even file)

tests := map[string]struct {
filesToWrite map[string]*clientcmdapi.Config
initConfig *kubeadmapi.InitConfiguration
expectedError bool
Copy link
Member

Choose a reason for hiding this comment

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

all expected errors in this test are "false", i guess we should have at least one with "true"?

Config: certutil.Config{CommonName: "kubernetes intermediate CA"},
})
if err != nil {
t.Fatalf("failure while generating intermediate CA certificate and key: %v", err)
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
t.Fatalf("failure while generating intermediate CA certificate and key: %v", err)
t.Fatalf("Failure while generating intermediate CA certificate and key: %v", err)

test output messages should be capitalized unlike non-test errors

multipleCAConfigNoRootCA.Clusters[clusterName].CertificateAuthorityData = caCertBytesNoRootCA

if err := pkiutil.WriteCertBundle(pkiDir, kubeadmconstants.CACertAndKeyBaseName, caCertBundleNoRootCA); err != nil {
t.Fatalf("failure while saving CA certificate: %v", err)
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
t.Fatalf("failure while saving CA certificate: %v", err)
t.Fatalf("Failure while saving CA certificate: %v", err)

// Parse the expected certificate authority data
expectedCACerts, err := certutil.ParseCertsPEM(caExpected)
if err != nil {
return errors.Errorf("the expected base64 encoded CA cert %q could not be parsed as a PEM", caExpected)
Copy link
Member

@neolit123 neolit123 Feb 18, 2024

Choose a reason for hiding this comment

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

this would print the whole base64 string?
if so i think it should be at the end of the message:

Suggested change
return errors.Errorf("the expected base64 encoded CA cert %q could not be parsed as a PEM", caExpected)
return errors.Errorf("the expected base64 encoded CA cert could not be parsed as a PEM:\n%s\n", caExpected)

generally users should not see this error as it would be a preparation error on our side - i.e. expected kubeconfig file was malformed. i think we already have tests for that.

@astundzia
Copy link
Author

Hey, will get the comments addressed and a commit up in the next few days.

@neolit123
Copy link
Member

Hey, will get the comments addressed and a commit up in the next few days.

ok, note that code freeze for 1.30 is march 6th.

@neolit123
Copy link
Member

Hey, will get the comments addressed and a commit up in the next few days.

@astundzia do you have plans to finish this PR?

@astundzia
Copy link
Author

Hey, apologies for the lack of commits here. I will try to get this merged in this week!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubeadm cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When using a external rootCA that contains multiple certificates, kubeadm will fail validation.
3 participants