From b04061023c941d879460d81e6e4c6019621dbc16 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Tue, 12 Nov 2024 10:42:49 +0100 Subject: [PATCH] =?UTF-8?q?[BUGFIX]=C2=A0Fix=20login=20command=20avoiding?= =?UTF-8?q?=20reusing=20previous=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Augustin Husson --- .github/ISSUE_TEMPLATE/bug_report.yml | 2 +- internal/cli/cmd/login/login.go | 29 +++++++++++++++------------ internal/cli/config/config.go | 23 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index f7fbce82d1..f27470fe29 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -42,7 +42,7 @@ body: - type: textarea attributes: label: Perses configuration file - description: Insert relevant configuration here. Don't forget to remove secrets. + description: Insert relevant configuration here. Don't forget to remove secrets. You can use the CLI to get it (all secret will be hidden). percli config --online render: yaml - type: textarea attributes: diff --git a/internal/cli/cmd/login/login.go b/internal/cli/cmd/login/login.go index b09b7a42b6..831daf2c6e 100644 --- a/internal/cli/cmd/login/login.go +++ b/internal/cli/cmd/login/login.go @@ -90,20 +90,22 @@ func (o *option) Complete(args []string) error { return fmt.Errorf("no URL has been provided neither found in the previous configuration") } - // create a new apiClient - o.restConfig = config.Global.RestClientConfig - o.restConfig.URL = o.url - if o.restConfig.TLSConfig == nil { - o.restConfig.TLSConfig = &secret.TLSConfig{} + // Create a new apiClient from scratch. + // We shouldn't use the previous context as for the moment we have a single config. + // So, switching from a Perses instance to another one without restarting from scratch the context/ the Perses client doesn't make sense. + o.restConfig = clientConfig.RestConfigClient{ + URL: o.url, + TLSConfig: &secret.TLSConfig{ + InsecureSkipVerify: o.insecureTLS, + }, } - o.restConfig.TLSConfig.InsecureSkipVerify = o.insecureTLS restClient, err := clientConfig.NewRESTClient(o.restConfig) if err != nil { return err } o.apiClient = api.NewWithClient(restClient) - // Finally get the API config, we will need for later + // Finally, get the API config; we will need it for later cfg, err := o.apiClient.Config() if err != nil { return err @@ -173,9 +175,10 @@ func (o *option) Execute() error { o.accessToken = token.AccessToken o.refreshToken = token.RefreshToken } - - o.restConfig.Authorization = secret.NewBearerToken(o.accessToken) - if writeErr := config.Write(&config.Config{ + if len(o.accessToken) > 0 { + o.restConfig.Authorization = secret.NewBearerToken(o.accessToken) + } + if writeErr := config.WriteFromScratch(&config.Config{ RestClientConfig: o.restConfig, RefreshToken: o.refreshToken, }); writeErr != nil { @@ -224,9 +227,9 @@ func (o *option) newLoginOption() (loginOption, error) { func (o *option) selectAndSetProvider() error { providers := o.remoteConfig.Security.Authentication.Providers - // The first step is to collect the different providers and store it into items + modifiers. - // items will be the selection items to display to users. - // modifiers will be the action to save the different user input into option struct. + // The first step is to collect the different providers and store it into options and modifiers. + // Options will be the selection items to display to users. + // Modifiers will be the action to save the different user input into option struct. modifiers := map[string]func(){} var options []huh.Option[string] diff --git a/internal/cli/config/config.go b/internal/cli/config/config.go index e301642c79..645ce90a20 100644 --- a/internal/cli/config/config.go +++ b/internal/cli/config/config.go @@ -220,3 +220,26 @@ func Write(cfg *Config) error { return os.WriteFile(filePath, data, 0600) } + +func WriteFromScratch(cfg *Config) error { + // this value has been set by the root command, and that will be the path where the config must be saved + filePath := Global.filePath + directory := filepath.Dir(filePath) + + if _, err := os.Stat(directory); os.IsNotExist(err) { + mkdirError := os.Mkdir(directory, 0700) + if mkdirError != nil { + return err + } + } else if err != nil { + return err + } + + data, err := json.Marshal(cfg) + + if err != nil { + return err + } + + return os.WriteFile(filePath, data, 0600) +}