Skip to content

Commit

Permalink
Merge pull request #391 from reubenmiller/fix-session-file-handling
Browse files Browse the repository at this point in the history
fix(session): fix handling of non-file based sessions
  • Loading branch information
reubenmiller authored Jun 10, 2024
2 parents 5be6aae + e01d6d4 commit e780324
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
17 changes: 16 additions & 1 deletion pkg/c8ysession/c8ysession.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -251,3 +258,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, "://")
}
5 changes: 4 additions & 1 deletion pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/sessions/get/get.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/sessions/login/login.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/cliConfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e780324

Please sign in to comment.