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

🐛 Checking cert's keypair for nil before accessing to avoid panics #10321

Merged
merged 2 commits into from
Apr 3, 2024

Conversation

makhov
Copy link
Contributor

@makhov makhov commented Mar 26, 2024

What this PR does / why we need it:

In some circumstances AsFiles() can panic. The PR adds additional nil check for c.KeyPair to avoid it.

2024-02-02T09:15:35Z	INFO	Observed a panic in reconciler: runtime error: invalid memory address or nil pointer dereference	{"controller": "k0scontrollerconfig", "controllerGroup": "bootstrap.cluster.x-k8s.io", "controllerKind": "K0sControllerConfig", "K0sControllerConfig": {"name":"bar-2-1","namespace":"default"}, "namespace": "default", "name": "bar-2-1", "reconcileID": "8a558d44-7dcf-406e-a7e0-e47c895189f6"}
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x16ca8fd]

goroutine 270 [running]:
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Reconcile.func1()
	/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.16.3/pkg/internal/controller/controller.go:116 +0x1e5
panic({0x1a37520?, 0x2e3bb30?})
	/usr/local/go/src/runtime/panic.go:914 +0x21f
sigs.k8s.io/cluster-api/util/secret.(*Certificate).AsFiles(...)
	/go/pkg/mod/sigs.k8s.io/cluster-api@v1.5.3/util/secret/certificates.go:374
sigs.k8s.io/cluster-api/util/secret.Certificates.AsFiles({0xc00035ffb0, 0x4, 0xc0002839b0?})
	/go/pkg/mod/sigs.k8s.io/cluster-api@v1.5.3/util/secret/certificates.go:419 +0xbd
github.com/k0sproject/k0smotron/internal/controller/bootstrap.(*ControlPlaneController).getCerts(0xc000144300, {0x1f94308, 0xc00043cb40}, 0xc000775be0)
	/workspace/internal/controller/bootstrap/controlplane_bootstrap_controller.go:415 +0x1d0
github.com/k0sproject/k0smotron/internal/controller/bootstrap.(*ControlPlaneController).genControlPlaneJoinFiles(0xc000144300, {0x1f94308, 0xc00043cb40}, 0xc000775be0, 0x192b7c0?, {0xc000801fb0, 0x1, 0x1})
	/workspace/internal/controller/bootstrap/controlplane_bootstrap_controller.go:279 +0x19f
github.com/k0sproject/k0smotron/internal/controller/bootstrap.(*ControlPlaneController).Reconcile(0xc000144300, {0x1f94308, 0xc00043cb40}, {{{0xc0006097d0, 0x7}, {0xc0006097c6, 0x7}}})
	/workspace/internal/controller/bootstrap/controlplane_bootstrap_controller.go:180 +0xf7f
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Reconcile(0x1f97e00?, {0x1f94308?, 0xc00043cb40?}, {{{0xc0006097d0?, 0xb?}, {0xc0006097c6?, 0x0?}}})
	/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.16.3/pkg/internal/controller/controller.go:119 +0xb7
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler(0xc0002bedc0, {0x1f94340, 0xc0004afa40}, {0x1afb900?, 0xc0003a25c0?})

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):

Fixes k0sproject/k0smotron#438

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-area PR is missing an area label labels Mar 26, 2024
@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Mar 26, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @makhov. Thanks for your PR.

I'm waiting for a kubernetes-sigs 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 k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Mar 26, 2024
@fabriziopandini
Copy link
Member

/ok-to-test

For sake of documentation, could you clarify when c.KeyPair could be nil?

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 27, 2024
@makhov
Copy link
Contributor Author

makhov commented Mar 27, 2024

We do

certificates := secret.NewCertificatesForInitialControlPlane(config)
err := certificates.Lookup(ctx, client, util.ObjectKey(cluster))
if err != nil {
	return err
}
for _, cert := range certificates.AsFiles() {
   ....
}

If the secret doesn't exist (eg, was removed manually), certificates.Lookup doesn't return an error, but cert.KeyPair is nil.
It might be not the best way of doing things, but I guess, it shouldn't panic anyway.

@fabriziopandini
Copy link
Member

Thanks for the explanation, let's add a test case for this scenario, then ok for me

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Apr 3, 2024
@fabriziopandini fabriziopandini added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Apr 3, 2024
@fabriziopandini
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 3, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 78b7fd9f559d0ba2761b693239ab9565b5bb0f15

Copy link
Member

@chrischdi chrischdi left a comment

Choose a reason for hiding this comment

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

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: chrischdi

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 Apr 3, 2024
@fabriziopandini
Copy link
Member

/area util

@k8s-ci-robot k8s-ci-robot added area/util Issues or PRs related to utils and removed do-not-merge/needs-area PR is missing an area label labels Apr 3, 2024
@k8s-ci-robot k8s-ci-robot merged commit a9508b2 into kubernetes-sigs:main Apr 3, 2024
23 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.8 milestone Apr 3, 2024
@sbueringer
Copy link
Member

Makes sense

/cherry-pick release-1.7

Let us know if we should cherry-pick it further back

@k8s-infra-cherrypick-robot

@sbueringer: new pull request created: #10368

In response to this:

Makes sense

/cherry-pick release-1.7

Let us know if we should cherry-pick it further back

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.

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. area/util Issues or PRs related to utils 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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Panic in the cluster-api/util/secret/certificates.go when certs are missing
6 participants