feat(cli): keyring backend + TTY prompter (PR-3)
Adds the OS keyring secret backend and the huh-based TTY Prompter so PR-4 (auth login) can persist credentials and read interactive input. The Factory's Client closure SDK wiring + Secrets closure landed in PR-1's foundation tree to keep PR-1 self-contained. internal/secrets/keyring.go: - KeyringStore: zalando/go-keyring backed; namespace "weknora:<context>". KeyringStore.Ref -> keychain://weknora/<context>/<key> - NewBestEffortStore probes the OS keyring with a sentinel get and falls back to FileStore on unsupported platforms (WSL without DBus, etc.) internal/prompt/tty.go: - TTYPrompter: huh.NewForm / huh.NewInput / huh.NewConfirm wrapper satisfying Prompter. Used when iostreams.IO reports both stdout and stderr are TTYs. Dependencies (zalando/go-keyring, charmbracelet/huh) are declared in PR-1's go.mod since the foundation tree referenced them via the Store interface and TTY-detection helpers.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package prompt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/charmbracelet/huh"
|
||||
)
|
||||
|
||||
// TTYPrompter implements Prompter using charmbracelet/huh for interactive
|
||||
// input. Used by the production Factory.Prompter closure when iostreams.IO
|
||||
// is interactive.
|
||||
type TTYPrompter struct{}
|
||||
|
||||
// NewTTYPrompter returns a Prompter backed by huh.
|
||||
func NewTTYPrompter() Prompter { return TTYPrompter{} }
|
||||
|
||||
func (TTYPrompter) Input(label, defaultValue string) (string, error) {
|
||||
v := defaultValue
|
||||
form := huh.NewForm(huh.NewGroup(
|
||||
huh.NewInput().Title(label).Value(&v),
|
||||
))
|
||||
if err := form.Run(); err != nil {
|
||||
return "", fmt.Errorf("prompt input: %w", err)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (TTYPrompter) Password(label string) (string, error) {
|
||||
var v string
|
||||
form := huh.NewForm(huh.NewGroup(
|
||||
huh.NewInput().Title(label).EchoMode(huh.EchoModePassword).Value(&v),
|
||||
))
|
||||
if err := form.Run(); err != nil {
|
||||
return "", fmt.Errorf("prompt password: %w", err)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (TTYPrompter) Confirm(label string, defaultValue bool) (bool, error) {
|
||||
v := defaultValue
|
||||
form := huh.NewForm(huh.NewGroup(
|
||||
huh.NewConfirm().Title(label).Value(&v),
|
||||
))
|
||||
if err := form.Run(); err != nil {
|
||||
return false, fmt.Errorf("prompt confirm: %w", err)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package secrets
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/zalando/go-keyring"
|
||||
)
|
||||
|
||||
// keyringService is the namespace prefix passed to OS keyring backends.
|
||||
// Final keyring entries look like service="weknora:<context>", account="<key>".
|
||||
const keyringService = "weknora"
|
||||
|
||||
// KeyringStore is the OS-backed credential store: macOS Keychain, GNOME
|
||||
// libsecret, KWallet, Windows Credential Manager. Falls back to ErrUnsupported
|
||||
// (callers swap to FileStore when this happens, see NewBestEffortStore).
|
||||
type KeyringStore struct{}
|
||||
|
||||
// NewKeyringStore returns a KeyringStore. The constructor never fails; first
|
||||
// real Get/Set surfaces ErrUnsupported on systems with no keyring backend.
|
||||
func NewKeyringStore() *KeyringStore { return &KeyringStore{} }
|
||||
|
||||
// service returns the per-context service identifier. Splitting by context
|
||||
// (rather than by host) follows the Supabase pattern (ADR-17): a user with
|
||||
// two tenants on the same host gets two distinct keyring namespaces.
|
||||
func (k *KeyringStore) service(context string) string {
|
||||
return keyringService + ":" + context
|
||||
}
|
||||
|
||||
func (k *KeyringStore) Get(context, key string) (string, error) {
|
||||
v, err := keyring.Get(k.service(context), key)
|
||||
if errors.Is(err, keyring.ErrNotFound) {
|
||||
return "", ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("keyring get: %w", err)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (k *KeyringStore) Set(context, key, value string) error {
|
||||
if err := keyring.Set(k.service(context), key, value); err != nil {
|
||||
return fmt.Errorf("keyring set: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *KeyringStore) Delete(context, key string) error {
|
||||
err := keyring.Delete(k.service(context), key)
|
||||
if errors.Is(err, keyring.ErrNotFound) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("keyring delete: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Ref returns the keychain:// URI under which a secret is stored.
|
||||
func (k *KeyringStore) Ref(context, key string) string {
|
||||
return "keychain://" + keyringService + "/" + context + "/" + key
|
||||
}
|
||||
|
||||
// NewBestEffortStore returns a Store that prefers keyring (when available)
|
||||
// and silently degrades to FileStore on systems without a keyring backend
|
||||
// (e.g. headless CI, WSL without DBus, agent containers).
|
||||
//
|
||||
// Probe is performed by attempting a no-op Get; if the backend signals that
|
||||
// it is unsupported we return the file store directly.
|
||||
func NewBestEffortStore() (Store, error) {
|
||||
k := NewKeyringStore()
|
||||
// Use a sentinel context/key that should not exist; we expect either
|
||||
// ErrNotFound (backend works, just empty) or ErrUnsupported (no backend).
|
||||
_, err := keyring.Get(k.service("__probe__"), "__probe__")
|
||||
if err == nil || errors.Is(err, keyring.ErrNotFound) {
|
||||
return k, nil
|
||||
}
|
||||
// Anything else (including keyring.ErrUnsupportedPlatform) → file store.
|
||||
return NewFileStore()
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package secrets
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/zalando/go-keyring"
|
||||
)
|
||||
|
||||
// Use the in-memory keyring backend for deterministic tests.
|
||||
//
|
||||
// go-keyring exposes MockInit so tests don't need a real OS keychain.
|
||||
func TestKeyringStore_RoundTrip(t *testing.T) {
|
||||
keyring.MockInit()
|
||||
t.Cleanup(func() {
|
||||
// Reset the mock so other test packages don't see leaked state.
|
||||
keyring.MockInit()
|
||||
})
|
||||
k := NewKeyringStore()
|
||||
|
||||
// Get on missing returns ErrNotFound.
|
||||
_, err := k.Get("prod", "access")
|
||||
require.ErrorIs(t, err, ErrNotFound)
|
||||
|
||||
// Set + Get.
|
||||
require.NoError(t, k.Set("prod", "access", "jwt-x"))
|
||||
got, err := k.Get("prod", "access")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "jwt-x", got)
|
||||
|
||||
// Delete + Get.
|
||||
require.NoError(t, k.Delete("prod", "access"))
|
||||
_, err = k.Get("prod", "access")
|
||||
require.ErrorIs(t, err, ErrNotFound)
|
||||
|
||||
// Delete on missing is a no-op.
|
||||
require.NoError(t, k.Delete("prod", "access"))
|
||||
}
|
||||
|
||||
func TestKeyringStore_NamespaceIsolation(t *testing.T) {
|
||||
keyring.MockInit()
|
||||
t.Cleanup(func() { keyring.MockInit() })
|
||||
k := NewKeyringStore()
|
||||
require.NoError(t, k.Set("prod", "access", "P"))
|
||||
require.NoError(t, k.Set("staging", "access", "S"))
|
||||
|
||||
got, _ := k.Get("prod", "access")
|
||||
assert.Equal(t, "P", got)
|
||||
got, _ = k.Get("staging", "access")
|
||||
assert.Equal(t, "S", got)
|
||||
}
|
||||
|
||||
func TestNewBestEffortStore(t *testing.T) {
|
||||
keyring.MockInit() // mock backend "supports" keyring → returns KeyringStore
|
||||
t.Cleanup(func() { keyring.MockInit() })
|
||||
s, err := NewBestEffortStore()
|
||||
require.NoError(t, err)
|
||||
_, ok := s.(*KeyringStore)
|
||||
assert.True(t, ok, "with mock keyring backend, BestEffort must return KeyringStore")
|
||||
}
|
||||
|
||||
func TestKeyringStore_Ref(t *testing.T) {
|
||||
k := NewKeyringStore()
|
||||
got := k.Ref("prod", "access")
|
||||
assert.Equal(t, "keychain://weknora/prod/access", got)
|
||||
}
|
||||
Reference in New Issue
Block a user