Compare commits
4
Commits
4f2f2c1424
...
ee7002a2cc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ee7002a2cc | ||
|
|
f0bf3b24a6 | ||
|
|
67c2829f58 | ||
|
|
5922de32df |
No files matched your search
@@ -271,6 +271,12 @@ Creates a new unlocker of the specified type:
|
|||||||
A vault has one passphrase unlocker: adding one replaces the one the vault has,
|
A vault has one passphrase unlocker: adding one replaces the one the vault has,
|
||||||
which is removed only once the new one is the current unlocker.
|
which is removed only once the new one is the current unlocker.
|
||||||
|
|
||||||
|
`secret unlocker add passphrase` asks on the terminal for the new passphrase
|
||||||
|
even when `SB_UNLOCK_PASSPHRASE` is set, and uses the variable to unlock the
|
||||||
|
vault. With no controlling terminal, as under cron or in a CI job, the new
|
||||||
|
unlocker takes its passphrase from `SB_UNLOCK_PASSPHRASE`, and the command says
|
||||||
|
so.
|
||||||
|
|
||||||
#### `secret unlocker remove <unlocker-id> [--force]` / `secret unlocker rm` ⚠️ 🛑
|
#### `secret unlocker remove <unlocker-id> [--force]` / `secret unlocker rm` ⚠️ 🛑
|
||||||
|
|
||||||
**DANGER**: Permanently removes an unlocker. It first asks for confirmation,
|
**DANGER**: Permanently removes an unlocker. It first asks for confirmation,
|
||||||
@@ -554,7 +560,11 @@ secret decrypt encryption/mykey --input document.txt.age --output document.txt
|
|||||||
### Vault Management
|
### Vault Management
|
||||||
|
|
||||||
- **Derivation Index**: Each vault uses a unique derivation index from the
|
- **Derivation Index**: Each vault uses a unique derivation index from the
|
||||||
mnemonic, and thus a unique key pair
|
mnemonic, and thus a unique key pair; creating a vault (`secret init` and
|
||||||
|
`secret vault create`) fails, with an error naming the file, when any vault's
|
||||||
|
`vault-metadata.json` exists but cannot be read or parsed; a vault directory
|
||||||
|
with no `vault-metadata.json` is skipped, so the index it uses can be given to
|
||||||
|
a new vault
|
||||||
- **Public Key Hash**: Double SHA-256 hash of the vault's public key; the same
|
- **Public Key Hash**: Double SHA-256 hash of the vault's public key; the same
|
||||||
hash of the index-0 public key identifies vaults from the same mnemonic
|
hash of the index-0 public key identifies vaults from the same mnemonic
|
||||||
- **Automatic Key Derivation**: When creating vaults with a mnemonic, keys are
|
- **Automatic Key Derivation**: When creating vaults with a mnemonic, keys are
|
||||||
|
|||||||
@@ -144,25 +144,20 @@ func TestVaultCreationLeavesNoSecretInEnvironment(t *testing.T) {
|
|||||||
// TestNoVaultImportCommand is a regression test for
|
// TestNoVaultImportCommand is a regression test for
|
||||||
// https://git.eeqj.de/sneak/secret/issues/139: `secret vault import`
|
// https://git.eeqj.de/sneak/secret/issues/139: `secret vault import`
|
||||||
// refused every vault that `secret vault create` makes, because each has a
|
// refused every vault that `secret vault create` makes, because each has a
|
||||||
// long-term key. The command is gone, so cobra answers it with the help of
|
// long-term key. The command is gone, so it fails as any subcommand of
|
||||||
// `secret vault`, as it does any subcommand of `secret vault` it does not
|
// `secret vault` that does not exist does.
|
||||||
// know.
|
|
||||||
func TestNoVaultImportCommand(t *testing.T) {
|
func TestNoVaultImportCommand(t *testing.T) {
|
||||||
t.Setenv(secret.EnvStateDir, t.TempDir())
|
t.Setenv(secret.EnvStateDir, t.TempDir())
|
||||||
|
|
||||||
run := func(args ...string) string {
|
|
||||||
t.Setenv(secret.EnvMnemonic, testMnemonic)
|
t.Setenv(secret.EnvMnemonic, testMnemonic)
|
||||||
t.Setenv(secret.EnvUnlockPassphrase, testPassphrase)
|
t.Setenv(secret.EnvUnlockPassphrase, testPassphrase)
|
||||||
|
|
||||||
output, err := cli.ExecuteCommandInProcess(args, "", nil)
|
_, err := cli.ExecuteCommandInProcess(
|
||||||
require.NoError(t, err, "%v", args)
|
strings.Fields("vault create work"), "", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
return output
|
_, err = cli.ExecuteCommandInProcess(
|
||||||
}
|
strings.Fields("vault import work"), "", nil)
|
||||||
|
require.EqualError(t, err, `unknown command "import" for "secret vault"`)
|
||||||
run("vault", "create", "work")
|
|
||||||
|
|
||||||
require.Equal(t, run("vault"), run("vault", "import", "work"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestStopAtPassphrasePromptLeavesNothing is a regression test for the
|
// TestStopAtPassphrasePromptLeavesNothing is a regression test for the
|
||||||
|
|||||||
@@ -29,6 +29,12 @@ func newGenerateCmd() *cobra.Command {
|
|||||||
Use: "generate",
|
Use: "generate",
|
||||||
Short: "Generate random data",
|
Short: "Generate random data",
|
||||||
Long: `Generate various types of random data including mnemonics and secrets.`,
|
Long: `Generate various types of random data including mnemonics and secrets.`,
|
||||||
|
// Cobra checks Args only on a command that has a RunE; without
|
||||||
|
// one, an unknown subcommand prints this help and exits 0.
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
|
return cmd.Help()
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(newGenerateMnemonicCmd())
|
cmd.AddCommand(newGenerateMnemonicCmd())
|
||||||
|
|||||||
@@ -0,0 +1,85 @@
|
|||||||
|
//nolint:testpackage // white-box test of unexported internals
|
||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/awnumar/memguard"
|
||||||
|
"github.com/spf13/afero"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"sneak.berlin/go/secret/internal/vault"
|
||||||
|
)
|
||||||
|
|
||||||
|
// errRenameFailed is returned by renameFailFs in place of a successful
|
||||||
|
// rename.
|
||||||
|
var errRenameFailed = errors.New("permission denied")
|
||||||
|
|
||||||
|
// renameFailFs fails every Rename of one path, as a read-only parent
|
||||||
|
// directory does.
|
||||||
|
type renameFailFs struct {
|
||||||
|
afero.Fs
|
||||||
|
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *renameFailFs) Rename(oldname, newname string) error {
|
||||||
|
if oldname == f.path {
|
||||||
|
return errRenameFailed
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.Fs.Rename(oldname, newname)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCrossVaultMoveFailsWhenSourceRemains is a regression test for
|
||||||
|
// https://git.eeqj.de/sneak/secret/issues/159, where a move between two
|
||||||
|
// vaults whose source could not be removed reported the secret moved and
|
||||||
|
// succeeded. It must fail, and the copy and the source must both be there.
|
||||||
|
// The state directory lock refuses the wrapped filesystem, so the test
|
||||||
|
// calls moveSecret, which MoveSecret runs once it holds the lock.
|
||||||
|
func TestCrossVaultMoveFailsWhenSourceRemains(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
base := afero.NewMemMapFs()
|
||||||
|
|
||||||
|
_, err := vault.CreateVault(base, testStateDir, "work",
|
||||||
|
testMnemonicBuffer(t), nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
source, err := vault.CreateVault(base, testStateDir, "default",
|
||||||
|
testMnemonicBuffer(t), nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = source.AddSecret("x",
|
||||||
|
memguard.NewBufferFromBytes([]byte("value")), false)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
instance := NewCLIInstanceWithStateDir(&renameFailFs{
|
||||||
|
Fs: base,
|
||||||
|
path: testStateDir + "/vaults.d/default/secrets.d/x",
|
||||||
|
}, testStateDir)
|
||||||
|
instance.Mnemonic = testMnemonicBuffer(t)
|
||||||
|
|
||||||
|
err = instance.moveSecret(&cobra.Command{}, "default:x", "work:y", false)
|
||||||
|
require.ErrorIs(t, err, errRenameFailed)
|
||||||
|
require.ErrorContains(t, err, "secret 'default:x' is copied with all "+
|
||||||
|
"1 version(s) to 'work:y', but the source remains")
|
||||||
|
|
||||||
|
// requireValue requires that the vault vaultName holds secretName with
|
||||||
|
// the value stored above.
|
||||||
|
requireValue := func(vaultName, secretName string) {
|
||||||
|
vlt := vault.NewVault(base, testStateDir, vaultName)
|
||||||
|
vlt.Mnemonic = testMnemonicBuffer(t)
|
||||||
|
|
||||||
|
value, err := vlt.GetSecret(secretName)
|
||||||
|
require.NoError(t, err, vaultName+":"+secretName)
|
||||||
|
|
||||||
|
defer value.Destroy()
|
||||||
|
|
||||||
|
require.Equal(t, []byte("value"), value.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
requireValue("default", "x")
|
||||||
|
requireValue("work", "y")
|
||||||
|
}
|
||||||
@@ -138,6 +138,148 @@ func TestInitWritesNoBlankLineAfterMnemonic(t *testing.T) {
|
|||||||
"stderr: %q", stderr.String())
|
"stderr: %q", stderr.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestUnlockerAddAsksForNewPassphrase is a regression test for
|
||||||
|
// https://git.eeqj.de/sneak/secret/issues/154: with SB_UNLOCK_PASSPHRASE set,
|
||||||
|
// `secret unlocker add passphrase` gave the new unlocker the passphrase in
|
||||||
|
// the variable without asking. At a terminal it must ask for the new
|
||||||
|
// passphrase there, twice, and unlock the vault with the variable, so that
|
||||||
|
// `secret get` then opens the vault with the passphrase typed. pty.Open is
|
||||||
|
// described above TestRemoveIgnoresTerminalOnStdout.
|
||||||
|
func TestUnlockerAddAsksForNewPassphrase(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
const newPassphrase = "new-passphrase"
|
||||||
|
|
||||||
|
stateDir := t.TempDir()
|
||||||
|
|
||||||
|
passphrase := memguard.NewBufferFromBytes([]byte(testPassphrase))
|
||||||
|
defer passphrase.Destroy()
|
||||||
|
|
||||||
|
vlt, err := vault.CreateVault(afero.NewOsFs(), stateDir, "default",
|
||||||
|
testMnemonicBuffer(t), passphrase)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
value := memguard.NewBufferFromBytes([]byte("v1"))
|
||||||
|
defer value.Destroy()
|
||||||
|
|
||||||
|
require.NoError(t, vlt.AddSecret("s1", value, false))
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(t.Context(), commandWait)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
//nolint:gosec // G204: test executes the freshly built secret binary
|
||||||
|
cmd := exec.CommandContext(ctx, secretBinaryPath(t),
|
||||||
|
"unlocker", "add", "passphrase")
|
||||||
|
cmd.Env = []string{
|
||||||
|
secret.EnvStateDir + "=" + stateDir,
|
||||||
|
secret.EnvUnlockPassphrase + "=" + testPassphrase,
|
||||||
|
"PATH=" + os.Getenv("PATH"),
|
||||||
|
"HOME=" + os.Getenv("HOME"),
|
||||||
|
}
|
||||||
|
|
||||||
|
ptmx, tty, err := pty.Open()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
defer func() { _ = ptmx.Close() }()
|
||||||
|
|
||||||
|
deadline, _ := ctx.Deadline()
|
||||||
|
require.NoError(t, ptmx.SetReadDeadline(deadline))
|
||||||
|
|
||||||
|
cmd.Stderr = tty
|
||||||
|
// Ctty is a descriptor in the new process: 2, its stderr.
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true, Ctty: 2}
|
||||||
|
|
||||||
|
require.NoError(t, cmd.Start())
|
||||||
|
|
||||||
|
_ = tty.Close()
|
||||||
|
|
||||||
|
var (
|
||||||
|
shown []byte
|
||||||
|
char byte
|
||||||
|
)
|
||||||
|
|
||||||
|
terminal := bufio.NewReader(ptmx)
|
||||||
|
|
||||||
|
for _, prompt := range []string{
|
||||||
|
"Enter passphrase for unlocker: ", "Confirm passphrase: ",
|
||||||
|
} {
|
||||||
|
for !bytes.HasSuffix(shown, []byte(prompt)) {
|
||||||
|
char, err = terminal.ReadByte()
|
||||||
|
require.NoError(t, err,
|
||||||
|
"secret unlocker add passphrase did not ask on the terminal: %s",
|
||||||
|
shown)
|
||||||
|
|
||||||
|
shown = append(shown, char)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = ptmx.WriteString(newPassphrase + "\n")
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The read ends once the command has exited and so closed the terminal.
|
||||||
|
rest, _ := io.ReadAll(terminal)
|
||||||
|
|
||||||
|
require.NoError(t, cmd.Wait(),
|
||||||
|
"secret unlocker add passphrase: %s%s", shown, rest)
|
||||||
|
|
||||||
|
// The binary, not this test process, opens the vault: the new unlocker
|
||||||
|
// has age's full scrypt work factor, which is slow under the race
|
||||||
|
// detector.
|
||||||
|
assert.Equal(t, "v1",
|
||||||
|
runWithoutTerminal(t, stateDir, newPassphrase, "get", "s1"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestUnlockerAddWithoutTerminalSaysPassphraseIsVariable is a regression
|
||||||
|
// test for https://git.eeqj.de/sneak/secret/issues/154: with no terminal to
|
||||||
|
// ask on, `secret unlocker add passphrase` gives the new unlocker the
|
||||||
|
// passphrase in SB_UNLOCK_PASSPHRASE, and its output must say so.
|
||||||
|
func TestUnlockerAddWithoutTerminalSaysPassphraseIsVariable(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
stateDir := t.TempDir()
|
||||||
|
|
||||||
|
passphrase := memguard.NewBufferFromBytes([]byte(testPassphrase))
|
||||||
|
defer passphrase.Destroy()
|
||||||
|
|
||||||
|
_, err := vault.CreateVault(afero.NewOsFs(), stateDir, "default",
|
||||||
|
testMnemonicBuffer(t), passphrase)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
output := runWithoutTerminal(t, stateDir, testPassphrase,
|
||||||
|
"unlocker", "add", "passphrase")
|
||||||
|
|
||||||
|
assert.Contains(t, output, "The new unlocker takes its passphrase "+
|
||||||
|
"from SB_UNLOCK_PASSPHRASE, since there is no terminal to ask for a "+
|
||||||
|
"new one on\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// runWithoutTerminal runs the secret binary with args and SB_UNLOCK_PASSPHRASE
|
||||||
|
// set to passphrase, in a new session, which has no terminal, and returns
|
||||||
|
// what it printed once it has succeeded.
|
||||||
|
func runWithoutTerminal(
|
||||||
|
t *testing.T, stateDir, passphrase string, args ...string,
|
||||||
|
) string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(t.Context(), commandWait)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
//nolint:gosec // G204: test executes the freshly built secret binary
|
||||||
|
cmd := exec.CommandContext(ctx, secretBinaryPath(t), args...)
|
||||||
|
cmd.Env = []string{
|
||||||
|
secret.EnvStateDir + "=" + stateDir,
|
||||||
|
secret.EnvUnlockPassphrase + "=" + passphrase,
|
||||||
|
"PATH=" + os.Getenv("PATH"),
|
||||||
|
"HOME=" + os.Getenv("HOME"),
|
||||||
|
}
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
|
||||||
|
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
require.NoError(t, err, "secret %v: %s", args, output)
|
||||||
|
|
||||||
|
return string(output)
|
||||||
|
}
|
||||||
|
|
||||||
// runAtTerminal runs the secret binary with args and input on stdin, in a
|
// runAtTerminal runs the secret binary with args and input on stdin, in a
|
||||||
// new session whose controlling terminal is a new terminal, which is also
|
// new session whose controlling terminal is a new terminal, which is also
|
||||||
// its stderr: a command typed at a terminal with its input piped. pty.Open
|
// its stderr: a command typed at a terminal with its input piped. pty.Open
|
||||||
|
|||||||
@@ -1097,12 +1097,10 @@ func (cli *Instance) moveSecretCrossVault(
|
|||||||
// Delete source secret
|
// Delete source secret
|
||||||
err = secret.RemoveDirAtomic(cli.fs, srcSecretDir)
|
err = secret.RemoveDirAtomic(cli.fs, srcSecretDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Copy succeeded but delete failed - warn but don't fail
|
return fmt.Errorf("secret '%s:%s' is copied with all %d version(s) "+
|
||||||
cmd.Printf("Warning: copied secret but failed to remove source: %v\n", err)
|
"to '%s:%s', but the source remains: %w",
|
||||||
cmd.Printf("Moved secret '%s:%s' to '%s:%s' (%d version(s))\n",
|
srcVault.Name, srcSecretName, versionCount,
|
||||||
srcVault.Name, srcSecretName, destVault.Name, destSecretName, versionCount)
|
destVault.Name, destSecretName, err)
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Printf("Moved secret '%s:%s' to '%s:%s' (%d version(s))\n",
|
cmd.Printf("Moved secret '%s:%s' to '%s:%s' (%d version(s))\n",
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package cli_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"sneak.berlin/go/secret/internal/secret"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestUnknownSubcommandFails is a regression test for
|
||||||
|
// https://git.eeqj.de/sneak/secret/issues/183: given a subcommand it does not
|
||||||
|
// have, `secret vault`, `secret unlocker`, `secret version` and `secret
|
||||||
|
// generate` printed their help and exited 0, so a script could not tell a
|
||||||
|
// mistyped command from a successful one. Each runs here as the built binary
|
||||||
|
// and must exit 1 naming the subcommand, as an unknown command does at the
|
||||||
|
// top level, and must still print its help and exit 0 when given nothing.
|
||||||
|
func TestUnknownSubcommandFails(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
stateDir := t.TempDir()
|
||||||
|
|
||||||
|
// run runs the built binary with args and returns what it printed and
|
||||||
|
// its exit code
|
||||||
|
run := func(args ...string) (string, int) {
|
||||||
|
//nolint:gosec // G204: test executes the freshly built secret binary
|
||||||
|
cmd := exec.CommandContext(t.Context(), secretBinaryPath(t), args...)
|
||||||
|
cmd.Env = []string{secret.EnvStateDir + "=" + stateDir}
|
||||||
|
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
var exitErr *exec.ExitError
|
||||||
|
require.ErrorAs(t, err, &exitErr, "secret %v did not run", args)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(output), cmd.ProcessState.ExitCode()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, call := range []string{
|
||||||
|
"vault improt", "unlocker foo", "version foo", "generate foo",
|
||||||
|
} {
|
||||||
|
parent, unknown, _ := strings.Cut(call, " ")
|
||||||
|
|
||||||
|
output, exitCode := run(parent, unknown)
|
||||||
|
assert.Equal(t, 1, exitCode, "secret %s should fail:\n%s", call, output)
|
||||||
|
assert.Contains(t, output, fmt.Sprintf(
|
||||||
|
"unknown command %q for \"secret %s\"", unknown, parent))
|
||||||
|
|
||||||
|
output, exitCode = run(parent)
|
||||||
|
assert.Equal(t, 0, exitCode,
|
||||||
|
"secret %s should succeed:\n%s", parent, output)
|
||||||
|
assert.Contains(t, output, usageHeading,
|
||||||
|
"secret %s should print its help", parent)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -126,6 +126,12 @@ func newUnlockerCmd() *cobra.Command {
|
|||||||
Use: "unlocker",
|
Use: "unlocker",
|
||||||
Short: "Manage unlockers",
|
Short: "Manage unlockers",
|
||||||
Long: `Create, list, and remove unlockers for the current vault.`,
|
Long: `Create, list, and remove unlockers for the current vault.`,
|
||||||
|
// Cobra checks Args only on a command that has a RunE; without
|
||||||
|
// one, an unknown subcommand prints this help and exits 0.
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
|
return cmd.Help()
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(newUnlockerListCmd())
|
cmd.AddCommand(newUnlockerListCmd())
|
||||||
@@ -441,15 +447,20 @@ func (cli *Instance) addPassphraseUnlocker(cmd *cobra.Command) error {
|
|||||||
// long-term key
|
// long-term key
|
||||||
vlt.Mnemonic, vlt.UnlockPassphrase = cli.Mnemonic, cli.UnlockPassphrase
|
vlt.Mnemonic, vlt.UnlockPassphrase = cli.Mnemonic, cli.UnlockPassphrase
|
||||||
|
|
||||||
// The new unlocker gets the passphrase from the environment, which also
|
// The new passphrase is asked for even when SB_UNLOCK_PASSPHRASE is
|
||||||
// unlocks the current passphrase unlocker, else the one entered here
|
// set, and the variable unlocks the vault. With no terminal to ask on,
|
||||||
passphraseBuffer := cli.UnlockPassphrase
|
// the new unlocker takes the passphrase in the variable.
|
||||||
if passphraseBuffer == nil {
|
fromVariable := false
|
||||||
// Use secure passphrase input with confirmation
|
|
||||||
passphraseBuffer, err = readSecurePassphrase("Enter passphrase for unlocker: ")
|
passphraseBuffer, err := readSecurePassphrase("Enter passphrase for unlocker: ")
|
||||||
if err != nil {
|
|
||||||
|
switch {
|
||||||
|
case errors.Is(err, secret.ErrNoTerminal) && cli.UnlockPassphrase != nil:
|
||||||
|
passphraseBuffer = cli.UnlockPassphrase
|
||||||
|
fromVariable = true
|
||||||
|
case err != nil:
|
||||||
return err
|
return err
|
||||||
}
|
default:
|
||||||
defer passphraseBuffer.Destroy()
|
defer passphraseBuffer.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -460,6 +471,12 @@ func (cli *Instance) addPassphraseUnlocker(cmd *cobra.Command) error {
|
|||||||
|
|
||||||
cmd.Printf("Created passphrase unlocker: %s\n", passphraseUnlocker.GetID())
|
cmd.Printf("Created passphrase unlocker: %s\n", passphraseUnlocker.GetID())
|
||||||
|
|
||||||
|
if fromVariable {
|
||||||
|
cmd.Printf("The new unlocker takes its passphrase from %s, since "+
|
||||||
|
"there is no terminal to ask for a new one on\n",
|
||||||
|
secret.EnvUnlockPassphrase)
|
||||||
|
}
|
||||||
|
|
||||||
// CreatePassphraseUnlocker has already made it the current unlocker
|
// CreatePassphraseUnlocker has already made it the current unlocker
|
||||||
cmd.Printf("Automatically selected as current unlocker\n")
|
cmd.Printf("Automatically selected as current unlocker\n")
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,12 @@ func newVaultCmd() *cobra.Command {
|
|||||||
Use: "vault",
|
Use: "vault",
|
||||||
Short: "Manage vaults",
|
Short: "Manage vaults",
|
||||||
Long: `Create, list, and select vaults for organizing secrets.`,
|
Long: `Create, list, and select vaults for organizing secrets.`,
|
||||||
|
// Cobra checks Args only on a command that has a RunE; without
|
||||||
|
// one, an unknown subcommand prints this help and exits 0.
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
|
return cmd.Help()
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(newVaultListCmd())
|
cmd.AddCommand(newVaultListCmd())
|
||||||
|
|||||||
@@ -43,6 +43,12 @@ func VersionCommands(cli *Instance) *cobra.Command {
|
|||||||
Short: "Manage secret versions",
|
Short: "Manage secret versions",
|
||||||
Long: "Commands for managing secret versions including listing, " +
|
Long: "Commands for managing secret versions including listing, " +
|
||||||
"promoting, and retrieving specific versions",
|
"promoting, and retrieving specific versions",
|
||||||
|
// Cobra checks Args only on a command that has a RunE; without
|
||||||
|
// one, an unknown subcommand prints this help and exits 0.
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
|
return cmd.Help()
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// List versions command
|
// List versions command
|
||||||
|
|||||||
@@ -15,10 +15,13 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
errNilPassphraseBuffer = errors.New("passphrase buffer is nil")
|
errNilPassphraseBuffer = errors.New("passphrase buffer is nil")
|
||||||
errNoTerminal = errors.New("there is no terminal to ask on")
|
|
||||||
errNothingEntered = errors.New("nothing was entered")
|
errNothingEntered = errors.New("nothing was entered")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrNoTerminal is wrapped in the error of ReadPassphrase and ReadMnemonic
|
||||||
|
// when the process has no controlling terminal to ask on.
|
||||||
|
var ErrNoTerminal = errors.New("there is no terminal to ask on")
|
||||||
|
|
||||||
// ErrMnemonicNotRead is wrapped in every error of ReadMnemonic: there is no
|
// ErrMnemonicNotRead is wrapped in every error of ReadMnemonic: there is no
|
||||||
// terminal to read the mnemonic from, reading it failed, or it was empty.
|
// terminal to read the mnemonic from, reading it failed, or it was empty.
|
||||||
var ErrMnemonicNotRead = errors.New("failed to read mnemonic")
|
var ErrMnemonicNotRead = errors.New("failed to read mnemonic")
|
||||||
@@ -203,7 +206,7 @@ func readFromTerminal(
|
|||||||
|
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
"%w: %w. Please set the %s environment variable or run interactively",
|
"%w: %w. Please set the %s environment variable or run interactively",
|
||||||
notRead, errNoTerminal, envVar)
|
notRead, ErrNoTerminal, envVar)
|
||||||
}
|
}
|
||||||
defer func() { _ = terminal.Close() }()
|
defer func() { _ = terminal.Close() }()
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
@@ -33,7 +35,10 @@ func ComputeDoubleSHA256(data []byte) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetNextDerivationIndex finds the next available derivation index for a given mnemonic
|
// GetNextDerivationIndex finds the next available derivation index for a given mnemonic
|
||||||
// by deriving the public key for index 0 and using its hash to identify related vaults
|
// by deriving the public key for index 0 and using its hash to identify related vaults.
|
||||||
|
// It fails when a vault's metadata file exists but cannot be read or parsed: the
|
||||||
|
// index that vault uses is then unknown, and handing it out again would give a
|
||||||
|
// second vault the same long-term key.
|
||||||
func GetNextDerivationIndex(
|
func GetNextDerivationIndex(
|
||||||
fs afero.Fs, stateDir string, mnemonic string,
|
fs afero.Fs, stateDir string, mnemonic string,
|
||||||
) (uint32, error) {
|
) (uint32, error) {
|
||||||
@@ -72,21 +77,23 @@ func GetNextDerivationIndex(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to read vault metadata
|
|
||||||
metadataPath := filepath.Join(vaultsDir, entry.Name(), "vault-metadata.json")
|
metadataPath := filepath.Join(vaultsDir, entry.Name(), "vault-metadata.json")
|
||||||
|
|
||||||
metadataBytes, err := afero.ReadFile(fs, metadataPath)
|
metadataBytes, err := afero.ReadFile(fs, metadataPath)
|
||||||
if err != nil {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
// Skip vaults without metadata
|
// Skip directories without metadata
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("failed to read vault metadata %s: %w", metadataPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
var metadata Metadata
|
var metadata Metadata
|
||||||
|
|
||||||
err = json.Unmarshal(metadataBytes, &metadata)
|
err = json.Unmarshal(metadataBytes, &metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Skip vaults with invalid metadata
|
return 0, fmt.Errorf("failed to parse vault metadata %s: %w", metadataPath, err)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if this vault uses the same mnemonic by comparing family hashes
|
// Check if this vault uses the same mnemonic by comparing family hashes
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
package vault_test
|
package vault_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"sneak.berlin/go/secret/internal/secret"
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
"sneak.berlin/go/secret/internal/vault"
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
"sneak.berlin/go/secret/pkg/agehd"
|
||||||
)
|
)
|
||||||
@@ -163,6 +167,88 @@ func testGetNextDerivationIndex(t *testing.T, fs afero.Fs) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// errMetadataUnreadable is returned by the test filesystem in place of a
|
||||||
|
// successful open of one vault's metadata file.
|
||||||
|
var errMetadataUnreadable = errors.New("input/output error")
|
||||||
|
|
||||||
|
// metadataReadFailFs fails every open of the file at unreadablePath. The
|
||||||
|
// file still exists, so checking for it succeeds and only reading it fails.
|
||||||
|
type metadataReadFailFs struct {
|
||||||
|
afero.Fs
|
||||||
|
|
||||||
|
unreadablePath string
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint:ireturn // afero.File is the interface required by afero.Fs
|
||||||
|
func (f *metadataReadFailFs) Open(name string) (afero.File, error) {
|
||||||
|
if name == f.unreadablePath {
|
||||||
|
return nil, errMetadataUnreadable
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.Fs.Open(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCreateVaultWithCorruptMetadataFails checks that creating a vault from
|
||||||
|
// a mnemonic fails, naming the file and leaving no new vault, when another
|
||||||
|
// vault's metadata file exists but cannot be parsed or read. That vault's
|
||||||
|
// derivation index is then unknown, so the new vault could get the same one
|
||||||
|
// and the same key.
|
||||||
|
func TestCreateVaultWithCorruptMetadataFails(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
vaultsDir := filepath.Join(testStateDir, "vaults.d")
|
||||||
|
metadataPath := filepath.Join(vaultsDir, "work", "vault-metadata.json")
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
// breakMetadata makes the work vault's metadata file unusable on
|
||||||
|
// base and returns the filesystem to create the second vault on.
|
||||||
|
breakMetadata func(t *testing.T, base afero.Fs) afero.Fs
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "parsing the file fails",
|
||||||
|
breakMetadata: func(t *testing.T, base afero.Fs) afero.Fs {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
err := afero.WriteFile(base, metadataPath, []byte("{"),
|
||||||
|
secret.FilePerms)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
return base
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "reading the file fails",
|
||||||
|
breakMetadata: func(_ *testing.T, base afero.Fs) afero.Fs {
|
||||||
|
return &metadataReadFailFs{Fs: base, unreadablePath: metadataPath}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
base := afero.NewMemMapFs()
|
||||||
|
|
||||||
|
_, err := vault.CreateVault(base, testStateDir, "work",
|
||||||
|
testMnemonicBuffer(t), nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
fs := tt.breakMetadata(t, base)
|
||||||
|
|
||||||
|
_, err = vault.CreateVault(fs, testStateDir, "other",
|
||||||
|
testMnemonicBuffer(t), nil)
|
||||||
|
require.ErrorContains(t, err, metadataPath)
|
||||||
|
|
||||||
|
entries, err := afero.ReadDir(base, vaultsDir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, entries, 1)
|
||||||
|
assert.Equal(t, "work", entries[0].Name())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func testMetadataPersistence(t *testing.T, fs afero.Fs) {
|
func testMetadataPersistence(t *testing.T, fs afero.Fs) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user