Skip to content

Commit 8673793

Browse files
committed
v0.0.3: add sudo prompt warning
1 parent ae6656b commit 8673793

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

trust/trust.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import (
2020
"github.com/anchordotdev/cli/truststore"
2121
)
2222

23+
const (
24+
sudoWarning = "! Anchor needs sudo permission to add the specified certificates to your local trust stores."
25+
)
26+
2327
type Command struct {
2428
Config *cli.Config
2529
}
@@ -88,6 +92,8 @@ func (c *Command) run(ctx context.Context, tty termenv.File) error {
8892
log.Fatal(err)
8993
}
9094

95+
fmt.Fprintln(tty, sudoWarning)
96+
9197
rootFS := truststore.RootFS()
9298
systemStore := &truststore.Platform{
9399
HomeDir: homeDir,
@@ -131,23 +137,26 @@ func (c *Command) run(ctx context.Context, tty termenv.File) error {
131137
UniqueName: uniqueName,
132138
}
133139

134-
if c.Config.Trust.MockMode {
135-
fmt.Fprintf(tty, "\"%s\" %s cert (%s) installed in the mock store\n", ca.Subject.CommonName, ca.PublicKeyAlgorithm, uniqueName)
140+
if ca.SignatureAlgorithm == x509.PureEd25519 {
141+
fmt.Fprintf(tty, "Installing \"%s\" %s (%s) certificate:\n", ca.Subject.CommonName, ca.PublicKeyAlgorithm, uniqueName)
142+
fmt.Fprintf(tty, " - skipped awaiting broader support.\n")
136143
continue
137144
}
138145

139-
if ca.SignatureAlgorithm == x509.PureEd25519 {
140-
fmt.Fprintf(tty, "skipping \"%s\" %s cert (%s), Ed25519 certificates are not yet supported\n", ca.Subject.CommonName, ca.PublicKeyAlgorithm, uniqueName)
146+
fmt.Fprintf(tty, "Installing \"%s\" %s (%s) certificate:\n", ca.Subject.CommonName, ca.PublicKeyAlgorithm, uniqueName)
147+
148+
if c.Config.Trust.MockMode {
149+
fmt.Fprintf(tty, " - installed in the mock store.\n")
141150
continue
142151
}
143152

144153
if installed, err := systemStore.InstallCA(ca); installed {
145-
fmt.Fprintf(tty, "\"%s\" %s cert (%s) installed in the system store\n", ca.Subject.CommonName, ca.PublicKeyAlgorithm, uniqueName)
154+
fmt.Fprintf(tty, " - installed in the system store.\n")
146155
} else if err != nil {
147156
return err
148157
}
149158
if installed, err := nssStore.InstallCA(ca); installed {
150-
fmt.Fprintf(tty, "\"%s\" %s cert (%s) installed in the NSS store\n", ca.Subject.CommonName, ca.PublicKeyAlgorithm, uniqueName)
159+
fmt.Fprintf(tty, " - installed in the Network Security Services (NSS) store.\n")
151160
} else if err != nil {
152161
return err
153162
}

trust/trust_test.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package trust
22

33
import (
4+
"bufio"
45
"context"
56
"encoding/json"
67
"flag"
78
"os"
89
"regexp"
9-
"strings"
1010
"testing"
1111

1212
"github.com/anchordotdev/cli"
@@ -45,6 +45,10 @@ func TestTrust(t *testing.T) {
4545
t.Fatal(err)
4646
}
4747

48+
headerPattern := regexp.MustCompile(`Installing "[^"]+ - AnchorCA" \w+ \([a-z0-9]+\) certificate:$`)
49+
installPattern := regexp.MustCompile(` - installed in the mock store.$`)
50+
skipPattern := regexp.MustCompile(` - skipped awaiting broader support.$`)
51+
4852
t.Run("default to personal org and localhost realm", func(t *testing.T) {
4953
cmd := &Command{
5054
Config: cfg,
@@ -55,15 +59,24 @@ func TestTrust(t *testing.T) {
5559
t.Fatal(err)
5660
}
5761

58-
msgPattern := regexp.MustCompile(` - AnchorCA" \w+ cert \([a-z0-9]+\) installed in the mock store$`)
62+
scanner := bufio.NewScanner(buf)
63+
if !scanner.Scan() {
64+
t.Fatalf("want sudo warning line got %q %v (nil is EOF)", scanner.Err(), scanner.Err())
65+
}
66+
if line := scanner.Text(); line != sudoWarning {
67+
t.Errorf("want output %q to match %q", line, sudoWarning)
68+
}
69+
for scanner.Scan() {
70+
if line := scanner.Text(); !headerPattern.MatchString(line) {
71+
t.Errorf("want output %q to match %q", line, headerPattern)
72+
}
5973

60-
for _, line := range strings.Split(buf.String(), "\n") {
61-
if len(line) == 0 {
62-
continue
74+
if !scanner.Scan() {
75+
t.Fatalf("want detail line got %q %v (nil is EOF)", scanner.Err(), scanner.Err())
6376
}
6477

65-
if !msgPattern.MatchString(line) {
66-
t.Errorf("want output %q to match %q", line, msgPattern)
78+
if line := scanner.Text(); !(installPattern.MatchString(line) || skipPattern.MatchString(line)) {
79+
t.Errorf("want output %q to match %q or %q", line, installPattern, skipPattern)
6780
}
6881
}
6982
})
@@ -81,15 +94,24 @@ func TestTrust(t *testing.T) {
8194
t.Fatal(err)
8295
}
8396

84-
msgPattern := regexp.MustCompile(` - AnchorCA" \w+ cert \([a-z0-9]+\) installed in the mock store$`)
97+
scanner := bufio.NewScanner(buf)
98+
if !scanner.Scan() {
99+
t.Fatalf("want sudo warning line got %q %v (nil is EOF)", scanner.Err(), scanner.Err())
100+
}
101+
if line := scanner.Text(); line != sudoWarning {
102+
t.Errorf("want output %q to match %q", line, sudoWarning)
103+
}
104+
for scanner.Scan() {
105+
if line := scanner.Text(); !headerPattern.MatchString(line) {
106+
t.Errorf("want output %q to match %q", line, headerPattern)
107+
}
85108

86-
for _, line := range strings.Split(buf.String(), "\n") {
87-
if len(line) == 0 {
88-
continue
109+
if !scanner.Scan() {
110+
t.Fatalf("want detail line got %q %v (nil is EOF)", scanner.Err(), scanner.Err())
89111
}
90112

91-
if !msgPattern.MatchString(line) {
92-
t.Errorf("want output %q to match %q", line, msgPattern)
113+
if line := scanner.Text(); !(installPattern.MatchString(line) || skipPattern.MatchString(line)) {
114+
t.Errorf("want output %q to match %q or %q", line, installPattern, skipPattern)
93115
}
94116
}
95117
})

0 commit comments

Comments
 (0)