-
Notifications
You must be signed in to change notification settings - Fork 4
/
trust.go
360 lines (295 loc) · 6.94 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package trust
import (
"context"
"crypto/x509"
"encoding/pem"
"errors"
"log"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/anchordotdev/cli"
"github.com/anchordotdev/cli/api"
"github.com/anchordotdev/cli/auth"
"github.com/anchordotdev/cli/ext509"
"github.com/anchordotdev/cli/ext509/oid"
"github.com/anchordotdev/cli/trust/models"
"github.com/anchordotdev/cli/truststore"
"github.com/anchordotdev/cli/ui"
)
type Command struct {
Config *cli.Config
Anc *api.Session
OrgSlug, RealmSlug string
}
func (c Command) UI() cli.UI {
return cli.UI{
RunTUI: c.runTUI,
}
}
func (c *Command) runTUI(ctx context.Context, drv *ui.Driver) error {
anc := c.Anc
if anc == nil {
var err error
if anc, err = c.apiClient(ctx, drv); err != nil {
return err
}
}
orgSlug, realmSlug := c.OrgSlug, c.RealmSlug
if orgSlug == "" || realmSlug == "" {
if orgSlug != "" || realmSlug != "" {
panic("trust: OrgSlug & RealmSlug must be initialized together")
}
var err error
if orgSlug, realmSlug, err = fetchOrgAndRealm(ctx, c.Config, anc); err != nil {
return err
}
}
confirmc := make(chan struct{})
drv.Activate(ctx, &models.TrustPreflight{
Config: c.Config,
ConfirmCh: confirmc,
})
cas, err := fetchExpectedCAs(ctx, anc, orgSlug, realmSlug)
if err != nil {
return err
}
stores, sudoMgr, err := loadStores(c.Config)
if err != nil {
return err
}
// TODO: handle nosudo
sudoMgr.AroundSudo = func(sudo func()) {
unpausec := drv.Pause()
defer close(unpausec)
sudo()
}
audit := &truststore.Audit{
Expected: cas,
Stores: stores,
SelectFn: checkAnchorCert,
}
info, err := audit.Perform()
if err != nil {
return err
}
drv.Send(models.AuditInfoMsg(info))
if len(info.Missing) == 0 {
drv.Send(models.PreflightFinishedMsg{})
return nil
}
if !c.Config.NonInteractive {
select {
case <-confirmc:
case <-ctx.Done():
return ctx.Err()
}
}
tmpDir, err := os.MkdirTemp("", "anchor-trust")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)
// FIXME: this write is required for the InstallCAs to work, feels like a leaky abstraction
for _, ca := range info.Missing {
if err := writeCAFile(ca, tmpDir); err != nil {
return err
}
}
for _, store := range stores {
drv.Activate(ctx, &models.TrustUpdateStore{
Store: store,
})
for _, ca := range info.Missing {
if info.IsPresent(ca, store) {
continue
}
drv.Send(models.TrustStoreInstallingCAMsg{CA: *ca})
if ok, err := store.InstallCA(ca); err != nil {
return err
} else if !ok {
panic("impossible")
}
drv.Send(models.TrustStoreInstalledCAMsg{CA: *ca})
}
}
return nil
}
func (c *Command) apiClient(ctx context.Context, drv *ui.Driver) (*api.Session, error) {
anc, err := api.NewClient(c.Config)
if errors.Is(err, api.ErrSignedOut) {
if err := c.runSignIn(ctx, drv); err != nil {
return nil, err
}
if anc, err = api.NewClient(c.Config); err != nil {
return nil, err
}
} else if err != nil {
return nil, err
}
return anc, nil
}
func (c *Command) runSignIn(ctx context.Context, drv *ui.Driver) error {
cmdSignIn := &auth.SignIn{
Hint: &models.TrustSignInHint{},
}
return cmdSignIn.RunTUI(ctx, drv)
}
func fetchOrgAndRealm(ctx context.Context, cfg *cli.Config, anc *api.Session) (string, string, error) {
org, realm := cfg.Trust.Org, cfg.Trust.Realm
if (org == "") != (realm == "") {
return "", "", errors.New("--org and --realm flags must both be present or absent")
}
if org == "" && realm == "" {
userInfo, err := anc.UserInfo(ctx)
if err != nil {
return "", "", err
}
org = userInfo.PersonalOrg.Slug
// TODO: use personal org's default realm value from API check-in call,
// instead of hard-coding "localhost" here
realm = "localhost"
}
return org, realm, nil
}
func PerformAudit(ctx context.Context, cfg *cli.Config, anc *api.Session, org string, realm string) (*truststore.AuditInfo, error) {
cas, err := fetchExpectedCAs(ctx, anc, org, realm)
if err != nil {
return nil, err
}
stores, _, err := loadStores(cfg)
if err != nil {
return nil, err
}
audit := &truststore.Audit{
Expected: cas,
Stores: stores,
SelectFn: checkAnchorCert,
}
auditInfo, err := audit.Perform()
if err != nil {
return nil, err
}
time.Sleep(1 * time.Second)
return auditInfo, nil
}
func fetchExpectedCAs(ctx context.Context, anc *api.Session, org, realm string) ([]*truststore.CA, error) {
creds, err := anc.FetchCredentials(ctx, org, realm)
if err != nil {
return nil, err
}
var cas []*truststore.CA
for _, item := range creds {
blk, _ := pem.Decode([]byte(item.TextualEncoding))
cert, err := x509.ParseCertificate(blk.Bytes)
if err != nil {
return nil, err
}
uniqueName := cert.SerialNumber.Text(16)
ca := &truststore.CA{
Certificate: cert,
UniqueName: uniqueName,
}
// TODO: make this variable based on cli.Config
if ca.PublicKeyAlgorithm == x509.Ed25519 {
continue
}
cas = append(cas, ca)
}
return cas, nil
}
func loadStores(cfg *cli.Config) ([]truststore.Store, *SudoManager, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
rootFS := truststore.RootFS()
sysFS := &SudoManager{
CmdFS: rootFS,
NoSudo: cfg.Trust.NoSudo,
}
var stores []truststore.Store
for _, storeName := range cfg.Trust.Stores {
switch storeName {
case "system":
systemStore := &truststore.Platform{
HomeDir: homeDir,
DataFS: rootFS,
SysFS: sysFS,
}
stores = append(stores, systemStore)
case "nss":
nssStore := &truststore.NSS{
HomeDir: homeDir,
DataFS: rootFS,
SysFS: sysFS,
}
stores = append(stores, nssStore)
case "homebrew":
brewStore := &truststore.Brew{
RootDir: "/",
DataFS: rootFS,
SysFS: sysFS,
}
stores = append(stores, brewStore)
case "mock":
stores = append(stores, new(truststore.Mock))
}
}
return stores, sysFS, nil
}
func checkAnchorCert(ca *truststore.CA) (bool, error) {
for _, ext := range ca.Extensions {
if ext.Id.Equal(oid.AnchorCertificateExtension) {
var ac ext509.AnchorCertificate
if err := ac.Unmarshal(ext); err != nil {
return false, err
}
return true, nil
}
}
return false, nil
}
func writeCAFile(ca *truststore.CA, dir string) error {
fileName := filepath.Join(ca.UniqueName + ".pem")
file, err := os.Create(filepath.Join(dir, fileName))
if err != nil {
return err
}
defer file.Close()
blk := &pem.Block{
Type: "CERTIFICATE",
Bytes: ca.Raw,
}
if err := pem.Encode(file, blk); err != nil {
return err
}
if err := file.Close(); err != nil {
return err
}
ca.FilePath = file.Name()
return nil
}
type SudoManager struct {
truststore.CmdFS
NoSudo bool
AroundSudo func(sudoExec func())
}
func (s *SudoManager) SudoExec(cmd *exec.Cmd) ([]byte, error) {
sudoFn := s.CmdFS.SudoExec
if s.NoSudo {
sudoFn = s.CmdFS.Exec
}
if s.AroundSudo == nil {
return sudoFn(cmd)
}
var (
out []byte
err error
)
s.AroundSudo(func() {
out, err = sudoFn(cmd)
})
return out, err
}