Skip to content

Commit 0bd4ac6

Browse files
committed
linux: use unique per-CA names when installing certificates
Fixes #52 Closes #59
1 parent 4f82e1c commit 0bd4ac6

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

truststore_linux.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main
66

77
import (
88
"bytes"
9+
"fmt"
910
"io/ioutil"
1011
"log"
1112
"os"
@@ -25,13 +26,13 @@ var (
2526

2627
func init() {
2728
if pathExists("/etc/pki/ca-trust/source/anchors/") {
28-
SystemTrustFilename = "/etc/pki/ca-trust/source/anchors/mkcert-rootCA.pem"
29+
SystemTrustFilename = "/etc/pki/ca-trust/source/anchors/%s.pem"
2930
SystemTrustCommand = []string{"update-ca-trust", "extract"}
3031
} else if pathExists("/usr/local/share/ca-certificates/") {
31-
SystemTrustFilename = "/usr/local/share/ca-certificates/mkcert-rootCA.crt"
32+
SystemTrustFilename = "/usr/local/share/ca-certificates/%s.crt"
3233
SystemTrustCommand = []string{"update-ca-certificates"}
3334
} else if pathExists("/etc/ca-certificates/trust-source/anchors/") {
34-
SystemTrustFilename = "/etc/ca-certificates/trust-source/anchors/mkcert-rootCA.crt"
35+
SystemTrustFilename = "/etc/ca-certificates/trust-source/anchors/%s.crt"
3536
SystemTrustCommand = []string{"trust", "extract-compat"}
3637
}
3738
if SystemTrustCommand != nil {
@@ -47,6 +48,10 @@ func pathExists(path string) bool {
4748
return err == nil
4849
}
4950

51+
func (m *mkcert) systemTrustFilename() string {
52+
return fmt.Sprintf(SystemTrustFilename, strings.Replace(m.caUniqueName(), " ", "_", -1))
53+
}
54+
5055
func (m *mkcert) installPlatform() bool {
5156
if SystemTrustCommand == nil {
5257
log.Printf("Installing to the system store is not yet supported on this Linux 😣 but %s will still work.", NSSBrowsers)
@@ -57,7 +62,7 @@ func (m *mkcert) installPlatform() bool {
5762
cert, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
5863
fatalIfErr(err, "failed to read root certificate")
5964

60-
cmd := CommandWithSudo("tee", SystemTrustFilename)
65+
cmd := CommandWithSudo("tee", m.systemTrustFilename())
6166
cmd.Stdin = bytes.NewReader(cert)
6267
out, err := cmd.CombinedOutput()
6368
fatalIfCmdErr(err, "tee", out)
@@ -74,10 +79,18 @@ func (m *mkcert) uninstallPlatform() bool {
7479
return false
7580
}
7681

77-
cmd := CommandWithSudo("rm", SystemTrustFilename)
82+
cmd := CommandWithSudo("rm", "-f", m.systemTrustFilename())
7883
out, err := cmd.CombinedOutput()
7984
fatalIfCmdErr(err, "rm", out)
8085

86+
// We used to install under non-unique filenames.
87+
legacyFilename := fmt.Sprintf(SystemTrustFilename, "mkcert-rootCA")
88+
if pathExists(legacyFilename) {
89+
cmd := CommandWithSudo("rm", "-f", legacyFilename)
90+
out, err := cmd.CombinedOutput()
91+
fatalIfCmdErr(err, "rm (legacy filename)", out)
92+
}
93+
8194
cmd = CommandWithSudo(SystemTrustCommand...)
8295
out, err = cmd.CombinedOutput()
8396
fatalIfCmdErr(err, strings.Join(SystemTrustCommand, " "), out)

0 commit comments

Comments
 (0)