WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
// Package secrets stores and retrieves credentials. Production wires
|
||||
// KeyringStore (OS keychain, primary) with FileStore (0600 plaintext under
|
||||
// $XDG_CONFIG_HOME/weknora/secrets/, used as a fallback when no keyring
|
||||
// backend is available).
|
||||
//
|
||||
// Namespace convention: "weknora:<profile>:<key>" where key is "access",
|
||||
// "refresh", or "api_key".
|
||||
package secrets
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/Tencent/WeKnora/cli/internal/xdg"
|
||||
)
|
||||
|
||||
// ErrNotFound is returned when the requested secret does not exist.
|
||||
var ErrNotFound = errors.New("secret: not found")
|
||||
|
||||
// Store is the abstraction CLI commands depend on; tests inject in-memory impls.
|
||||
//
|
||||
// Ref returns a stable URI (e.g. file://<profile>/<key> or
|
||||
// keychain://weknora/<profile>/<key>) describing where a saved secret lives.
|
||||
// Backends own their scheme so commands never need to type-assert the
|
||||
// concrete implementation.
|
||||
type Store interface {
|
||||
Get(profile, key string) (string, error)
|
||||
Set(profile, key, value string) error
|
||||
Delete(profile, key string) error
|
||||
Ref(profile, key string) string
|
||||
}
|
||||
|
||||
// FileStore writes 0600 plain-text files under $XDG_CONFIG_HOME/weknora/secrets/<profile>.
|
||||
// It is the headless / CI default and the keychain fallback.
|
||||
type FileStore struct {
|
||||
root string
|
||||
}
|
||||
|
||||
// NewFileStore returns a FileStore rooted at $XDG_CONFIG_HOME/weknora/secrets
|
||||
// (or ~/.config/weknora/secrets if XDG_CONFIG_HOME is unset). Same convention
|
||||
// as config.Path - see that file for the rationale (CLI convention).
|
||||
func NewFileStore() (*FileStore, error) {
|
||||
root, err := defaultRoot()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &FileStore{root: root}, nil
|
||||
}
|
||||
|
||||
func defaultRoot() (string, error) {
|
||||
return xdg.Path("XDG_CONFIG_HOME", ".config", "secrets")
|
||||
}
|
||||
|
||||
func (f *FileStore) path(profile, key string) string {
|
||||
return filepath.Join(f.root, profile, key)
|
||||
}
|
||||
|
||||
func (f *FileStore) Get(profile, key string) (string, error) {
|
||||
data, err := os.ReadFile(f.path(profile, key))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return "", ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read secret: %w", err)
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func (f *FileStore) Set(profile, key, value string) error {
|
||||
dir := filepath.Join(f.root, profile)
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
return fmt.Errorf("mkdir secrets dir: %w", err)
|
||||
}
|
||||
if err := os.WriteFile(f.path(profile, key), []byte(value), 0o600); err != nil {
|
||||
return fmt.Errorf("write secret: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *FileStore) Delete(profile, key string) error {
|
||||
err := os.Remove(f.path(profile, key))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Ref returns the file:// URI under which a secret would be stored. Path
|
||||
// component is forward-slash-normalized per RFC 8089 (file:///path on Unix,
|
||||
// file:///C:/... on Windows) - the wire format must not depend on platform
|
||||
// path separator since this string is persisted to config.yaml and may be
|
||||
// consumed by tooling on a different OS.
|
||||
func (f *FileStore) Ref(profile, key string) string {
|
||||
p := filepath.ToSlash(f.path(profile, key))
|
||||
if !strings.HasPrefix(p, "/") {
|
||||
// Windows absolute path "C:/..." needs a leading slash to form a
|
||||
// proper file:/// URI ("file:///C:/...").
|
||||
p = "/" + p
|
||||
}
|
||||
return "file://" + p
|
||||
}
|
||||
|
||||
// MemStore is an in-memory implementation of Store used by tests across
|
||||
// packages. It is intentionally exported (not _test.go-only) so that
|
||||
// downstream packages can compose it without copying the same 20 lines.
|
||||
type MemStore struct{ m map[string]string }
|
||||
|
||||
// NewMemStore returns an empty in-memory secrets store.
|
||||
func NewMemStore() *MemStore { return &MemStore{m: map[string]string{}} }
|
||||
|
||||
func (m *MemStore) k(c, key string) string { return c + ":" + key }
|
||||
|
||||
func (m *MemStore) Get(c, key string) (string, error) {
|
||||
v, ok := m.m[m.k(c, key)]
|
||||
if !ok {
|
||||
return "", ErrNotFound
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (m *MemStore) Set(c, key, value string) error { m.m[m.k(c, key)] = value; return nil }
|
||||
|
||||
func (m *MemStore) Delete(c, key string) error { delete(m.m, m.k(c, key)); return nil }
|
||||
|
||||
// Ref returns a mem:// URI; meaningful only inside tests.
|
||||
func (m *MemStore) Ref(c, key string) string { return "mem://" + c + "/" + key }
|
||||
Reference in New Issue
Block a user