Skip to content

Commit fb93ee4

Browse files
authored
Merge pull request #598 from reubenmiller/fix-load-session-with-empty-token
fix(sessions): fallback to username/password auth (if present) if a token is not set
2 parents 6cf0369 + ba3f5eb commit fb93ee4

File tree

8 files changed

+106
-11
lines changed

8 files changed

+106
-11
lines changed

pkg/cmd/devices/enroll/enroll.manual.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (n *DeviceEnrollCmd) RunE(cmd *cobra.Command, args []string) error {
135135
if err != nil {
136136
return err
137137
}
138-
_ = llog
138+
c8y.Logger = llog
139139

140140
c8yclient, err := n.factory.Client()
141141
if err != nil {

pkg/cmd/factory/c8yclient.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ func CreateCumulocityClient(f *cmdutil.Factory, sessionFile, username, password
266266
}
267267
}
268268

269+
func parseBasicAuthCredentials(client *c8y.Client, tenant, username, password string) {
270+
if t, u, found := strings.Cut(username, "/"); found {
271+
tenant = t
272+
username = u
273+
}
274+
client.SetTenantUsernamePassword(tenant, username, password)
275+
}
276+
269277
func loadAuthentication(conf *config.Config, client *c8y.Client) error {
270278
loginType := conf.GetLoginTypeRaw()
271279
if loginType == "" {
@@ -280,7 +288,7 @@ func loadAuthentication(conf *config.Config, client *c8y.Client) error {
280288

281289
// password
282290
if p, err := conf.GetPassword(); err == nil && p != "" {
283-
client.SetTenantUsernamePassword(conf.GetTenant(), conf.GetUsername(), p)
291+
parseBasicAuthCredentials(client, conf.GetTenant(), conf.GetUsername(), p)
284292
return nil
285293
}
286294

@@ -308,7 +316,7 @@ func loadAuthentication(conf *config.Config, client *c8y.Client) error {
308316
if err != nil {
309317
return err
310318
}
311-
client.SetTenantUsernamePassword(conf.GetTenant(), conf.GetUsername(), password)
319+
parseBasicAuthCredentials(client, conf.GetTenant(), conf.GetUsername(), password)
312320
return nil
313321
}
314322

pkg/cmd/root/root.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -820,12 +820,6 @@ func (c *CmdRoot) Configure(disableEncryptionCheck, forceVerbose, forceDebug boo
820820
return c.client, nil
821821
}
822822
client, err := factory.CreateCumulocityClient(c.Factory, c.SessionFile, c.SessionUsername, c.SessionPassword, disableEncryptionCheck)()
823-
if client != nil {
824-
if c.SessionUsername != "" || c.SessionPassword != "" {
825-
client.SetUsernamePassword(c.SessionUsername, c.SessionPassword)
826-
c.log.Debug("Forcing basic authentication as user provided username/password")
827-
}
828-
}
829823

830824
if c.log != nil {
831825
c8y.Logger = c.log
@@ -872,11 +866,15 @@ func (c *CmdRoot) checkSessionExists(cmd *cobra.Command, args []string) error {
872866
// print log information
873867
sessionFile := cfg.GetSessionFile()
874868
if sessionFile != "" {
875-
log.Infof("Loaded session: %s", cfg.HideSensitiveInformationIfActive(client, sessionFile))
869+
log.Infof("Loading session from file: %s", cfg.HideSensitiveInformationIfActive(client, sessionFile))
876870
if _, err := os.Stat(sessionFile); err != nil {
877871
if c8ysession.IsSessionFilePath(sessionFile) {
878872
log.Warnf("Failed to verify session file. %s", err)
873+
} else {
874+
log.Warnf("Given file is not a session file. %s", err)
879875
}
876+
} else {
877+
log.Infof("Loaded session: %s", cfg.HideSensitiveInformationIfActive(client, sessionFile))
880878
}
881879
}
882880

pkg/cmd/sessions/create/create.manual.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func (n *CmdCreate) promptArgs(cmd *cobra.Command, args []string) error {
174174
if err != nil {
175175
return err
176176
}
177+
c8y.Logger = log
177178
prompter := prompt.NewPrompt(log)
178179

179180
if !cmd.Flags().Changed("host") {

pkg/cmd/sessions/login/login.manual.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ func (n *CmdLogin) RunE(cmd *cobra.Command, args []string) error {
401401
if err != nil {
402402
return err
403403
}
404+
c8y.Logger = log
404405

405406
canChangeActiveSession := true
406407
// Warn users if they try to use this command directly

pkg/config/cliConfiguration.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,10 @@ func (c *Config) BindAuthorization() error {
701701

702702
// GetUsername returns the Cumulocity username for the session
703703
func (c *Config) GetUsername() string {
704+
if v := c.GetSessionUsername(); v != "" {
705+
c.Logger.Infof("Using session username override")
706+
return v
707+
}
704708
v := c.viper.GetString("username")
705709

706710
if v != "" {
@@ -961,6 +965,11 @@ func (c *Config) WritePersistentConfig() error {
961965

962966
// GetPassword returns the decrypted password of the current session
963967
func (c *Config) GetPassword() (string, error) {
968+
if v := c.GetSessionPassword(); v != "" {
969+
c.Logger.Infof("Using session password override")
970+
return v, nil
971+
}
972+
964973
value := c.GetPasswordRaw()
965974

966975
if value == "" {
@@ -1720,16 +1729,32 @@ func ParseLoginTypeWithDefault(v string) string {
17201729

17211730
// GetLoginTypeWithDefault get the preferred login type
17221731
func (c *Config) GetLoginTypeWithDefault() string {
1723-
v := c.viper.GetString(SettingsLoginType)
1732+
v := c.GetLoginTypeRaw()
17241733
return ParseLoginTypeWithDefault(v)
17251734
}
17261735

17271736
// GetLoginTypeRaw get the raw value, where it could also be an empty value
17281737
func (c *Config) GetLoginTypeRaw() string {
1738+
if c.HasSessionUsernameOrPassword() {
1739+
// Force BASIC AUTH
1740+
return c8y.LoginTypeBasic
1741+
}
17291742
v := c.viper.GetString(SettingsLoginType)
17301743
return strings.ToUpper(v)
17311744
}
17321745

1746+
func (c *Config) HasSessionUsernameOrPassword() bool {
1747+
return c.GetSessionUsername() != "" || c.GetSessionPassword() != ""
1748+
}
1749+
1750+
func (c *Config) GetSessionUsername() string {
1751+
return c.viper.GetString("settings.defaults.sessionUsername")
1752+
}
1753+
1754+
func (c *Config) GetSessionPassword() string {
1755+
return c.viper.GetString("settings.defaults.sessionPassword")
1756+
}
1757+
17331758
// SetLoginType sets the authorization method, e.g. BASIC, OAUTH2_INTERNAL, NONE
17341759
func (c *Config) SetLoginType(v string) {
17351760
value, err := c8y.ParseLoginType(v)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
if [ -n "$CI" ]; then
4+
set -x
5+
fi
6+
7+
# store the current session variables, before clearing the session (to simulate creating a new session)
8+
BACKUP_C8Y_HOST="$C8Y_HOST"
9+
BACKUP_C8Y_TENANT="$C8Y_TENANT"
10+
BACKUP_C8Y_USER="$C8Y_USER"
11+
BACKUP_C8Y_PASSWORD="$C8Y_PASSWORD"
12+
13+
if [ -z "$C8Y_PASSWORD" ]; then
14+
echo "This test requires the 'C8Y_PASSWORD' env variable to be set!"
15+
exit 1
16+
fi
17+
18+
echo "Clearing existing session" >&2
19+
eval "$(c8y sessions clear --shell bash)" ||:
20+
21+
TMPDIR=$(mktemp -d)
22+
cleanup () {
23+
rm -rf "$TMPDIR"
24+
}
25+
trap cleanup EXIT
26+
export C8Y_SESSION_HOME="$TMPDIR"
27+
28+
#
29+
# Test case 1: Create a session and reference it via the --session global flag
30+
#
31+
c8y sessions create \
32+
--mode dev \
33+
--username "$BACKUP_C8Y_USER" \
34+
--host "$BACKUP_C8Y_HOST" \
35+
--tenant "$BACKUP_C8Y_TENANT" \
36+
--password "$BACKUP_C8Y_PASSWORD" \
37+
--name subtenant
38+
c8y devices list -p 1 -n --session "$TMPDIR/subtenant.json"
39+
40+
#
41+
# Test case 2: Create a session without storing the password and use a combination
42+
# of --session and --sessionPassword
43+
#
44+
c8y sessions create \
45+
--mode dev \
46+
--username "$BACKUP_C8Y_USER" \
47+
--host "$BACKUP_C8Y_HOST" \
48+
--tenant "$BACKUP_C8Y_TENANT" \
49+
--password "$BACKUP_C8Y_PASSWORD" \
50+
--name subtenant \
51+
--noStorage
52+
53+
echo "Checking session credentials using custom session password" >&2
54+
c8y devices list -p 1 -n --session "$TMPDIR/subtenant.json" --sessionPassword "$BACKUP_C8Y_PASSWORD"
55+
56+
echo "Checking resolution of session using just the name" >&2
57+
c8y devices list -p 1 -n --session "subtenant.json" --sessionPassword "$BACKUP_C8Y_PASSWORD"

tests/manual/sessions/create/session_create.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,8 @@ tests:
6464
"password": "test",
6565
"username": "dummy@me.com"
6666
}
67+
68+
It supports creating a session and referering it using the session global flag:
69+
command: |
70+
./manual/sessions/create/create-session.sh
71+
exit-code: 0

0 commit comments

Comments
 (0)