Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
joanlopez committed Sep 9, 2024
1 parent cdbca39 commit e0de560
Showing 1 changed file with 38 additions and 31 deletions.
69 changes: 38 additions & 31 deletions cmd/cloud_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ the "k6 run -o cloud" command.
}

// run is the code that runs when the user executes `k6 cloud login`
//
//nolint:funlen
func (c *cmdCloudLogin) run(cmd *cobra.Command, _ []string) error {
currentDiskConf, err := readDiskConfig(c.globalState)
if err != nil {
Expand All @@ -82,18 +80,6 @@ func (c *cmdCloudLogin) run(cmd *cobra.Command, _ []string) error {
}
}

// We want to use this fully consolidated config for things like
// host addresses, so users can overwrite them with env vars.
consolidatedCurrentConfig, warn, err := cloudapi.GetConsolidatedConfig(
currentJSONConfigRaw, c.globalState.Env, "", nil, nil)
if err != nil {
return err
}

if warn != "" {
c.globalState.Logger.Warn(warn)
}

// But we don't want to save them back to the JSON file, we only
// want to save what already existed there and the login details.
newCloudConf := currentJSONConfig
Expand Down Expand Up @@ -135,24 +121,9 @@ func (c *cmdCloudLogin) run(cmd *cobra.Command, _ []string) error {
}

if newCloudConf.Token.Valid {
client := cloudapi.NewClient(
c.globalState.Logger,
newCloudConf.Token.String,
consolidatedCurrentConfig.Host.String,
consts.Version,
consolidatedCurrentConfig.Timeout.TimeDuration(),
)

var res *cloudapi.ValidateTokenResponse
res, err = client.ValidateToken()
err := validateToken(c.globalState, currentJSONConfigRaw, newCloudConf.Token.String)
if err != nil {
return fmt.Errorf("can't validate the API token: %s", err.Error())
}

if !res.IsValid {
return errors.New("your API token is invalid - " +
"please, consult the Grafana Cloud k6 documentation for instructions on how to generate a new one:\n" +
"https://grafana.com/docs/grafana-cloud/testing/k6/author-run/tokens-and-cli-authentication")
return err
}
}

Expand All @@ -174,3 +145,39 @@ func (c *cmdCloudLogin) run(cmd *cobra.Command, _ []string) error {
}
return nil
}

func validateToken(gs *state.GlobalState, jsonRawConf json.RawMessage, token string) error {
// We want to use this fully consolidated config for things like
// host addresses, so users can overwrite them with env vars.
consolidatedCurrentConfig, warn, err := cloudapi.GetConsolidatedConfig(
jsonRawConf, gs.Env, "", nil, nil)
if err != nil {
return err
}

if warn != "" {
gs.Logger.Warn(warn)
}

client := cloudapi.NewClient(
gs.Logger,
token,
consolidatedCurrentConfig.Host.String,
consts.Version,
consolidatedCurrentConfig.Timeout.TimeDuration(),
)

var res *cloudapi.ValidateTokenResponse
res, err = client.ValidateToken()
if err != nil {
return fmt.Errorf("can't validate the API token: %s", err.Error())
}

if !res.IsValid {
return errors.New("your API token is invalid - " +
"please, consult the Grafana Cloud k6 documentation for instructions on how to generate a new one:\n" +
"https://grafana.com/docs/grafana-cloud/testing/k6/author-run/tokens-and-cli-authentication")
}

return nil
}

0 comments on commit e0de560

Please # to comment.