Skip to content

Commit aa02ed0

Browse files
Do not ask again to update the certificate if the user refuses once
1 parent 8006310 commit aa02ed0

File tree

4 files changed

+55
-55
lines changed

4 files changed

+55
-55
lines changed

certificates/certificates.go

+2-28
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ import (
3030
"math/big"
3131
"net"
3232
"os"
33-
"strings"
3433
"time"
3534

36-
"github.com/arduino/arduino-create-agent/utilities"
3735
"github.com/arduino/go-paths-helper"
3836
log "github.com/sirupsen/logrus"
3937
)
@@ -270,8 +268,8 @@ func DeleteCertificates(certDir *paths.Path) {
270268
certDir.Join("cert.cer").Remove()
271269
}
272270

273-
// isExpired checks if a certificate is expired or about to expire (less than 1 month)
274-
func isExpired() (bool, error) {
271+
// IsExpired checks if a certificate is expired or about to expire (less than 1 month)
272+
func IsExpired() (bool, error) {
275273
bound := time.Now().AddDate(0, 1, 0)
276274
dateS, err := GetExpirationDate()
277275
if err != nil {
@@ -281,30 +279,6 @@ func isExpired() (bool, error) {
281279
return date.Before(bound), nil
282280
}
283281

284-
// PromptInstallCertsSafari prompts the user to install the HTTPS certificates if they are using Safari
285-
func PromptInstallCertsSafari() bool {
286-
buttonPressed := utilities.UserPrompt("display dialog \"The Arduino Agent needs a local HTTPS certificate to work correctly with Safari.\nIf you use Safari, you need to install it.\" buttons {\"Do not install\", \"Install the certificate for Safari\"} default button 2 with title \"Arduino Agent: Install certificate\"")
287-
return strings.Contains(string(buttonPressed), "button returned:Install the certificate for Safari")
288-
}
289-
290-
// PromptExpiredCerts prompts the user to update the HTTPS certificates if they are using Safari
291-
func PromptExpiredCerts(certDir *paths.Path) {
292-
if expired, err := isExpired(); err != nil {
293-
log.Errorf("cannot check if certificates are expired something went wrong: %s", err)
294-
} else if expired {
295-
buttonPressed := utilities.UserPrompt("display dialog \"The Arduino Agent needs a local HTTPS certificate to work correctly with Safari.\nYour certificate is expired or close to expiration. Do you want to update it?\" buttons {\"Do not update\", \"Update the certificate for Safari\"} default button 2 with title \"Arduino Agent: Update certificate\"")
296-
if strings.Contains(string(buttonPressed), "button returned:Update the certificate for Safari") {
297-
err := UninstallCertificates()
298-
if err != nil {
299-
log.Errorf("cannot uninstall certificates something went wrong: %s", err)
300-
} else {
301-
DeleteCertificates(certDir)
302-
GenerateAndInstallCertificates(certDir)
303-
}
304-
}
305-
}
306-
}
307-
308282
// GenerateAndInstallCertificates generates and installs the certificates
309283
func GenerateAndInstallCertificates(certDir *paths.Path) {
310284
GenerateCertificates(certDir)

main.go

+27-4
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func loop() {
178178
// If we are updating manually from 1.2.7 to 1.3.0 we have to uninstall the old agent manually first.
179179
// This check will inform the user if he needs to run the uninstall first
180180
if runtime.GOOS == "darwin" && oldInstallExists() {
181-
utilities.UserPrompt("display dialog \"Old agent installation of the Arduino Create Agent found, please uninstall it before launching the new one\" buttons \"OK\" with title \"Error\"")
181+
utilities.UserPrompt("Old agent installation of the Arduino Create Agent found, please uninstall it before launching the new one", "\"OK\"", "OK", "Error")
182182
os.Exit(0)
183183
}
184184

@@ -233,7 +233,7 @@ func loop() {
233233
log.Panicf("config.ini cannot be parsed: %s", err)
234234
}
235235
} else if cert.GetDefaultBrowserName() == "Safari" {
236-
if cert.PromptInstallCertsSafari() {
236+
if promptInstallCertsSafari() {
237237
err = config.SetInstallCertsIni(configPath.String(), "true")
238238
if err != nil {
239239
log.Panicf("config.ini cannot be parsed: %s", err)
@@ -374,8 +374,27 @@ func loop() {
374374
// check if the HTTPS certificates are expired or expiring and prompt the user to update them on macOS
375375
if runtime.GOOS == "darwin" && *installCerts {
376376
if config.CertsExist() {
377-
cert.PromptExpiredCerts(config.GetCertificatesDir())
378-
} else if cert.PromptInstallCertsSafari() {
377+
certDir := config.GetCertificatesDir()
378+
if expired, err := cert.IsExpired(); err != nil {
379+
log.Errorf("cannot check if certificates are expired something went wrong: %s", err)
380+
} else if expired {
381+
buttonPressed := utilities.UserPrompt("The Arduino Agent needs a local HTTPS certificate to work correctly with Safari.\nYour certificate is expired or close to expiration. Do you want to update it?", "{\"Do not update\", \"Update the certificate for Safari\"}", "Update the certificate for Safari", "Arduino Agent: Update certificate")
382+
if buttonPressed {
383+
err := cert.UninstallCertificates()
384+
if err != nil {
385+
log.Errorf("cannot uninstall certificates something went wrong: %s", err)
386+
} else {
387+
cert.DeleteCertificates(certDir)
388+
cert.GenerateAndInstallCertificates(certDir)
389+
}
390+
} else {
391+
err = config.SetInstallCertsIni(configPath.String(), "false")
392+
if err != nil {
393+
log.Panicf("config.ini cannot be parsed: %s", err)
394+
}
395+
}
396+
}
397+
} else if promptInstallCertsSafari() {
379398
// installing the certificates from scratch at this point should only happen if
380399
// something went wrong during previous installation attempts
381400
cert.GenerateAndInstallCertificates(config.GetCertificatesDir())
@@ -534,3 +553,7 @@ func installCertsKeyExists(filename string) (bool, error) {
534553
}
535554
return cfg.Section("").HasKey("installCerts"), nil
536555
}
556+
557+
func promptInstallCertsSafari() bool {
558+
return utilities.UserPrompt("The Arduino Agent needs a local HTTPS certificate to work correctly with Safari.\nIf you use Safari, you need to install it.", "{\"Do not install\", \"Install the certificate for Safari\"}", "Install the certificate for Safari", "Arduino Agent: Install certificate")
559+
}

systray/systray_real.go

+23-20
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package systray
2222
import (
2323
"os"
2424
"runtime"
25-
"strings"
2625

2726
"fyne.io/systray"
2827
cert "github.com/arduino/arduino-create-agent/certificates"
@@ -95,38 +94,42 @@ func (s *Systray) start() {
9594
s.updateMenuItem(mRmCrashes, config.LogsIsEmpty())
9695
case <-mManageCerts.ClickedCh:
9796
infoMsg := "The Arduino Agent needs a local HTTPS certificate to work correctly with Safari.\n\nYour HTTPS certificate status:\n"
98-
buttons := "{\"Install the certificate for Safari\", \"OK\"} default button \"OK\""
97+
buttons := "{\"OK\", \"Install the certificate for Safari\"}"
98+
defaultButton := "Install the certificate for Safari"
9999
certDir := config.GetCertificatesDir()
100100
if config.CertsExist() {
101101
expDate, err := cert.GetExpirationDate()
102102
if err != nil {
103103
log.Errorf("cannot get certificates expiration date, something went wrong: %s", err)
104104
}
105105
infoMsg = infoMsg + "- Certificate installed: Yes\n- Certificate trusted: Yes\n- Certificate expiration date: " + expDate
106-
buttons = "{\"Uninstall the certificate for Safari\", \"OK\"} default button \"OK\""
106+
buttons = "{\"OK\", \"Uninstall the certificate for Safari\"}"
107+
defaultButton = "Uninstall the certificate for Safari"
108+
pressedButton := utilities.UserPrompt(infoMsg, buttons, defaultButton, "Arduino Agent: Manage HTTPS certificate")
109+
if pressedButton {
110+
err := cert.UninstallCertificates()
111+
if err != nil {
112+
log.Errorf("cannot uninstall certificates something went wrong: %s", err)
113+
} else {
114+
cert.DeleteCertificates(certDir)
115+
err = config.SetInstallCertsIni(s.currentConfigFilePath.String(), "false")
116+
if err != nil {
117+
log.Errorf("cannot set installCerts value in config.ini: %s", err)
118+
}
119+
}
120+
s.Restart()
121+
}
107122
} else {
108123
infoMsg = infoMsg + "- Certificate installed: No\n- Certificate trusted: N/A\n- Certificate expiration date: N/A"
109-
}
110-
pressedButton := utilities.UserPrompt("display dialog \"" + infoMsg + "\" buttons " + buttons + " with title \"Arduino Agent: Manage HTTPS certificate\"")
111-
if strings.Contains(pressedButton, "Install the certificate for Safari") {
112-
cert.GenerateAndInstallCertificates(certDir)
113-
err := config.SetInstallCertsIni(s.currentConfigFilePath.String(), "true")
114-
if err != nil {
115-
log.Errorf("cannot set installCerts value in config.ini: %s", err)
116-
}
117-
s.Restart()
118-
} else if strings.Contains(pressedButton, "Uninstall the certificate for Safari") {
119-
err := cert.UninstallCertificates()
120-
if err != nil {
121-
log.Errorf("cannot uninstall certificates something went wrong: %s", err)
122-
} else {
123-
cert.DeleteCertificates(certDir)
124-
err = config.SetInstallCertsIni(s.currentConfigFilePath.String(), "false")
124+
pressedButton := utilities.UserPrompt(infoMsg, buttons, defaultButton, "Arduino Agent: Manage HTTPS certificate")
125+
if pressedButton {
126+
cert.GenerateAndInstallCertificates(certDir)
127+
err := config.SetInstallCertsIni(s.currentConfigFilePath.String(), "true")
125128
if err != nil {
126129
log.Errorf("cannot set installCerts value in config.ini: %s", err)
127130
}
131+
s.Restart()
128132
}
129-
s.Restart()
130133
}
131134
case <-mPause.ClickedCh:
132135
s.Pause()

utilities/utilities.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ func VerifyInput(input string, signature string) error {
151151
}
152152

153153
// UserPrompt executes an osascript and returns the pressed button
154-
func UserPrompt(dialog string) string {
155-
oscmd := exec.Command("osascript", "-e", dialog)
154+
func UserPrompt(dialog string, buttons string, defaultButton string, title string) bool {
155+
oscmd := exec.Command("osascript", "-e", "display dialog \""+dialog+"\" buttons "+buttons+" default button\""+defaultButton+"\" with title \""+title+"\"")
156156
pressedButton, _ := oscmd.Output()
157-
return string(pressedButton)
157+
return strings.Contains(string(pressedButton), "button returned:"+defaultButton)
158158
}

0 commit comments

Comments
 (0)