-
Notifications
You must be signed in to change notification settings - Fork 4
/
audit.go
146 lines (115 loc) · 3.16 KB
/
audit.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package trust
import (
"context"
"fmt"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/api"
"github.com/anchordotdev/cli/truststore"
)
type Audit struct {
Config *cli.Config
}
func (a Audit) UI() cli.UI {
return cli.UI{
RunTTY: a.run,
}
}
func (a *Audit) run(ctx context.Context, tty termenv.File) error {
anc, err := api.NewClient(a.Config)
if err != nil {
return err
}
org, realm, err := fetchOrgAndRealm(ctx, a.Config, anc)
if err != nil {
return err
}
expectedCAs, err := fetchExpectedCAs(ctx, anc, org, realm)
if err != nil {
return err
}
stores, _, err := loadStores(a.Config)
if err != nil {
return err
}
audit := &truststore.Audit{
Expected: expectedCAs,
Stores: stores,
SelectFn: checkAnchorCert,
}
info, err := audit.Perform()
if err != nil {
return err
}
for _, ca := range info.Valid {
fmt.Fprintf(tty, "%s (%s) %-7s \"%s\"\n",
boldGreen.Render(fmt.Sprintf("%-8s", "VALID")),
period(ca),
ca.PublicKeyAlgorithm,
commonName(ca),
)
fmt.Fprintln(tty)
for _, store := range stores {
fmt.Fprintf(tty, "%7s %35s %s\n", "", store.Description(), boldGreen.Render("TRUSTED"))
}
fmt.Fprintln(tty)
}
for _, ca := range info.Missing {
fmt.Fprintf(tty, "%s (%s) %-7s \"%s\"\n",
boldRed.Render(fmt.Sprintf("%-8s", "MISSING")),
period(ca),
ca.PublicKeyAlgorithm,
commonName(ca),
)
fmt.Fprintln(tty)
for _, store := range stores {
if info.IsPresent(ca, store) {
fmt.Fprintf(tty, "%7s %35s %s\n", "", store.Description(), boldGreen.Render("TRUSTED"))
} else {
fmt.Fprintf(tty, "%7s %35s %s\n", "", store.Description(), boldRed.Render("NOT PRESENT"))
}
}
fmt.Fprintln(tty)
}
for _, ca := range info.Extra {
fmt.Fprintf(tty, "%s (%s) %-7s \"%s\"\n",
faint.Render(fmt.Sprintf("%-8s", "EXTRA")),
period(ca),
ca.PublicKeyAlgorithm,
commonName(ca),
)
fmt.Fprintln(tty)
for _, store := range stores {
if info.IsPresent(ca, store) {
fmt.Fprintf(tty, "%7s %35s %s\n", "", store.Description(), faintGreen.Render("TRUSTED"))
} else {
fmt.Fprintf(tty, "%7s %35s %s\n", "", store.Description(), faint.Render("NOT PRESENT"))
}
}
fmt.Fprintln(tty)
}
return nil
}
var (
darkGreen = lipgloss.Color("#008000")
darkRed = lipgloss.Color("#800000")
lightGreen = lipgloss.Color("#00ff00")
lightRed = lipgloss.Color("#ff0000")
boldGreen = lipgloss.NewStyle().Bold(true).Foreground(lightGreen)
boldRed = lipgloss.NewStyle().Bold(true).Foreground(lightRed)
faintGreen = lipgloss.NewStyle().Faint(true).Foreground(darkGreen)
faintRed = lipgloss.NewStyle().Faint(true).Foreground(darkRed)
faint = lipgloss.NewStyle().Faint(true)
italic = lipgloss.NewStyle().Italic(true)
underline = lipgloss.NewStyle().Underline(true)
)
func commonName(ca *truststore.CA) string {
return underline.Render(fmt.Sprintf("%s", ca.Subject.CommonName))
}
func period(ca *truststore.CA) string {
startAt := ca.NotBefore.Format("2006-01-02")
expireAt := ca.NotAfter.Add(1 * time.Second).Format("2006-01-02")
return italic.Render(fmt.Sprintf("%s - %s", startAt, expireAt))
}