Skip to content

Commit e71e239

Browse files
committed
fix(authentication): service account authentication becomes invalid on mac when the Service Account Key is too big for keychain
1 parent 03f7993 commit e71e239

3 files changed

Lines changed: 337 additions & 18 deletions

File tree

‎internal/pkg/auth/service_account.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ func AuthenticateServiceAccount(p *print.Printer, rt http.RoundTripper, disableW
107107
authFields[IDP_TOKEN_ENDPOINT] = ""
108108

109109
if !disableWriting {
110+
// Clean up user credentials to avoid stale fields
111+
_ = DeleteAuthField(USER_EMAIL)
112+
_ = DeleteAuthField(REFRESH_TOKEN)
113+
_ = DeleteAuthField(SERVICE_ACCOUNT_TOKEN)
114+
_ = DeleteAuthField(SERVICE_ACCOUNT_KEY)
115+
_ = DeleteAuthField(PRIVATE_KEY)
116+
110117
err = SetAuthFlow(authFlowType)
111118
if err != nil {
112119
return "", "", fmt.Errorf("set auth flow type: %w", err)

‎internal/pkg/auth/storage.go‎

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ func setAuthFieldWithProfile(profile string, key authFieldKey, value string) err
106106
if errFallback != nil {
107107
return fmt.Errorf("write to keyring failed (%w), try writing to encoded text file: %w", err, errFallback)
108108
}
109+
// If writing to the text file succeeded as a fallback, clean up any stale key in the keyring
110+
// so GetAuthField won't return outdated credentials.
111+
_ = deleteAuthFieldInKeyring(profile, key)
112+
} else {
113+
// If writing to the keyring succeeded, clean up any old entry from the encoded text file.
114+
_ = deleteAuthFieldInEncodedTextFile(profile, key)
109115
}
110116

111117
return nil
@@ -130,31 +136,34 @@ func DeleteAuthField(key authFieldKey) error {
130136
}
131137

132138
func deleteAuthFieldWithProfile(profile string, key authFieldKey) error {
139+
var errKeyring, errFile error
140+
133141
err := deleteAuthFieldInKeyring(profile, key)
142+
if err != nil && !errors.Is(err, keyring.ErrNotFound) {
143+
errKeyring = err
144+
}
145+
146+
err = deleteAuthFieldInEncodedTextFile(profile, key)
134147
if err != nil {
135-
// if the key is not found, we can ignore the error
136-
if !errors.Is(err, keyring.ErrNotFound) {
137-
errFallback := deleteAuthFieldInEncodedTextFile(profile, key)
138-
if errFallback != nil {
139-
return fmt.Errorf("delete from keyring failed (%w), try deleting from encoded text file: %w", err, errFallback)
140-
}
141-
}
148+
errFile = err
149+
}
150+
151+
if errKeyring != nil && errFile != nil {
152+
return fmt.Errorf("delete from keyring: %w, delete from encoded text file: %w", errKeyring, errFile)
142153
}
143154

144155
return nil
145156
}
146157

147158
func deleteAuthFieldInEncodedTextFile(activeProfile string, key authFieldKey) error {
148-
err := createEncodedTextFile(activeProfile)
149-
if err != nil {
150-
return err
151-
}
152-
153159
textFileDir := config.GetProfileFolderPath(activeProfile)
154160
textFilePath := filepath.Join(textFileDir, textFileName)
155161

156162
contentEncoded, err := os.ReadFile(textFilePath)
157163
if err != nil {
164+
if os.IsNotExist(err) {
165+
return nil
166+
}
158167
return fmt.Errorf("read file: %w", err)
159168
}
160169

@@ -170,8 +179,20 @@ func deleteAuthFieldInEncodedTextFile(activeProfile string, key authFieldKey) er
170179
return fmt.Errorf("unmarshal file: %w", err)
171180
}
172181

182+
if _, ok := content[key]; !ok {
183+
return nil
184+
}
185+
173186
delete(content, key)
174187

188+
if len(content) == 0 {
189+
err = os.Remove(textFilePath)
190+
if err != nil && !os.IsNotExist(err) {
191+
return fmt.Errorf("remove empty encoded text file: %w", err)
192+
}
193+
return nil
194+
}
195+
175196
contentBytes, err = json.Marshal(content)
176197
if err != nil {
177198
return fmt.Errorf("marshal file: %w", err)
@@ -291,11 +312,6 @@ func getAuthFieldFromKeyring(activeProfile string, key authFieldKey) (string, er
291312
}
292313

293314
func getAuthFieldFromEncodedTextFile(activeProfile string, key authFieldKey) (string, error) {
294-
err := createEncodedTextFile(activeProfile)
295-
if err != nil {
296-
return "", err
297-
}
298-
299315
textFileDir := config.GetProfileFolderPath(activeProfile)
300316
textFilePath := filepath.Join(textFileDir, textFileName)
301317

@@ -403,6 +419,17 @@ func GetAuthEmail() (string, error) {
403419
}
404420

405421
func LoginUser(email, accessToken, refreshToken, sessionExpiresAtUnix string) error {
422+
// Clean up any service account credentials that might be present
423+
saFields := []authFieldKey{
424+
SERVICE_ACCOUNT_KEY,
425+
PRIVATE_KEY,
426+
SERVICE_ACCOUNT_TOKEN,
427+
SERVICE_ACCOUNT_EMAIL,
428+
}
429+
for _, key := range saFields {
430+
_ = DeleteAuthField(key)
431+
}
432+
406433
authFields := map[authFieldKey]string{
407434
SESSION_EXPIRES_AT_UNIX: sessionExpiresAtUnix,
408435
ACCESS_TOKEN: accessToken,
@@ -419,13 +446,25 @@ func LoginUser(email, accessToken, refreshToken, sessionExpiresAtUnix string) er
419446
}
420447

421448
func LogoutUser() error {
422-
for _, key := range loginAuthFieldKeys {
449+
for _, key := range authFieldKeys {
423450
err := DeleteAuthField(key)
424451
if err != nil {
425452
return fmt.Errorf("delete auth field \"%s\": %w", key, err)
426453
}
427454
}
428455

456+
activeProfile, err := config.GetProfile()
457+
if err != nil {
458+
return fmt.Errorf("get profile: %w", err)
459+
}
460+
461+
textFileDir := config.GetProfileFolderPath(activeProfile)
462+
textFilePath := filepath.Join(textFileDir, textFileName)
463+
err = os.Remove(textFilePath)
464+
if err != nil && !os.IsNotExist(err) {
465+
return fmt.Errorf("remove auth storage file: %w", err)
466+
}
467+
429468
return nil
430469
}
431470

@@ -446,5 +485,12 @@ func DeleteProfileAuth(profile string) error {
446485
}
447486
}
448487

488+
textFileDir := config.GetProfileFolderPath(profile)
489+
textFilePath := filepath.Join(textFileDir, textFileName)
490+
err = os.Remove(textFilePath)
491+
if err != nil && !os.IsNotExist(err) {
492+
return fmt.Errorf("remove auth storage file: %w", err)
493+
}
494+
449495
return nil
450496
}

0 commit comments

Comments
 (0)