/
cert_install.go
91 lines (79 loc) · 2.12 KB
/
cert_install.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
package command
import (
"context"
"os"
"path/filepath"
"github.com/local-deploy/dl/helper"
"github.com/local-deploy/dl/utils/cert"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
var reinstallCert bool
func installCertCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "install",
Aliases: []string{"i"},
Short: "Installing CA certificate",
Long: `Generating a self-signed CA certificate and installing it in Firefox and/or Chrome/Chromium browsers.`,
Run: func(cmd *cobra.Command, args []string) {
installCertRun()
},
ValidArgs: []string{"--reinstall", "-r"},
}
cmd.Flags().BoolVarP(&reinstallCert, "reinstall", "r", false, "Reinstall certificate")
return cmd
}
func installCertRun() {
certutilPath, err := helper.CertutilPath()
if err != nil {
pterm.FgRed.Printfln("Error: %s", err)
return
}
err = helper.CreateDirectory(filepath.Join(helper.CertDir(), "conf"))
if err != nil {
pterm.FgRed.Printfln("Error: %s \n", err)
os.Exit(1)
}
c := &cert.Cert{
CertutilPath: certutilPath,
CaFileName: cert.CaRootName,
CaFileKeyName: cert.CaRootKeyName,
CaPath: helper.CertDir(),
}
if reinstallCert {
err = c.LoadCA()
if err != nil {
pterm.FgRed.Printfln("Error: %s", err)
return
}
c.Uninstall()
helper.RemoveFilesInPath(filepath.Join(helper.CertDir(), "conf"))
helper.RemoveFilesInPath(helper.CertDir())
}
_, err = os.Stat(filepath.Join(helper.CertDir(), cert.CaRootName))
if err != nil {
err := c.CreateCA()
if err != nil {
pterm.FgRed.Printfln("Error: %s", err)
return
}
}
err = c.LoadCA()
if err != nil {
pterm.FgRed.Printfln("Error: %s", err)
return
}
if c.Check() {
pterm.FgGreen.Println("The local CA is already installed in the browsers trust store!")
} else if c.Install() {
storeCertConfig(true)
pterm.FgGreen.Println("The local CA is now installed in the browsers trust store (requires browser restart)!")
// Recreate local services
recreate = true
ctx := context.Background()
err = upServiceRun(ctx)
if err != nil {
pterm.FgYellow.Println("Restart services for changes to take effect: dl service recreate")
}
}
}