Help us shape the future of Go! Take the 2026 Go Developer Survey

vault

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package vault is the local, encrypted store of protected env files and their revision history (~/.syncmyenv/vault.db).

Package vault owns the local SQLite vault (~/.syncmyenv/vault.db): projects, files, revisions, sync_queue, devices, settings.

Revisions are full encrypted snapshots (env files are tiny). Paths and project names are stored encrypted; dedup uses HMAC(vault key, content). See docs/architecture.md.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotProtected = errors.New("file is not protected — run `sme protect` first")
	ErrNoRevision   = errors.New("no such revision")
)
View Source
var ErrAlreadyInitialized = errors.New("vault already initialized")

ErrAlreadyInitialized protects an existing vault key from being overwritten (that would make every revision sealed to it unrecoverable).

View Source
var ErrNotInitialized = errors.New("vault not initialized — run `sme init`")

ErrNotInitialized means `sme init` hasn't been run for this SYNCMYENV_HOME.

Functions

func CleanPath

func CleanPath(p string) (string, error)

CleanPath makes a path absolute and clean, with symlinks in its directories resolved — even when some of those directories don't exist (yet, or any more): the deepest existing ancestor is resolved and the rest re-appended. So "/var/x/app/.env" and "/private/var/x/app/.env" (macOS) are the same file whether or not app/ still exists. The file itself is never resolved: a symlinked .env is not followed.

func CreateKeyFile

func CreateKeyFile(f *crypto.KeyFile) error

CreateKeyFile writes a new key file, refusing to replace an existing one.

func FromPortable

func FromPortable(p string) string

FromPortable is the inverse of ToPortable on this machine.

func KeyFilePath

func KeyFilePath() (string, error)

KeyFilePath is ~/.syncmyenv/keys.json.

func LoadKeyFile

func LoadKeyFile() (*crypto.KeyFile, error)

LoadKeyFile reads the wrapped vault key.

func SafeImportPath

func SafeImportPath(p string) bool

SafeImportPath limits what a remote entry may name: a home-relative or absolute path, no ".." tricks, and an env-file name (.env, .env.*, env, env.*). Even a legitimate-but-compromised device can't make `restore` write ~/.bashrc or ~/.ssh/authorized_keys.

func ToPortable

func ToPortable(abs string) string

ToPortable turns an absolute path into "~/…" when it's under the home directory, so it maps onto other machines with different usernames.

func UpdateKeyFile

func UpdateKeyFile(f *crypto.KeyFile) error

UpdateKeyFile replaces the key file (e.g. after a password change).

Types

type File

type File struct {
	ID          string
	Path        string
	Project     string
	ProtectedAt time.Time
	Revisions   int
	LastChange  time.Time
	Size        int
}

File is a protected (or formerly protected) env file.

type ImportResult

type ImportResult struct {
	Path      string
	Seq       int  // local seq assigned (0 = already had it)
	Applied   bool // written to disk (fast-forward)
	Conflict  bool // disk had local, unsnapshotted edits; kept them, remote is in history
	OnlyStore bool // file missing on disk: stored in history (sme restore --missing)
}

ImportResult says what happened to a remote revision.

type PendingRevision

type PendingRevision struct {
	RevisionID string
	Path       string
	Project    string
	Seq        int
	Size       int
	Source     string
	Device     string
	CreatedAt  int64
	Sealed     []byte
}

PendingRevision is a local revision not yet pushed to the server.

type ProtectResult

type ProtectResult struct {
	Path    string
	Added   bool // newly protected (false = already protected)
	Skipped string
}

ProtectResult says what Protect did for one path.

type RestoreResult

type RestoreResult struct {
	Path      string
	FromSeq   int // revision that was restored
	SavedSeq  int // revision capturing the overwritten content (0 if none/unchanged)
	NewSeq    int // revision recording the restore
	Unchanged bool
}

RestoreResult describes a restore.

type Revision

type Revision struct {
	Seq       int
	Size      int
	Source    string
	Device    string
	CreatedAt time.Time
	// contains filtered or unexported fields
}

Revision is one sealed version of a file. Contents need the vault key.

type SnapshotResult

type SnapshotResult struct {
	Path    string
	Seq     int    // new revision number, 0 if unchanged
	Missing bool   // file no longer exists on disk
	Error   string // unreadable, too large…
}

