Make keyring availability checks read-only

Merged
CO
3mo ago
+99-20

What changed

Credential-store initialization now checks Keychain availability with a read-only lookup. A missing availability entry means the keyring is working. Other errors keep the existing plaintext-file fallback.

The keyring interface lets the tests verify that initialization calls Get once and never calls Set or Delete.

Why

Every CLI process previously wrote and deleted the same hey::test item before loading the real credential. Parallel processes could make that probe fail. The affected process then switched to an empty plaintext store and reported not logged in, even though valid credentials were still in Keychain.

I reproduced this with 60 read-only hey auth status --json calls at concurrency 6. Before the change, 25 printed the keyring warning and reported unauthenticated. After the change, all 60 completed without either failure.

Checks

  • env GOWORK=off mise x golangci-lint@2.10.1 -- make check
  • env GOWORK=off mise x -- go test -race -count=1 ./internal/auth
  • 60 concurrent installed CLI auth checks, with zero keyring warnings and zero false unauthenticated results

Summary by cubic

Makes keyring availability checks read-only to remove concurrency races that caused false plaintext fallbacks. Previously the CLI wrote/deleted hey::test; now it does a single Get on hey::availability, where ErrNotFound means the keyring is available and only other errors fall back to the file store.

  • Introduces a credentialKeyring interface with a default systemCredentialKeyring wrapper around github.com/zalando/go-keyring; Store now depends on this interface so tests assert exactly one Get and zero Set/Delete during init.
  • Replaces direct calls to github.com/zalando/go-keyring with s.keyring across load/save/delete paths. External behavior is unchanged except fewer keyring warnings and no spurious unauthenticated states under concurrency.