-
Notifications
You must be signed in to change notification settings - Fork 3
/
clean.go
180 lines (142 loc) · 3.91 KB
/
clean.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package models
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/truststore"
"github.com/anchordotdev/cli/ui"
)
type TrustCleanHeader struct{}
func (m *TrustCleanHeader) Init() tea.Cmd { return nil }
func (m *TrustCleanHeader) Update(tea.Msg) (tea.Model, tea.Cmd) { return m, nil }
func (m *TrustCleanHeader) View() string {
var b strings.Builder
fmt.Fprintln(&b, ui.Header(fmt.Sprintf("Clean CA Certificates from Local Trust Store(s) %s", ui.Whisper("`anchor trust clean`"))))
return b.String()
}
type TrustCleanHint struct {
CertStates, TrustStores []string
spinner spinner.Model
}
func (c *TrustCleanHint) Init() tea.Cmd {
c.spinner = ui.WaitingSpinner()
return c.spinner.Tick
}
func (c *TrustCleanHint) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
c.spinner, cmd = c.spinner.Update(msg)
return c, cmd
}
func (c *TrustCleanHint) View() string {
states := strings.Join(c.CertStates, ", ")
stores := strings.Join(c.TrustStores, ", ")
var b strings.Builder
fmt.Fprintln(&b, ui.Hint(fmt.Sprintf("Removing %s CA certificates from the %s store(s).", states, stores)))
return b.String()
}
type TrustCleanAudit struct {
finished bool
spinner spinner.Model
targetCAs []*truststore.CA
}
func (m *TrustCleanAudit) Init() tea.Cmd {
m.spinner = ui.WaitingSpinner()
return m.spinner.Tick
}
func (m *TrustCleanAudit) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case []*truststore.CA:
m.targetCAs = msg
m.finished = true
return m, nil
default:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
}
func (m *TrustCleanAudit) View() string {
var b strings.Builder
if !m.finished {
fmt.Fprintln(&b, ui.StepInProgress(fmt.Sprintf("Auditing local CA certificates…%s", m.spinner.View())))
return b.String()
}
fmt.Fprintln(&b, ui.StepDone(fmt.Sprintf("Audited local CA certificates: need to remove %d certificates.", len(m.targetCAs))))
return b.String()
}
type TrustCleanCA struct {
Config *cli.Config
CA *truststore.CA
ConfirmCh chan<- struct{}
cleaning truststore.Store
cleaned []string
spinner spinner.Model
}
func (m *TrustCleanCA) Init() tea.Cmd {
m.spinner = ui.WaitingSpinner()
return m.spinner.Tick
}
type (
CACleaningMsg struct {
truststore.Store
}
CACleanedMsg struct {
truststore.Store
}
)
func (m *TrustCleanCA) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case CACleaningMsg:
m.cleaning = msg.Store
return m, nil
case CACleanedMsg:
m.cleaning = nil
m.cleaned = append(m.cleaned, msg.Store.Description())
return m, nil
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter:
if m.ConfirmCh == nil {
return m, nil
}
close(m.ConfirmCh)
m.ConfirmCh = nil
}
return m, nil
default:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
}
func (m *TrustCleanCA) View() string {
var b strings.Builder
if m.ConfirmCh != nil && !m.Config.NonInteractive {
fmt.Fprintln(&b, ui.StepAlert(fmt.Sprintf("%s to remove %s %s CA Certificate. (%s)",
ui.Action("Press Enter"),
ui.EmphasizeUnderline(m.CA.Subject.CommonName),
ui.Emphasize(m.CA.PublicKeyAlgorithm.String()),
ui.Accentuate("requires sudo"),
)))
return b.String()
}
if m.cleaning != nil {
fmt.Fprintln(&b, ui.StepInProgress(fmt.Sprintf("Cleaning %s %s: removing from %s…%s",
ui.EmphasizeUnderline(m.CA.Subject.CommonName),
ui.Emphasize(m.CA.PublicKeyAlgorithm.String()),
ui.Whisper(m.cleaning.Description()),
m.spinner.View(),
)))
return b.String()
}
if len(m.cleaned) > 0 {
fmt.Fprintln(&b, ui.StepDone(fmt.Sprintf("Cleaned %s %s: removed from %s",
ui.EmphasizeUnderline(m.CA.Subject.CommonName),
ui.Emphasize(m.CA.PublicKeyAlgorithm.String()),
fmt.Sprintf("[%s]", ui.Whisper(strings.Join(m.cleaned, ", "))),
)))
}
return b.String()
}