Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
64bb346
Add function to retrieve certificates expiration date
MatteoPologruto Apr 23, 2024
0cb308a
Check the certificate expiration date
MatteoPologruto Apr 23, 2024
431dc58
Obtain certificates info using the systray icon
MatteoPologruto Apr 24, 2024
0463976
Manage errors that may occur retrieving certificates expiration date
MatteoPologruto Apr 24, 2024
6a0017f
Obtain default browser name on macOS
MatteoPologruto Apr 30, 2024
f80791d
Prompt Safari users to install HTTPS certificates and check if they a…
MatteoPologruto Apr 30, 2024
40f50f4
Skip some tests on macOS because the user is prompted to install cert…
MatteoPologruto May 2, 2024
f1f76a3
Set installCerts value in config.ini if certicates are manually insta…
MatteoPologruto May 2, 2024
88495ca
Always set installCerts if the certificates exist
MatteoPologruto May 2, 2024
a438fed
Add "Arduino Agent" to the title of dialogs
Xayton May 2, 2024
66ba136
Fix check for pressed buttons
Xayton May 2, 2024
52961e2
Move osascript execution function to Utilities to avoid code duplication
MatteoPologruto May 6, 2024
a759046
Modify certificate management from the systray menu
MatteoPologruto May 6, 2024
46ceb5d
Install certificates if they are missing and the flag inside the conf…
MatteoPologruto May 7, 2024
144515b
Avoid code duplication
MatteoPologruto May 7, 2024
e9c71b3
Fix button order and title
Xayton May 7, 2024
1ba5ed1
Do not restart the Agent if no action is performed on the certificate
MatteoPologruto May 7, 2024
1ec7171
Do not modify the config if the default browser is not Safari
MatteoPologruto May 7, 2024
27d8b76
Small messages/titles fixes
Xayton May 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move osascript execution function to Utilities to avoid code duplication
  • Loading branch information
MatteoPologruto committed May 8, 2024
commit 52961e28e121d93a3c3bb8ea8c1d7610656a180f
12 changes: 6 additions & 6 deletions certificates/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import (
"math/big"
"net"
"os"
"os/exec"
"strings"
"time"

"github.com/arduino/arduino-create-agent/utilities"
"github.com/arduino/go-paths-helper"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -285,18 +286,17 @@ func PromptInstallCertsSafari() bool {
if GetDefaultBrowserName() != "Safari" {
return false
}
oscmd := exec.Command("osascript", "-e", "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 Certificates\"")
pressed, _ := oscmd.Output()
return strings.Contains(string(pressed), "button returned:Install the certificate for Safari")
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 Certificates\"")
return strings.Contains(string(buttonPressed), "button returned:Install the certificate for Safari")
}

