From 5877f3f6b31a38c2b83110f5d4554eddb6fa237a Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Mon, 10 Jun 2024 17:42:17 +0200 Subject: [PATCH 1/3] fix(session): fix handling of non-file based sessions --- pkg/c8ysession/c8ysession.go | 8 ++++++++ pkg/cmd/root/root.go | 5 ++++- pkg/cmd/sessions/get/get.manual.go | 3 ++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/c8ysession/c8ysession.go b/pkg/c8ysession/c8ysession.go index 582315bf1..d1af4425b 100644 --- a/pkg/c8ysession/c8ysession.go +++ b/pkg/c8ysession/c8ysession.go @@ -251,3 +251,11 @@ func ClearEnvironmentVariables(shell utilities.ShellType) { func ClearProcessEnvironment() { utilities.ClearProcessEnvironment(GetSessionEnvKeys()) } + +func IsSessionFilePath(path string) bool { + if path == "" { + return false + } + path = strings.TrimPrefix(path, "file://") + return !strings.Contains(path, "://") +} diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 87c9075a2..4f95158fd 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -11,6 +11,7 @@ import ( "github.com/MakeNowJust/heredoc/v2" "github.com/reubenmiller/go-c8y-cli/v2/pkg/activitylogger" "github.com/reubenmiller/go-c8y-cli/v2/pkg/c8ydefaults" + "github.com/reubenmiller/go-c8y-cli/v2/pkg/c8ysession" activityLogCmd "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/activitylog" agentsCmd "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/agents" alarmsCmd "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/alarms" @@ -815,7 +816,9 @@ func (c *CmdRoot) checkSessionExists(cmd *cobra.Command, args []string) error { if sessionFile != "" { log.Infof("Loaded session: %s", cfg.HideSensitiveInformationIfActive(client, sessionFile)) if _, err := os.Stat(sessionFile); err != nil { - log.Warnf("Failed to verify session file. %s", err) + if c8ysession.IsSessionFilePath(sessionFile) { + log.Warnf("Failed to verify session file. %s", err) + } } } diff --git a/pkg/cmd/sessions/get/get.manual.go b/pkg/cmd/sessions/get/get.manual.go index 750c6768b..16504296b 100644 --- a/pkg/cmd/sessions/get/get.manual.go +++ b/pkg/cmd/sessions/get/get.manual.go @@ -5,6 +5,7 @@ import ( "encoding/json" "github.com/MakeNowJust/heredoc/v2" + "github.com/reubenmiller/go-c8y-cli/v2/pkg/c8ysession" "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/subcommand" "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil" "github.com/spf13/cobra" @@ -64,7 +65,7 @@ func (n *CmdGetSession) RunE(cmd *cobra.Command, args []string) error { } // Support looking up a session which is only controlled via env variables - if sessionPath == "" && client != nil { + if !c8ysession.IsSessionFilePath(sessionPath) && client != nil { cfg.Persistent.Set("host", client.GetHostname()) cfg.Persistent.Set("tenant", client.TenantName) cfg.Persistent.Set("username", client.GetUsername()) From 73996ab9528154f94661d53bcb092d738a9c7dcb Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Mon, 10 Jun 2024 17:42:57 +0200 Subject: [PATCH 2/3] fix(sessions login): ignore existing C8Y_SESSION variable when logging in --- pkg/cmd/sessions/login/login.manual.go | 4 ++++ pkg/config/cliConfiguration.go | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/pkg/cmd/sessions/login/login.manual.go b/pkg/cmd/sessions/login/login.manual.go index c651601e4..5c3f83c4f 100644 --- a/pkg/cmd/sessions/login/login.manual.go +++ b/pkg/cmd/sessions/login/login.manual.go @@ -284,6 +284,10 @@ func (n *CmdLogin) RunE(cmd *cobra.Command, args []string) error { } } + // Clear any existing session file. + // If the user wants to load from a file, then use the --from-file option + cfg.ClearSessionFile() + if strings.EqualFold(n.Provider, ProviderTypeAuto) { // // Try guessing a sensible default diff --git a/pkg/config/cliConfiguration.go b/pkg/config/cliConfiguration.go index 36c1c5d93..4bcd1e521 100644 --- a/pkg/config/cliConfiguration.go +++ b/pkg/config/cliConfiguration.go @@ -1864,6 +1864,10 @@ func SupportsFileExtension(p string) bool { return false } +func (c *Config) ClearSessionFile() { + c.sessionFile = "" +} + func (c *Config) SetSessionFile(path string) { if _, fileErr := os.Stat(path); fileErr != nil { home := c.GetSessionHomeDir() @@ -1901,6 +1905,7 @@ func (c *Config) GetSessionFile(overrideSession ...string) string { sessionFile = os.Getenv("C8Y_SESSION") } + sessionFile = strings.TrimPrefix(sessionFile, "file://") if _, fileErr := os.Stat(sessionFile); fileErr != nil { home := c.GetSessionHomeDir() c.Logger.Debugf("Resolving session %s in %s", sessionFile, home) From e01d6d407809ab24e92a31401ed9144bb28765ec Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Mon, 10 Jun 2024 17:43:21 +0200 Subject: [PATCH 3/3] fix(sessions): set C8Y_SESSION based on either path or sessionUri --- pkg/c8ysession/c8ysession.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/c8ysession/c8ysession.go b/pkg/c8ysession/c8ysession.go index d1af4425b..611935f72 100644 --- a/pkg/c8ysession/c8ysession.go +++ b/pkg/c8ysession/c8ysession.go @@ -195,7 +195,7 @@ func GetVariablesFromSession(session *CumulocitySession, client *c8y.Client, set } output := map[string]interface{}{ - // "C8Y_SESSION": c.GetSessionFile(), + "C8Y_SESSION": "", "C8Y_URL": host, "C8Y_BASEURL": host, "C8Y_HOST": host, @@ -209,6 +209,13 @@ func GetVariablesFromSession(session *CumulocitySession, client *c8y.Client, set "C8Y_HEADER_AUTHORIZATION": authHeaderValue, "C8Y_HEADER": authHeader, } + + // Favor older path style over sessionUri to help with backwards compatibility + if session.Path != "" { + output["C8Y_SESSION"] = session.Path + } else if session.SessionUri != "" { + output["C8Y_SESSION"] = session.SessionUri + } return output }