Skip to content

Commit efd736a

Browse files
fix(keyring): read keyring.key as fallback when env var not set
After keyring auto-setup, the password is saved to keyring.key and credentials.env is sourced in the shell profile — but only for new shell sessions. In the current session, subsequent wk commands failed because WK_KEYRING_PASSWORD wasn't in the environment yet. Now fileKeyringPasswordFunc reads keyring.key directly as a fallback, so all wk commands work immediately after auth without requiring manual `source credentials.env`.
1 parent 42db3a1 commit efd736a

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

internal/secrets/store.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"os"
8+
"path/filepath"
89
"runtime"
910
"sort"
1011
"strings"
@@ -138,6 +139,22 @@ func fileKeyringPasswordFuncFrom(password string, passwordSet bool, isTTY bool)
138139

139140
func fileKeyringPasswordFunc() keyring.PromptFunc {
140141
password, passwordSet := os.LookupEnv(keyringPasswordEnv)
142+
143+
// Fallback: if env var not set, try reading the auto-generated keyring.key file.
144+
// This covers the gap between keyring auto-setup (which writes keyring.key and
145+
// updates the shell profile) and the user's next shell session (where the profile
146+
// source directive takes effect).
147+
if !passwordSet {
148+
if dir, err := config.Dir(); err == nil {
149+
if data, err := os.ReadFile(filepath.Join(dir, "keyring.key")); err == nil { //nolint:gosec // config path
150+
if pw := strings.TrimSpace(string(data)); pw != "" {
151+
password = pw
152+
passwordSet = true
153+
}
154+
}
155+
}
156+
}
157+
141158
return fileKeyringPasswordFuncFrom(password, passwordSet, term.IsTerminal(int(os.Stdin.Fd())))
142159
}
143160

0 commit comments

Comments
 (0)