Compare commits
3
Commits
baa7ee86a7
...
d2835cb1d4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d2835cb1d4 | ||
|
|
00713b8677 | ||
|
|
71c386ecbf |
No files matched your search
@@ -1,13 +1,7 @@
|
|||||||
export CGO_ENABLED=1
|
export CGO_ENABLED=1
|
||||||
export DOCKER_HOST := ssh://root@ber1app1.local
|
|
||||||
|
|
||||||
# Version information
|
.PHONY: default bootstrap setup build test lint fmt fmt-check check docker \
|
||||||
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
|
docker-run clean install hooks
|
||||||
GIT_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo "unknown")
|
|
||||||
LDFLAGS := -X 'git.eeqj.de/sneak/secret/internal/cli.Version=$(VERSION)' \
|
|
||||||
-X 'git.eeqj.de/sneak/secret/internal/cli.GitCommit=$(GIT_COMMIT)'
|
|
||||||
|
|
||||||
.PHONY: default bootstrap setup test lint fmt fmt-check check docker hooks vet
|
|
||||||
|
|
||||||
default: check
|
default: check
|
||||||
|
|
||||||
@@ -17,13 +11,9 @@ bootstrap:
|
|||||||
setup:
|
setup:
|
||||||
@script/setup
|
@script/setup
|
||||||
|
|
||||||
build: ./secret
|
# Build ./secret; `make build VERSION=x` stamps x instead of `git describe`
|
||||||
|
build:
|
||||||
./secret: ./internal/*/*.go ./pkg/*/*.go ./cmd/*/*.go ./go.*
|
@script/build
|
||||||
go build -v -ldflags "$(LDFLAGS)" -o $@ cmd/secret/main.go
|
|
||||||
|
|
||||||
vet:
|
|
||||||
go vet ./...
|
|
||||||
|
|
||||||
test:
|
test:
|
||||||
@script/test
|
@script/test
|
||||||
@@ -49,7 +39,7 @@ docker-run:
|
|||||||
clean:
|
clean:
|
||||||
rm -f ./secret
|
rm -f ./secret
|
||||||
|
|
||||||
install: ./secret
|
install: build
|
||||||
cp ./secret $(HOME)/bin/secret
|
cp ./secret $(HOME)/bin/secret
|
||||||
|
|
||||||
fmt-check:
|
fmt-check:
|
||||||
|
|||||||
@@ -506,6 +506,9 @@ them. We provide:
|
|||||||
`script/bootstrap`, then `script/install-precommit`
|
`script/bootstrap`, then `script/install-precommit`
|
||||||
- `script/projectname` — output the project name (`secret`); used by
|
- `script/projectname` — output the project name (`secret`); used by
|
||||||
other scripts such as `script/docker`
|
other scripts such as `script/docker`
|
||||||
|
- `script/build` — build the `secret` binary into the repo root, stamping
|
||||||
|
the version (`VERSION` from the environment, else `git describe`) and
|
||||||
|
the git commit
|
||||||
- `script/test` — run `go vet` and the test suite (verbose rerun on
|
- `script/test` — run `go vet` and the test suite (verbose rerun on
|
||||||
failure)
|
failure)
|
||||||
- `script/lint` — run `golangci-lint` in docker only: builds
|
- `script/lint` — run `golangci-lint` in docker only: builds
|
||||||
|
|||||||
@@ -25,6 +25,23 @@ Bring the repo into policy compliance in one commit:
|
|||||||
|
|
||||||
# Completed Steps
|
# Completed Steps
|
||||||
|
|
||||||
|
- 2026-10-04: A failed unlocker add no longer leaves a partial unlocker
|
||||||
|
directory (https://git.eeqj.de/sneak/secret/issues/48).
|
||||||
|
`CreatePGPUnlocker` resolves the GPG key's fingerprint, and it and
|
||||||
|
`CreateKeychainUnlocker` get the long-term key and encrypt everything,
|
||||||
|
before writing anything. All four unlocker types write their files
|
||||||
|
through `secret.WriteDir`: a new unlocker is built in a temporary
|
||||||
|
directory, renamed into place when complete and removed on a failure.
|
||||||
|
One added under the directory name of an existing unlocker is still
|
||||||
|
written into that directory in place
|
||||||
|
(https://git.eeqj.de/sneak/secret/issues/71).
|
||||||
|
- 2026-10-04: The `Makefile` no longer sets `DOCKER_HOST`, so its docker
|
||||||
|
targets use the local docker daemon, or whatever `DOCKER_HOST` the
|
||||||
|
environment sets. `make build` calls the new `script/build`, which
|
||||||
|
stamps the version (`VERSION` from the environment, else
|
||||||
|
`git describe`) and the git commit as before. `build`, `clean`,
|
||||||
|
`install` and `docker-run` are in `.PHONY`; `make install` depends on
|
||||||
|
`build`. The `vet` target is gone: `script/test` runs `go vet` first.
|
||||||
- 2026-10-04: `.gitignore` is the org's standard file, which ignores
|
- 2026-10-04: `.gitignore` is the org's standard file, which ignores
|
||||||
`.env`, `.env.*`, `*.pem` and `*.key` and editor and OS files, plus
|
`.env`, `.env.*`, `*.pem` and `*.key` and editor and OS files, plus
|
||||||
this repo's `/secret`, `*.log`, `*.test` and `settings.local.json`
|
this repo's `/secret`, `*.log`, `*.test` and `settings.local.json`
|
||||||
@@ -96,14 +113,18 @@ Bring the repo into policy compliance in one commit:
|
|||||||
- from `init` or `vault create` killed after the passphrase prompt
|
- from `init` or `vault create` killed after the passphrase prompt
|
||||||
but before the unlocker is written, a vault with no unlocker,
|
but before the unlocker is written, a vault with no unlocker,
|
||||||
which `vault create` has already made the current vault;
|
which `vault create` has already made the current vault;
|
||||||
- from an unlocker add stopped before its metadata is written, a
|
- data under a `.tmp-` name in the state directory: a secret,
|
||||||
directory that `unlocker list` warns about and `unlocker rm`
|
version or unlocker being added, or the secret, version, unlocker
|
||||||
cannot remove;
|
or vault being removed, encrypted keys included. Nothing deletes
|
||||||
- data under a `.tmp-` name in the state directory: a secret or
|
it; it must be deleted by hand
|
||||||
version being added, or the secret, version, unlocker or vault
|
|
||||||
being removed, encrypted keys included. Nothing deletes it; it
|
|
||||||
must be deleted by hand
|
|
||||||
(https://git.eeqj.de/sneak/secret/issues/75).
|
(https://git.eeqj.de/sneak/secret/issues/75).
|
||||||
|
- 2026-10-03: The checks run before changing a vault now stop with an
|
||||||
|
error naming the path and cause when they cannot read what they
|
||||||
|
inspect, instead of reading the failure as "nothing there": the
|
||||||
|
duplicate check before `unlocker add pgp` (an unreadable
|
||||||
|
`unlockers.d` or unlocker metadata file), the secret count that
|
||||||
|
guards removing the last unlocker and removing a vault, and the
|
||||||
|
existing long-term key check before `vault import`.
|
||||||
- 2026-10-03: `version rm`, `version promote` and `get --version`
|
- 2026-10-03: `version rm`, `version promote` and `get --version`
|
||||||
accept a version only if it is one of the versions `version list`
|
accept a version only if it is one of the versions `version list`
|
||||||
lists for that secret, compared as typed before any path is built
|
lists for that secret, compared as typed before any path is built
|
||||||
|
|||||||
+65
-30
@@ -49,7 +49,6 @@ var (
|
|||||||
"is already added as an unlocker")
|
"is already added as an unlocker")
|
||||||
errUnsupportedUnlockerType = errors.New("unsupported unlocker type")
|
errUnsupportedUnlockerType = errors.New("unsupported unlocker type")
|
||||||
errLastUnlocker = errors.New("refusing to remove last unlocker")
|
errLastUnlocker = errors.New("refusing to remove last unlocker")
|
||||||
errUnlockerExists = errors.New("unlocker already exists")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnlockerInfo represents unlocker information for display
|
// UnlockerInfo represents unlocker information for display
|
||||||
@@ -695,8 +694,15 @@ func (cli *Instance) addPGPUnlocker(cmd *cobra.Command) error {
|
|||||||
// Check if this GPG key is already added
|
// Check if this GPG key is already added
|
||||||
expectedID := "pgp-" + fingerprint
|
expectedID := "pgp-" + fingerprint
|
||||||
|
|
||||||
err = cli.checkUnlockerExists(vlt, expectedID)
|
exists, err := cli.checkUnlockerExists(vlt, expectedID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"could not check whether GPG key %s is already an unlocker: %w",
|
||||||
|
gpgKeyID, err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if exists {
|
||||||
return fmt.Errorf("GPG key %s %w", gpgKeyID, errGPGKeyAlreadyUnlocker)
|
return fmt.Errorf("GPG key %s %w", gpgKeyID, errGPGKeyAlreadyUnlocker)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -714,7 +720,8 @@ func (cli *Instance) addPGPUnlocker(cmd *cobra.Command) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnlockersRemove removes an unlocker with safety checks
|
// UnlockersRemove removes an unlocker, holding the state directory lock
|
||||||
|
// while removeUnlocker runs
|
||||||
func (cli *Instance) UnlockersRemove(
|
func (cli *Instance) UnlockersRemove(
|
||||||
unlockerID string, force bool, cmd *cobra.Command,
|
unlockerID string, force bool, cmd *cobra.Command,
|
||||||
) error {
|
) error {
|
||||||
@@ -724,6 +731,13 @@ func (cli *Instance) UnlockersRemove(
|
|||||||
}
|
}
|
||||||
defer release()
|
defer release()
|
||||||
|
|
||||||
|
return cli.removeUnlocker(unlockerID, force, cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// removeUnlocker removes an unlocker with safety checks
|
||||||
|
func (cli *Instance) removeUnlocker(
|
||||||
|
unlockerID string, force bool, cmd *cobra.Command,
|
||||||
|
) error {
|
||||||
// Get current vault
|
// Get current vault
|
||||||
vlt, err := vault.GetCurrentVault(cli.fs, cli.stateDir)
|
vlt, err := vault.GetCurrentVault(cli.fs, cli.stateDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -788,44 +802,65 @@ func (cli *Instance) UnlockerSelect(unlockerID string) error {
|
|||||||
return vlt.SelectUnlocker(unlockerID)
|
return vlt.SelectUnlocker(unlockerID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkUnlockerExists checks if an unlocker with the given ID exists
|
// checkUnlockerExists reports whether the vault already has an unlocker
|
||||||
func (cli *Instance) checkUnlockerExists(vlt *vault.Vault, unlockerID string) error {
|
// with the given ID. It returns an error, and no answer, when unlockers.d
|
||||||
// Get the list of unlockers and check if any match the ID
|
// or an unlocker's metadata file cannot be read; the caller must then not
|
||||||
unlockers, err := vlt.ListUnlockers()
|
// create the unlocker. It reads unlockers.d itself because
|
||||||
if err != nil {
|
// vault.ListUnlockers skips an unlocker it cannot read, which suits
|
||||||
secret.Warn("Could not list unlockers during duplicate check", "error", err)
|
// `unlocker list` but not this check: the skipped unlocker may be the
|
||||||
|
// duplicate. A directory whose metadata file is missing or corrupt is not
|
||||||
return nil // If we can't list unlockers, assume it doesn't exist
|
// a working unlocker and is passed over.
|
||||||
}
|
func (cli *Instance) checkUnlockerExists(
|
||||||
|
vlt *vault.Vault, unlockerID string,
|
||||||
// Get vault directory to construct unlocker instances
|
) (bool, error) {
|
||||||
vaultDir, err := vlt.GetDirectory()
|
vaultDir, err := vlt.GetDirectory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
secret.Warn("Could not get vault directory during duplicate check",
|
return false, fmt.Errorf("failed to get vault directory: %w", err)
|
||||||
"error", err)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check each unlocker's ID
|
|
||||||
unlockersDir := filepath.Join(vaultDir, "unlockers.d")
|
unlockersDir := filepath.Join(vaultDir, "unlockers.d")
|
||||||
|
|
||||||
for _, metadata := range unlockers {
|
entries, err := afero.ReadDir(cli.fs, unlockersDir)
|
||||||
// Construct the unlocker matching this metadata to get its ID
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
id, err := findUnlockerIDByMetadata(cli.fs, unlockersDir, metadata, true)
|
return false, nil
|
||||||
if err != nil {
|
}
|
||||||
secret.Warn(
|
|
||||||
"Could not read unlockers directory during duplicate check, "+
|
|
||||||
"skipping unlocker",
|
|
||||||
"unlockers_dir", unlockersDir, "error", err)
|
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf(
|
||||||
|
"failed to read unlockers directory %s: %w", unlockersDir, err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entry := range entries {
|
||||||
|
if !entry.IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if id != "" && id == unlockerID {
|
unlockerDir := filepath.Join(unlockersDir, entry.Name())
|
||||||
return errUnlockerExists
|
|
||||||
|
metadataBytes, err := afero.ReadFile(
|
||||||
|
cli.fs, filepath.Join(unlockerDir, "unlocker-metadata.json"))
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf(
|
||||||
|
"failed to read metadata of unlocker %s: %w", unlockerDir, err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
var metadata secret.UnlockerMetadata
|
||||||
|
|
||||||
|
err = json.Unmarshal(metadataBytes, &metadata)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if unlockerIDFromDir(cli.fs, unlockerDir, metadata, true) == unlockerID {
|
||||||
|
return true, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return false, nil
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,353 @@
|
|||||||
|
// Unreadable Directory Tests
|
||||||
|
//
|
||||||
|
// The checks that guard adding a PGP unlocker (is this key already an
|
||||||
|
// unlocker?), removing the last unlocker and removing a vault (does the
|
||||||
|
// vault hold secrets?), and importing a mnemonic (does the vault already
|
||||||
|
// have a long-term key?) each look at the vault on disk before acting.
|
||||||
|
// When that look fails they must refuse to act, not read the failure as
|
||||||
|
// "nothing there" and go ahead.
|
||||||
|
//
|
||||||
|
// The tests make the look fail with a wrapper around the in-memory
|
||||||
|
// filesystem, which the state directory lock refuses. So they call the
|
||||||
|
// function each command runs once it holds the lock, such as removeVault
|
||||||
|
// for RemoveVault.
|
||||||
|
|
||||||
|
//nolint:testpackage // white-box test of unexported internals
|
||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"github.com/spf13/afero"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// unreadableTestGPGUserID is the user ID of the throwaway GPG key the
|
||||||
|
// PGP unlocker tests generate, and the --keyid they pass.
|
||||||
|
unreadableTestGPGUserID = "unlocker-test@example.com"
|
||||||
|
|
||||||
|
// unreadableTestSecretName is the secret stored in the vaults the
|
||||||
|
// removal tests remove from.
|
||||||
|
unreadableTestSecretName = "api-key"
|
||||||
|
|
||||||
|
// unreadableTestOtherVault is a second vault for the vault removal
|
||||||
|
// test, since the last vault can never be removed.
|
||||||
|
unreadableTestOtherVault = "work"
|
||||||
|
|
||||||
|
// unreadableTestSecretsDirName is the directory holding a vault's
|
||||||
|
// secrets, and unreadableTestCurrentFileName the per-secret file
|
||||||
|
// naming its current version.
|
||||||
|
unreadableTestSecretsDirName = "secrets.d"
|
||||||
|
unreadableTestCurrentFileName = "current"
|
||||||
|
)
|
||||||
|
|
||||||
|
// errStatFailed is returned by statFailFs in place of a successful stat.
|
||||||
|
var errStatFailed = errors.New("input/output error")
|
||||||
|
|
||||||
|
// statFailFs fails every Stat of one path, as an I/O or permission error
|
||||||
|
// on that path would.
|
||||||
|
type statFailFs struct {
|
||||||
|
afero.Fs
|
||||||
|
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *statFailFs) Stat(name string) (os.FileInfo, error) {
|
||||||
|
if name == f.path {
|
||||||
|
return nil, errStatFailed
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.Fs.Stat(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// errOpenFailed is returned by openFailFs in place of a successful open.
|
||||||
|
var errOpenFailed = errors.New("permission denied")
|
||||||
|
|
||||||
|
// openFailFs fails every Open of one path, as a directory without read
|
||||||
|
// permission does: checking that it exists succeeds, listing it fails.
|
||||||
|
type openFailFs struct {
|
||||||
|
afero.Fs
|
||||||
|
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint:ireturn // afero.File is the interface required by afero.Fs
|
||||||
|
func (f *openFailFs) Open(name string) (afero.File, error) {
|
||||||
|
if name == f.path {
|
||||||
|
return nil, errOpenFailed
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.Fs.Open(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// testVaultDir returns the directory of the named vault in the synthetic
|
||||||
|
// state directory built by newListTestVault.
|
||||||
|
func testVaultDir(vaultName string) string {
|
||||||
|
return filepath.Join(listTestStateDir, "vaults.d", vaultName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newTestInstance returns a CLI instance on fs whose output is discarded.
|
||||||
|
func newTestInstance(fs afero.Fs) (*Instance, *cobra.Command) {
|
||||||
|
cmd := &cobra.Command{}
|
||||||
|
cmd.SetOut(io.Discard)
|
||||||
|
cmd.SetErr(io.Discard)
|
||||||
|
|
||||||
|
return &Instance{fs: fs, stateDir: listTestStateDir, cmd: cmd}, cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// assertDirEntries asserts that dir holds exactly the named entries.
|
||||||
|
func assertDirEntries(t *testing.T, fs afero.Fs, dir string, want ...string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
entries, err := afero.ReadDir(fs, dir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
names := make([]string, 0, len(entries))
|
||||||
|
for _, entry := range entries {
|
||||||
|
names = append(names, entry.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.ElementsMatch(t, want, names)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newTestGPGKey points GNUPGHOME at a fresh directory, generates a GPG key
|
||||||
|
// without a passphrase there, and returns the key's fingerprint.
|
||||||
|
func newTestGPGKey(t *testing.T) string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
// Not t.TempDir(): on macOS its path is too long for the gpg-agent
|
||||||
|
// socket, which is created inside GNUPGHOME there.
|
||||||
|
gnupgHome, err := os.MkdirTemp("", "gpg") //nolint:usetesting // short path
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
t.Cleanup(func() { _ = os.RemoveAll(gnupgHome) })
|
||||||
|
t.Setenv("GNUPGHOME", gnupgHome)
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
|
// Stop the gpg-agent that key generation starts; cleanups run in
|
||||||
|
// reverse order, so this happens before its directory is removed.
|
||||||
|
// t.Context is already canceled when cleanup runs.
|
||||||
|
ctx := context.WithoutCancel(t.Context())
|
||||||
|
_ = exec.CommandContext(ctx, "gpgconf", "--kill", "gpg-agent").Run()
|
||||||
|
})
|
||||||
|
|
||||||
|
output, err := exec.CommandContext(t.Context(), "gpg", "--batch",
|
||||||
|
"--pinentry-mode", "loopback", "--passphrase", "",
|
||||||
|
"--quick-gen-key", unreadableTestGPGUserID, "ed25519", "sign", "never",
|
||||||
|
).CombinedOutput()
|
||||||
|
require.NoError(t, err, "generating the test GPG key: %s", output)
|
||||||
|
|
||||||
|
fingerprint, err := secret.ResolveGPGKeyFingerprint(unreadableTestGPGUserID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
return fingerprint
|
||||||
|
}
|
||||||
|
|
||||||
|
// addTestPGPUnlocker runs `secret unlocker add pgp` for the test key
|
||||||
|
// against fs.
|
||||||
|
func addTestPGPUnlocker(fs afero.Fs) error {
|
||||||
|
instance, cmd := newTestInstance(fs)
|
||||||
|
cmd.Flags().String("keyid", unreadableTestGPGUserID, "")
|
||||||
|
|
||||||
|
return instance.addPGPUnlocker(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAddPGPUnlockerDuplicateCheck asserts that adding a PGP unlocker for
|
||||||
|
// a key that already has one fails, and creates no unlocker directory,
|
||||||
|
// when unlockers.d or the existing unlocker's metadata file cannot be
|
||||||
|
// read; and, as the control case, that the existing unlocker is refused
|
||||||
|
// as a duplicate when everything can be read.
|
||||||
|
//
|
||||||
|
//nolint:paralleltest // t.Setenv (GNUPGHOME) forbids parallel tests
|
||||||
|
func TestAddPGPUnlockerDuplicateCheck(t *testing.T) {
|
||||||
|
fingerprint := newTestGPGKey(t)
|
||||||
|
unlockersDir := filepath.Join(
|
||||||
|
testVaultDir(listTestVaultName), listTestUnlockersDirName)
|
||||||
|
duplicateDir := filepath.Join(unlockersDir, listTestUnlockerDirTwo)
|
||||||
|
|
||||||
|
// newVaultWithDuplicate returns a vault holding an unlocker for the
|
||||||
|
// test key, beside the one newListTestVault writes.
|
||||||
|
newVaultWithDuplicate := func(t *testing.T) afero.Fs {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
base := newListTestVault(t, 1)
|
||||||
|
writePGPUnlocker(t, base, unlockersDir, listTestUnlockerDirTwo,
|
||||||
|
time.Date(2026, time.August, 10, 12, 30, 0, 0, time.UTC),
|
||||||
|
fingerprint)
|
||||||
|
|
||||||
|
return base
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
failFs func(base afero.Fs) afero.Fs
|
||||||
|
wantErr error
|
||||||
|
// wantPath is the path the error must name.
|
||||||
|
wantPath string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "unlockers.d unreadable",
|
||||||
|
failFs: func(base afero.Fs) afero.Fs {
|
||||||
|
return &unlockersDirFailFs{Fs: base}
|
||||||
|
},
|
||||||
|
wantErr: errUnlockersDirUnreadable,
|
||||||
|
wantPath: unlockersDir,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "existing unlocker's metadata unreadable",
|
||||||
|
failFs: func(base afero.Fs) afero.Fs {
|
||||||
|
return &metadataReadFailFs{
|
||||||
|
Fs: base,
|
||||||
|
unreadablePath: filepath.Join(
|
||||||
|
duplicateDir, listTestMetadataFileName),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
wantErr: errMetadataUnreadable,
|
||||||
|
wantPath: duplicateDir,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
base := newVaultWithDuplicate(t)
|
||||||
|
|
||||||
|
err := addTestPGPUnlocker(tt.failFs(base))
|
||||||
|
|
||||||
|
require.ErrorIs(t, err, tt.wantErr)
|
||||||
|
require.NotErrorIs(t, err, errGPGKeyAlreadyUnlocker)
|
||||||
|
assert.Contains(t, err.Error(), tt.wantPath,
|
||||||
|
"the error must name what it could not read")
|
||||||
|
assertDirEntries(t, base, unlockersDir,
|
||||||
|
listTestUnlockerDirOne, listTestUnlockerDirTwo)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("duplicate refused", func(t *testing.T) {
|
||||||
|
base := newVaultWithDuplicate(t)
|
||||||
|
|
||||||
|
err := addTestPGPUnlocker(base)
|
||||||
|
|
||||||
|
require.ErrorIs(t, err, errGPGKeyAlreadyUnlocker)
|
||||||
|
assertDirEntries(t, base, unlockersDir,
|
||||||
|
listTestUnlockerDirOne, listTestUnlockerDirTwo)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeTestSecret stores a secret with a current-version pointer, which is
|
||||||
|
// what makes it count as a secret, in the given vault directory.
|
||||||
|
func writeTestSecret(t *testing.T, fs afero.Fs, vaultDir string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
secretDir := filepath.Join(
|
||||||
|
vaultDir, unreadableTestSecretsDirName, unreadableTestSecretName)
|
||||||
|
require.NoError(t, fs.MkdirAll(secretDir, listTestDirPerm))
|
||||||
|
require.NoError(t, afero.WriteFile(fs,
|
||||||
|
filepath.Join(secretDir, unreadableTestCurrentFileName),
|
||||||
|
[]byte("20260809.001"), listTestFilePerm))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestRemoveLastUnlockerAbortsWhenSecretsUnreadable asserts that the last
|
||||||
|
// unlocker is kept when the secrets it protects cannot be counted.
|
||||||
|
func TestRemoveLastUnlockerAbortsWhenSecretsUnreadable(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
vaultDir := testVaultDir(listTestVaultName)
|
||||||
|
unlockersDir := filepath.Join(vaultDir, listTestUnlockersDirName)
|
||||||
|
secretsDir := filepath.Join(vaultDir, unreadableTestSecretsDirName)
|
||||||
|
|
||||||
|
for _, path := range []string{
|
||||||
|
secretsDir,
|
||||||
|
filepath.Join(secretsDir, unreadableTestSecretName,
|
||||||
|
unreadableTestCurrentFileName),
|
||||||
|
} {
|
||||||
|
t.Run(filepath.Base(path), func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
base := newListTestVault(t, 1)
|
||||||
|
writeTestSecret(t, base, vaultDir)
|
||||||
|
instance, cmd := newTestInstance(&statFailFs{Fs: base, path: path})
|
||||||
|
|
||||||
|
err := instance.removeUnlocker(
|
||||||
|
"pgp-"+listTestGPGKeyID+"A", false, cmd)
|
||||||
|
|
||||||
|
require.ErrorIs(t, err, errStatFailed)
|
||||||
|
assertDirEntries(t, base, unlockersDir, listTestUnlockerDirOne)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestRemoveVaultAbortsWhenSecretsDirUnreadable asserts that a vault is
|
||||||
|
// kept when whether it holds secrets cannot be determined: when checking
|
||||||
|
// that secrets.d exists fails, and when it exists but cannot be listed.
|
||||||
|
func TestRemoveVaultAbortsWhenSecretsDirUnreadable(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
vaultDir := testVaultDir(unreadableTestOtherVault)
|
||||||
|
secretsDir := filepath.Join(vaultDir, unreadableTestSecretsDirName)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
failFs func(base afero.Fs) afero.Fs
|
||||||
|
wantErr error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "check fails",
|
||||||
|
failFs: func(base afero.Fs) afero.Fs {
|
||||||
|
return &statFailFs{Fs: base, path: secretsDir}
|
||||||
|
},
|
||||||
|
wantErr: errStatFailed,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "listing fails",
|
||||||
|
failFs: func(base afero.Fs) afero.Fs {
|
||||||
|
return &openFailFs{Fs: base, path: secretsDir}
|
||||||
|
},
|
||||||
|
wantErr: errOpenFailed,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
base := newListTestVault(t, 1)
|
||||||
|
writeTestSecret(t, base, vaultDir)
|
||||||
|
instance, cmd := newTestInstance(tt.failFs(base))
|
||||||
|
|
||||||
|
err := instance.removeVault(cmd, unreadableTestOtherVault, false)
|
||||||
|
|
||||||
|
require.ErrorIs(t, err, tt.wantErr)
|
||||||
|
|
||||||
|
exists, err := afero.DirExists(base, vaultDir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.True(t, exists, "the vault must not be removed")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestVaultImportAbortsWhenPubKeyUnreadable asserts that a mnemonic import
|
||||||
|
// stops when whether the vault already has a long-term key cannot be
|
||||||
|
// determined.
|
||||||
|
func TestVaultImportAbortsWhenPubKeyUnreadable(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
base := newListTestVault(t, 1)
|
||||||
|
instance, cmd := newTestInstance(&statFailFs{
|
||||||
|
Fs: base, path: filepath.Join(testVaultDir(listTestVaultName), "pub.age"),
|
||||||
|
})
|
||||||
|
|
||||||
|
err := instance.importMnemonic(cmd, listTestVaultName)
|
||||||
|
|
||||||
|
require.ErrorIs(t, err, errStatFailed)
|
||||||
|
}
|
||||||
+30
-8
@@ -401,8 +401,12 @@ func (cli *Instance) vaultImportPreflight(
|
|||||||
// Check if vault already has a public key
|
// Check if vault already has a public key
|
||||||
pubKeyPath := vaultDir + "/pub.age"
|
pubKeyPath := vaultDir + "/pub.age"
|
||||||
|
|
||||||
_, err = cli.fs.Stat(pubKeyPath)
|
exists, err = afero.Exists(cli.fs, pubKeyPath)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
|
return "", "", "", fmt.Errorf("failed to check %s: %w", pubKeyPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if exists {
|
||||||
return "", "", "", fmt.Errorf("vault '%s' %w",
|
return "", "", "", fmt.Errorf("vault '%s' %w",
|
||||||
vaultName, errVaultHasLongTermKey)
|
vaultName, errVaultHasLongTermKey)
|
||||||
}
|
}
|
||||||
@@ -562,17 +566,26 @@ func (cli *Instance) importMnemonic(cmd *cobra.Command, vaultName string) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// vaultHasSecrets reports whether the vault directory contains any secrets
|
// vaultHasSecrets reports whether the vault directory contains any secrets
|
||||||
func (cli *Instance) vaultHasSecrets(vaultDir string) bool {
|
func (cli *Instance) vaultHasSecrets(vaultDir string) (bool, error) {
|
||||||
secretsDir := filepath.Join(vaultDir, "secrets.d")
|
secretsDir := filepath.Join(vaultDir, "secrets.d")
|
||||||
|
|
||||||
exists, _ := afero.DirExists(cli.fs, secretsDir)
|
exists, err := afero.DirExists(cli.fs, secretsDir)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to check secrets directory %s: %w",
|
||||||
|
secretsDir, err)
|
||||||
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return false
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
entries, err := afero.ReadDir(cli.fs, secretsDir)
|
entries, err := afero.ReadDir(cli.fs, secretsDir)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to read secrets directory %s: %w",
|
||||||
|
secretsDir, err)
|
||||||
|
}
|
||||||
|
|
||||||
return err == nil && len(entries) > 0
|
return len(entries) > 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// switchAwayFromVault selects another vault as current before removal
|
// switchAwayFromVault selects another vault as current before removal
|
||||||
@@ -601,7 +614,8 @@ func (cli *Instance) switchAwayFromVault(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveVault removes a vault with safety checks
|
// RemoveVault removes a vault, holding the state directory lock while
|
||||||
|
// removeVault runs
|
||||||
func (cli *Instance) RemoveVault(cmd *cobra.Command, name string, force bool) error {
|
func (cli *Instance) RemoveVault(cmd *cobra.Command, name string, force bool) error {
|
||||||
release, err := vault.LockStateDir(cli.fs, cli.stateDir)
|
release, err := vault.LockStateDir(cli.fs, cli.stateDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -609,6 +623,11 @@ func (cli *Instance) RemoveVault(cmd *cobra.Command, name string, force bool) er
|
|||||||
}
|
}
|
||||||
defer release()
|
defer release()
|
||||||
|
|
||||||
|
return cli.removeVault(cmd, name, force)
|
||||||
|
}
|
||||||
|
|
||||||
|
// removeVault removes a vault with safety checks
|
||||||
|
func (cli *Instance) removeVault(cmd *cobra.Command, name string, force bool) error {
|
||||||
// Get list of all vaults
|
// Get list of all vaults
|
||||||
vaults, err := vault.ListVaults(cli.fs, cli.stateDir)
|
vaults, err := vault.ListVaults(cli.fs, cli.stateDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -642,7 +661,10 @@ func (cli *Instance) RemoveVault(cmd *cobra.Command, name string, force bool) er
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if vault has secrets
|
// Check if vault has secrets
|
||||||
hasSecrets := cli.vaultHasSecrets(vaultDir)
|
hasSecrets, err := cli.vaultHasSecrets(vaultDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Require --force if vault has secrets
|
// Require --force if vault has secrets
|
||||||
if hasSecrets && !force {
|
if hasSecrets && !force {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package secret
|
package secret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@@ -61,6 +62,52 @@ func TempDirFor(fs afero.Fs, target string) (string, error) {
|
|||||||
return dir, nil
|
return dir, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteDir calls write to write the files of the directory dir. When dir does
|
||||||
|
// not exist yet, write writes them into a temporary directory from TempDirFor,
|
||||||
|
// which is then renamed to dir, so that neither a failure nor a crash leaves
|
||||||
|
// dir half-written; on a failure the temporary directory is removed, and a
|
||||||
|
// failure to remove it is returned along with the first. A directory cannot be
|
||||||
|
// renamed over one that has files in it, so when dir already exists, write
|
||||||
|
// writes into it in place; dir is then never removed.
|
||||||
|
func WriteDir(fs afero.Fs, dir string, write func(dir string) error) error {
|
||||||
|
exists, err := afero.Exists(fs, dir)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to check for %s: %w", dir, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if exists {
|
||||||
|
return write(dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the directory the finished one is renamed into
|
||||||
|
err = fs.MkdirAll(filepath.Dir(dir), DirPerms)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create %s: %w", filepath.Dir(dir), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp, err := TempDirFor(fs, dir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = write(tmp)
|
||||||
|
if err == nil {
|
||||||
|
err = fs.Rename(tmp, dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
removeErr := fs.RemoveAll(tmp)
|
||||||
|
if removeErr != nil {
|
||||||
|
err = errors.Join(err,
|
||||||
|
fmt.Errorf("failed to remove %s: %w", tmp, removeErr))
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// RemoveDirAtomic deletes the directory dir so that it disappears in one
|
// RemoveDirAtomic deletes the directory dir so that it disappears in one
|
||||||
// rename: dir is moved into a new directory from TempDirFor, which is then
|
// rename: dir is moved into a new directory from TempDirFor, which is then
|
||||||
// deleted. A crash part-way leaves only that temporary directory behind.
|
// deleted. A crash part-way leaves only that temporary directory behind.
|
||||||
|
|||||||
+101
-15
@@ -35,6 +35,10 @@ const currentFile = "current"
|
|||||||
// unlockerMetadataFile is the file a new unlocker writes last.
|
// unlockerMetadataFile is the file a new unlocker writes last.
|
||||||
const unlockerMetadataFile = "unlocker-metadata.json"
|
const unlockerMetadataFile = "unlocker-metadata.json"
|
||||||
|
|
||||||
|
// privKeyFile is the file that holds the encrypted private key of a version
|
||||||
|
// or of a passphrase unlocker.
|
||||||
|
const privKeyFile = "priv.age"
|
||||||
|
|
||||||
// unlockerPassphrase protects the passphrase unlockers the tests create.
|
// unlockerPassphrase protects the passphrase unlockers the tests create.
|
||||||
//
|
//
|
||||||
//nolint:gosec // G101: test data, not a real credential
|
//nolint:gosec // G101: test data, not a real credential
|
||||||
@@ -447,7 +451,7 @@ func TestVersionSaveIsWholeOrAbsent(t *testing.T) {
|
|||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
assert.ElementsMatch(t,
|
assert.ElementsMatch(t,
|
||||||
[]string{"pub.age", "value.age", "priv.age", "metadata.age"},
|
[]string{"pub.age", "value.age", privKeyFile, "metadata.age"},
|
||||||
dirNames(t, base, versionDir),
|
dirNames(t, base, versionDir),
|
||||||
"version directory visible before it was complete")
|
"version directory visible before it was complete")
|
||||||
}
|
}
|
||||||
@@ -490,7 +494,7 @@ func TestVersionSaveFailureLeavesNothing(t *testing.T) {
|
|||||||
writeLongTermKey(t, base, stateDir)
|
writeLongTermKey(t, base, stateDir)
|
||||||
|
|
||||||
fs := hookFs{Fs: base, before: func(op, path string) error {
|
fs := hookFs{Fs: base, before: func(op, path string) error {
|
||||||
if op == opRename && filepath.Base(path) == "priv.age" {
|
if op == opRename && filepath.Base(path) == privKeyFile {
|
||||||
return errInjected
|
return errInjected
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -636,15 +640,21 @@ func TestPassphraseUnlockerGetsKeyFirst(t *testing.T) {
|
|||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestPassphraseUnlockerWritesMetadataLast checks that the last file a new
|
// TestPassphraseUnlockerIsWholeOrAbsent checks, before every change that
|
||||||
// passphrase unlocker writes in its directory is its metadata: an unlocker
|
// creating a passphrase unlocker makes, that the unlocker's directory either
|
||||||
// directory without metadata is never used, so one interrupted earlier
|
// does not exist or holds all of its files: a crash or a failure at any point
|
||||||
// cannot be.
|
// leaves no partial unlocker.
|
||||||
func TestPassphraseUnlockerWritesMetadataLast(t *testing.T) {
|
//
|
||||||
|
//nolint:paralleltest // t.Setenv forbids t.Parallel
|
||||||
|
func TestPassphraseUnlockerIsWholeOrAbsent(t *testing.T) {
|
||||||
t.Setenv(secret.EnvMnemonic, testMnemonic)
|
t.Setenv(secret.EnvMnemonic, testMnemonic)
|
||||||
|
|
||||||
base := afero.NewMemMapFs()
|
files := []string{"pub.age", privKeyFile, "longterm.age", unlockerMetadataFile}
|
||||||
vlt, err := vault.CreateVault(base, testVaultStateDir, testVaultName)
|
|
||||||
|
for _, tfs := range testFilesystems {
|
||||||
|
t.Run(tfs.name, func(t *testing.T) {
|
||||||
|
base, stateDir := tfs.open(t)
|
||||||
|
vlt, err := vault.CreateVault(base, stateDir, testVaultName)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
vaultDir, err := vlt.GetDirectory()
|
vaultDir, err := vlt.GetDirectory()
|
||||||
@@ -652,11 +662,13 @@ func TestPassphraseUnlockerWritesMetadataLast(t *testing.T) {
|
|||||||
|
|
||||||
unlockerDir := filepath.Join(vaultDir, "unlockers.d", "passphrase")
|
unlockerDir := filepath.Join(vaultDir, "unlockers.d", "passphrase")
|
||||||
|
|
||||||
var last string
|
fs := hookFs{Fs: base, before: func(string, string) error {
|
||||||
|
exists, err := afero.DirExists(base, unlockerDir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
fs := hookFs{Fs: base, before: func(_, path string) error {
|
if exists {
|
||||||
if filepath.Dir(path) == unlockerDir {
|
assert.ElementsMatch(t, files, dirNames(t, base, unlockerDir),
|
||||||
last = filepath.Base(path)
|
"unlocker directory visible before it was complete")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -665,8 +677,82 @@ func TestPassphraseUnlockerWritesMetadataLast(t *testing.T) {
|
|||||||
passphrase := memguard.NewBufferFromBytes([]byte(unlockerPassphrase))
|
passphrase := memguard.NewBufferFromBytes([]byte(unlockerPassphrase))
|
||||||
defer passphrase.Destroy()
|
defer passphrase.Destroy()
|
||||||
|
|
||||||
_, err = vault.NewVault(fs, testVaultStateDir, testVaultName).
|
_, err = vault.NewVault(fs, stateDir, testVaultName).
|
||||||
CreatePassphraseUnlocker(passphrase)
|
CreatePassphraseUnlocker(passphrase)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, unlockerMetadataFile, last)
|
assert.ElementsMatch(t, files, dirNames(t, base, unlockerDir))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestWriteDirFailureLeavesNothing makes writing a new directory fail after
|
||||||
|
// a file has been written in it, and checks that neither the directory nor
|
||||||
|
// its temporary directory is left behind; and, when the temporary directory
|
||||||
|
// cannot be removed either, that both failures are reported.
|
||||||
|
func TestWriteDirFailureLeavesNothing(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
for _, tfs := range testFilesystems {
|
||||||
|
t.Run(tfs.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
base, dir := tfs.open(t)
|
||||||
|
listed := filepath.Join(dir, "unlockers.d")
|
||||||
|
target := filepath.Join(listed, "new")
|
||||||
|
|
||||||
|
writeThenFail := func(tmp string) error {
|
||||||
|
require.NoError(t, secret.WriteFileAtomic(base,
|
||||||
|
filepath.Join(tmp, unlockerMetadataFile), []byte("{}")))
|
||||||
|
|
||||||
|
return errInjected
|
||||||
|
}
|
||||||
|
|
||||||
|
err := secret.WriteDir(base, target, writeThenFail)
|
||||||
|
require.ErrorIs(t, err, errInjected)
|
||||||
|
|
||||||
|
// Nothing in the directory that is listed, nor beside it
|
||||||
|
assert.Empty(t, dirNames(t, base, listed))
|
||||||
|
assert.Equal(t, []string{"unlockers.d"}, dirNames(t, base, dir))
|
||||||
|
|
||||||
|
fs := hookFs{Fs: base, before: func(op, _ string) error {
|
||||||
|
if op == opRemove {
|
||||||
|
return os.ErrPermission
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}}
|
||||||
|
|
||||||
|
err = secret.WriteDir(fs, target, writeThenFail)
|
||||||
|
require.ErrorIs(t, err, errInjected)
|
||||||
|
require.ErrorIs(t, err, os.ErrPermission)
|
||||||
|
assert.Empty(t, dirNames(t, base, listed))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestWriteDirKeepsExistingDir makes writing into a directory that already
|
||||||
|
// exists fail, and checks that the directory, with what was in it, is still
|
||||||
|
// there: WriteDir writes into it in place and never removes it.
|
||||||
|
func TestWriteDirKeepsExistingDir(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
for _, tfs := range testFilesystems {
|
||||||
|
t.Run(tfs.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
fs, dir := tfs.open(t)
|
||||||
|
target := filepath.Join(dir, "unlockers.d", "passphrase")
|
||||||
|
require.NoError(t, fs.MkdirAll(target, secret.DirPerms))
|
||||||
|
require.NoError(t, secret.WriteFileAtomic(fs,
|
||||||
|
filepath.Join(target, unlockerMetadataFile), []byte("{}")))
|
||||||
|
|
||||||
|
err := secret.WriteDir(fs, target, func(got string) error {
|
||||||
|
assert.Equal(t, target, got)
|
||||||
|
|
||||||
|
return errInjected
|
||||||
|
})
|
||||||
|
require.ErrorIs(t, err, errInjected)
|
||||||
|
assert.Equal(t, []string{unlockerMetadataFile}, dirNames(t, fs, target))
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -341,16 +341,13 @@ func CreateKeychainUnlocker(fs afero.Fs, stateDir string) (*KeychainUnlocker, er
|
|||||||
return nil, fmt.Errorf("failed to generate keychain item name: %w", err)
|
return nil, fmt.Errorf("failed to generate keychain item name: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create unlocker directory using the keychain item name as the directory name
|
// The unlocker directory is named after the keychain item
|
||||||
vaultDir, err := vault.GetDirectory()
|
vaultDir, err := vault.GetDirectory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get vault directory: %w", err)
|
return nil, fmt.Errorf("failed to get vault directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
unlockerDir := filepath.Join(vaultDir, "unlockers.d", keychainItemName)
|
unlockerDir := filepath.Join(vaultDir, "unlockers.d", keychainItemName)
|
||||||
if err := fs.MkdirAll(unlockerDir, DirPerms); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to create unlocker directory: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 1: Generate a new age keypair for the keychain unlocker
|
// Step 1: Generate a new age keypair for the keychain unlocker
|
||||||
ageIdentity, err := age.GenerateX25519Identity()
|
ageIdentity, err := age.GenerateX25519Identity()
|
||||||
@@ -358,6 +355,8 @@ func CreateKeychainUnlocker(fs afero.Fs, stateDir string) (*KeychainUnlocker, er
|
|||||||
return nil, fmt.Errorf("failed to generate age keypair: %w", err)
|
return nil, fmt.Errorf("failed to generate age keypair: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ageRecipient := ageIdentity.Recipient().String()
|
||||||
|
|
||||||
// Step 2: Generate a random passphrase for encrypting the age private key
|
// Step 2: Generate a random passphrase for encrypting the age private key
|
||||||
agePrivKeyPassphrase, err := generateRandomPassphrase(agePrivKeyPassphraseLength)
|
agePrivKeyPassphrase, err := generateRandomPassphrase(agePrivKeyPassphraseLength)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -365,14 +364,7 @@ func CreateKeychainUnlocker(fs afero.Fs, stateDir string) (*KeychainUnlocker, er
|
|||||||
}
|
}
|
||||||
defer agePrivKeyPassphrase.Destroy()
|
defer agePrivKeyPassphrase.Destroy()
|
||||||
|
|
||||||
// Step 3: Store age recipient as plaintext
|
// Step 3: Encrypt age private key with the generated passphrase
|
||||||
ageRecipient := ageIdentity.Recipient().String()
|
|
||||||
recipientPath := filepath.Join(unlockerDir, "pub.txt")
|
|
||||||
if err := WriteFileAtomic(fs, recipientPath, []byte(ageRecipient)); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to write age recipient: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 4: Encrypt age private key with the generated passphrase and store on disk
|
|
||||||
// Create a secure buffer for the private key
|
// Create a secure buffer for the private key
|
||||||
agePrivKeyStr := ageIdentity.String()
|
agePrivKeyStr := ageIdentity.String()
|
||||||
agePrivKeyBuffer := memguard.NewBufferFromBytes([]byte(agePrivKeyStr))
|
agePrivKeyBuffer := memguard.NewBufferFromBytes([]byte(agePrivKeyStr))
|
||||||
@@ -383,31 +375,20 @@ func CreateKeychainUnlocker(fs afero.Fs, stateDir string) (*KeychainUnlocker, er
|
|||||||
return nil, fmt.Errorf("failed to encrypt age private key with passphrase: %w", err)
|
return nil, fmt.Errorf("failed to encrypt age private key with passphrase: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
agePrivKeyPath := filepath.Join(unlockerDir, "priv.age")
|
// Step 4: Get or derive the long-term private key
|
||||||
if err := WriteFileAtomic(fs, agePrivKeyPath, encryptedAgePrivKey); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to write encrypted age private key: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 5: Get or derive the long-term private key
|
|
||||||
ltPrivKeyData, err := getLongTermPrivateKey(fs, vault)
|
ltPrivKeyData, err := getLongTermPrivateKey(fs, vault)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer ltPrivKeyData.Destroy()
|
defer ltPrivKeyData.Destroy()
|
||||||
|
|
||||||
// Step 6: Encrypt long-term private key to the new age unlocker
|
// Step 5: Encrypt long-term private key to the new age unlocker
|
||||||
encryptedLtPrivKeyToAge, err := EncryptToRecipient(ltPrivKeyData, ageIdentity.Recipient())
|
encryptedLtPrivKeyToAge, err := EncryptToRecipient(ltPrivKeyData, ageIdentity.Recipient())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to encrypt long-term private key to age unlocker: %w", err)
|
return nil, fmt.Errorf("failed to encrypt long-term private key to age unlocker: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write encrypted long-term private key
|
// Step 6: Prepare keychain data
|
||||||
ltPrivKeyPath := filepath.Join(unlockerDir, "longterm.age")
|
|
||||||
if err := WriteFileAtomic(fs, ltPrivKeyPath, encryptedLtPrivKeyToAge); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to write encrypted long-term private key: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 7: Prepare keychain data
|
|
||||||
keychainData := KeychainData{
|
keychainData := KeychainData{
|
||||||
AgePublicKey: ageRecipient,
|
AgePublicKey: ageRecipient,
|
||||||
AgePrivKeyPassphrase: agePrivKeyPassphrase,
|
AgePrivKeyPassphrase: agePrivKeyPassphrase,
|
||||||
@@ -420,12 +401,7 @@ func CreateKeychainUnlocker(fs afero.Fs, stateDir string) (*KeychainUnlocker, er
|
|||||||
}
|
}
|
||||||
defer keychainDataBuffer.Destroy()
|
defer keychainDataBuffer.Destroy()
|
||||||
|
|
||||||
// Step 8: Store data in keychain
|
// Step 7: Prepare enhanced metadata
|
||||||
if err := storeInKeychain(keychainItemName, keychainDataBuffer); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to store data in keychain: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 9: Create and write enhanced metadata
|
|
||||||
keychainMetadata := KeychainUnlockerMetadata{
|
keychainMetadata := KeychainUnlockerMetadata{
|
||||||
UnlockerMetadata: UnlockerMetadata{
|
UnlockerMetadata: UnlockerMetadata{
|
||||||
Type: "keychain",
|
Type: "keychain",
|
||||||
@@ -440,10 +416,33 @@ func CreateKeychainUnlocker(fs afero.Fs, stateDir string) (*KeychainUnlocker, er
|
|||||||
return nil, fmt.Errorf("failed to marshal unlocker metadata: %w", err)
|
return nil, fmt.Errorf("failed to marshal unlocker metadata: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := WriteFileAtomic(fs,
|
// Step 8: Write the unlocker's files and store the data in the keychain,
|
||||||
filepath.Join(unlockerDir, "unlocker-metadata.json"),
|
// the metadata last
|
||||||
metadataBytes); err != nil {
|
err = WriteDir(fs, unlockerDir, func(dir string) error {
|
||||||
return nil, fmt.Errorf("failed to write unlocker metadata: %w", err)
|
if err := WriteFileAtomic(fs, filepath.Join(dir, "pub.txt"), []byte(ageRecipient)); err != nil {
|
||||||
|
return fmt.Errorf("failed to write age recipient: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := WriteFileAtomic(fs, filepath.Join(dir, "priv.age"), encryptedAgePrivKey); err != nil {
|
||||||
|
return fmt.Errorf("failed to write encrypted age private key: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := WriteFileAtomic(fs, filepath.Join(dir, "longterm.age"), encryptedLtPrivKeyToAge); err != nil {
|
||||||
|
return fmt.Errorf("failed to write encrypted long-term private key: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := storeInKeychain(keychainItemName, keychainDataBuffer); err != nil {
|
||||||
|
return fmt.Errorf("failed to store data in keychain: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := WriteFileAtomic(fs, filepath.Join(dir, "unlocker-metadata.json"), metadataBytes); err != nil {
|
||||||
|
return fmt.Errorf("failed to write unlocker metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &KeychainUnlocker{
|
return &KeychainUnlocker{
|
||||||
|
|||||||
@@ -222,20 +222,13 @@ func generatePGPUnlockerName() (string, error) {
|
|||||||
return fmt.Sprintf("%s-pgp-%s", hostname, enrollmentDate), nil
|
return fmt.Sprintf("%s-pgp-%s", hostname, enrollmentDate), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// preparePGPUnlockerDir checks GPG availability and creates the
|
// pgpUnlockerDir returns the current vault and the directory in it for a
|
||||||
// unlocker directory in the current vault, returning the vault and the
|
// new PGP unlocker, named after the host and the day.
|
||||||
// directory path.
|
|
||||||
//
|
//
|
||||||
//nolint:ireturn // the vault is only available behind VaultInterface
|
//nolint:ireturn // the vault is only available behind VaultInterface
|
||||||
func preparePGPUnlockerDir(
|
func pgpUnlockerDir(
|
||||||
fs afero.Fs, stateDir string,
|
fs afero.Fs, stateDir string,
|
||||||
) (VaultInterface, string, error) {
|
) (VaultInterface, string, error) {
|
||||||
// Check if GPG is available
|
|
||||||
err := checkGPGAvailable()
|
|
||||||
if err != nil {
|
|
||||||
return nil, "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get current vault
|
// Get current vault
|
||||||
vault, err := GetCurrentVault(fs, stateDir)
|
vault, err := GetCurrentVault(fs, stateDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -248,27 +241,32 @@ func preparePGPUnlockerDir(
|
|||||||
return nil, "", fmt.Errorf("failed to generate unlocker name: %w", err)
|
return nil, "", fmt.Errorf("failed to generate unlocker name: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create unlocker directory using the generated name
|
|
||||||
vaultDir, err := vault.GetDirectory()
|
vaultDir, err := vault.GetDirectory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", fmt.Errorf("failed to get vault directory: %w", err)
|
return nil, "", fmt.Errorf("failed to get vault directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
unlockerDir := filepath.Join(vaultDir, "unlockers.d", unlockerName)
|
return vault, filepath.Join(vaultDir, "unlockers.d", unlockerName), nil
|
||||||
|
|
||||||
err = fs.MkdirAll(unlockerDir, DirPerms)
|
|
||||||
if err != nil {
|
|
||||||
return nil, "", fmt.Errorf("failed to create unlocker directory: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return vault, unlockerDir, nil
|
// CreatePGPUnlocker creates a new PGP unlocker and stores it in the vault.
|
||||||
}
|
// Everything that can fail short of writing a file, resolving the GPG key's
|
||||||
|
// fingerprint included, is done before anything is written, and the files
|
||||||
// CreatePGPUnlocker creates a new PGP unlocker and stores it in the vault
|
// are written through WriteDir, so a failure leaves no partial unlocker.
|
||||||
func CreatePGPUnlocker(
|
func CreatePGPUnlocker(
|
||||||
fs afero.Fs, stateDir string, gpgKeyID string,
|
fs afero.Fs, stateDir string, gpgKeyID string,
|
||||||
) (*PGPUnlocker, error) {
|
) (*PGPUnlocker, error) {
|
||||||
vault, unlockerDir, err := preparePGPUnlockerDir(fs, stateDir)
|
err := checkGPGAvailable()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
fingerprint, err := ResolveGPGKeyFingerprint(gpgKeyID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to resolve GPG key fingerprint: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
vault, unlockerDir, err := pgpUnlockerDir(fs, stateDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -279,77 +277,13 @@ func CreatePGPUnlocker(
|
|||||||
return nil, fmt.Errorf("failed to generate age keypair: %w", err)
|
return nil, fmt.Errorf("failed to generate age keypair: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: Store age recipient as plaintext
|
// Step 2: Encrypt the long-term private key to the new keypair, and the
|
||||||
ageRecipient := ageIdentity.Recipient().String()
|
// keypair's private key to the GPG key
|
||||||
recipientPath := filepath.Join(unlockerDir, "pub.txt")
|
encryptedLtPrivKey, encryptedAgePrivKey, err := encryptPGPUnlockerKeys(
|
||||||
|
fs, vault, ageIdentity, gpgKeyID)
|
||||||
err = WriteFileAtomic(fs, recipientPath, []byte(ageRecipient))
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to write age recipient: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 3: Get or derive the long-term private key
|
|
||||||
ltPrivKeyData, err := getLongTermPrivateKey(fs, vault)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer ltPrivKeyData.Destroy()
|
|
||||||
|
|
||||||
// Step 7: Encrypt long-term private key to the new age unlocker
|
|
||||||
encryptedLtPrivKeyToAge, err := EncryptToRecipient(
|
|
||||||
ltPrivKeyData, ageIdentity.Recipient())
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"failed to encrypt long-term private key to age unlocker: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write encrypted long-term private key
|
|
||||||
ltPrivKeyPath := filepath.Join(unlockerDir, "longterm.age")
|
|
||||||
|
|
||||||
err = WriteFileAtomic(fs, ltPrivKeyPath, encryptedLtPrivKeyToAge)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to write encrypted long-term private key: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 8: Encrypt age private key to the GPG key ID
|
|
||||||
// Use memguard to protect the private key in memory
|
|
||||||
agePrivateKeyBuffer := memguard.NewBufferFromBytes([]byte(ageIdentity.String()))
|
|
||||||
defer agePrivateKeyBuffer.Destroy()
|
|
||||||
|
|
||||||
encryptedAgePrivKey, err := GPGEncryptFunc(agePrivateKeyBuffer, gpgKeyID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to encrypt age private key with GPG: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
agePrivKeyPath := filepath.Join(unlockerDir, "priv.age.gpg")
|
|
||||||
|
|
||||||
err = WriteFileAtomic(fs, agePrivKeyPath, encryptedAgePrivKey)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to write encrypted age private key: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Steps 9-10: Resolve the fingerprint and write enhanced metadata
|
|
||||||
pgpMetadata, err := writePGPUnlockerMetadata(fs, unlockerDir, gpgKeyID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &PGPUnlocker{
|
|
||||||
Directory: unlockerDir,
|
|
||||||
Metadata: pgpMetadata.UnlockerMetadata,
|
|
||||||
fs: fs,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// writePGPUnlockerMetadata resolves the GPG key fingerprint and writes
|
|
||||||
// the unlocker metadata file, returning the metadata written.
|
|
||||||
func writePGPUnlockerMetadata(
|
|
||||||
fs afero.Fs, unlockerDir string, gpgKeyID string,
|
|
||||||
) (*PGPUnlockerMetadata, error) {
|
|
||||||
fingerprint, err := ResolveGPGKeyFingerprint(gpgKeyID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to resolve GPG key fingerprint: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pgpMetadata := PGPUnlockerMetadata{
|
pgpMetadata := PGPUnlockerMetadata{
|
||||||
UnlockerMetadata: UnlockerMetadata{
|
UnlockerMetadata: UnlockerMetadata{
|
||||||
@@ -365,13 +299,85 @@ func writePGPUnlockerMetadata(
|
|||||||
return nil, fmt.Errorf("failed to marshal unlocker metadata: %w", err)
|
return nil, fmt.Errorf("failed to marshal unlocker metadata: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = WriteFileAtomic(fs,
|
// Step 3: Write the unlocker's files, the metadata last
|
||||||
filepath.Join(unlockerDir, "unlocker-metadata.json"), metadataBytes)
|
err = WriteDir(fs, unlockerDir, func(dir string) error {
|
||||||
|
return writePGPUnlockerFiles(fs, dir, ageIdentity.Recipient(),
|
||||||
|
encryptedLtPrivKey, encryptedAgePrivKey, metadataBytes)
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to write unlocker metadata: %w", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &pgpMetadata, nil
|
return &PGPUnlocker{
|
||||||
|
Directory: unlockerDir,
|
||||||
|
Metadata: pgpMetadata.UnlockerMetadata,
|
||||||
|
fs: fs,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// encryptPGPUnlockerKeys returns the vault's long-term private key encrypted
|
||||||
|
// to the new PGP unlocker's age keypair, and that keypair's private key
|
||||||
|
// encrypted to the GPG key gpgKeyID.
|
||||||
|
func encryptPGPUnlockerKeys(
|
||||||
|
fs afero.Fs, vault VaultInterface,
|
||||||
|
ageIdentity *age.X25519Identity, gpgKeyID string,
|
||||||
|
) ([]byte, []byte, error) {
|
||||||
|
// Get or derive the long-term private key
|
||||||
|
ltPrivKeyData, err := getLongTermPrivateKey(fs, vault)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
defer ltPrivKeyData.Destroy()
|
||||||
|
|
||||||
|
encryptedLtPrivKey, err := EncryptToRecipient(
|
||||||
|
ltPrivKeyData, ageIdentity.Recipient())
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf(
|
||||||
|
"failed to encrypt long-term private key to age unlocker: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use memguard to protect the private key in memory
|
||||||
|
agePrivateKeyBuffer := memguard.NewBufferFromBytes([]byte(ageIdentity.String()))
|
||||||
|
defer agePrivateKeyBuffer.Destroy()
|
||||||
|
|
||||||
|
encryptedAgePrivKey, err := GPGEncryptFunc(agePrivateKeyBuffer, gpgKeyID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf(
|
||||||
|
"failed to encrypt age private key with GPG: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return encryptedLtPrivKey, encryptedAgePrivKey, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// writePGPUnlockerFiles writes the files of a PGP unlocker into dir, the
|
||||||
|
// metadata last.
|
||||||
|
func writePGPUnlockerFiles(
|
||||||
|
fs afero.Fs, dir string, ageRecipient *age.X25519Recipient,
|
||||||
|
encryptedLtPrivKey, encryptedAgePrivKey, metadataBytes []byte,
|
||||||
|
) error {
|
||||||
|
err := WriteFileAtomic(fs, filepath.Join(dir, "pub.txt"),
|
||||||
|
[]byte(ageRecipient.String()))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write age recipient: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = WriteFileAtomic(fs, filepath.Join(dir, "longterm.age"), encryptedLtPrivKey)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write encrypted long-term private key: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = WriteFileAtomic(fs, filepath.Join(dir, "priv.age.gpg"), encryptedAgePrivKey)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write encrypted age private key: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = WriteFileAtomic(fs,
|
||||||
|
filepath.Join(dir, "unlocker-metadata.json"), metadataBytes)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write unlocker metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateGPGKeyID validates that a GPG key ID is safe for command execution
|
// validateGPGKeyID validates that a GPG key ID is safe for command execution
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package secret_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
|
"github.com/spf13/afero"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The one GPG key the gpg from installFakeGPG knows, its fingerprint, and a
|
||||||
|
// key ID it does not know.
|
||||||
|
const (
|
||||||
|
knownGPGKeyID = "0123456789ABCDEF"
|
||||||
|
knownGPGFingerprint = "0123456789ABCDEF0123456789ABCDEF01234567"
|
||||||
|
unknownGPGKeyID = "FEDCBA9876543210"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fakeGPGScript is a gpg for which `gpg --version` succeeds, listing
|
||||||
|
// knownGPGKeyID prints its fingerprint in gpg's colon format, and anything
|
||||||
|
// else fails.
|
||||||
|
const fakeGPGScript = `#!/bin/sh
|
||||||
|
case "$*" in
|
||||||
|
--version) ;;
|
||||||
|
*" ` + knownGPGKeyID + `") echo "fpr:::::::::` + knownGPGFingerprint + `:" ;;
|
||||||
|
*) exit 2 ;;
|
||||||
|
esac
|
||||||
|
`
|
||||||
|
|
||||||
|
// installFakeGPG makes fakeGPGScript the only gpg on PATH for the test.
|
||||||
|
func installFakeGPG(t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
dir := t.TempDir()
|
||||||
|
|
||||||
|
//nolint:gosec // G306: the script must be executable
|
||||||
|
err := os.WriteFile(filepath.Join(dir, "gpg"), []byte(fakeGPGScript), 0o700)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
t.Setenv("PATH", dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCreatePGPUnlockerFailureWritesNothing makes CreatePGPUnlocker fail at
|
||||||
|
// the two steps that used to come after part of the unlocker was written:
|
||||||
|
// resolving the GPG key's fingerprint, and getting the vault's long-term key
|
||||||
|
// (there is no mnemonic, and no current unlocker to get it from). Each must
|
||||||
|
// fail before anything is written, leaving no unlocker directory.
|
||||||
|
//
|
||||||
|
//nolint:paralleltest // t.Setenv forbids t.Parallel
|
||||||
|
func TestCreatePGPUnlockerFailureWritesNothing(t *testing.T) {
|
||||||
|
installFakeGPG(t)
|
||||||
|
t.Setenv(secret.EnvMnemonic, "")
|
||||||
|
|
||||||
|
// The fake gpg resolves the known key and no other
|
||||||
|
fingerprint, err := secret.ResolveGPGKeyFingerprint(knownGPGKeyID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, knownGPGFingerprint, fingerprint)
|
||||||
|
|
||||||
|
_, err = secret.ResolveGPGKeyFingerprint(unknownGPGKeyID)
|
||||||
|
require.Error(t, err)
|
||||||
|
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
gpgKeyID string
|
||||||
|
}{
|
||||||
|
{"fingerprint not found", unknownGPGKeyID},
|
||||||
|
{"long-term key not available", knownGPGKeyID},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
base := afero.NewMemMapFs()
|
||||||
|
vlt, err := vault.CreateVault(base, testVaultStateDir, testVaultName)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
fs := hookFs{Fs: base, before: func(_, path string) error {
|
||||||
|
t.Errorf("changed %s", path)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}}
|
||||||
|
|
||||||
|
_, err = secret.CreatePGPUnlocker(fs, testVaultStateDir, tc.gpgKeyID)
|
||||||
|
require.Error(t, err)
|
||||||
|
|
||||||
|
vaultDir, err := vlt.GetDirectory()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Empty(t, dirNames(t, base, filepath.Join(vaultDir, "unlockers.d")))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -254,7 +254,7 @@ func CreateSecureEnclaveUnlocker(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 4: Create unlocker directory and write files
|
// Step 4: Prepare the unlocker directory's path and metadata
|
||||||
vaultDir, err := vault.GetDirectory()
|
vaultDir, err := vault.GetDirectory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get vault directory: %w", err)
|
return nil, fmt.Errorf("failed to get vault directory: %w", err)
|
||||||
@@ -262,23 +262,7 @@ func CreateSecureEnclaveUnlocker(
|
|||||||
|
|
||||||
unlockerDirName := fmt.Sprintf("se-%s", filepath.Base(seKeyLabel))
|
unlockerDirName := fmt.Sprintf("se-%s", filepath.Base(seKeyLabel))
|
||||||
unlockerDir := filepath.Join(vaultDir, "unlockers.d", unlockerDirName)
|
unlockerDir := filepath.Join(vaultDir, "unlockers.d", unlockerDirName)
|
||||||
if err := fs.MkdirAll(unlockerDir, DirPerms); err != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"failed to create unlocker directory: %w",
|
|
||||||
err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write SE-encrypted long-term key
|
|
||||||
ltKeyPath := filepath.Join(unlockerDir, seLongtermFilename)
|
|
||||||
if err := WriteFileAtomic(fs, ltKeyPath, encryptedLtKey); err != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"failed to write SE-encrypted long-term key: %w",
|
|
||||||
err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write metadata
|
|
||||||
seMetadata := SecureEnclaveUnlockerMetadata{
|
seMetadata := SecureEnclaveUnlockerMetadata{
|
||||||
UnlockerMetadata: UnlockerMetadata{
|
UnlockerMetadata: UnlockerMetadata{
|
||||||
Type: seUnlockerType,
|
Type: seUnlockerType,
|
||||||
@@ -294,9 +278,25 @@ func CreateSecureEnclaveUnlocker(
|
|||||||
return nil, fmt.Errorf("failed to marshal metadata: %w", err)
|
return nil, fmt.Errorf("failed to marshal metadata: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
metadataPath := filepath.Join(unlockerDir, "unlocker-metadata.json")
|
// Step 5: Write the SE-encrypted long-term key, then the metadata
|
||||||
|
err = WriteDir(fs, unlockerDir, func(dir string) error {
|
||||||
|
ltKeyPath := filepath.Join(dir, seLongtermFilename)
|
||||||
|
if err := WriteFileAtomic(fs, ltKeyPath, encryptedLtKey); err != nil {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"failed to write SE-encrypted long-term key: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
metadataPath := filepath.Join(dir, "unlocker-metadata.json")
|
||||||
if err := WriteFileAtomic(fs, metadataPath, metadataBytes); err != nil {
|
if err := WriteFileAtomic(fs, metadataPath, metadataBytes); err != nil {
|
||||||
return nil, fmt.Errorf("failed to write metadata: %w", err)
|
return fmt.Errorf("failed to write metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &SecureEnclaveUnlocker{
|
return &SecureEnclaveUnlocker{
|
||||||
|
|||||||
+23
-28
@@ -350,26 +350,14 @@ func (v *Vault) CreatePassphraseUnlocker(
|
|||||||
return nil, fmt.Errorf("failed to get long-term key: %w", err)
|
return nil, fmt.Errorf("failed to get long-term key: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create unlocker directory
|
|
||||||
unlockerDir := filepath.Join(vaultDir, "unlockers.d", unlockerTypePassphrase)
|
unlockerDir := filepath.Join(vaultDir, "unlockers.d", unlockerTypePassphrase)
|
||||||
|
|
||||||
err = v.fs.MkdirAll(unlockerDir, secret.DirPerms)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to create unlocker directory: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate new age keypair for unlocker
|
// Generate new age keypair for unlocker
|
||||||
unlockerIdentity, err := age.GenerateX25519Identity()
|
unlockerIdentity, err := age.GenerateX25519Identity()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to generate unlocker: %w", err)
|
return nil, fmt.Errorf("failed to generate unlocker: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the unlocker keypair (public and passphrase-encrypted private)
|
|
||||||
err = v.writeUnlockerKeypair(unlockerDir, unlockerIdentity, passphrase)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encrypt long-term private key to this unlocker
|
// Encrypt long-term private key to this unlocker
|
||||||
ltPrivKeyBuffer := memguard.NewBufferFromBytes([]byte(ltIdentity.String()))
|
ltPrivKeyBuffer := memguard.NewBufferFromBytes([]byte(ltIdentity.String()))
|
||||||
defer ltPrivKeyBuffer.Destroy()
|
defer ltPrivKeyBuffer.Destroy()
|
||||||
@@ -380,15 +368,6 @@ func (v *Vault) CreatePassphraseUnlocker(
|
|||||||
return nil, fmt.Errorf("failed to encrypt long-term private key: %w", err)
|
return nil, fmt.Errorf("failed to encrypt long-term private key: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ltPrivKeyPath := filepath.Join(unlockerDir, "longterm.age")
|
|
||||||
|
|
||||||
err = secret.WriteFileAtomic(v.fs, ltPrivKeyPath, encryptedLtPrivKey)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to write encrypted long-term private key: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write the metadata last: readers skip an unlocker directory without
|
|
||||||
// it, so an unlocker interrupted before this point is never used.
|
|
||||||
metadata := UnlockerMetadata{
|
metadata := UnlockerMetadata{
|
||||||
Type: unlockerTypePassphrase,
|
Type: unlockerTypePassphrase,
|
||||||
CreatedAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
@@ -400,11 +379,13 @@ func (v *Vault) CreatePassphraseUnlocker(
|
|||||||
return nil, fmt.Errorf("failed to marshal metadata: %w", err)
|
return nil, fmt.Errorf("failed to marshal metadata: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
metadataPath := filepath.Join(unlockerDir, "unlocker-metadata.json")
|
// Write the unlocker's files, the metadata last
|
||||||
|
err = secret.WriteDir(v.fs, unlockerDir, func(dir string) error {
|
||||||
err = secret.WriteFileAtomic(v.fs, metadataPath, metadataBytes)
|
return v.writeUnlockerFiles(dir, unlockerIdentity, passphrase,
|
||||||
|
encryptedLtPrivKey, metadataBytes)
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to write unlocker metadata: %w", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the unlocker instance
|
// Create the unlocker instance
|
||||||
@@ -450,12 +431,14 @@ func (v *Vault) readUnlockerMetadata(unlockerDir string) (UnlockerMetadata, erro
|
|||||||
return metadata, nil
|
return metadata, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeUnlockerKeypair writes the unlocker's public key and its
|
// writeUnlockerFiles writes the files of a passphrase unlocker into
|
||||||
// passphrase-encrypted private key into the unlocker directory.
|
// unlockerDir: its public key, its passphrase-encrypted private key, the
|
||||||
func (v *Vault) writeUnlockerKeypair(
|
// long-term private key encrypted to it, and its metadata, last.
|
||||||
|
func (v *Vault) writeUnlockerFiles(
|
||||||
unlockerDir string,
|
unlockerDir string,
|
||||||
unlockerIdentity *age.X25519Identity,
|
unlockerIdentity *age.X25519Identity,
|
||||||
passphrase *memguard.LockedBuffer,
|
passphrase *memguard.LockedBuffer,
|
||||||
|
encryptedLtPrivKey, metadataBytes []byte,
|
||||||
) error {
|
) error {
|
||||||
// Write public key
|
// Write public key
|
||||||
pubKeyPath := filepath.Join(unlockerDir, "pub.age")
|
pubKeyPath := filepath.Join(unlockerDir, "pub.age")
|
||||||
@@ -485,5 +468,17 @@ func (v *Vault) writeUnlockerKeypair(
|
|||||||
return fmt.Errorf("failed to write encrypted unlocker private key: %w", err)
|
return fmt.Errorf("failed to write encrypted unlocker private key: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = secret.WriteFileAtomic(v.fs,
|
||||||
|
filepath.Join(unlockerDir, "longterm.age"), encryptedLtPrivKey)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write encrypted long-term private key: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = secret.WriteFileAtomic(v.fs,
|
||||||
|
filepath.Join(unlockerDir, "unlocker-metadata.json"), metadataBytes)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write unlocker metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -138,7 +138,12 @@ func (v *Vault) NumSecrets() (int, error) {
|
|||||||
|
|
||||||
secretsDir := filepath.Join(vaultDir, "secrets.d")
|
secretsDir := filepath.Join(vaultDir, "secrets.d")
|
||||||
|
|
||||||
exists, _ := afero.DirExists(v.fs, secretsDir)
|
exists, err := afero.DirExists(v.fs, secretsDir)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("failed to check secrets directory %s: %w",
|
||||||
|
secretsDir, err)
|
||||||
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
@@ -162,7 +167,7 @@ func (v *Vault) NumSecrets() (int, error) {
|
|||||||
|
|
||||||
exists, err := afero.Exists(v.fs, currentFile)
|
exists, err := afero.Exists(v.fs, currentFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue // Skip directories we can't read
|
return 0, fmt.Errorf("failed to check %s: %w", currentFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
|
|||||||
Executable
+29
@@ -0,0 +1,29 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# script/build: build the `secret` binary into the repo root, with its
|
||||||
|
# version and git commit stamped in (`secret info` shows both).
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
||||||
|
|
||||||
|
main() {
|
||||||
|
cd "$ROOT"
|
||||||
|
# CGO is required (Makefile exports this too)
|
||||||
|
export CGO_ENABLED=1
|
||||||
|
# A VERSION set in the environment wins (`make build VERSION=x`, as
|
||||||
|
# the Dockerfile does); otherwise `git describe` of this checkout.
|
||||||
|
version="${VERSION:-}"
|
||||||
|
if [ -z "$version" ]; then
|
||||||
|
version="$(git describe --tags --always --dirty 2>/dev/null ||
|
||||||
|
echo dev)"
|
||||||
|
fi
|
||||||
|
commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
|
||||||
|
pkg=git.eeqj.de/sneak/secret/internal/cli
|
||||||
|
# Build the file, not the package `./cmd/secret`: a package build
|
||||||
|
# also stamps git status into the binary and fails where git cannot
|
||||||
|
# read the checkout, instead of falling back to `dev`/`unknown`.
|
||||||
|
go build -v \
|
||||||
|
-ldflags "-X '$pkg.Version=$version' -X '$pkg.GitCommit=$commit'" \
|
||||||
|
-o secret cmd/secret/main.go
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@"
|
||||||
Reference in new issue
Block a user