Skip to content

Rework active profile handling in auth #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 21 additions & 36 deletions internal/pkg/auth/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,31 @@ 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)
}
}
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)
}
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
}
Expand All @@ -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)
Expand Down Expand Up @@ -153,32 +148,32 @@ 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)
}
}
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))
}
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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
131 changes: 106 additions & 25 deletions internal/pkg/auth/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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)
}
Expand All @@ -231,15 +279,15 @@ 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
} else if value != valueExpected {
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)
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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)
}
Expand All @@ -320,15 +411,15 @@ 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
} else if value != valueExpected {
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)
}
Expand All @@ -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))
Expand All @@ -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
}
Expand All @@ -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)
Expand Down