From 458356da454cd87a35c16df3d1b7fe1ff480f03e Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Thu, 28 Sep 2023 00:06:12 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20fix=20panic=20during=20login=20w?= =?UTF-8?q?ith=20wrong=20token=20(#1963)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/cnquery/cmd/login.go | 20 ++++++++++++++------ apps/cnquery/cmd/status.go | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/apps/cnquery/cmd/login.go b/apps/cnquery/cmd/login.go index f927801ca4..f645e110a8 100644 --- a/apps/cnquery/cmd/login.go +++ b/apps/cnquery/cmd/login.go @@ -5,6 +5,7 @@ package cmd import ( "context" + "os" "strings" "time" @@ -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 != "" { @@ -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") @@ -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 @@ -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")) diff --git a/apps/cnquery/cmd/status.go b/apps/cnquery/cmd/status.go index 655fc83dc1..6e2418df00 100644 --- a/apps/cnquery/cmd/status.go +++ b/apps/cnquery/cmd/status.go @@ -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())