Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
64d0c0763d |
No files matched your search
@@ -440,11 +440,10 @@ in a CI job, the command fails and names the variable to set.
|
|||||||
|
|
||||||
A command that changes anything locks the state directory, through its file
|
A command that changes anything locks the state directory, through its file
|
||||||
`lock`, and another such command waits until the lock is released.
|
`lock`, and another such command waits until the lock is released.
|
||||||
`secret init`, `secret vault create`, `secret unlocker add`, `secret mv` from
|
`secret init`, `secret vault create`, `secret unlocker add`, and `secret add`,
|
||||||
one vault to another, and `secret add`, `secret import` and
|
`secret import` and `secret generate secret` with `--force`, ask for the
|
||||||
`secret generate secret` with `--force`, ask for the mnemonic or passphrase
|
mnemonic or passphrase before they take the lock, so a prompt of theirs left
|
||||||
before they take the lock, so a prompt of theirs left open in one terminal holds
|
open in one terminal holds up no command in another.
|
||||||
up no command in another.
|
|
||||||
|
|
||||||
## Security Features
|
## Security Features
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -229,55 +230,68 @@ func TestCrossVaultMoveUnlocksBeforeLocking(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestCrossVaultMoveChecksAgainOnceLocked runs a move between vaults, given
|
// TestCrossVaultMoveChecksAgainOnceLocked starts a move between two vaults,
|
||||||
// the mnemonic, as TestCommandsCheckAgainOnceLocked runs the commands in its
|
// given the mnemonic, while the test holds the state directory lock. Once
|
||||||
// table: once the move waits for the state directory lock, the test removes
|
// the move waits for the lock, the test removes what another command could
|
||||||
// the source secret or the destination vault, and the move must then fail
|
// have removed in the meantime, then releases the lock. The move must fail
|
||||||
// and change nothing.
|
// with the error that says what is missing.
|
||||||
//
|
//
|
||||||
//nolint:paralleltest // waitingForLock sees any test's command waiting for the lock
|
//nolint:paralleltest // waitingForLock sees any test's command waiting for the lock
|
||||||
func TestCrossVaultMoveChecksAgainOnceLocked(t *testing.T) {
|
func TestCrossVaultMoveChecksAgainOnceLocked(t *testing.T) {
|
||||||
mnemonic := testMnemonicBuffer(t)
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
// removed is the directory, under the state directory, that the
|
||||||
|
// test removes
|
||||||
|
removed string
|
||||||
|
want error
|
||||||
|
}{
|
||||||
|
{"source", "/vaults.d/default/secrets.d/x", vault.ErrSecretNotFound},
|
||||||
|
{"destination vault", "/vaults.d/work", vault.ErrVaultNotFound},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
createCrossVaults(t, fs)
|
||||||
|
|
||||||
move := func(cli *Instance) error {
|
release, err := vault.LockStateDir(fs, testStateDir)
|
||||||
cli.Mnemonic = mnemonic
|
require.NoError(t, err)
|
||||||
|
|
||||||
return cli.MoveSecret(cli.cmd, "work:test/secret", "other:moved", false)
|
// Released at most once, and also if the test fails while
|
||||||
|
// holding it, so that later tests can take it
|
||||||
|
release = sync.OnceFunc(release)
|
||||||
|
defer release()
|
||||||
|
|
||||||
|
instance := NewCLIInstanceWithStateDir(fs, testStateDir)
|
||||||
|
instance.Mnemonic = testMnemonicBuffer(t)
|
||||||
|
|
||||||
|
done := make(chan error, 1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
done <- instance.MoveSecret(&cobra.Command{},
|
||||||
|
"default:x", "work:y", false)
|
||||||
|
}()
|
||||||
|
|
||||||
|
timeout := time.After(lockWait)
|
||||||
|
|
||||||
|
for !waitingForLock() {
|
||||||
|
select {
|
||||||
|
case err := <-done:
|
||||||
|
t.Fatalf("finished while the lock was held, with error %v",
|
||||||
|
err)
|
||||||
|
case <-timeout:
|
||||||
|
t.Fatal("never waited for the lock")
|
||||||
|
case <-time.After(time.Millisecond):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NoError(t, fs.RemoveAll(testStateDir+tc.removed))
|
||||||
|
release()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-done:
|
||||||
|
require.ErrorIs(t, err, tc.want)
|
||||||
|
case <-time.After(lockWait):
|
||||||
|
t.Fatal("did not finish once the lock was released")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("source removed", func(t *testing.T) {
|
|
||||||
requireChecksAgainOnceLocked(t, move, func(fs afero.Fs) error {
|
|
||||||
return fs.RemoveAll(testStateDir + "/vaults.d/work/secrets.d/test%secret")
|
|
||||||
}, vault.ErrSecretNotFound)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("destination vault removed", func(t *testing.T) {
|
|
||||||
requireChecksAgainOnceLocked(t, move, func(fs afero.Fs) error {
|
|
||||||
return fs.RemoveAll(testStateDir + "/vaults.d/other")
|
|
||||||
}, vault.ErrVaultNotFound)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestCrossVaultMoveRefusesExistingSecretBeforeUnlocking checks that a move
|
|
||||||
// between vaults onto a secret that exists, without --force, is refused
|
|
||||||
// before either vault is unlocked, since unlocking can ask for a
|
|
||||||
// passphrase. The vaults have no unlockers and the move is given no
|
|
||||||
// mnemonic, so unlocking either vault fails.
|
|
||||||
func TestCrossVaultMoveRefusesExistingSecretBeforeUnlocking(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
createCrossVaults(t, fs)
|
|
||||||
|
|
||||||
work := vault.NewVault(fs, testStateDir, "work")
|
|
||||||
work.Mnemonic = testMnemonicBuffer(t)
|
|
||||||
|
|
||||||
err := work.AddSecret("y",
|
|
||||||
memguard.NewBufferFromBytes([]byte("other")), false)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
instance := NewCLIInstanceWithStateDir(fs, testStateDir)
|
|
||||||
|
|
||||||
err = instance.MoveSecret(&cobra.Command{}, "default:x", "work:y", false)
|
|
||||||
require.ErrorIs(t, err, vault.ErrSecretExists)
|
|
||||||
}
|
}
|
||||||
+6
-21
@@ -1095,11 +1095,10 @@ func (cli *Instance) moveSecretCrossVault(
|
|||||||
force bool,
|
force bool,
|
||||||
) error {
|
) error {
|
||||||
// Before unlocking, check that the source secret and the destination
|
// Before unlocking, check that the source secret and the destination
|
||||||
// vault exist, that the destination is not the source under another
|
// vault exist, and that the destination is not the source under another
|
||||||
// name, and, without --force, that the destination secret does not
|
// name
|
||||||
// exist
|
|
||||||
_, err := cli.checkCrossVaultMove(
|
_, err := cli.checkCrossVaultMove(
|
||||||
srcVault, srcSecretName, destVault, destSecretName, force)
|
srcVault, srcSecretName, destVault, destSecretName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1141,7 +1140,7 @@ func (cli *Instance) copySecretAndRemoveSource(
|
|||||||
force bool,
|
force bool,
|
||||||
) error {
|
) error {
|
||||||
srcSecretDir, err := cli.checkCrossVaultMove(
|
srcSecretDir, err := cli.checkCrossVaultMove(
|
||||||
srcVault, srcSecretName, destVault, destSecretName, force)
|
srcVault, srcSecretName, destVault, destSecretName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1172,14 +1171,11 @@ func (cli *Instance) copySecretAndRemoveSource(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// checkCrossVaultMove checks that the secret srcSecretName exists in
|
// checkCrossVaultMove checks that the secret srcSecretName exists in
|
||||||
// srcVault, that destVault exists, that destSecretName in destVault is not
|
// srcVault, that destVault exists, and that destSecretName in destVault is
|
||||||
// the source under another name, and, unless force is set, that
|
// not the source under another name. It returns the source's directory.
|
||||||
// destSecretName does not exist in destVault. It returns the source's
|
|
||||||
// directory.
|
|
||||||
func (cli *Instance) checkCrossVaultMove(
|
func (cli *Instance) checkCrossVaultMove(
|
||||||
srcVault *vault.Vault, srcSecretName string,
|
srcVault *vault.Vault, srcSecretName string,
|
||||||
destVault *vault.Vault, destSecretName string,
|
destVault *vault.Vault, destSecretName string,
|
||||||
force bool,
|
|
||||||
) (string, error) {
|
) (string, error) {
|
||||||
srcVaultDir, err := srcVault.GetDirectory()
|
srcVaultDir, err := srcVault.GetDirectory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1222,17 +1218,6 @@ func (cli *Instance) checkCrossVaultMove(
|
|||||||
destVault.Name, destSecretName)
|
destVault.Name, destSecretName)
|
||||||
}
|
}
|
||||||
|
|
||||||
exists, err = afero.DirExists(cli.fs, destSecretDir)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to check if destination secret exists: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if exists && !force {
|
|
||||||
return "", fmt.Errorf(
|
|
||||||
"secret '%s' %w in vault '%s' (use --force to overwrite)",
|
|
||||||
destSecretName, vault.ErrSecretExists, destVault.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
return srcSecretDir, nil
|
return srcSecretDir, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user