-
Notifications
You must be signed in to change notification settings - Fork 3
/
trust.go
161 lines (128 loc) · 3.91 KB
/
trust.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
package models
import (
"fmt"
"strings"
"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/truststore"
"github.com/anchordotdev/cli/ui"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
)
var (
TrustHeader = ui.Section{
Name: "TrustHeader",
Model: ui.MessageLines{
ui.Header(fmt.Sprintf("Manage CA Certificates in your Local Trust Store(s) %s", ui.Whisper("`anchor trust`"))),
},
}
TrustHint = ui.Section{
Name: "TrustHint",
Model: ui.MessageLines{
ui.StepHint("We will check your local trust stores, then make any needed updates."),
},
}
)
type TrustUpdateConfirm struct {
ConfirmCh chan<- struct{}
}
func (m *TrustUpdateConfirm) Init() tea.Cmd { return nil }
func (m *TrustUpdateConfirm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter:
if m.ConfirmCh != nil {
close(m.ConfirmCh)
m.ConfirmCh = nil
}
}
}
return m, nil
}
func (m *TrustUpdateConfirm) View() string {
var b strings.Builder
if m.ConfirmCh != nil {
fmt.Fprintln(&b, ui.StepHint(fmt.Sprintf("%s %s",
ui.Accentuate("Updates may require sudo privileges, learn why here:"),
ui.URL("https://lcl.host/why-sudo"),
)))
fmt.Fprintln(&b, ui.StepAlert(fmt.Sprintf("%s to install missing certificates. (%s)", ui.Action("Press Enter"), ui.Accentuate("requires sudo"))))
return b.String()
}
return b.String()
}
type TrustUpdateStore struct {
Config *cli.Config
Store truststore.Store
installing *truststore.CA
installed map[string][]string
spinner spinner.Model
}
func (m *TrustUpdateStore) Init() tea.Cmd {
m.installed = make(map[string][]string)
m.spinner = ui.WaitingSpinner()
return m.spinner.Tick
}
type (
TrustStoreInstallingCAMsg struct {
truststore.CA
}
TrustStoreInstalledCAMsg struct {
truststore.CA
}
)
func (m *TrustUpdateStore) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case TrustStoreInstallingCAMsg:
m.installing = &msg.CA
return m, nil
case TrustStoreInstalledCAMsg:
m.installing = nil
m.installed[msg.CA.Subject.CommonName] = append(m.installed[msg.CA.Subject.CommonName], msg.CA.PublicKeyAlgorithm.String())
return m, nil
}
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
func (m *TrustUpdateStore) View() string {
var b strings.Builder
if len(m.installed) > 0 {
var styledCAs []string
for subjectCommonName, algorithms := range m.installed {
styledCAs = append(styledCAs, fmt.Sprintf("%s [%s]",
ui.Underline(subjectCommonName),
ui.Whisper(strings.Join(algorithms, ", ")),
))
}
fmt.Fprintln(&b, ui.StepDone(fmt.Sprintf("Updated %s: installed %s",
ui.Emphasize(m.Store.Description()),
strings.Join(styledCAs, ", "),
)))
}
if m.installing != nil {
// present thumbprint for comparison with pop up prompt
if m.Config.GOOS() == "windows" {
fmt.Fprintln(&b, ui.StepHint(fmt.Sprintf("\"%s\" Thumbprint (sha1): %s",
m.installing.Subject.CommonName,
m.installing.WindowsThumbprint(),
)))
}
fmt.Fprintln(&b, ui.StepInProgress(fmt.Sprintf("Updating %s: installing %s %s… %s",
ui.Emphasize(m.Store.Description()),
ui.Underline(m.installing.Subject.CommonName),
ui.Whisper(m.installing.PublicKeyAlgorithm.String()),
m.spinner.View())))
}
return b.String()
}
type VMHint struct{}
func (m *VMHint) Init() tea.Cmd { return nil }
func (m *VMHint) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, nil }
func (m *VMHint) View() string {
var b strings.Builder
fmt.Fprintln(&b, ui.StepWarning("Running trust inside a VM or container will not update the host system."))
fmt.Fprintln(&b, ui.StepHint("Rerun this command on your host system to update it's trust stores and enable")) // enable secure communication."))
fmt.Fprintln(&b, ui.StepHint(fmt.Sprintf("secure communication, learn more here: %s", ui.URL("https://cl.host/vm-container-setup"))))
return b.String()
}