-
Notifications
You must be signed in to change notification settings - Fork 24
/
list.go
78 lines (61 loc) · 1.63 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package certs
import (
"crypto/x509"
"encoding/pem"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/aserto-dev/topaz/pkg/cli/cc"
)
type ListCertsCmd struct {
CertsDir string `flag:"" required:"false" default:"${topaz_certs_dir}" help:"path to dev certs folder" `
}
func (cmd *ListCertsCmd) Run(c *cc.CommonCtx) error {
certsDir := cmd.CertsDir
if fi, err := os.Stat(certsDir); os.IsNotExist(err) || !fi.IsDir() {
return fmt.Errorf("directory %s not found", certsDir)
}
c.UI.Normal().Msgf("certs directory: %s", certsDir)
certDetails := make(map[string]*x509.Certificate)
for _, fqn := range getFileList(certsDir, withCerts()) {
if fi, err := os.Stat(fqn); os.IsNotExist(err) || fi.IsDir() {
continue
}
content, err := os.ReadFile(fqn)
if err != nil {
return err
}
block, _ := pem.Decode(content)
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return err
}
_, fn := filepath.Split(fqn)
certDetails[fn] = cert
}
table := c.UI.Normal().WithTable("File", "Not Before", "Not After", "Valid", "CN", "DNS names")
fileNames := make([]string, 0, len(certDetails))
for k := range certDetails {
fileNames = append(fileNames, k)
}
sort.Strings(fileNames)
table.WithTableNoAutoWrapText()
for _, k := range fileNames {
isValid := true
if time.Until(certDetails[k].NotAfter) < 0 {
isValid = false
}
table.WithTableRow(k,
certDetails[k].NotBefore.Format(time.RFC3339),
certDetails[k].NotAfter.Format(time.RFC3339),
fmt.Sprintf("%t", isValid),
certDetails[k].Issuer.CommonName,
strings.Join(certDetails[k].DNSNames, ","),
)
}
table.Do()
return nil
}