Skip to content

Commit f94d69d

Browse files
fixes for trust manager
1 parent 5c0def1 commit f94d69d

9 files changed

Lines changed: 138 additions & 43 deletions

File tree

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func init() {
5454

5555
webCmd.AddCommand(web.BuildInstallCommand())
5656
webCmd.AddCommand(web.BuildUninstallCommand())
57+
webCmd.AddCommand(web.BuildTrustCommand())
5758

5859
rootCmd.AddCommand(webCmd)
5960

cmd/web/trust.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package web
2+
3+
import (
4+
"path/filepath"
5+
6+
"github.com/fatih/color"
7+
"github.com/lumosolutions/yerd/internal/config"
8+
"github.com/lumosolutions/yerd/internal/constants"
9+
"github.com/lumosolutions/yerd/internal/manager"
10+
"github.com/lumosolutions/yerd/internal/utils"
11+
"github.com/lumosolutions/yerd/internal/version"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
func BuildTrustCommand() *cobra.Command {
16+
return &cobra.Command{
17+
Use: "trust",
18+
Short: "Attempts to refresh the YERD CA for Chrome",
19+
Run: func(cmd *cobra.Command, args []string) {
20+
version.PrintSplash()
21+
red := color.New(color.FgRed)
22+
green := color.New(color.FgGreen)
23+
24+
if !utils.CheckAndPromptForSudo() {
25+
return
26+
}
27+
28+
webConfig := config.GetWebConfig()
29+
30+
if !webConfig.Installed {
31+
red.Println("YERD web components are not installed")
32+
return
33+
}
34+
35+
cm := manager.NewCertificateManager()
36+
37+
caPath := filepath.Join(constants.CertsDir, "ca")
38+
caFile := "yerd.crt"
39+
40+
cm.ChromeUntrust()
41+
if err := cm.ChromeTrust(caPath, caFile); err != nil {
42+
red.Println("Unable to trust CA cert with chrome due to the following error:")
43+
red.Println(err)
44+
return
45+
}
46+
47+
green.Println("Chrome Trust Updated")
48+
},
49+
}
50+
}

cmd/web/uninstall.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package web
22

33
import (
44
"github.com/fatih/color"
5+
"github.com/lumosolutions/yerd/internal/config"
56
"github.com/lumosolutions/yerd/internal/installers/nginx"
67
"github.com/lumosolutions/yerd/internal/utils"
78
"github.com/lumosolutions/yerd/internal/version"
@@ -14,9 +15,7 @@ func BuildUninstallCommand() *cobra.Command {
1415
Short: "Uninstalls the web components required for local development",
1516
Run: func(cmd *cobra.Command, args []string) {
1617
version.PrintSplash()
17-
//green := color.New(color.FgGreen)
18-
//yellow := color.New(color.FgYellow)
19-
//blue := color.New(color.FgBlue)
18+
green := color.New(color.FgGreen)
2019
red := color.New(color.FgRed)
2120

2221
if !utils.CheckAndPromptForSudo() {
@@ -29,6 +28,14 @@ func BuildUninstallCommand() *cobra.Command {
2928
}
3029

3130
installer.Uninstall()
31+
32+
newConfig := &config.WebConfig{
33+
Installed: false,
34+
}
35+
36+
config.SetStruct("web", newConfig)
37+
38+
green.Println("Successfully uninstalled web components")
3239
},
3340
}
3441
}

internal/config/web.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,16 @@ type SiteConfig struct {
1111
Domain string `json:"domain"`
1212
PhpVersion string `json:"php_version"`
1313
}
14+
15+
func GetWebConfig() *WebConfig {
16+
var webConfig *WebConfig
17+
err := GetStruct("web", &webConfig)
18+
if err != nil || webConfig == nil {
19+
webConfig = &WebConfig{
20+
Installed: false,
21+
Sites: make(map[string]SiteConfig),
22+
}
23+
}
24+
25+
return webConfig
26+
}

internal/installers/nginx/installer.go

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ func NewNginxInstaller(update, forceConfig bool) (*NginxInstaller, error) {
4242
func (installer *NginxInstaller) Uninstall() error {
4343
installer.Spinner.UpdatePhrase("Uninstalling Web Components...")
4444

45-
var webConfig *config.WebConfig
46-
err := config.GetStruct("web", &webConfig)
47-
if err != nil || webConfig == nil {
48-
webConfig = &config.WebConfig{}
49-
}
45+
webConfig := config.GetWebConfig()
5046

5147
if webConfig.Sites != nil {
5248
for _, site := range webConfig.Sites {
@@ -58,32 +54,27 @@ func (installer *NginxInstaller) Uninstall() error {
5854
utils.SystemdStopService("yerd-nginx")
5955
utils.SystemdDisable("yerd-nginx")
6056

61-
params := []string{"-D", "-n", "YERD CA", "-d", "sql:$HOME/.pki/nssdb"}
62-
utils.ExecuteCommand("certutil", params...)
63-
6457
utils.RemoveFolder(constants.YerdWebDir)
6558
utils.RemoveFile(filepath.Join(constants.SystemdDir, "yerd-nginx.service"))
6659

6760
utils.SystemdReload()
6861

62+
dm, _ := manager.NewDependencyManager()
63+
dm.RemoveTrust()
64+
6965
return nil
7066
}
7167

7268
func (installer *NginxInstaller) Install() error {
7369
installer.Spinner.Start()
74-
75-
var webConfig *config.WebConfig
76-
err := config.GetStruct("web", &webConfig)
77-
if err != nil || webConfig == nil {
78-
webConfig = &config.WebConfig{Installed: false}
79-
}
70+
webConfig := config.GetWebConfig()
8071

8172
if webConfig.Installed {
8273
installer.Spinner.StopWithError("Web Components are already installed")
8374
return fmt.Errorf("already installed")
8475
}
8576

86-
err = utils.RunAll(
77+
err := utils.RunAll(
8778
func() error { return installer.installDependencies() },
8879
func() error { return installer.prepareInstall() },
8980
func() error { return installer.downloadSource() },
@@ -287,25 +278,15 @@ func (installer *NginxInstaller) addSystemdService() error {
287278
func (installer *NginxInstaller) writeConfig() error {
288279
installer.Spinner.UpdatePhrase("Writing YERD Configuration")
289280

290-
var existing *config.WebConfig
291-
err := config.GetStruct("web", &existing)
292-
if err != nil || existing == nil {
293-
newConfig := config.WebConfig{
294-
Installed: true,
295-
}
281+
webConfig := config.GetWebConfig()
296282

297-
config.SetStruct("web", newConfig)
298-
installer.Spinner.AddSuccessStatus("YERD Configuration Created")
299-
return nil
300-
}
301-
302-
if existing.Installed {
283+
if webConfig.Installed {
303284
installer.Spinner.AddInfoStatus("- YERD configuration does not need updating")
304285
return nil
305286
}
306287

307-
existing.Installed = true
308-
config.SetStruct("web", existing)
288+
webConfig.Installed = true
289+
config.SetStruct("web", webConfig)
309290

310291
hostManager := utils.NewHostsManager()
311292
hostManager.Install()

internal/manager/certificate.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,20 @@ func (certManager *CertificateManager) GenerateCaCertificate(name string) error
3737
depMan, _ := NewDependencyManager()
3838
depMan.TrustCertificate(filepath.Join(caPath, certName), name)
3939

40-
params = []string{"-A", "-n", "YERD CA", "-t", "TCu,Cu,Tu", "-i", filepath.Join(caPath, certName), "-d", "sql:$HOME/.pki/nssdb"}
41-
utils.ExecuteCommand("certutil", params...)
40+
certManager.ChromeTrust(caPath, certName)
41+
42+
return nil
43+
}
44+
45+
func (certManager *CertificateManager) ChromeTrust(caPath, certName string) error {
46+
userCtx, _ := utils.GetRealUser()
47+
48+
params := []string{"-A", "-n", "YERD CA", "-t", "TCu,Cu,Tu", "-i", filepath.Join(caPath, certName), "-d", fmt.Sprintf("sql:%s/.pki/nssdb", userCtx.HomeDir)}
49+
50+
if _, success := utils.ExecuteCommandAsUser("certutil", params...); !success {
51+
utils.LogInfo("cacert", "cert command failed")
52+
return fmt.Errorf("failed to trust certificate")
53+
}
4254

4355
return nil
4456
}
@@ -121,3 +133,10 @@ func (certManager *CertificateManager) generateSiteCertificate(certPath, domain,
121133

122134
return true
123135
}
136+
137+
func (certManager *CertificateManager) ChromeUntrust() {
138+
userCtx, _ := utils.GetRealUser()
139+
140+
params := []string{"-D", "-n", "YERD CA", "-d", fmt.Sprintf("sql:%s/.pki/nssdb", userCtx.HomeDir)}
141+
utils.ExecuteCommandAsUser("certutil", params...)
142+
}

internal/manager/manager.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ func NewDependencyManager() (*DependencyManager, error) {
4747
}, nil
4848
}
4949

50-
func (dm *DependencyManager) TrustCertificate(certificate, name string) error {
51-
// Map distros to their certificate paths
50+
func (d *DependencyManager) getCertPath(distro string) (string, error) {
5251
certPaths := map[string]string{
5352
"ubuntu": "/usr/local/share/ca-certificates",
5453
"debian": "/usr/local/share/ca-certificates",
@@ -66,16 +65,33 @@ func (dm *DependencyManager) TrustCertificate(certificate, name string) error {
6665
"sles": "/etc/pki/trust/anchors",
6766
}
6867

69-
certPath, ok := certPaths[dm.distro]
68+
certPath, ok := certPaths[distro]
7069
if !ok {
71-
return fmt.Errorf("distro '%s' not supported yet", dm.distro)
70+
return "", fmt.Errorf("distro '%s' not supported yet", distro)
71+
}
72+
73+
return certPath, nil
74+
}
75+
76+
func (dm *DependencyManager) TrustCertificate(certificate, name string) error {
77+
certPath, err := dm.getCertPath(dm.distro)
78+
if err != nil {
79+
return err
7280
}
7381

7482
destFile := fmt.Sprintf("%s/%s-ca.crt", certPath, name)
7583
if _, success := utils.ExecuteCommand("cp", certificate, destFile); !success {
7684
return fmt.Errorf("failed to copy certificate for %s", dm.distro)
7785
}
7886

87+
if err := dm.execTrustUpdate(); err != nil {
88+
return err
89+
}
90+
91+
return nil
92+
}
93+
94+
func (dm *DependencyManager) execTrustUpdate() error {
7995
switch dm.distro {
8096
case "arch", "manjaro":
8197
if _, success := utils.ExecuteCommand("trust", "extract-compat"); !success {
@@ -96,6 +112,17 @@ func (dm *DependencyManager) TrustCertificate(certificate, name string) error {
96112
return nil
97113
}
98114

115+
func (dm *DependencyManager) RemoveTrust() error {
116+
certPath, err := dm.getCertPath(dm.distro)
117+
if err != nil {
118+
return err
119+
}
120+
121+
destFile := fmt.Sprintf("%s/%s-ca.crt", certPath, "yerd")
122+
utils.RemoveFile(destFile)
123+
return dm.execTrustUpdate()
124+
}
125+
99126
// detectDistribution identifies the Linux distribution using multiple detection methods.
100127
// Returns distribution name or error if detection fails.
101128
func detectDistribution() (string, error) {

internal/manager/site.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ func NewSiteManager() (*SiteManager, error) {
2626
s := utils.NewSpinner("Managing Sites...")
2727
s.SetDelay(150)
2828

29-
var webConfig *config.WebConfig
30-
if err := config.GetStruct("web", &webConfig); err != nil {
31-
webConfig = &config.WebConfig{}
32-
}
29+
webConfig := config.GetWebConfig()
3330

3431
if !webConfig.Installed {
3532
return nil, fmt.Errorf("web not installed")

internal/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/fatih/color"
77
)
88

9-
const Version = "1.1.5"
9+
const Version = "1.1.6"
1010
const Branch = "main"
1111
const Repo = "LumoSolutions/yerd"
1212

0 commit comments

Comments
 (0)