Skip to content

Commit b3a5cee

Browse files
committed
Cleanup code slightly
1 parent 0bb1ffb commit b3a5cee

2 files changed

Lines changed: 35 additions & 156 deletions

File tree

internal/tiger/common/password_storage.go

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package common
22

33
import (
44
"bufio"
5+
"errors"
56
"fmt"
67
"os"
78
"path/filepath"
@@ -339,26 +340,44 @@ type AutoFallbackStorage struct {
339340

340341
func (a *AutoFallbackStorage) Save(service api.Service, password string, role string) error {
341342
// Try keyring first
342-
if err := a.keyring.Save(service, password, role); err == nil {
343-
a.lastMethod = "keyring"
343+
a.lastMethod = "keyring"
344+
keyringErr := a.keyring.Save(service, password, role)
345+
if keyringErr == nil {
344346
return nil
345347
}
348+
346349
// Any keyring error -> fall back to pgpass
347-
if err := a.pgpass.Save(service, password, role); err != nil {
348-
a.lastMethod = "auto"
349-
return err
350-
}
351350
a.lastMethod = "pgpass"
352-
return nil
351+
pgpassErr := a.pgpass.Save(service, password, role)
352+
if pgpassErr == nil {
353+
return nil
354+
}
355+
356+
// Both failed - return combined error
357+
return errors.Join(
358+
fmt.Errorf("keyring: %w", keyringErr),
359+
fmt.Errorf("pgpass: %w", pgpassErr),
360+
)
353361
}
354362

355363
func (a *AutoFallbackStorage) Get(service api.Service, role string) (string, error) {
356364
// Try keyring first
357-
if password, err := a.keyring.Get(service, role); err == nil {
365+
password, keyringErr := a.keyring.Get(service, role)
366+
if keyringErr == nil {
358367
return password, nil
359368
}
369+
360370
// Any keyring error -> try pgpass
361-
return a.pgpass.Get(service, role)
371+
password, pgpassErr := a.pgpass.Get(service, role)
372+
if pgpassErr == nil {
373+
return password, nil
374+
}
375+
376+
// Both failed
377+
return "", errors.Join(
378+
fmt.Errorf("keyring: %w", keyringErr),
379+
fmt.Errorf("pgpass: %w", pgpassErr),
380+
)
362381
}
363382

364383
func (a *AutoFallbackStorage) Remove(service api.Service, role string) error {
@@ -370,8 +389,11 @@ func (a *AutoFallbackStorage) Remove(service api.Service, role string) error {
370389
if keyringErr == nil || pgpassErr == nil {
371390
return nil
372391
}
373-
// If both failed, return a combined error
374-
return fmt.Errorf("keyring: %v, pgpass: %v", keyringErr, pgpassErr)
392+
// Both failed
393+
return errors.Join(
394+
fmt.Errorf("keyring: %w", keyringErr),
395+
fmt.Errorf("pgpass: %w", pgpassErr),
396+
)
375397
}
376398

377399
func (a *AutoFallbackStorage) GetStorageResult(err error, password string) PasswordStorageResult {
@@ -391,18 +413,12 @@ func (a *AutoFallbackStorage) GetStorageResult(err error, password string) Passw
391413
Method: "keyring",
392414
Message: "Password saved to system keyring for automatic authentication",
393415
}
394-
case "pgpass":
416+
default:
395417
return PasswordStorageResult{
396418
Success: true,
397419
Method: "pgpass",
398420
Message: "Password saved to ~/.pgpass for automatic authentication",
399421
}
400-
default:
401-
return PasswordStorageResult{
402-
Success: true,
403-
Method: "auto",
404-
Message: "Password saved for automatic authentication",
405-
}
406422
}
407423
}
408424

@@ -416,13 +432,7 @@ func GetPasswordStorage() PasswordStorage {
416432
return &PgpassStorage{}
417433
case "none":
418434
return &NoStorage{}
419-
case "auto":
420-
return &AutoFallbackStorage{
421-
keyring: &KeyringStorage{},
422-
pgpass: &PgpassStorage{},
423-
}
424435
default:
425-
// Default to auto for best compatibility across environments
426436
return &AutoFallbackStorage{
427437
keyring: &KeyringStorage{},
428438
pgpass: &PgpassStorage{},

internal/tiger/common/password_storage_test.go

Lines changed: 1 addition & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func TestGetPasswordStorage(t *testing.T) {
296296
{"pgpass", "pgpass", "*common.PgpassStorage"},
297297
{"none", "none", "*common.NoStorage"},
298298
{"auto", "auto", "*common.AutoFallbackStorage"},
299-
{"default", "", "*common.AutoFallbackStorage"}, // Default case now uses auto
299+
{"default", "", "*common.AutoFallbackStorage"},
300300
{"invalid", "invalid", "*common.AutoFallbackStorage"}, // Falls back to auto
301301
}
302302

@@ -990,134 +990,3 @@ func TestAutoFallbackStorage_Integration(t *testing.T) {
990990
t.Error("AutoFallbackStorage.Get() should fail after Remove()")
991991
}
992992
}
993-
994-
// Test AutoFallbackStorage GetStorageResult with keyring success
995-
func TestAutoFallbackStorage_GetStorageResult_KeyringSuccess(t *testing.T) {
996-
storage := &AutoFallbackStorage{
997-
keyring: &KeyringStorage{},
998-
pgpass: &PgpassStorage{},
999-
lastMethod: "keyring",
1000-
}
1001-
testPassword := "test-password"
1002-
1003-
result := storage.GetStorageResult(nil, testPassword)
1004-
if !result.Success {
1005-
t.Errorf("GetStorageResult() success should be true, got: %t", result.Success)
1006-
}
1007-
if result.Method != "keyring" {
1008-
t.Errorf("GetStorageResult() method should be 'keyring', got: %s", result.Method)
1009-
}
1010-
if !strings.Contains(result.Message, "Password saved to system keyring") {
1011-
t.Errorf("GetStorageResult() should mention keyring, got: %s", result.Message)
1012-
}
1013-
if strings.Contains(result.Message, testPassword) {
1014-
t.Error("GetStorageResult() should never include actual passwords")
1015-
}
1016-
}
1017-
1018-
// Test AutoFallbackStorage GetStorageResult with pgpass fallback success
1019-
func TestAutoFallbackStorage_GetStorageResult_PgpassSuccess(t *testing.T) {
1020-
storage := &AutoFallbackStorage{
1021-
keyring: &KeyringStorage{},
1022-
pgpass: &PgpassStorage{},
1023-
lastMethod: "pgpass",
1024-
}
1025-
testPassword := "test-password"
1026-
1027-
result := storage.GetStorageResult(nil, testPassword)
1028-
if !result.Success {
1029-
t.Errorf("GetStorageResult() success should be true, got: %t", result.Success)
1030-
}
1031-
if result.Method != "pgpass" {
1032-
t.Errorf("GetStorageResult() method should be 'pgpass', got: %s", result.Method)
1033-
}
1034-
if !strings.Contains(result.Message, "Password saved to ~/.pgpass") {
1035-
t.Errorf("GetStorageResult() should mention pgpass, got: %s", result.Message)
1036-
}
1037-
if strings.Contains(result.Message, testPassword) {
1038-
t.Error("GetStorageResult() should never include actual passwords")
1039-
}
1040-
}
1041-
1042-
// Test AutoFallbackStorage GetStorageResult with failure
1043-
func TestAutoFallbackStorage_GetStorageResult_Failure(t *testing.T) {
1044-
storage := &AutoFallbackStorage{
1045-
keyring: &KeyringStorage{},
1046-
pgpass: &PgpassStorage{},
1047-
lastMethod: "auto",
1048-
}
1049-
testPassword := "test-password"
1050-
testError := fmt.Errorf("both storage methods failed")
1051-
1052-
result := storage.GetStorageResult(testError, testPassword)
1053-
if result.Success {
1054-
t.Errorf("GetStorageResult() success should be false, got: %t", result.Success)
1055-
}
1056-
if result.Method != "auto" {
1057-
t.Errorf("GetStorageResult() method should be 'auto', got: %s", result.Method)
1058-
}
1059-
if !strings.Contains(result.Message, "Failed to save password") {
1060-
t.Errorf("GetStorageResult() should mention failure, got: %s", result.Message)
1061-
}
1062-
if strings.Contains(result.Message, testPassword) {
1063-
t.Error("GetStorageResult() should never include actual passwords")
1064-
}
1065-
}
1066-
1067-
// Test AutoFallbackStorage Remove succeeds if at least one location succeeds
1068-
func TestAutoFallbackStorage_Remove_PartialSuccess(t *testing.T) {
1069-
// Set up test service name for keyring
1070-
config.SetTestServiceName(t)
1071-
1072-
// Create a temporary directory for pgpass
1073-
tempDir := t.TempDir()
1074-
originalHome := os.Getenv("HOME")
1075-
os.Setenv("HOME", tempDir)
1076-
defer os.Setenv("HOME", originalHome)
1077-
1078-
// Save password only to pgpass (not keyring)
1079-
pgpassStorage := &PgpassStorage{}
1080-
service := createTestService("partial-remove-test")
1081-
password := "test-password"
1082-
role := "tsdbadmin"
1083-
1084-
err := pgpassStorage.Save(service, password, role)
1085-
if err != nil {
1086-
t.Fatalf("PgpassStorage.Save() failed: %v", err)
1087-
}
1088-
1089-
// Now remove using AutoFallbackStorage - should succeed even if keyring fails
1090-
autoStorage := &AutoFallbackStorage{
1091-
keyring: &KeyringStorage{},
1092-
pgpass: &PgpassStorage{},
1093-
}
1094-
1095-
err = autoStorage.Remove(service, role)
1096-
if err != nil {
1097-
t.Errorf("AutoFallbackStorage.Remove() should succeed when at least one location succeeds, got: %v", err)
1098-
}
1099-
}
1100-
1101-
// Security test: Ensure AutoFallbackStorage never includes passwords in messages
1102-
func TestAutoFallbackStorage_SecurityTest_NoPasswordInResults(t *testing.T) {
1103-
testPassword := "super-secret-password-123"
1104-
testError := fmt.Errorf("failed to save %s", testPassword)
1105-
1106-
storage := &AutoFallbackStorage{
1107-
keyring: &KeyringStorage{},
1108-
pgpass: &PgpassStorage{},
1109-
lastMethod: "auto",
1110-
}
1111-
1112-
// Test success case
1113-
successResult := storage.GetStorageResult(nil, testPassword)
1114-
if strings.Contains(successResult.Message, testPassword) {
1115-
t.Errorf("GetStorageResult() success should never include password, got: %s", successResult.Message)
1116-
}
1117-
1118-
// Test error case
1119-
errorResult := storage.GetStorageResult(testError, testPassword)
1120-
if strings.Contains(errorResult.Message, testPassword) {
1121-
t.Errorf("GetStorageResult() error should never include password, got: %s", errorResult.Message)
1122-
}
1123-
}

0 commit comments

Comments
 (0)