fix panic with SIGSEGV in kubeadm certs check-expiration
This commit is contained in:
		@@ -322,7 +322,7 @@ func (rm *Manager) CertificateExists(name string) (bool, error) {
 | 
				
			|||||||
		return false, errors.Errorf("%s is not a known certificate", name)
 | 
							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.
 | 
					// GetCertificateExpirationInfo returns certificate expiration info.
 | 
				
			||||||
@@ -358,7 +358,7 @@ func (rm *Manager) CAExists(name string) (bool, error) {
 | 
				
			|||||||
		return false, errors.Errorf("%s is not a known certificate", name)
 | 
							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.
 | 
					// GetCAExpirationInfo returns CA expiration info.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -54,8 +54,8 @@ type fakecertificateReadWriter struct {
 | 
				
			|||||||
	cert  *x509.Certificate
 | 
						cert  *x509.Certificate
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (cr fakecertificateReadWriter) Exists() bool {
 | 
					func (cr fakecertificateReadWriter) Exists() (bool, error) {
 | 
				
			||||||
	return cr.exist
 | 
						return cr.exist, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (cr fakecertificateReadWriter) Read() (*x509.Certificate, error) {
 | 
					func (cr fakecertificateReadWriter) Read() (*x509.Certificate, error) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,7 +36,7 @@ import (
 | 
				
			|||||||
// read or write a certificate stored/embedded in a file
 | 
					// read or write a certificate stored/embedded in a file
 | 
				
			||||||
type certificateReadWriter interface {
 | 
					type certificateReadWriter interface {
 | 
				
			||||||
	//Exists return true if the certificate exists
 | 
						//Exists return true if the certificate exists
 | 
				
			||||||
	Exists() bool
 | 
						Exists() (bool, error)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Read a certificate stored/embedded in a file
 | 
						// Read a certificate stored/embedded in a file
 | 
				
			||||||
	Read() (*x509.Certificate, error)
 | 
						Read() (*x509.Certificate, error)
 | 
				
			||||||
@@ -61,17 +61,20 @@ func newPKICertificateReadWriter(certificateDir string, baseName string) *pkiCer
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Exists checks if a certificate exist
 | 
					// Exists checks if a certificate exist
 | 
				
			||||||
func (rw *pkiCertificateReadWriter) Exists() bool {
 | 
					func (rw *pkiCertificateReadWriter) Exists() (bool, error) {
 | 
				
			||||||
	certificatePath, _ := pkiutil.PathsForCertAndKey(rw.certificateDir, rw.baseName)
 | 
						certificatePath, _ := pkiutil.PathsForCertAndKey(rw.certificateDir, rw.baseName)
 | 
				
			||||||
	return fileExists(certificatePath)
 | 
						return fileExists(certificatePath)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func fileExists(filename string) bool {
 | 
					func fileExists(filename string) (bool, error) {
 | 
				
			||||||
	info, err := os.Stat(filename)
 | 
						info, err := os.Stat(filename)
 | 
				
			||||||
	if os.IsNotExist(err) {
 | 
						if err != nil {
 | 
				
			||||||
		return false
 | 
							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
 | 
					// Read a certificate from a file the K8s pki managed by kubeadm
 | 
				
			||||||
@@ -120,7 +123,7 @@ func newKubeconfigReadWriter(kubernetesDir string, kubeConfigFileName string, ce
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Exists checks if a certificate embedded in kubeConfig file exists
 | 
					// 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)
 | 
						return fileExists(rw.kubeConfigFilePath)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -239,7 +239,7 @@ func TestFileExists(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	for _, tt := range tests {
 | 
						for _, tt := range tests {
 | 
				
			||||||
		t.Run(tt.name, func(t *testing.T) {
 | 
							t.Run(tt.name, func(t *testing.T) {
 | 
				
			||||||
			if got := fileExists(tt.filename); got != tt.want {
 | 
								if got, _ := fileExists(tt.filename); got != tt.want {
 | 
				
			||||||
				t.Errorf("fileExists() = %v, want %v", got, tt.want)
 | 
									t.Errorf("fileExists() = %v, want %v", got, tt.want)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
@@ -295,7 +295,7 @@ func TestPKICertificateReadWriterExists(t *testing.T) {
 | 
				
			|||||||
				baseName:       tt.fields.baseName,
 | 
									baseName:       tt.fields.baseName,
 | 
				
			||||||
				certificateDir: tt.fields.certificateDir,
 | 
									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)
 | 
									t.Errorf("pkiCertificateReadWriter.Exists() = %v, want %v", got, tt.want)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
@@ -338,7 +338,7 @@ func TestKubeConfigReadWriterExists(t *testing.T) {
 | 
				
			|||||||
			rw := &kubeConfigReadWriter{
 | 
								rw := &kubeConfigReadWriter{
 | 
				
			||||||
				kubeConfigFilePath: tt.kubeConfigFilePath,
 | 
									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)
 | 
									t.Errorf("kubeConfigReadWriter.Exists() = %v, want %v", got, tt.want)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user