From e0dde6c01e9c0ba15d3f614b4b55d34a0282189d Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Tue, 17 Oct 2023 23:01:27 -0700 Subject: [PATCH] allow version and help to run without a valid cert --- api/client/transport.go | 18 +++++++----------- cmd/grlx/cmd/root.go | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/api/client/transport.go b/api/client/transport.go index 8c6753b..fd497bf 100644 --- a/api/client/transport.go +++ b/api/client/transport.go @@ -3,13 +3,13 @@ package client import ( "crypto/tls" "crypto/x509" + "errors" + "fmt" "net" "net/http" "os" "time" - "github.com/taigrr/log-socket/log" - "github.com/gogrlx/grlx/config" "github.com/gogrlx/grlx/pki" "github.com/gogrlx/grlx/types" @@ -17,27 +17,22 @@ import ( var APIClient *http.Client -func init() { - CreateSecureTransport() -} - -func CreateSecureTransport() { +func CreateSecureTransport() error { APIClient = &http.Client{} config.LoadConfig("grlx") err := pki.LoadRootCA("grlx") if err != nil { - log.Error(err) + return err } RootCA := config.GrlxRootCA certPool := x509.NewCertPool() rootPEM, err := os.ReadFile(RootCA) if err != nil || rootPEM == nil { - log.Error(err) + return err } ok := certPool.AppendCertsFromPEM(rootPEM) if !ok { - log.Errorf("apiClient: failed to parse root certificate from %q", RootCA) - log.Error(types.ErrCannotParseRootCA) + return errors.Join(types.ErrCannotParseRootCA, fmt.Errorf("apiClient: failed to parse root certificate from %q", RootCA)) } var apiTransport http.RoundTripper = &http.Transport{ Proxy: http.ProxyFromEnvironment, @@ -57,4 +52,5 @@ func CreateSecureTransport() { } APIClient.Transport = apiTransport APIClient.Timeout = time.Second * 10 + return nil } diff --git a/cmd/grlx/cmd/root.go b/cmd/grlx/cmd/root.go index 1cd8357..f929225 100644 --- a/cmd/grlx/cmd/root.go +++ b/cmd/grlx/cmd/root.go @@ -74,12 +74,23 @@ func init() { } } err := pki.LoadRootCA("grlx") - if err != nil { + isVersionOrHelp := false + if len(os.Args) > 1 { + isVersionOrHelp = os.Args[1] == "version" || os.Args[1] == "help" + } + if err != nil && !isVersionOrHelp { fmt.Printf("error: %v\n", err) color.Red("The RootCA could not be loaded from %s. Exiting!", config.GrlxRootCA) os.Exit(1) } - client.CreateSecureTransport() + err = client.CreateSecureTransport() + if err != nil && !isVersionOrHelp { + if os.Args[1] != "version" { + fmt.Printf("error: %v\n", err) + color.Red("The API client could not be created. Exiting!") + os.Exit(1) + } + } } // initConfig reads in config file and ENV variables if set.