Skip to content

Commit e008523

Browse files
authored
Merge pull request kubernetes#85092 from fabriziopandini/alpha-certs-skips-missing-certs
kubeadm: alpha certs should skip missing files
2 parents 01e014c + 7d986a9 commit e008523

File tree

3 files changed

+85
-19
lines changed

3 files changed

+85
-19
lines changed

cmd/kubeadm/app/cmd/alpha/certs.go

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ func renewCert(flags *renewFlags, kdir string, handler *renewal.CertificateRenew
206206
return err
207207
}
208208

209+
if ok, _ := rm.CertificateExists(handler.Name); !ok {
210+
fmt.Printf("MISSING! %s\n", handler.LongName)
211+
return nil
212+
}
213+
209214
// if the renewal operation is set to generate CSR request only
210215
if flags.csrOnly {
211216
// checks a path for storing CSR request is given
@@ -282,36 +287,54 @@ func newCmdCertsExpiration(out io.Writer, kdir string) *cobra.Command {
282287
w := tabwriter.NewWriter(out, 10, 4, 3, ' ', 0)
283288
fmt.Fprintln(w, "CERTIFICATE\tEXPIRES\tRESIDUAL TIME\tCERTIFICATE AUTHORITY\tEXTERNALLY MANAGED")
284289
for _, handler := range rm.Certificates() {
285-
e, err := rm.GetCertificateExpirationInfo(handler.Name)
286-
if err != nil {
287-
return err
290+
if ok, _ := rm.CertificateExists(handler.Name); ok {
291+
e, err := rm.GetCertificateExpirationInfo(handler.Name)
292+
if err != nil {
293+
return err
294+
}
295+
296+
s := fmt.Sprintf("%s\t%s\t%s\t%s\t%-8v",
297+
e.Name,
298+
e.ExpirationDate.Format("Jan 02, 2006 15:04 MST"),
299+
duration.ShortHumanDuration(e.ResidualTime()),
300+
handler.CAName,
301+
yesNo(e.ExternallyManaged),
302+
)
303+
304+
fmt.Fprintln(w, s)
305+
continue
288306
}
289307

290-
s := fmt.Sprintf("%s\t%s\t%s\t%s\t%-8v",
291-
e.Name,
292-
e.ExpirationDate.Format("Jan 02, 2006 15:04 MST"),
293-
duration.ShortHumanDuration(e.ResidualTime()),
294-
handler.CAName,
295-
yesNo(e.ExternallyManaged),
308+
// the certificate does not exist (for any reason)
309+
s := fmt.Sprintf("!MISSING! %s\t\t\t\t",
310+
handler.Name,
296311
)
297-
298312
fmt.Fprintln(w, s)
299313
}
300314
fmt.Fprintln(w)
301315
fmt.Fprintln(w, "CERTIFICATE AUTHORITY\tEXPIRES\tRESIDUAL TIME\tEXTERNALLY MANAGED")
302316
for _, handler := range rm.CAs() {
303-
e, err := rm.GetCAExpirationInfo(handler.Name)
304-
if err != nil {
305-
return err
317+
if ok, _ := rm.CAExists(handler.Name); ok {
318+
e, err := rm.GetCAExpirationInfo(handler.Name)
319+
if err != nil {
320+
return err
321+
}
322+
323+
s := fmt.Sprintf("%s\t%s\t%s\t%-8v",
324+
e.Name,
325+
e.ExpirationDate.Format("Jan 02, 2006 15:04 MST"),
326+
duration.ShortHumanDuration(e.ResidualTime()),
327+
yesNo(e.ExternallyManaged),
328+
)
329+
330+
fmt.Fprintln(w, s)
331+
continue
306332
}
307333

308-
s := fmt.Sprintf("%s\t%s\t%s\t%-8v",
309-
e.Name,
310-
e.ExpirationDate.Format("Jan 02, 2006 15:04 MST"),
311-
duration.ShortHumanDuration(e.ResidualTime()),
312-
yesNo(e.ExternallyManaged),
334+
// the CA does not exist (for any reason)
335+
s := fmt.Sprintf("!MISSING! %s\t\t\t",
336+
handler.Name,
313337
)
314-
315338
fmt.Fprintln(w, s)
316339
}
317340
w.Flush()

cmd/kubeadm/app/phases/certs/renewal/manager.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,16 @@ func (rm *Manager) CreateRenewCSR(name, outdir string) error {
315315
return nil
316316
}
317317

318+
// CertificateExists returns true if a certificate exists.
319+
func (rm *Manager) CertificateExists(name string) (bool, error) {
320+
handler, ok := rm.certificates[name]
321+
if !ok {
322+
return false, errors.Errorf("%s is not a known certificate", name)
323+
}
324+
325+
return handler.readwriter.Exists(), nil
326+
}
327+
318328
// GetCertificateExpirationInfo returns certificate expiration info.
319329
// For PKI certificates, use the name defined in the certsphase package, while for certificates
320330
// embedded in the kubeConfig files, use the kubeConfig file name defined in the kubeadm constants package.
@@ -341,6 +351,16 @@ func (rm *Manager) GetCertificateExpirationInfo(name string) (*ExpirationInfo, e
341351
return newExpirationInfo(name, cert, externallyManaged), nil
342352
}
343353

354+
// CAExists returns true if a certificate authority exists.
355+
func (rm *Manager) CAExists(name string) (bool, error) {
356+
handler, ok := rm.cas[name]
357+
if !ok {
358+
return false, errors.Errorf("%s is not a known certificate", name)
359+
}
360+
361+
return handler.readwriter.Exists(), nil
362+
}
363+
344364
// GetCAExpirationInfo returns CA expiration info.
345365
func (rm *Manager) GetCAExpirationInfo(name string) (*ExpirationInfo, error) {
346366
handler, ok := rm.cas[name]

cmd/kubeadm/app/phases/certs/renewal/readwriter.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package renewal
1919
import (
2020
"crypto"
2121
"crypto/x509"
22+
"os"
2223
"path/filepath"
2324

2425
"github.com/pkg/errors"
@@ -33,6 +34,9 @@ import (
3334
// certificateReadWriter defines the behavior of a component that
3435
// read or write a certificate stored/embedded in a file
3536
type certificateReadWriter interface {
37+
//Exists return true if the certificate exists
38+
Exists() bool
39+
3640
// Read a certificate stored/embedded in a file
3741
Read() (*x509.Certificate, error)
3842

@@ -55,6 +59,20 @@ func newPKICertificateReadWriter(certificateDir string, baseName string) *pkiCer
5559
}
5660
}
5761

62+
// Exists checks if a certificate exist
63+
func (rw *pkiCertificateReadWriter) Exists() bool {
64+
certificatePath, _ := pkiutil.PathsForCertAndKey(rw.certificateDir, rw.baseName)
65+
return fileExists(certificatePath)
66+
}
67+
68+
func fileExists(filename string) bool {
69+
info, err := os.Stat(filename)
70+
if os.IsNotExist(err) {
71+
return false
72+
}
73+
return !info.IsDir()
74+
}
75+
5876
// Read a certificate from a file the K8s pki managed by kubeadm
5977
func (rw *pkiCertificateReadWriter) Read() (*x509.Certificate, error) {
6078
certificatePath, _ := pkiutil.PathsForCertAndKey(rw.certificateDir, rw.baseName)
@@ -97,6 +115,11 @@ func newKubeconfigReadWriter(kubernetesDir string, kubeConfigFileName string) *k
97115
}
98116
}
99117

118+
// Exists checks if a certificate embedded in kubeConfig file exists
119+
func (rw *kubeConfigReadWriter) Exists() bool {
120+
return fileExists(rw.kubeConfigFilePath)
121+
}
122+
100123
// Read a certificate embedded in kubeConfig file managed by kubeadm.
101124
// Please note that the kubeConfig file itself is kept in the ReadWriter state thus allowing
102125
// to preserve the attributes (Context, Servers, AuthInfo etc.)

0 commit comments

Comments
 (0)