Skip to content

Commit

Permalink
🧹 fix panic during login with wrong token (#1963)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock authored Sep 27, 2023
1 parent a006762 commit 458356d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions apps/cnquery/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"context"
"os"
"strings"
"time"

Expand Down Expand Up @@ -147,14 +148,16 @@ func register(token string) {
// try to read local options
opts, optsErr := config.Read()
if optsErr != nil {
log.Fatal().Msg("could not load configuration, please use --token or --config with the appropriate values")
log.Warn().Msg("could not load configuration, please use --token or --config with the appropriate values")
os.Exit(1)
}
// print the used config to the user
config.DisplayUsedConfig()

httpClient, err = opts.GetHttpClient()
if err != nil {
log.Fatal().Err(err).Msg("could not create http client")
log.Warn().Err(err).Msg("could not create http client")
os.Exit(1)
}

if opts.AgentMrn != "" {
Expand All @@ -170,12 +173,14 @@ func register(token string) {
certAuth, err := upstream.NewServiceAccountRangerPlugin(credential)
if err != nil {
log.Warn().Err(err).Msg("could not initialize certificate authentication")
os.Exit(1)
}
plugins = append(plugins, certAuth)

client, err := upstream.NewAgentManagerClient(apiEndpoint, httpClient, plugins...)
if err != nil {
log.Fatal().Err(err).Msg("could not connect to Mondoo Platform")
log.Warn().Err(err).Msg("could not connect to Mondoo Platform")
os.Exit(1)
}

name := viper.GetString("name")
Expand Down Expand Up @@ -210,7 +215,8 @@ func register(token string) {

err = config.StoreConfig()
if err != nil {
log.Fatal().Err(err).Msg("could not write mondoo configuration")
log.Warn().Err(err).Msg("could not write mondoo configuration")
os.Exit(1)
}

// run ping pong to validate the service account
Expand All @@ -223,12 +229,14 @@ func register(token string) {
plugins = append(plugins, certAuth)
client, err := upstream.NewAgentManagerClient(apiEndpoint, httpClient, plugins...)
if err != nil {
log.Fatal().Err(err).Msg("could not connect to mondoo platform")
log.Warn().Err(err).Msg("could not connect to mondoo platform")
os.Exit(1)
}

_, err = client.PingPong(context.Background(), &upstream.Ping{})
if err != nil {
log.Fatal().Msg(err.Error())
log.Warn().Msg(err.Error())
os.Exit(1)
}

log.Info().Msgf("client %s has logged in successfully", viper.Get("agent_mrn"))
Expand Down
2 changes: 1 addition & 1 deletion apps/cnquery/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (s Status) RenderCliStatus() {

if s.Client.Registered && s.Client.PingPongError == nil {
log.Info().Msg(theme.DefaultTheme.Success("client authenticated successfully"))
} else {
} else if s.Client.PingPongError == nil {
log.Error().Err(s.Client.PingPongError).
Msgf("The Mondoo Platform credentials provided at %s didn't successfully authenticate with Mondoo Platform. Please re-authenticate with Mondoo Platform. To learn how, read https://mondoo.com/docs/cnspec/cnspec-adv-install/registration.",
viper.ConfigFileUsed())
Expand Down

0 comments on commit 458356d

Please sign in to comment.