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

fix panic with SIGSEGV in kubeadm certs check-expiration #124124

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/kubeadm/app/phases/certs/renewal/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (rm *Manager) CertificateExists(name string) (bool, error) {
return false, errors.Errorf("%s is not a known certificate", name)
}

return handler.readwriter.Exists(), nil
return handler.readwriter.Exists()
}

// GetCertificateExpirationInfo returns certificate expiration info.
Expand Down Expand Up @@ -358,7 +358,7 @@ func (rm *Manager) CAExists(name string) (bool, error) {
return false, errors.Errorf("%s is not a known certificate", name)
}

return handler.readwriter.Exists(), nil
return handler.readwriter.Exists()
}

// GetCAExpirationInfo returns CA expiration info.
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubeadm/app/phases/certs/renewal/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ type fakecertificateReadWriter struct {
cert *x509.Certificate
}

func (cr fakecertificateReadWriter) Exists() bool {
return cr.exist
func (cr fakecertificateReadWriter) Exists() (bool, error) {
return cr.exist, nil
}

func (cr fakecertificateReadWriter) Read() (*x509.Certificate, error) {
Expand Down
17 changes: 10 additions & 7 deletions cmd/kubeadm/app/phases/certs/renewal/readwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
// read or write a certificate stored/embedded in a file
type certificateReadWriter interface {
//Exists return true if the certificate exists
Exists() bool
Exists() (bool, error)

// Read a certificate stored/embedded in a file
Read() (*x509.Certificate, error)
Expand All @@ -61,17 +61,20 @@ func newPKICertificateReadWriter(certificateDir string, baseName string) *pkiCer
}

// Exists checks if a certificate exist
func (rw *pkiCertificateReadWriter) Exists() bool {
func (rw *pkiCertificateReadWriter) Exists() (bool, error) {
certificatePath, _ := pkiutil.PathsForCertAndKey(rw.certificateDir, rw.baseName)
return fileExists(certificatePath)
}

func fileExists(filename string) bool {
func fileExists(filename string) (bool, error) {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return !info.IsDir()
return !info.IsDir(), nil
}

// Read a certificate from a file the K8s pki managed by kubeadm
Expand Down Expand Up @@ -120,7 +123,7 @@ func newKubeconfigReadWriter(kubernetesDir string, kubeConfigFileName string, ce
}

// Exists checks if a certificate embedded in kubeConfig file exists
func (rw *kubeConfigReadWriter) Exists() bool {
func (rw *kubeConfigReadWriter) Exists() (bool, error) {
return fileExists(rw.kubeConfigFilePath)
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/kubeadm/app/phases/certs/renewal/readwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func TestFileExists(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := fileExists(tt.filename); got != tt.want {
if got, _ := fileExists(tt.filename); got != tt.want {
Copy link
Member

Choose a reason for hiding this comment

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

after this change there is a missing unit test case for the return false, err return of func fileExists(filename string) (bool, error) { but i don't insist of adding it.

we could use os.Chmod() or a os.WriteFile with a restrictive FileMode, but then this test will not work on Windows, i think.

so fine to leave this test case out for now.

t.Errorf("fileExists() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -295,7 +295,7 @@ func TestPKICertificateReadWriterExists(t *testing.T) {
baseName: tt.fields.baseName,
certificateDir: tt.fields.certificateDir,
}
if got := rw.Exists(); got != tt.want {
if got, _ := rw.Exists(); got != tt.want {
t.Errorf("pkiCertificateReadWriter.Exists() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -338,7 +338,7 @@ func TestKubeConfigReadWriterExists(t *testing.T) {
rw := &kubeConfigReadWriter{
kubeConfigFilePath: tt.kubeConfigFilePath,
}
if got := rw.Exists(); got != tt.want {
if got, _ := rw.Exists(); got != tt.want {
t.Errorf("kubeConfigReadWriter.Exists() = %v, want %v", got, tt.want)
}
})
Expand Down