diff --git a/internal/pkg/auth/storage.go b/internal/pkg/auth/storage.go index de08f25d5..87ecd9747 100644 --- a/internal/pkg/auth/storage.go +++ b/internal/pkg/auth/storage.go @@ -60,9 +60,14 @@ func SetAuthFieldMap(keyMap map[authFieldKey]string) error { } func SetAuthField(key authFieldKey, value string) error { - err := setAuthFieldInKeyring(key, value) + activeProfile, err := config.GetProfile() + if err != nil { + return fmt.Errorf("get profile: %w", err) + } + + err = setAuthFieldInKeyring(activeProfile, key, value) if err != nil { - errFallback := setAuthFieldInEncodedTextFile(key, value) + errFallback := setAuthFieldInEncodedTextFile(activeProfile, key, value) if errFallback != nil { return fmt.Errorf("write to keyring failed (%w), try writing to encoded text file: %w", err, errFallback) } @@ -70,12 +75,7 @@ func SetAuthField(key authFieldKey, value string) error { return nil } -func setAuthFieldInKeyring(key authFieldKey, value string) error { - activeProfile, err := config.GetProfile() - if err != nil { - return fmt.Errorf("get profile: %w", err) - } - +func setAuthFieldInKeyring(activeProfile string, key authFieldKey, value string) error { if activeProfile != "" { activeProfileKeyring := filepath.Join(keyringService, activeProfile) return keyring.Set(activeProfileKeyring, string(key), value) @@ -83,8 +83,8 @@ func setAuthFieldInKeyring(key authFieldKey, value string) error { return keyring.Set(keyringService, string(key), value) } -func setAuthFieldInEncodedTextFile(key authFieldKey, value string) error { - err := createEncodedTextFile() +func setAuthFieldInEncodedTextFile(activeProfile string, key authFieldKey, value string) error { + err := createEncodedTextFile(activeProfile) if err != nil { return err } @@ -94,11 +94,6 @@ func setAuthFieldInEncodedTextFile(key authFieldKey, value string) error { return fmt.Errorf("get config dir: %w", err) } - activeProfile, err := config.GetProfile() - if err != nil { - return fmt.Errorf("get profile: %w", err) - } - profileTextFileFolderName := textFileFolderName if activeProfile != "" { profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName) @@ -153,10 +148,15 @@ func GetAuthFlow() (AuthFlow, error) { } func GetAuthField(key authFieldKey) (string, error) { - value, err := getAuthFieldFromKeyring(key) + activeProfile, err := config.GetProfile() + if err != nil { + return "", fmt.Errorf("get profile: %w", err) + } + + value, err := getAuthFieldFromKeyring(activeProfile, key) if err != nil { var errFallback error - value, errFallback = getAuthFieldFromEncodedTextFile(key) + value, errFallback = getAuthFieldFromEncodedTextFile(activeProfile, key) if errFallback != nil { return "", fmt.Errorf("read from keyring: %w, read from encoded file as fallback: %w", err, errFallback) } @@ -164,12 +164,7 @@ func GetAuthField(key authFieldKey) (string, error) { return value, nil } -func getAuthFieldFromKeyring(key authFieldKey) (string, error) { - activeProfile, err := config.GetProfile() - if err != nil { - return "", fmt.Errorf("get profile: %w", err) - } - +func getAuthFieldFromKeyring(activeProfile string, key authFieldKey) (string, error) { if activeProfile != "" { activeProfileKeyring := filepath.Join(keyringService, activeProfile) return keyring.Get(activeProfileKeyring, string(key)) @@ -177,8 +172,8 @@ func getAuthFieldFromKeyring(key authFieldKey) (string, error) { return keyring.Get(keyringService, string(key)) } -func getAuthFieldFromEncodedTextFile(key authFieldKey) (string, error) { - err := createEncodedTextFile() +func getAuthFieldFromEncodedTextFile(activeProfile string, key authFieldKey) (string, error) { + err := createEncodedTextFile(activeProfile) if err != nil { return "", err } @@ -188,11 +183,6 @@ func getAuthFieldFromEncodedTextFile(key authFieldKey) (string, error) { return "", fmt.Errorf("get config dir: %w", err) } - activeProfile, err := config.GetProfile() - if err != nil { - return "", fmt.Errorf("get profile: %w", err) - } - profileTextFileFolderName := textFileFolderName if activeProfile != "" { profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName) @@ -224,17 +214,12 @@ func getAuthFieldFromEncodedTextFile(key authFieldKey) (string, error) { // Checks if the encoded text file exist. // If it doesn't, creates it with the content "{}" encoded. // If it does, does nothing (and returns nil). -func createEncodedTextFile() error { +func createEncodedTextFile(activeProfile string) error { configDir, err := os.UserConfigDir() if err != nil { return fmt.Errorf("get config dir: %w", err) } - activeProfile, err := config.GetProfile() - if err != nil { - return fmt.Errorf("get profile: %w", err) - } - profileTextFileFolderName := textFileFolderName if activeProfile != "" { profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName) diff --git a/internal/pkg/auth/storage_test.go b/internal/pkg/auth/storage_test.go index 306b4deee..5733ab69a 100644 --- a/internal/pkg/auth/storage_test.go +++ b/internal/pkg/auth/storage_test.go @@ -115,6 +115,11 @@ func TestSetGetAuthField(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { + activeProfile, err := config.GetProfile() + if err != nil { + t.Errorf("get profile: %v", err) + } + if !tt.keyringFails { keyring.MockInit() } else { @@ -142,12 +147,12 @@ func TestSetGetAuthField(t *testing.T) { } if !tt.keyringFails { - err = deleteAuthFieldInKeyring(key) + err = deleteAuthFieldInKeyring(activeProfile, key) if err != nil { t.Errorf("Post-test cleanup failed: remove field \"%s\" from keyring: %v. Please remove it manually", key, err) } } else { - err = deleteAuthFieldInEncodedTextFile(key) + err = deleteAuthFieldInEncodedTextFile(activeProfile, key) if err != nil { t.Errorf("Post-test cleanup failed: remove field \"%s\" from text file: %v. Please remove it manually", key, err) } @@ -174,9 +179,11 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { description string valueAssignments []valueAssignment expectedValues map[authFieldKey]string + activeProfile string }{ { - description: "simple assignments", + description: "simple assignments with default profile", + activeProfile: "", valueAssignments: []valueAssignment{ { key: testField1, @@ -193,7 +200,48 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { }, }, { - description: "overlapping assignments", + description: "overlapping assignments with default profile", + activeProfile: "", + valueAssignments: []valueAssignment{ + { + key: testField1, + value: testValue1, + }, + { + key: testField2, + value: testValue2, + }, + { + key: testField1, + value: testValue3, + }, + }, + expectedValues: map[authFieldKey]string{ + testField1: testValue3, + testField2: testValue2, + }, + }, + { + description: "simple assignments with testProfile", + activeProfile: "testProfile", + valueAssignments: []valueAssignment{ + { + key: testField1, + value: testValue1, + }, + { + key: testField2, + value: testValue2, + }, + }, + expectedValues: map[authFieldKey]string{ + testField1: testValue1, + testField2: testValue2, + }, + }, + { + description: "overlapping assignments with testProfile", + activeProfile: "testProfile", valueAssignments: []valueAssignment{ { key: testField1, @@ -220,7 +268,7 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { keyring.MockInit() for _, assignment := range tt.valueAssignments { - err := setAuthFieldInKeyring(assignment.key, assignment.value) + err := setAuthFieldInKeyring(tt.activeProfile, assignment.key, assignment.value) if err != nil { t.Fatalf("Failed to set \"%s\" as \"%s\": %v", assignment.key, assignment.value, err) } @@ -231,7 +279,7 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { } for key, valueExpected := range tt.expectedValues { - value, err := getAuthFieldFromKeyring(key) + value, err := getAuthFieldFromKeyring(tt.activeProfile, key) if err != nil { t.Errorf("Failed to get value of \"%s\": %v", key, err) continue @@ -239,7 +287,7 @@ func TestSetGetAuthFieldKeyring(t *testing.T) { t.Errorf("Value of field \"%s\" is wrong: expected \"%s\", got \"%s\"", key, valueExpected, value) } - err = deleteAuthFieldInKeyring(key) + err = deleteAuthFieldInKeyring(tt.activeProfile, key) if err != nil { t.Errorf("Post-test cleanup failed: remove field \"%s\" from keyring: %v. Please remove it manually", key, err) } @@ -263,11 +311,13 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { tests := []struct { description string + activeProfile string valueAssignments []valueAssignment expectedValues map[authFieldKey]string }{ { - description: "simple assignments", + description: "simple assignments with default profile", + activeProfile: "", valueAssignments: []valueAssignment{ { key: testField1, @@ -284,7 +334,48 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { }, }, { - description: "overlapping assignments", + description: "overlapping assignments with default profile", + activeProfile: "", + valueAssignments: []valueAssignment{ + { + key: testField1, + value: testValue1, + }, + { + key: testField2, + value: testValue2, + }, + { + key: testField1, + value: testValue3, + }, + }, + expectedValues: map[authFieldKey]string{ + testField1: testValue3, + testField2: testValue2, + }, + }, + { + description: "simple assignments with testProfile", + activeProfile: "testProfile", + valueAssignments: []valueAssignment{ + { + key: testField1, + value: testValue1, + }, + { + key: testField2, + value: testValue2, + }, + }, + expectedValues: map[authFieldKey]string{ + testField1: testValue1, + testField2: testValue2, + }, + }, + { + description: "overlapping assignments with testProfile", + activeProfile: "testProfile", valueAssignments: []valueAssignment{ { key: testField1, @@ -309,7 +400,7 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { for _, assignment := range tt.valueAssignments { - err := setAuthFieldInEncodedTextFile(assignment.key, assignment.value) + err := setAuthFieldInEncodedTextFile(tt.activeProfile, assignment.key, assignment.value) if err != nil { t.Fatalf("Failed to set \"%s\" as \"%s\": %v", assignment.key, assignment.value, err) } @@ -320,7 +411,7 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { } for key, valueExpected := range tt.expectedValues { - value, err := getAuthFieldFromEncodedTextFile(key) + value, err := getAuthFieldFromEncodedTextFile(tt.activeProfile, key) if err != nil { t.Errorf("Failed to get value of \"%s\": %v", key, err) continue @@ -328,7 +419,7 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { t.Errorf("Value of field \"%s\" is wrong: expected \"%s\", got \"%s\"", key, valueExpected, value) } - err = deleteAuthFieldInEncodedTextFile(key) + err = deleteAuthFieldInEncodedTextFile(tt.activeProfile, key) if err != nil { t.Errorf("Post-test cleanup failed: remove field \"%s\" from text file: %v. Please remove it manually", key, err) } @@ -337,12 +428,7 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) { } } -func deleteAuthFieldInKeyring(key authFieldKey) error { - activeProfile, err := config.GetProfile() - if err != nil { - return fmt.Errorf("get profile: %w", err) - } - +func deleteAuthFieldInKeyring(activeProfile string, key authFieldKey) error { if activeProfile != "" { activeProfileKeyring := filepath.Join(keyringService, activeProfile) return keyring.Delete(activeProfileKeyring, string(key)) @@ -351,8 +437,8 @@ func deleteAuthFieldInKeyring(key authFieldKey) error { return keyring.Delete(keyringService, string(key)) } -func deleteAuthFieldInEncodedTextFile(key authFieldKey) error { - err := createEncodedTextFile() +func deleteAuthFieldInEncodedTextFile(activeProfile string, key authFieldKey) error { + err := createEncodedTextFile(activeProfile) if err != nil { return err } @@ -362,11 +448,6 @@ func deleteAuthFieldInEncodedTextFile(key authFieldKey) error { return fmt.Errorf("get config dir: %w", err) } - activeProfile, err := config.GetProfile() - if err != nil { - return fmt.Errorf("get profile: %w", err) - } - profileTextFileFolderName := textFileFolderName if activeProfile != "" { profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName)