Files
lazysecrets/internal/ui/app/cache_test.go
T
Karl-Einar Bergstrøm 4377eb98bf feat: share env files as encrypted links (#54)
Share `.env` files as an encrypted `lazysecrets://share/…` link. The files are encrypted with a secret's value, so anyone who can read that secret can open the link.

**Sharing:** `S` → `f` on a repo (all its plain env files), an env file, or a key in one (its file).
- **The key** is `share.key` from the repo's `.lazysecrets.yaml`, else from the config. It can be a `lazysecrets://` link, a Key Vault URL or an `@Microsoft.KeyVault(...)` reference.
- **Without `share.key`,** a picker built on Go to opens, titled "Share .env", with "Pick a secret to encrypt with" in its own border. It lists only secrets, leaving out values from plain `.env` files. Pasting a reference picks it. Picking shares at once, and the picker stays open until the link is copied. If the secret can't be used, the reason shows in red in the picker: it can't be read, or it doesn't meet `share.keyRules` (which rule failed is named). `esc` cancels.
- **`share.keyRules`** (defaults `minLength: 32`, `minClasses: 2`; never under 16) applies when sharing, not when opening.
- **On Azure,** the link pins the key's version, so rotating the key doesn't break old links, and disabling a version revokes them.
- **The command line:** `lazysecrets share [--key <secret>] <env files>`.

**Opening** works by paste, `ctrl+o`, the clipboard offer, the link handler or `lazysecrets <link>`.
1. It first says which repo the link is from, how many files it holds and which key it uses, and decrypts only after `o`.
2. It saves into the repo's clone, found by its remote, showing which files are new, which would replace one there, and which git doesn't ignore.
3. Without a clone, it asks for a folder and saves straight away. The prompt stays open, with the reason in red, until the files are written.
4. If decryption fails, a popup says why: the first line of the reason, `r` for the whole message, `g` to go to the key's secret. A key that has changed is named by where it lives: a Databricks or SOPS value changed in place, an Azure current version that isn't the one the link was made with, or a pinned version, meaning the link may be damaged.

**Format** (`internal/share`): a version byte, then a JSON header with the key, the repo and the file count, then an age file (scrypt 2^15, with the key's value as passphrase) holding the files as deflated JSON, all as unpadded base64url. Links are capped at 16 KB. A future format gets a new version, and old ones stay readable.

**Safety:**
- A link can only write env files, inside the folder: never in `.git`, never through a symlink.
- New files are created with mode 0600.
- Opening accepts an scrypt work factor of at most 2^20, and unpacks at most 1 MB.

**Along the way:**
- Opening a link to a secret ends on its details, as `enter` would.
- `o` on an Azure secret opens its portal page, and in the versions view, that version's page.
- The details pane follows its secret's list when it reloads in the background. Before, it kept what it showed first, including an old portal address.
- `ref.Read` is shared by `get` and sharing, with Azure `VersionedValue` for pinning.
- `provider.Contents` lets the demo repos share their env files. `--demo` has a share key (`kv-shared-prod / env-share`), so the whole flow works there.
- New dependency: `filippo.io/age`.

Tested end to end in the TUI and the demo, the CLI, and the `share` package. In a real vault: version pinning, opening into a clone, and revocation by disabling the version.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed-on: #54
2026-10-13 14:55:22 +02:00

193 lines
5.5 KiB
Go

package app
import (
"context"
"slices"
"testing"
"time"
tea "charm.land/bubbletea/v2"
"git.kebcloud.no/keb/lazysecrets/internal/provider"
"git.kebcloud.no/keb/lazysecrets/internal/provider/fake"
)
// counting records how often lists and details are fetched.
type counting struct {
provider.Provider
containers, objects, describes, values, versions int
}
func (c *counting) Versions(ctx context.Context, o provider.Object) ([]provider.Object, error) {
c.versions++
return c.Provider.Versions(ctx, o)
}
func (c *counting) Containers(ctx context.Context, src provider.Source) ([]provider.Container, error) {
c.containers++
return c.Provider.Containers(ctx, src)
}
func (c *counting) Objects(ctx context.Context, ct provider.Container, t provider.ObjectType) ([]provider.Object, error) {
c.objects++
return c.Provider.Objects(ctx, ct, t)
}
func (c *counting) Describe(ctx context.Context, o provider.Object) (provider.Object, error) {
c.describes++
return c.Provider.Describe(ctx, o)
}
func (c *counting) Value(ctx context.Context, o provider.Object) (provider.SecretValue, error) {
c.values++
return c.Provider.Value(ctx, o)
}
func newCachingDriver(t *testing.T) (*driver, *counting, *time.Time) {
t.Helper()
p := &counting{Provider: fake.Azure(0)}
d := newDriver(t, p)
// Entries from the initial load were stamped with the real clock.
clock := time.Now()
d.m.now = func() time.Time { return clock }
return d, p, &clock
}
func TestCacheAvoidsRefetchAndRestoresCursor(t *testing.T) {
t.Parallel()
d, p, _ := newCachingDriver(t)
d.keys("2", "j", "3", "j") // kv-shared-prod... then back
d.keys("2", "k", "3", "j") // kv-app-prod, cursor to api-key
d.keys("1", "j", "k") // away to platform-dev and back
before := *p
d.keys("j", "k")
if p.containers != before.containers || p.objects != before.objects {
t.Errorf("fresh cache refetched: containers %d→%d, objects %d→%d",
before.containers, p.containers, before.objects, p.objects)
}
if _, c, s := d.selected(); c != "kv-app-prod" || s != "api-key" {
t.Errorf("cursor not restored: %s/%s", c, s)
}
}
func TestStaleCacheShownThenReloaded(t *testing.T) {
t.Parallel()
d, p, clock := newCachingDriver(t)
d.keys("j") // platform-dev
*clock = clock.Add(cacheTTL + time.Second)
before := p.containers
d.keys("k") // back to platform-prod: stale
if p.containers != before+1 {
t.Errorf("stale entry not reloaded: %d calls, want %d", p.containers, before+1)
}
if _, c, _ := d.selected(); c != "kv-app-prod" {
t.Errorf("selection lost across background reload: %q", c)
}
}
func TestRefreshBypassesCache(t *testing.T) {
t.Parallel()
d, p, _ := newCachingDriver(t)
before := p.objects
d.keys("3", "r")
if p.objects != before+1 {
t.Errorf("r did not reload: %d calls, want %d", p.objects, before+1)
}
}
func TestValuesAndErrorsNotCached(t *testing.T) {
t.Parallel()
d, p, _ := newCachingDriver(t)
d.keys("space", "space", "space")
if p.values != 2 {
t.Errorf("value fetched %d times for two reveals, want 2", p.values)
}
d.keys("2", "G") // kv-restricted: 403
if _, ok := d.m.cache.objects[d.m.selContainer]; ok {
t.Error("error result cached")
}
before := p.objects
d.keys("k", "j")
if p.objects != before+2 {
t.Errorf("failed list not retried: %d calls, want %d", p.objects, before+2)
}
}
func TestSignInDropsCache(t *testing.T) {
t.Parallel()
d, _, _ := newCachingDriver(t)
d.send(loginDoneMsg{})
// Everything is reloaded fresh, so only the current selection is cached.
if n := len(d.m.cache.containers); n != 1 {
t.Errorf("containers cached after sign-in = %d, want 1", n)
}
}
// runAll runs cmd and the commands it batches, ignoring their messages.
func runAll(cmd tea.Cmd) {
if cmd == nil {
return
}
if b, ok := cmd().(tea.BatchMsg); ok {
for _, c := range b {
runAll(c)
}
}
}
func TestMovingOnDropsUnsettledLoads(t *testing.T) {
t.Parallel()
c := &counting{Provider: fake.Azure(0)}
d := newDriverOpts(t, Options{}, c)
d.m.settle = 20 * time.Millisecond
before := c.containers
// j to platform-dev and straight back: its load never goes out.
m1, passed := d.m.Update(tea.KeyPressMsg{Code: 'j', Text: "j"})
_, back := m1.Update(tea.KeyPressMsg{Code: 'k', Text: "k"})
runAll(back)
runAll(passed)
if c.containers != before {
t.Errorf("passing over a subscription loaded it: %d calls", c.containers-before)
}
// Stopping on it loads it once the cursor settles.
_, stop := d.m.Update(tea.KeyPressMsg{Code: 'j', Text: "j"})
runAll(stop)
if c.containers != before+1 {
t.Errorf("stopping on a subscription: %d calls, want 1", c.containers-before)
}
}
func TestReloadedListRefreshesShownSecret(t *testing.T) {
t.Parallel()
d, _, clock := newCachingDriver(t)
d.keys("3") // kv-app-prod's secrets, db-password selected
shown, _ := d.m.detail.Secret()
// What an older lazysecrets listed and cached for it.
const old = "https://portal.example/old"
key := d.m.selContainer
e := d.m.cache.objects[key]
items := slices.Clone(e.value)
for i := range items {
if items[i].ID == shown.ID {
items[i].URL = old
}
}
d.m.cache.objects[key] = entry[[]provider.Object]{items, e.at}
stale := shown
stale.URL = old
d.m.cache.details[detailKey(shown)] = entry[provider.Object]{stale, *clock}
// Away and back once the list is stale: it's shown from the cache,
// then reloaded, and the details pane follows.
d.keys("1", "j")
*clock = clock.Add(cacheTTL + time.Second)
d.keys("k", "3")
if got, _ := d.m.detail.Secret(); got.ID != shown.ID || got.URL == old {
t.Errorf("shown %s with %q after the reload", got.Name, got.URL)
}
}