Skip to content

Commit

Permalink
allow version and help to run without a valid cert
Browse files Browse the repository at this point in the history
  • Loading branch information
taigrr committed Oct 18, 2023
1 parent 1c73c91 commit e0dde6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
18 changes: 7 additions & 11 deletions api/client/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,36 @@ 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"
)

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,
Expand All @@ -57,4 +52,5 @@ func CreateSecureTransport() {
}
APIClient.Transport = apiTransport
APIClient.Timeout = time.Second * 10
return nil
}
15 changes: 13 additions & 2 deletions cmd/grlx/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e0dde6c

Please sign in to comment.