-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate-trust.go
109 lines (93 loc) · 3.01 KB
/
certificate-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
package main
import (
"fmt"
"os/exec"
"path"
"runtime"
"time"
"github.com/lstellway/go/command"
)
// TrustDarwin trust a PKI certificate on macOS (Darwin)
func TrustDarwin(cert string) {
cmd := exec.Command("sudo", "security", "add-trusted-cert", "-d", "-r", "trustRoot", "-k", "/Library/Keychains/System.keychain", cert)
err := cmd.Run()
exitOnError(err)
}
// TrustLinux trust a PKI certificate on Linux
func TrustLinux(cert string) {
var (
command []string
file string
)
switch {
case fileExists("/etc/pki/ca-trust/source/anchors/"):
command = []string{"update-ca-trust", "extract"}
file = "/etc/pki/ca-trust/source/anchors/%s-%d.pem"
case fileExists("/usr/local/share/ca-certificates/"):
command = []string{"update-ca-certificates"}
file = "/usr/local/share/ca-certificates/%s-%d.crt"
case fileExists("/etc/ca-certificates/trust-source/anchors/"):
command = []string{"trust", "extract-compat"}
file = "/etc/ca-certificates/trust-source/anchors/%s-%d.crt"
case fileExists("/usr/share/pki/trust/anchors/"):
command = []string{"update-ca-certificates"}
file = "/usr/share/pki/trust/anchors/%s-%d.pem"
default:
exit(1, "Supported certificate management not found.")
}
// Build file path
file = fmt.Sprintf(file, path.Base(cert), time.Now().Unix())
// Copy certificate
cmd := exec.Command("sudo", "cp", cert, file)
err := cmd.Run()
exitOnError(err, err)
fmt.Printf("Certificate copied to '%s'", file)
// Trust certificate
cmd = exec.Command("sudo", command...)
err = cmd.Run()
exitOnError(err, err)
}
// TrustWindows trust a PKI certificate on Windows
func TrustWindows(cert string) {
command, err := exec.LookPath("certutil")
exitOnError(err, "Could not find 'certutil' command")
cmd := exec.Command(command, "-addstore", "-f", "ROOT", cert)
err = cmd.Run()
exitOnError(err, err)
}
// TrustCertificate trusts a PKI certificate.
// The method used to trust is determined based on the operating system
// and available tools installed on the machine.
func Trust(cert string) {
requireFileValue(&cert, "certificate")
// Execute trust strategy based on OS
switch runtime.GOOS {
case "darwin":
TrustDarwin(cert)
case "linux":
TrustLinux(cert)
case "windows":
TrustWindows(cert)
default:
exit(1, fmt.Sprintf("The operating system '%s' is currently unsupported.\n", runtime.GOOS))
}
}
// trustCertificate defines the CLI command to trust a PKI certificate.
func trustCertificates(flags ...string) {
// Initialize command
cmd, args = command.NewCommand(commandName("trust"), "Trust PKI certificates", func(h *command.Command) {
h.AddArgument("CERTIFICATE_FILES...")
h.AddExample("Trust a single certificate", "test.com.csr.pem")
h.AddExample("Trust multiple certificates", "local-root.ca.cert.pem remote.ca.cert.pem test.com.csr.pem")
h.AddSubcommand("help", "Display this help screen")
}, flags...)
switch getArgument(true) {
case "", "help":
cmd.Usage()
default:
log("Sudo permissions are required to trust certificates")
for _, cert := range flags {
Trust(cert)
}
}
}