Skip to content

Commit

Permalink
1098 add ca into macos os store (#1107)
Browse files Browse the repository at this point in the history
* extract runner package

* task run in debug

* WIP

* Add a basic repair flag.

* Use MassaStation repair in MacOS postinstall script.

* implement add CA for darwin

* fix certutilBinaryPath for windows

* fix a fmt.Errorf

* Clean Add certificate to store for MacOS.

* enable debug log in macos postinstall script

* remove a debug log

* format code

* Remove logger import in store_darwin.go

* Fix CR comments.

* check type assertion

* refactor updateTrustSettings

* move config dir check

* log file for repair mode

* Add a basic unit test for MacOS Add certificate

* Add sudo check in MacOS Add certificate unit test

* improve a log message

Co-authored-by: Grégory Libert <[email protected]>

---------

Co-authored-by: thomas-senechal <[email protected]>
Co-authored-by: Thomas Sénéchal <[email protected]>
Co-authored-by: Grégory Libert <[email protected]>
  • Loading branch information
4 people authored Aug 21, 2023
1 parent 6802b4e commit 3046b23
Show file tree
Hide file tree
Showing 20 changed files with 367 additions and 54 deletions.
2 changes: 2 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ tasks:
- cmd: go build -o build/massastation/massastation.exe cmd/massastation/main.go

run:
env:
LOG_LEVEL: DEBUG
cmds:
- cmd: ./build/massastation/massastation
platforms: [linux, darwin]
Expand Down
42 changes: 23 additions & 19 deletions cmd/massastation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,45 @@ import (
"github.com/massalabs/station/int/initialize"
"github.com/massalabs/station/int/systray"
"github.com/massalabs/station/int/systray/update"
"github.com/massalabs/station/pkg/dirs"
"github.com/massalabs/station/pkg/logger"
"github.com/massalabs/station/pkg/plugin"
)

func ParseFlags() api.StartServerFlags {
type StartFlags struct {
Version bool
Repair bool
}

func ParseFlags() (api.StartServerFlags, StartFlags) {
const httpPort = 80

const httpsPort = 443

var flags api.StartServerFlags
var serverFlags api.StartServerFlags

_, err := dirs.GetConfigDir()
if err != nil {
logger.Errorf(
"Unable to read config dir: %s\n%s",
err,
`MassaStation can't run without a config directory.\n
Please reinstall MassaStation using the installer at https://github.com/massalabs/station and try again.`)
}
var startFlags StartFlags

flag.IntVar(&flags.Port, "http-port", httpPort, "HTTP port to listen to")
flag.IntVar(&flags.TLSPort, "https-port", httpsPort, "HTTPS port to listen to")
flag.BoolVar(&flags.Version, "version", false, "Print version info and exit")
flag.IntVar(&serverFlags.Port, "http-port", httpPort, "HTTP port to listen to")
flag.IntVar(&serverFlags.TLSPort, "https-port", httpsPort, "HTTPS port to listen to")
flag.BoolVar(&startFlags.Version, "version", false, "Print version info and exit")
flag.BoolVar(&startFlags.Repair, "repair", false, "Repair MassaStation")

flag.Parse()

return flags
return serverFlags, startFlags
}

func main() {
err := initialize.Logger()
serverFlags, stationStartFlags := ParseFlags()

err := initialize.Logger(stationStartFlags.Repair)
if err != nil {
log.Fatalf("while initializing logger: %s", err.Error())
}

defer logger.Close()

flags := ParseFlags()
if flags.Version {
if stationStartFlags.Version {
//nolint:forbidigo
fmt.Printf("Version:%s\n", config.Version)
logger.Close()
Expand All @@ -63,6 +62,11 @@ func main() {
logger.Fatalf("Error with you current system configuration: %s", err.Error())
}

if stationStartFlags.Repair {
logger.Infof("Repair process completed. Please check the logs for any potential errors.")
os.Exit(0)
}

networkManager, err := config.NewNetworkManager()
if err != nil {
logger.Fatalf("Failed to create NetworkManager:%s", err.Error())
Expand All @@ -71,7 +75,7 @@ func main() {
pluginManager := plugin.NewManager()

stationGUI, systrayMenu := systray.MakeGUI()
server := api.NewServer(flags)
server := api.NewServer(serverFlags)

update.StartUpdateCheck(&stationGUI, systrayMenu)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
go.uber.org/mock v0.2.0
go.uber.org/zap v1.24.0
golang.org/x/sys v0.5.0
howett.net/plist v1.0.0
)

require (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jackmordaunt/icns/v2 v2.2.1/go.mod h1:6aYIB9eSzyfHHMKqDf17Xrs1zetQPReAkiUSHzdw4cI=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
Expand Down Expand Up @@ -850,6 +851,7 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand All @@ -871,6 +873,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
howett.net/plist v1.0.0 h1:7CrbWYbPPO/PyNy38b2EB/+gYbjCe2DXBxgtOOZbSQM=
howett.net/plist v1.0.0/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
2 changes: 2 additions & 0 deletions installer/macos/scripts/postinstall
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ mkdir -m 777 -p $MASSASTATION_CERT_DIR || fatal "certs directory creation failed
ping -c 1 -t 1 test.massa >/dev/null 2>&1 || set_local_dns

mv ./uninstall.sh $MASSASTATION_CONFIG_DIR || fatal "uninstall script move failed."

LOG_LEVEL=DEBUG $2/MassaStation.app/Contents/MacOS/massastation --repair || fatal "failed to repair MassaStation"
1 change: 0 additions & 1 deletion int/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type StartServerFlags struct {
TLSPort int
TLSCertificate string
TLSCertificateKey string
Version bool
}

func setAPIFlags(server *restapi.Server, startFlags StartServerFlags) {
Expand Down
11 changes: 11 additions & 0 deletions int/config/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ const failureConsequence = "Station will only work using http, or you will have
func Check() error {
var resultErr error

// Check config directory.
_, err := dirs.GetConfigDir()
if err != nil {
logger.Errorf(
"Unable to read config dir: %s\n%s",
err,
`MassaStation can't run without a config directory.\n
Please reinstall MassaStation using the installer at https://github.com/massalabs/station and try again.`)
}

// Check certificates.
caRootPath, err := dirs.GetCertDir()
if err != nil {
return caNonBlockingError("failed to get CA path", err)
Expand Down
16 changes: 12 additions & 4 deletions int/initialize/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@ import (
)

const (
LogFileName = "massastation.log"
LogSubDirName = "logs"
LogFileName = "massastation.log"
RepairLogFileName = "massastation-repair.log"
LogSubDirName = "logs"
)

// Logger sets up the global logger.
func Logger() error {
func Logger(repairMode bool) error {
logDir, err := dirs.GetConfigDir()
if err != nil {
return fmt.Errorf("get config dir: %w", err)
}

logDirPath := filepath.Join(logDir, LogSubDirName)
logFilePath := filepath.Join(logDirPath, LogFileName)

var logFilePath string

if repairMode {
logFilePath = filepath.Join(logDirPath, RepairLogFileName)
} else {
logFilePath = filepath.Join(logDirPath, LogFileName)
}

if err := logger.InitializeGlobal(logFilePath); err != nil {
return fmt.Errorf("initialize global logger: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/certificate/store/store.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package store

import "fmt"
import "errors"

var ErrCertificateNotFound = fmt.Errorf("certificate not found")
var ErrCertificateNotFound = errors.New("certificate not found")
Loading

0 comments on commit 3046b23

Please sign in to comment.