Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 fix panic during login with wrong token #1963

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading