| 1 | //go:build !linux && !netbsd && !openbsd && !(dragonfly && cgo) && !(freebsd && cgo) |
| 2 | |
| 3 | package config |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/zalando/go-keyring" |
| 11 | ) |
| 12 | |
| 13 | // legacyKeyringProbe reads one legacy credential from the platform keyring. |
| 14 | // keyring.ErrNotFound (and empty values) are absent; other errors stay error. |
| 15 | // The platform Get is not context-aware on these OSes, so the lookup is run |
| 16 | // under the shared migration budget via legacyKeyringGetBounded: a wedged OS |
| 17 | // keyring (locked macOS login keychain, hung Windows Credential Manager) must |
| 18 | // not hang `serve` forever, and headless boxes must not stall startup. |
| 19 | func legacyKeyringProbe(ctx context.Context, key string) legacyKeyringOutcome { |
| 20 | key = strings.TrimSpace(key) |
| 21 | if key == "" { |
| 22 | return legacyKeyringOutcome{Status: legacyKeyringAbsent} |
| 23 | } |
| 24 | if err := ctx.Err(); err != nil { |
| 25 | return legacyKeyringOutcome{Status: legacyKeyringTimeout} |
| 26 | } |
| 27 | value, err, timedOut := legacyKeyringGetBounded(ctx, func() (string, error) { |
| 28 | return keyring.Get(credentialsKeyringService, key) |
| 29 | }) |
| 30 | if timedOut { |
| 31 | return legacyKeyringOutcome{Status: legacyKeyringTimeout} |
| 32 | } |
| 33 | if err != nil { |
| 34 | if errors.Is(err, keyring.ErrNotFound) { |
| 35 | return legacyKeyringOutcome{Status: legacyKeyringAbsent} |
| 36 | } |
| 37 | if ctx.Err() != nil { |
| 38 | return legacyKeyringOutcome{Status: legacyKeyringTimeout} |
| 39 | } |
| 40 | return legacyKeyringOutcome{Status: legacyKeyringError} |
| 41 | } |
| 42 | if strings.TrimSpace(value) == "" { |
| 43 | return legacyKeyringOutcome{Status: legacyKeyringAbsent} |
| 44 | } |
| 45 | return legacyKeyringOutcome{Status: legacyKeyringFound, Value: value} |
| 46 | } |
| 47 |