// PromptExpiredCerts prompts the user to update the HTTPS certificates if they are using Safari
func PromptExpiredCerts(certDir *paths.Path) {
if expired, err := isExpired(); err != nil {
log.Errorf("cannot check if certificates are expired something went wrong: %s", err)
} else if expired {
oscmd := exec.Command("osascript", "-e", "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 Certificates\"")
if pressed, _ := oscmd.Output(); strings.Contains(string(pressed), "button returned:Update the certificate for Safari") {
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 Certificates\"")
if strings.Contains(string(buttonPressed), "button returned:Update the certificate for Safari") {
err := UninstallCertificates()
if err != nil {
log.Errorf("cannot uninstall certificates something went wrong: %s", err)
Expand Down
13 changes: 5 additions & 8 deletions certificates/install_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ const char *getDefaultBrowserName() {
import "C"
import (
"errors"
"os/exec"
"strings"
"unsafe"

log "github.com/sirupsen/logrus"

"github.com/arduino/arduino-create-agent/utilities"
"github.com/arduino/go-paths-helper"
)

Expand All @@ -172,9 +172,8 @@ func InstallCertificate(cert *paths.Path) error {
p := C.installCert(ccert)
s := C.GoString(p)
if len(s) != 0 {
oscmd := exec.Command("osascript", "-e", "display dialog \""+s+"\" buttons \"OK\" with title \"Arduino Agent: Error installing certificates\"")
_ = oscmd.Run()
_ = UninstallCertificates()
utilities.UserPrompt("display dialog \"" + s + "\" buttons \"OK\" with title \"Arduino Agent: Error installing certificates\"")
UninstallCertificates()
return errors.New(s)
}
return nil
Expand All @@ -187,8 +186,7 @@ func UninstallCertificates() error {
p := C.uninstallCert()
s := C.GoString(p)
if len(s) != 0 {
oscmd := exec.Command("osascript", "-e", "display dialog \""+s+"\" buttons \"OK\" with title \"Arduino Agent: Error uninstalling certificates\"")
_ = oscmd.Run()
utilities.UserPrompt("display dialog \"" + s + "\" buttons \"OK\" with title \"Arduino Agent: Error uninstalling certificates\"")
return errors.New(s)
}
return nil
Expand All @@ -202,8 +200,7 @@ func GetExpirationDate() (string, error) {
p := C.getExpirationDate(dateString)
s := C.GoString(p)
if len(s) != 0 {
oscmd := exec.Command("osascript", "-e", "display dialog \""+s+"\" buttons \"OK\" with title \"Arduino Agent: Error retrieving expiration date\"")
_ = oscmd.Run()
utilities.UserPrompt("display dialog \"" + s + "\" buttons \"OK\" with title \"Arduino Agent: Error retrieving expiration date\"")
return "", errors.New(s)
}
date := C.GoString(dateString)
Expand Down
10 changes: 2 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"html/template"
"io"
"os"
"os/exec"
"regexp"
"runtime"
"runtime/debug"
Expand All @@ -40,6 +39,7 @@ import (
"github.com/arduino/arduino-create-agent/systray"
"github.com/arduino/arduino-create-agent/tools"
"github.com/arduino/arduino-create-agent/updater"
"github.com/arduino/arduino-create-agent/utilities"
v2 "github.com/arduino/arduino-create-agent/v2"
paths "github.com/arduino/go-paths-helper"
cors "github.com/gin-contrib/cors"
Expand Down Expand Up @@ -178,7 +178,7 @@ func loop() {
// If we are updating manually from 1.2.7 to 1.3.0 we have to uninstall the old agent manually first.
// This check will inform the user if he needs to run the uninstall first
if runtime.GOOS == "darwin" && oldInstallExists() {
printDialog("Old agent installation of the Arduino Create Agent found, please uninstall it before launching the new one")
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\"")
os.Exit(0)
}

Expand Down Expand Up @@ -498,12 +498,6 @@ func oldInstallExists() bool {
return oldAgentPath.Join("ArduinoCreateAgent.app").Exist()
}

// printDialog will print a GUI error dialog on macos
func printDialog(dialogText string) {
oscmd := exec.Command("osascript", "-e", "display dialog \""+dialogText+"\" buttons \"OK\" with title \"Error\"")
_ = oscmd.Run()
}

func parseIni(filename string) (args []string, err error) {
cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: false, AllowPythonMultilineValues: true}, filename)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions systray/systray_real.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ package systray

import (
"os"
"os/exec"
"runtime"

"fyne.io/systray"
cert "github.com/arduino/arduino-create-agent/certificates"
"github.com/arduino/arduino-create-agent/config"
"github.com/arduino/arduino-create-agent/icon"
"github.com/arduino/arduino-create-agent/utilities"
"github.com/go-ini/ini"
log "github.com/sirupsen/logrus"
"github.com/skratchdot/open-golang/open"
Expand Down Expand Up @@ -133,8 +133,7 @@ func (s *Systray) start() {
} else {
infoMsg = infoMsg + "- Certificate installed: No\n- Certificate trusted: N/A\n- Certificate expiration date: N/A"
}
oscmd := exec.Command("osascript", "-e", "display dialog \""+infoMsg+"\" buttons \"OK\" with title \"Arduino Agent: certificates info\"")
_ = oscmd.Run()
utilities.UserPrompt("display dialog \"" + infoMsg + "\" buttons \"OK\" with title \"Arduino Agent: certificates info\"")
case <-mPause.ClickedCh:
s.Pause()
case <-mQuit.ClickedCh:
Expand Down
7 changes: 7 additions & 0 deletions utilities/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,10 @@ func VerifyInput(input string, signature string) error {
d := h.Sum(nil)
return rsa.VerifyPKCS1v15(rsaKey, crypto.SHA256, d, sign)
}

// UserPrompt executes an osascript and returns the pressed button
func UserPrompt(dialog string) string {
oscmd := exec.Command("osascript", "-e", dialog)
pressedButton, _ := oscmd.Output()
return string(pressedButton)
}