SnapshotResult reports what a snapshot found for one file.

type Vault

type Vault struct {
	// contains filtered or unexported fields
}

Vault is an open local vault. It can protect, snapshot and list without the master password; reading contents (Restore) needs an unlocked VaultKey.

func Open

func Open(ctx context.Context) (*Vault, error)

Open opens the vault for the current SYNCMYENV_HOME. Requires `sme init`.

func (*Vault) Close

func (v *Vault) Close() error

func (*Vault) CountPending

func (v *Vault) CountPending(ctx context.Context) (int, error)

CountPending returns how many local revisions await pushing.

func (*Vault) DeviceCert

func (v *Vault) DeviceCert(ctx context.Context) ([]byte, error)

DeviceCert returns this device's certificate, if one has been issued.

func (*Vault) DeviceKey

func (v *Vault) DeviceKey() (ed25519.PrivateKey, error)

DeviceKey is this machine's log-signing key (~/.syncmyenv/device.key, 0600). It never leaves the device. Its public half is certified by the vault key.

func (*Vault) History

func (v *Vault) History(ctx context.Context, path string) ([]Revision, error)

History lists revisions of a file, newest first. Works for unprotected files too.

func (*Vault) ImportRemote

func (v *Vault) ImportRemote(ctx context.Context, key *crypto.VaultKey, p protocol.EntryPayload, originDevice, objectID string, sealed []byte) (*ImportResult, error)

ImportRemote adds a revision that came from another device. key is needed to compute the change MAC (and to write the content to disk on fast-forward).

Fast-forward rule: if the file on disk still equals this device's latest known revision (no local edits since), the remote content is written to disk. Otherwise disk wins for now and the remote revision is kept in history — nothing is ever lost.

func (*Vault) KeyFile

func (v *Vault) KeyFile() *crypto.KeyFile

KeyFile returns the vault's (wrapped) key file.

func (*Vault) List

func (v *Vault) List(ctx context.Context) ([]File, error)

List returns protected files (not unprotected ones).

func (*Vault) MarkPushed

func (v *Vault) MarkPushed(ctx context.Context, revisionIDs, objectIDs []string) error

MarkPushed records that revisions reached the server.

func (*Vault) Missing

func (v *Vault) Missing(ctx context.Context) ([]File, error)

Missing returns protected files that don't exist on disk (e.g. new machine, deleted).

func (*Vault) NextDeviceSeq

func (v *Vault) NextDeviceSeq(ctx context.Context, n int) (int64, error)

NextDeviceSeq atomically reserves n device sequence numbers, returning the first.

func (*Vault) Pending

func (v *Vault) Pending(ctx context.Context, limit int) ([]PendingRevision, error)

Pending lists revisions created on this device that haven't been pushed.

func (*Vault) Protect

func (v *Vault) Protect(ctx context.Context, paths []string) ([]ProtectResult, error)

Protect starts protecting files and seals their current contents as revision 1.

func (*Vault) Restore

func (v *Vault) Restore(ctx context.Context, key *crypto.VaultKey, path string, seq int, to string) (*RestoreResult, error)

Restore writes revision seq (0 = latest) back to disk, or to `to` if set. The current on-disk content is snapshotted first, so a restore never loses data.

func (*Vault) SetDeviceCert

func (v *Vault) SetDeviceCert(ctx context.Context, cert []byte) error

SetDeviceCert stores the certificate issued by an unlocked vault key.

func (*Vault) SetState

func (v *Vault) SetState(ctx context.Context, key, value string) error

SetState writes a sync_state value.

func (*Vault) Snapshot

func (v *Vault) Snapshot(ctx context.Context) ([]SnapshotResult, error)

Snapshot seals a new revision for every protected file whose content changed. Needs no password: it only uses the public recipient.

func (*Vault) SnapshotPaths

func (v *Vault) SnapshotPaths(ctx context.Context, only []string) ([]SnapshotResult, error)

SnapshotPaths is Snapshot limited to the given absolute paths (nil = all). Paths that aren't protected are ignored.

func (*Vault) State

func (v *Vault) State(ctx context.Context, key string) (string, error)

State reads a sync_state value ("" if unset).

func (*Vault) Unprotect

func (v *Vault) Unprotect(ctx context.Context, path string) error

Unprotect stops watching a file. Its history is kept (and restorable).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL