Skip to content

Commit 74c5fa3

Browse files
authored
Rework active profile handling in auth (#329)
* move reading profile to caller functions * adapt unit tests * adapt unit tests * adapt unit tests
1 parent bbdb98f commit 74c5fa3

File tree

2 files changed

+127
-61
lines changed

2 files changed

+127
-61
lines changed

internal/pkg/auth/storage.go

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,31 @@ func SetAuthFieldMap(keyMap map[authFieldKey]string) error {
6060
}
6161

6262
func SetAuthField(key authFieldKey, value string) error {
63-
err := setAuthFieldInKeyring(key, value)
63+
activeProfile, err := config.GetProfile()
64+
if err != nil {
65+
return fmt.Errorf("get profile: %w", err)
66+
}
67+
68+
err = setAuthFieldInKeyring(activeProfile, key, value)
6469
if err != nil {
65-
errFallback := setAuthFieldInEncodedTextFile(key, value)
70+
errFallback := setAuthFieldInEncodedTextFile(activeProfile, key, value)
6671
if errFallback != nil {
6772
return fmt.Errorf("write to keyring failed (%w), try writing to encoded text file: %w", err, errFallback)
6873
}
6974
}
7075
return nil
7176
}
7277

73-
func setAuthFieldInKeyring(key authFieldKey, value string) error {
74-
activeProfile, err := config.GetProfile()
75-
if err != nil {
76-
return fmt.Errorf("get profile: %w", err)
77-
}
78-
78+
func setAuthFieldInKeyring(activeProfile string, key authFieldKey, value string) error {
7979
if activeProfile != "" {
8080
activeProfileKeyring := filepath.Join(keyringService, activeProfile)
8181
return keyring.Set(activeProfileKeyring, string(key), value)
8282
}
8383
return keyring.Set(keyringService, string(key), value)
8484
}
8585

86-
func setAuthFieldInEncodedTextFile(key authFieldKey, value string) error {
87-
err := createEncodedTextFile()
86+
func setAuthFieldInEncodedTextFile(activeProfile string, key authFieldKey, value string) error {
87+
err := createEncodedTextFile(activeProfile)
8888
if err != nil {
8989
return err
9090
}
@@ -94,11 +94,6 @@ func setAuthFieldInEncodedTextFile(key authFieldKey, value string) error {
9494
return fmt.Errorf("get config dir: %w", err)
9595
}
9696

97-
activeProfile, err := config.GetProfile()
98-
if err != nil {
99-
return fmt.Errorf("get profile: %w", err)
100-
}
101-
10297
profileTextFileFolderName := textFileFolderName
10398
if activeProfile != "" {
10499
profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName)
@@ -153,32 +148,32 @@ func GetAuthFlow() (AuthFlow, error) {
153148
}
154149

155150
func GetAuthField(key authFieldKey) (string, error) {
156-
value, err := getAuthFieldFromKeyring(key)
151+
activeProfile, err := config.GetProfile()
152+
if err != nil {
153+
return "", fmt.Errorf("get profile: %w", err)
154+
}
155+
156+
value, err := getAuthFieldFromKeyring(activeProfile, key)
157157
if err != nil {
158158
var errFallback error
159-
value, errFallback = getAuthFieldFromEncodedTextFile(key)
159+
value, errFallback = getAuthFieldFromEncodedTextFile(activeProfile, key)
160160
if errFallback != nil {
161161
return "", fmt.Errorf("read from keyring: %w, read from encoded file as fallback: %w", err, errFallback)
162162
}
163163
}
164164
return value, nil
165165
}
166166

167-
func getAuthFieldFromKeyring(key authFieldKey) (string, error) {
168-
activeProfile, err := config.GetProfile()
169-
if err != nil {
170-
return "", fmt.Errorf("get profile: %w", err)
171-
}
172-
167+
func getAuthFieldFromKeyring(activeProfile string, key authFieldKey) (string, error) {
173168
if activeProfile != "" {
174169
activeProfileKeyring := filepath.Join(keyringService, activeProfile)
175170
return keyring.Get(activeProfileKeyring, string(key))
176171
}
177172
return keyring.Get(keyringService, string(key))
178173
}
179174

180-
func getAuthFieldFromEncodedTextFile(key authFieldKey) (string, error) {
181-
err := createEncodedTextFile()
175+
func getAuthFieldFromEncodedTextFile(activeProfile string, key authFieldKey) (string, error) {
176+
err := createEncodedTextFile(activeProfile)
182177
if err != nil {
183178
return "", err
184179
}
@@ -188,11 +183,6 @@ func getAuthFieldFromEncodedTextFile(key authFieldKey) (string, error) {
188183
return "", fmt.Errorf("get config dir: %w", err)
189184
}
190185

191-
activeProfile, err := config.GetProfile()
192-
if err != nil {
193-
return "", fmt.Errorf("get profile: %w", err)
194-
}
195-
196186
profileTextFileFolderName := textFileFolderName
197187
if activeProfile != "" {
198188
profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName)
@@ -224,17 +214,12 @@ func getAuthFieldFromEncodedTextFile(key authFieldKey) (string, error) {
224214
// Checks if the encoded text file exist.
225215
// If it doesn't, creates it with the content "{}" encoded.
226216
// If it does, does nothing (and returns nil).
227-
func createEncodedTextFile() error {
217+
func createEncodedTextFile(activeProfile string) error {
228218
configDir, err := os.UserConfigDir()
229219
if err != nil {
230220
return fmt.Errorf("get config dir: %w", err)
231221
}
232222

233-
activeProfile, err := config.GetProfile()
234-
if err != nil {
235-
return fmt.Errorf("get profile: %w", err)
236-
}
237-
238223
profileTextFileFolderName := textFileFolderName
239224
if activeProfile != "" {
240225
profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName)

internal/pkg/auth/storage_test.go

Lines changed: 106 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ func TestSetGetAuthField(t *testing.T) {
115115

116116
for _, tt := range tests {
117117
t.Run(tt.description, func(t *testing.T) {
118+
activeProfile, err := config.GetProfile()
119+
if err != nil {
120+
t.Errorf("get profile: %v", err)
121+
}
122+
118123
if !tt.keyringFails {
119124
keyring.MockInit()
120125
} else {
@@ -142,12 +147,12 @@ func TestSetGetAuthField(t *testing.T) {
142147
}
143148

144149
if !tt.keyringFails {
145-
err = deleteAuthFieldInKeyring(key)
150+
err = deleteAuthFieldInKeyring(activeProfile, key)
146151
if err != nil {
147152
t.Errorf("Post-test cleanup failed: remove field \"%s\" from keyring: %v. Please remove it manually", key, err)
148153
}
149154
} else {
150-
err = deleteAuthFieldInEncodedTextFile(key)
155+
err = deleteAuthFieldInEncodedTextFile(activeProfile, key)
151156
if err != nil {
152157
t.Errorf("Post-test cleanup failed: remove field \"%s\" from text file: %v. Please remove it manually", key, err)
153158
}
@@ -174,9 +179,11 @@ func TestSetGetAuthFieldKeyring(t *testing.T) {
174179
description string
175180
valueAssignments []valueAssignment
176181
expectedValues map[authFieldKey]string
182+
activeProfile string
177183
}{
178184
{
179-
description: "simple assignments",
185+
description: "simple assignments with default profile",
186+
activeProfile: "",
180187
valueAssignments: []valueAssignment{
181188
{
182189
key: testField1,
@@ -193,7 +200,48 @@ func TestSetGetAuthFieldKeyring(t *testing.T) {
193200
},
194201
},
195202
{
196-
description: "overlapping assignments",
203+
description: "overlapping assignments with default profile",
204+
activeProfile: "",
205+
valueAssignments: []valueAssignment{
206+
{
207+
key: testField1,
208+
value: testValue1,
209+
},
210+
{
211+
key: testField2,
212+
value: testValue2,
213+
},
214+
{
215+
key: testField1,
216+
value: testValue3,
217+
},
218+
},
219+
expectedValues: map[authFieldKey]string{
220+
testField1: testValue3,
221+
testField2: testValue2,
222+
},
223+
},
224+
{
225+
description: "simple assignments with testProfile",
226+
activeProfile: "testProfile",
227+
valueAssignments: []valueAssignment{
228+
{
229+
key: testField1,
230+
value: testValue1,
231+
},
232+
{
233+
key: testField2,
234+
value: testValue2,
235+
},
236+
},
237+
expectedValues: map[authFieldKey]string{
238+
testField1: testValue1,
239+
testField2: testValue2,
240+
},
241+
},
242+
{
243+
description: "overlapping assignments with testProfile",
244+
activeProfile: "testProfile",
197245
valueAssignments: []valueAssignment{
198246
{
199247
key: testField1,
@@ -220,7 +268,7 @@ func TestSetGetAuthFieldKeyring(t *testing.T) {
220268
keyring.MockInit()
221269

222270
for _, assignment := range tt.valueAssignments {
223-
err := setAuthFieldInKeyring(assignment.key, assignment.value)
271+
err := setAuthFieldInKeyring(tt.activeProfile, assignment.key, assignment.value)
224272
if err != nil {
225273
t.Fatalf("Failed to set \"%s\" as \"%s\": %v", assignment.key, assignment.value, err)
226274
}
@@ -231,15 +279,15 @@ func TestSetGetAuthFieldKeyring(t *testing.T) {
231279
}
232280

233281
for key, valueExpected := range tt.expectedValues {
234-
value, err := getAuthFieldFromKeyring(key)
282+
value, err := getAuthFieldFromKeyring(tt.activeProfile, key)
235283
if err != nil {
236284
t.Errorf("Failed to get value of \"%s\": %v", key, err)
237285
continue
238286
} else if value != valueExpected {
239287
t.Errorf("Value of field \"%s\" is wrong: expected \"%s\", got \"%s\"", key, valueExpected, value)
240288
}
241289

242-
err = deleteAuthFieldInKeyring(key)
290+
err = deleteAuthFieldInKeyring(tt.activeProfile, key)
243291
if err != nil {
244292
t.Errorf("Post-test cleanup failed: remove field \"%s\" from keyring: %v. Please remove it manually", key, err)
245293
}
@@ -263,11 +311,13 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) {
263311

264312
tests := []struct {
265313
description string
314+
activeProfile string
266315
valueAssignments []valueAssignment
267316
expectedValues map[authFieldKey]string
268317
}{
269318
{
270-
description: "simple assignments",
319+
description: "simple assignments with default profile",
320+
activeProfile: "",
271321
valueAssignments: []valueAssignment{
272322
{
273323
key: testField1,
@@ -284,7 +334,48 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) {
284334
},
285335
},
286336
{
287-
description: "overlapping assignments",
337+
description: "overlapping assignments with default profile",
338+
activeProfile: "",
339+
valueAssignments: []valueAssignment{
340+
{
341+
key: testField1,
342+
value: testValue1,
343+
},
344+
{
345+
key: testField2,
346+
value: testValue2,
347+
},
348+
{
349+
key: testField1,
350+
value: testValue3,
351+
},
352+
},
353+
expectedValues: map[authFieldKey]string{
354+
testField1: testValue3,
355+
testField2: testValue2,
356+
},
357+
},
358+
{
359+
description: "simple assignments with testProfile",
360+
activeProfile: "testProfile",
361+
valueAssignments: []valueAssignment{
362+
{
363+
key: testField1,
364+
value: testValue1,
365+
},
366+
{
367+
key: testField2,
368+
value: testValue2,
369+
},
370+
},
371+
expectedValues: map[authFieldKey]string{
372+
testField1: testValue1,
373+
testField2: testValue2,
374+
},
375+
},
376+
{
377+
description: "overlapping assignments with testProfile",
378+
activeProfile: "testProfile",
288379
valueAssignments: []valueAssignment{
289380
{
290381
key: testField1,
@@ -309,7 +400,7 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) {
309400
for _, tt := range tests {
310401
t.Run(tt.description, func(t *testing.T) {
311402
for _, assignment := range tt.valueAssignments {
312-
err := setAuthFieldInEncodedTextFile(assignment.key, assignment.value)
403+
err := setAuthFieldInEncodedTextFile(tt.activeProfile, assignment.key, assignment.value)
313404
if err != nil {
314405
t.Fatalf("Failed to set \"%s\" as \"%s\": %v", assignment.key, assignment.value, err)
315406
}
@@ -320,15 +411,15 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) {
320411
}
321412

322413
for key, valueExpected := range tt.expectedValues {
323-
value, err := getAuthFieldFromEncodedTextFile(key)
414+
value, err := getAuthFieldFromEncodedTextFile(tt.activeProfile, key)
324415
if err != nil {
325416
t.Errorf("Failed to get value of \"%s\": %v", key, err)
326417
continue
327418
} else if value != valueExpected {
328419
t.Errorf("Value of field \"%s\" is wrong: expected \"%s\", got \"%s\"", key, valueExpected, value)
329420
}
330421

331-
err = deleteAuthFieldInEncodedTextFile(key)
422+
err = deleteAuthFieldInEncodedTextFile(tt.activeProfile, key)
332423
if err != nil {
333424
t.Errorf("Post-test cleanup failed: remove field \"%s\" from text file: %v. Please remove it manually", key, err)
334425
}
@@ -337,12 +428,7 @@ func TestSetGetAuthFieldEncodedTextFile(t *testing.T) {
337428
}
338429
}
339430

340-
func deleteAuthFieldInKeyring(key authFieldKey) error {
341-
activeProfile, err := config.GetProfile()
342-
if err != nil {
343-
return fmt.Errorf("get profile: %w", err)
344-
}
345-
431+
func deleteAuthFieldInKeyring(activeProfile string, key authFieldKey) error {
346432
if activeProfile != "" {
347433
activeProfileKeyring := filepath.Join(keyringService, activeProfile)
348434
return keyring.Delete(activeProfileKeyring, string(key))
@@ -351,8 +437,8 @@ func deleteAuthFieldInKeyring(key authFieldKey) error {
351437
return keyring.Delete(keyringService, string(key))
352438
}
353439

354-
func deleteAuthFieldInEncodedTextFile(key authFieldKey) error {
355-
err := createEncodedTextFile()
440+
func deleteAuthFieldInEncodedTextFile(activeProfile string, key authFieldKey) error {
441+
err := createEncodedTextFile(activeProfile)
356442
if err != nil {
357443
return err
358444
}
@@ -362,11 +448,6 @@ func deleteAuthFieldInEncodedTextFile(key authFieldKey) error {
362448
return fmt.Errorf("get config dir: %w", err)
363449
}
364450

365-
activeProfile, err := config.GetProfile()
366-
if err != nil {
367-
return fmt.Errorf("get profile: %w", err)
368-
}
369-
370451
profileTextFileFolderName := textFileFolderName
371452
if activeProfile != "" {
372453
profileTextFileFolderName = filepath.Join(activeProfile, textFileFolderName)

0 commit comments

Comments
 (0)