|
6 | 6 | "os"
|
7 | 7 | "path/filepath"
|
8 | 8 | "strings"
|
| 9 | + "text/tabwriter" |
9 | 10 | "time"
|
10 | 11 |
|
11 | 12 | "github.com/k3s-io/k3s/pkg/agent/util"
|
@@ -92,27 +93,60 @@ func check(app *cli.Context, cfg *cmds.Server) error {
|
92 | 93 |
|
93 | 94 | now := time.Now()
|
94 | 95 | warn := now.Add(time.Hour * 24 * config.CertificateRenewDays)
|
95 |
| - |
96 |
| - for service, files := range fileMap { |
97 |
| - logrus.Info("Checking certificates for " + service) |
98 |
| - for _, file := range files { |
99 |
| - // ignore errors, as some files may not exist, or may not contain certs. |
100 |
| - // Only check whatever exists and has certs. |
101 |
| - certs, _ := certutil.CertsFromFile(file) |
102 |
| - for _, cert := range certs { |
103 |
| - if now.Before(cert.NotBefore) { |
104 |
| - logrus.Errorf("%s: certificate %s is not valid before %s", file, cert.Subject, cert.NotBefore.Format(time.RFC3339)) |
105 |
| - } else if now.After(cert.NotAfter) { |
106 |
| - logrus.Errorf("%s: certificate %s expired at %s", file, cert.Subject, cert.NotAfter.Format(time.RFC3339)) |
107 |
| - } else if warn.After(cert.NotAfter) { |
108 |
| - logrus.Warnf("%s: certificate %s will expire within %d days at %s", file, cert.Subject, config.CertificateRenewDays, cert.NotAfter.Format(time.RFC3339)) |
109 |
| - } else { |
110 |
| - logrus.Infof("%s: certificate %s is ok, expires at %s", file, cert.Subject, cert.NotAfter.Format(time.RFC3339)) |
| 96 | + outFmt := app.String("output") |
| 97 | + switch outFmt { |
| 98 | + case "text": |
| 99 | + for service, files := range fileMap { |
| 100 | + logrus.Info("Checking certificates for " + service) |
| 101 | + for _, file := range files { |
| 102 | + // ignore errors, as some files may not exist, or may not contain certs. |
| 103 | + // Only check whatever exists and has certs. |
| 104 | + certs, _ := certutil.CertsFromFile(file) |
| 105 | + for _, cert := range certs { |
| 106 | + if now.Before(cert.NotBefore) { |
| 107 | + logrus.Errorf("%s: certificate %s is not valid before %s", file, cert.Subject, cert.NotBefore.Format(time.RFC3339)) |
| 108 | + } else if now.After(cert.NotAfter) { |
| 109 | + logrus.Errorf("%s: certificate %s expired at %s", file, cert.Subject, cert.NotAfter.Format(time.RFC3339)) |
| 110 | + } else if warn.After(cert.NotAfter) { |
| 111 | + logrus.Warnf("%s: certificate %s will expire within %d days at %s", file, cert.Subject, config.CertificateRenewDays, cert.NotAfter.Format(time.RFC3339)) |
| 112 | + } else { |
| 113 | + logrus.Infof("%s: certificate %s is ok, expires at %s", file, cert.Subject, cert.NotAfter.Format(time.RFC3339)) |
| 114 | + } |
111 | 115 | }
|
112 | 116 | }
|
113 | 117 | }
|
| 118 | + case "table": |
| 119 | + var tabBuffer bytes.Buffer |
| 120 | + w := tabwriter.NewWriter(&tabBuffer, 0, 0, 2, ' ', 0) |
| 121 | + fmt.Fprintf(w, "\n") |
| 122 | + fmt.Fprintf(w, "CERTIFICATE\tSUBJECT\tSTATUS\tEXPIRES\n") |
| 123 | + fmt.Fprintf(w, "-----------\t-------\t------\t-------") |
| 124 | + for _, files := range fileMap { |
| 125 | + for _, file := range files { |
| 126 | + certs, _ := certutil.CertsFromFile(file) |
| 127 | + for _, cert := range certs { |
| 128 | + baseName := filepath.Base(file) |
| 129 | + var status string |
| 130 | + expiration := cert.NotAfter.Format(time.RFC3339) |
| 131 | + if now.Before(cert.NotBefore) { |
| 132 | + status = "NOT YET VALID" |
| 133 | + expiration = cert.NotBefore.Format(time.RFC3339) |
| 134 | + } else if now.After(cert.NotAfter) { |
| 135 | + status = "EXPIRED" |
| 136 | + } else if warn.After(cert.NotAfter) { |
| 137 | + status = "WARNING" |
| 138 | + } else { |
| 139 | + status = "OK" |
| 140 | + } |
| 141 | + fmt.Fprintf(w, "\n%s\t%s\t%s\t%s", baseName, cert.Subject, status, expiration) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + w.Flush() |
| 146 | + fmt.Println(tabBuffer.String()) |
| 147 | + default: |
| 148 | + return fmt.Errorf("invalid output format %s", outFmt) |
114 | 149 | }
|
115 |
| - |
116 | 150 | return nil
|
117 | 151 | }
|
118 | 152 |
|
|
0 commit comments