|
| 1 | +// Package trust manages the local CA used for HTTPS at *.localhost (spec 05). It |
| 2 | +// shells out to the maintained `mkcert` binary (NOT smallstep/truststore) to |
| 3 | +// install/remove the root CA into the host + Firefox/NSS stores, and diagnoses |
| 4 | +// the platform tools mkcert needs at runtime so `trust status` can print exact |
| 5 | +// remediations. |
| 6 | +// |
| 7 | +// Installing a CA is sudo-/privilege-gated; per locked decision #3 the logic is |
| 8 | +// built + tested with a fake runner, the human/sudo step is flagged, and a |
| 9 | +// doctor-style Status probe self-verifies. The mkcert process is run through an |
| 10 | +// injectable Runner so the package is fully unit-testable without mkcert present. |
| 11 | +package trust |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "fmt" |
| 16 | + "os" |
| 17 | + "os/exec" |
| 18 | + "path/filepath" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/open-source-cloud/devstack/internal/xdg" |
| 22 | +) |
| 23 | + |
| 24 | +// Runner runs the external mkcert binary. Injectable for tests. |
| 25 | +type Runner interface { |
| 26 | + Output(ctx context.Context, name string, args ...string) ([]byte, error) |
| 27 | + Run(ctx context.Context, name string, args ...string) error |
| 28 | + LookPath(file string) (string, error) |
| 29 | +} |
| 30 | + |
| 31 | +// Trust wraps mkcert. The zero value uses the real OS exec runner. |
| 32 | +type Trust struct { |
| 33 | + Runner Runner |
| 34 | +} |
| 35 | + |
| 36 | +// New returns a Trust backed by the real exec runner. |
| 37 | +func New() *Trust { return &Trust{Runner: execRunner{}} } |
| 38 | + |
| 39 | +func (t *Trust) runner() Runner { |
| 40 | + if t.Runner != nil { |
| 41 | + return t.Runner |
| 42 | + } |
| 43 | + return execRunner{} |
| 44 | +} |
| 45 | + |
| 46 | +// Available reports whether the mkcert binary is on PATH. |
| 47 | +func (t *Trust) Available() bool { |
| 48 | + _, err := t.runner().LookPath("mkcert") |
| 49 | + return err == nil |
| 50 | +} |
| 51 | + |
| 52 | +// CARoot returns mkcert's CAROOT directory (where rootCA.pem lives). |
| 53 | +func (t *Trust) CARoot(ctx context.Context) (string, error) { |
| 54 | + out, err := t.runner().Output(ctx, "mkcert", "-CAROOT") |
| 55 | + if err != nil { |
| 56 | + return "", fmt.Errorf("mkcert -CAROOT: %w", err) |
| 57 | + } |
| 58 | + return strings.TrimSpace(string(out)), nil |
| 59 | +} |
| 60 | + |
| 61 | +// Install installs the local root CA into the system + NSS trust stores |
| 62 | +// (`mkcert -install`). Requires privileges; a failure carries mkcert's output. |
| 63 | +func (t *Trust) Install(ctx context.Context) error { |
| 64 | + if !t.Available() { |
| 65 | + return errMkcertMissing() |
| 66 | + } |
| 67 | + if err := t.runner().Run(ctx, "mkcert", "-install"); err != nil { |
| 68 | + return fmt.Errorf("mkcert -install (try sudo; ensure libnss3-tools/certutil on Linux): %w", err) |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// Uninstall removes the local root CA from the trust stores (`mkcert -uninstall`). |
| 74 | +func (t *Trust) Uninstall(ctx context.Context) error { |
| 75 | + if !t.Available() { |
| 76 | + return errMkcertMissing() |
| 77 | + } |
| 78 | + if err := t.runner().Run(ctx, "mkcert", "-uninstall"); err != nil { |
| 79 | + return fmt.Errorf("mkcert -uninstall: %w", err) |
| 80 | + } |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +// Status is a diagnostic snapshot of local-CA readiness (the `trust status` view |
| 85 | +// + a doctor probe). Each field has a one-line remediation when not OK. |
| 86 | +type Status struct { |
| 87 | + MkcertFound bool `json:"mkcertFound"` |
| 88 | + CARoot string `json:"caRoot,omitempty"` |
| 89 | + CAInstalled bool `json:"caInstalled"` // rootCA.pem exists in CAROOT |
| 90 | + FirefoxTrust bool `json:"firefoxTrust"` // certutil present (NSS / Firefox) |
| 91 | + WSL bool `json:"wsl"` |
| 92 | + Remediation string `json:"remediation,omitempty"` |
| 93 | +} |
| 94 | + |
| 95 | +// Status probes the environment. It never mutates anything (no sudo needed). |
| 96 | +func (t *Trust) Status(ctx context.Context) Status { |
| 97 | + s := Status{WSL: xdg.IsWSL2()} |
| 98 | + s.MkcertFound = t.Available() |
| 99 | + if !s.MkcertFound { |
| 100 | + s.Remediation = "install mkcert (https://github.com/FiloSottile/mkcert) then run `devstack trust install`" |
| 101 | + return s |
| 102 | + } |
| 103 | + if root, err := t.CARoot(ctx); err == nil { |
| 104 | + s.CARoot = root |
| 105 | + if root != "" { |
| 106 | + if _, err := os.Stat(filepath.Join(root, "rootCA.pem")); err == nil { |
| 107 | + s.CAInstalled = true |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + // certutil backs Firefox/NSS trust; absent on a clean Ubuntu/WSL2. |
| 112 | + _, certutilErr := t.runner().LookPath("certutil") |
| 113 | + s.FirefoxTrust = certutilErr == nil |
| 114 | + |
| 115 | + switch { |
| 116 | + case !s.CAInstalled: |
| 117 | + s.Remediation = "run `sudo devstack trust install` to create + trust the local CA" |
| 118 | + case !s.FirefoxTrust: |
| 119 | + s.Remediation = "install certutil for Firefox/NSS trust: `apt install libnss3-tools` (Debian/Ubuntu)" |
| 120 | + case s.WSL: |
| 121 | + s.Remediation = "WSL2: also import the CA into the Windows store so browsers-on-Windows trust it (certutil.exe -addstore -user Root <CAROOT>/rootCA.pem)" |
| 122 | + } |
| 123 | + return s |
| 124 | +} |
| 125 | + |
| 126 | +// OK reports whether local HTTPS trust is fully ready. |
| 127 | +func (s Status) OK() bool { return s.MkcertFound && s.CAInstalled && s.FirefoxTrust } |
| 128 | + |
| 129 | +func errMkcertMissing() error { |
| 130 | + return fmt.Errorf("mkcert not found on PATH — install it (https://github.com/FiloSottile/mkcert)") |
| 131 | +} |
| 132 | + |
| 133 | +// execRunner is the production Runner. |
| 134 | +type execRunner struct{} |
| 135 | + |
| 136 | +func (execRunner) Output(ctx context.Context, name string, args ...string) ([]byte, error) { |
| 137 | + return exec.CommandContext(ctx, name, args...).Output() |
| 138 | +} |
| 139 | +func (execRunner) Run(ctx context.Context, name string, args ...string) error { |
| 140 | + cmd := exec.CommandContext(ctx, name, args...) |
| 141 | + cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr |
| 142 | + return cmd.Run() |
| 143 | +} |
| 144 | +func (execRunner) LookPath(file string) (string, error) { return exec.LookPath(file) } |
0 commit comments