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
104 lines
3.2 KiB
Go
104 lines
3.2 KiB
Go
package ref
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"git.kebcloud.no/keb/lazysecrets/internal/provider"
|
|
)
|
|
|
|
// Read reads what r points at without listing its vault: the vault and
|
|
// name are enough for Azure, tried with each account (account, the one
|
|
// that last read it, first, in source); for Databricks and repos the
|
|
// profile or repo is found by its host. It also returns the version read,
|
|
// when the provider tells (provider.Versioned).
|
|
func Read(ctx context.Context, providers []provider.Provider, r Ref, account, source string) (provider.SecretValue, string, error) {
|
|
if r.IsShare() || r.IsSource() || r.IsContainer() {
|
|
what := "vault or scope"
|
|
switch {
|
|
case r.IsShare():
|
|
what = "share link"
|
|
case r.IsSource():
|
|
what = "subscription or workspace"
|
|
}
|
|
return provider.SecretValue{}, "", fmt.Errorf("%s points at a %s, not a secret", r, what)
|
|
}
|
|
if r.Kind != provider.KindAzure && r.Host == "" {
|
|
return provider.SecretValue{}, "", errors.New("{{secrets/…}} names no workspace: use its lazysecrets:// link (s in lazysecrets)")
|
|
}
|
|
|
|
type try struct {
|
|
p provider.Provider
|
|
o provider.Object
|
|
}
|
|
var tries []try
|
|
for _, p := range providers {
|
|
if p.Kind() != r.Kind {
|
|
continue
|
|
}
|
|
o := provider.Object{Name: r.Name, Type: cmp.Or(r.Type, provider.TypeSecret), Version: r.Version}
|
|
if r.Kind != provider.KindAzure { // by host: workspaces, repos
|
|
loc, ok := p.(provider.Locator)
|
|
if !ok {
|
|
continue
|
|
}
|
|
profile, found, err := loc.Locate(ctx, r.Host)
|
|
if err != nil || !found {
|
|
continue
|
|
}
|
|
src := provider.Source{Kind: r.Kind, Provider: p.ID(), ID: profile}
|
|
o.Container = provider.Container{Source: src, Name: r.Scope}
|
|
// Listing the scopes tells a Key Vault-backed one, whose value
|
|
// can then be read from the vault if Databricks refuses.
|
|
if cs, err := p.Containers(ctx, src); err == nil {
|
|
for _, c := range cs {
|
|
if c.Name == r.Scope {
|
|
c.Source = src
|
|
o.Container = c
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
src := provider.Source{Kind: r.Kind, Provider: p.ID(), ID: source}
|
|
o.Container = provider.Container{Source: src, Name: r.ContainerName(), Detail: r.VaultURL()}
|
|
}
|
|
t := try{p, o}
|
|
if p.ID() == account {
|
|
tries = append([]try{t}, tries...)
|
|
} else {
|
|
tries = append(tries, t)
|
|
}
|
|
}
|
|
if len(tries) == 0 {
|
|
switch r.Kind {
|
|
case provider.KindDatabricks:
|
|
return provider.SecretValue{}, "", fmt.Errorf("no Databricks profile for workspace %s", r.Host)
|
|
case provider.KindRepo:
|
|
return provider.SecretValue{}, "", fmt.Errorf("repo %s isn't here: run it inside the repo, or list it under providers.repos.paths", r.Host)
|
|
}
|
|
return provider.SecretValue{}, "", fmt.Errorf("no %s account to read %s with", r.Kind, r)
|
|
}
|
|
var errs []error
|
|
for _, t := range tries {
|
|
v, version, err := value(ctx, t.p, t.o)
|
|
if err == nil {
|
|
return v, version, nil
|
|
}
|
|
if len(tries) == 1 || ctx.Err() != nil {
|
|
return provider.SecretValue{}, "", err
|
|
}
|
|
errs = append(errs, fmt.Errorf("%s: %w", t.p.Label(), err))
|
|
}
|
|
return provider.SecretValue{}, "", errors.Join(errs...)
|
|
}
|
|
|
|
func value(ctx context.Context, p provider.Provider, o provider.Object) (provider.SecretValue, string, error) {
|
|
if vp, ok := p.(provider.Versioned); ok {
|
|
return vp.VersionedValue(ctx, o)
|
|
}
|
|
v, err := p.Value(ctx, o)
|
|
return v, o.Version, err
|
|
}
|