-
Notifications
You must be signed in to change notification settings - Fork 4
/
clean.go
139 lines (112 loc) · 2.47 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
package trust
import (
"context"
"os"
"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/api"
"github.com/anchordotdev/cli/auth"
"github.com/anchordotdev/cli/trust/models"
"github.com/anchordotdev/cli/truststore"
"github.com/anchordotdev/cli/ui"
)
type Clean struct {
Config *cli.Config
Anc *api.Session
OrgSlug, RealmSlug string
}
func (c Clean) UI() cli.UI {
return cli.UI{
RunTUI: c.runTUI,
}
}
func (c *Clean) runTUI(ctx context.Context, drv *ui.Driver) error {
var err error
cmd := &auth.Client{
Config: c.Config,
Anc: c.Anc,
}
c.Anc, err = cmd.Perform(ctx, drv)
if err != nil {
return err
}
drv.Activate(ctx, &models.TrustCleanHeader{})
drv.Activate(ctx, &models.TrustCleanHint{
CertStates: c.Config.Trust.Clean.States,
TrustStores: c.Config.Trust.Stores,
})
err = c.Perform(ctx, drv)
if err != nil {
return err
}
return nil
}
func (c Clean) Perform(ctx context.Context, drv *ui.Driver) error {
var err error
if c.OrgSlug == "" && c.RealmSlug == "" {
c.OrgSlug, c.RealmSlug, err = fetchOrgAndRealm(ctx, c.Config, c.Anc)
if err != nil {
return err
}
}
drv.Activate(ctx, &models.TrustCleanAudit{})
expectedCAs, err := fetchExpectedCAs(ctx, c.Anc, c.OrgSlug, c.RealmSlug)
if err != nil {
return err
}
stores, sudoMgr, err := loadStores(c.Config)
if err != nil {
return err
}
sudoMgr.AroundSudo = func(sudo func()) {
unpausec := drv.Pause()
defer close(unpausec)
sudo()
}
audit := &truststore.Audit{
Expected: expectedCAs,
Stores: stores,
SelectFn: checkAnchorCert,
}
info, err := audit.Perform()
if err != nil {
return err
}
targetCAs := info.AllCAs(c.Config.Trust.Clean.States...)
drv.Send(targetCAs)
tmpDir, err := os.MkdirTemp("", "anchor-trust-clean")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)
for _, ca := range targetCAs {
confirmc := make(chan struct{})
drv.Activate(ctx, &models.TrustCleanCA{
CA: ca,
Config: c.Config,
ConfirmCh: confirmc,
})
if !c.Config.NonInteractive {
select {
case <-confirmc:
case <-ctx.Done():
return ctx.Err()
}
}
if ca.FilePath == "" {
if err := writeCAFile(ca, tmpDir); err != nil {
return err
}
}
for _, store := range stores {
if !info.IsPresent(ca, store) {
continue
}
drv.Send(models.CACleaningMsg{Store: store})
if _, err := store.UninstallCA(ca); err != nil {
return err
}
drv.Send(models.CACleanedMsg{Store: store})
}
}
return nil
}