Compare commits
2
Commits
31dd23b4b6
...
816e0fd0ee
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
816e0fd0ee | ||
|
|
acb4b9cb6a |
No files matched your search
@@ -114,7 +114,10 @@ are allowed, and a name must not be empty, `.` or `..`.
|
|||||||
|
|
||||||
#### `secret vault select <name>`
|
#### `secret vault select <name>`
|
||||||
|
|
||||||
Switches to the specified vault for subsequent operations.
|
Switches to the specified vault for subsequent operations. When `currentvault`
|
||||||
|
is empty or names a vault that does not exist, every command that uses the
|
||||||
|
current vault fails with an error that names the file and says to run
|
||||||
|
`secret vault select <name>`; `secret vault list` still lists the vaults.
|
||||||
|
|
||||||
#### `secret vault remove <name> [--force]` / `secret vault rm` ⚠️ 🛑
|
#### `secret vault remove <name> [--force]` / `secret vault rm` ⚠️ 🛑
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package cli_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/afero"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"sneak.berlin/go/secret/internal/cli"
|
||||||
|
"sneak.berlin/go/secret/internal/secret"
|
||||||
|
"sneak.berlin/go/secret/internal/vault"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestListFailsWhenCurrentVaultDoesNotExist is a regression test for
|
||||||
|
// https://git.eeqj.de/sneak/secret/issues/156, where a currentvault file
|
||||||
|
// that was empty, or named a vault that does not exist, made `secret list`
|
||||||
|
// report an empty vault. Listing must fail with an error that names the
|
||||||
|
// file and says to run `secret vault select`, `secret vault list` must
|
||||||
|
// still list the vaults, and selecting a vault must make listing work
|
||||||
|
// again.
|
||||||
|
func TestListFailsWhenCurrentVaultDoesNotExist(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
for _, name := range []string{"", "gone"} {
|
||||||
|
t.Run(strconv.Quote(name), func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
fs := newTwoVaultFs(t)
|
||||||
|
currentVaultPath := filepath.Join(testStateDir, "currentvault")
|
||||||
|
require.NoError(t, afero.WriteFile(fs, currentVaultPath,
|
||||||
|
[]byte(name), secret.FilePerms))
|
||||||
|
|
||||||
|
c := cli.NewCLIInstanceWithStateDir(fs, testStateDir)
|
||||||
|
|
||||||
|
var out bytes.Buffer
|
||||||
|
|
||||||
|
cmd := &cobra.Command{}
|
||||||
|
cmd.SetOut(&out)
|
||||||
|
|
||||||
|
err := c.ListSecrets(cmd, false, true, "")
|
||||||
|
require.ErrorIs(t, err, vault.ErrVaultNotFound)
|
||||||
|
require.ErrorContains(t, err, currentVaultPath)
|
||||||
|
require.ErrorContains(t, err, "secret vault select")
|
||||||
|
assert.Empty(t, out.String())
|
||||||
|
|
||||||
|
require.NoError(t, c.ListVaults(cmd, false))
|
||||||
|
assert.Equal(t, "Available vaults:\n default\n work\n", out.String())
|
||||||
|
|
||||||
|
require.NoError(t, c.SelectVault(cmd, "default"))
|
||||||
|
out.Reset()
|
||||||
|
require.NoError(t, c.ListSecrets(cmd, false, true, ""))
|
||||||
|
assert.Equal(t, "x\n", out.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -30,17 +29,11 @@ func newSizeTestVault(t *testing.T) (afero.Fs, *vault.Vault) {
|
|||||||
|
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
// Create vault
|
// Create vault, which selects it as the current vault
|
||||||
_, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
_, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
||||||
testMnemonicBuffer(t), nil)
|
testMnemonicBuffer(t), nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Set current vault
|
|
||||||
currentVaultPath := filepath.Join(testStateDir, "currentvault")
|
|
||||||
vaultPath := filepath.Join(testStateDir, "vaults.d", testVaultName)
|
|
||||||
err = afero.WriteFile(fs, currentVaultPath, []byte(vaultPath), 0o600)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Get vault and set up long-term key
|
// Get vault and set up long-term key
|
||||||
vlt, err := vault.GetCurrentVault(fs, testStateDir)
|
vlt, err := vault.GetCurrentVault(fs, testStateDir)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func VersionCommands(cli *Instance) *cobra.Command {
|
|||||||
promoteCmd := &cobra.Command{
|
promoteCmd := &cobra.Command{
|
||||||
Use: "promote <secret-name> <version>",
|
Use: "promote <secret-name> <version>",
|
||||||
Short: "Promote a specific version to current",
|
Short: "Promote a specific version to current",
|
||||||
Long: "Updates the current symlink to point to the specified " +
|
Long: "Rewrites the secret's current file to name the specified " +
|
||||||
"version. The versions' own times are left unchanged; secret " +
|
"version. The versions' own times are left unchanged; secret " +
|
||||||
"list then gives the time of the promote as the secret's last " +
|
"list then gives the time of the promote as the secret's last " +
|
||||||
"update.",
|
"update.",
|
||||||
@@ -293,7 +293,7 @@ func (cli *Instance) PromoteVersion(
|
|||||||
version, vault.ErrVersionNotFound, secretName)
|
version, vault.ErrVersionNotFound, secretName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the current symlink using the proper function
|
// Rewrite the secret's current file to name the version
|
||||||
err = secret.SetCurrentVersion(cli.fs, secretDir, version)
|
err = secret.SetCurrentVersion(cli.fs, secretDir, version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to update current version: %w", err)
|
return fmt.Errorf("failed to update current version: %w", err)
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
// - TestListVersionsNonExistentSecret: Tests error handling for missing secrets
|
// - TestListVersionsNonExistentSecret: Tests error handling for missing secrets
|
||||||
// - TestPromoteVersionCommand: Tests `secret version promote` command
|
// - TestPromoteVersionCommand: Tests `secret version promote` command
|
||||||
// - TestPromoteNonExistentVersion: Tests error handling for invalid promotion
|
// - TestPromoteNonExistentVersion: Tests error handling for invalid promotion
|
||||||
|
// - TestPromoteHelpSaysCurrentFileIsRewritten: Tests `secret version promote
|
||||||
|
// --help` text
|
||||||
// - TestGetSecretWithVersion: Tests `secret get --version` flag functionality
|
// - TestGetSecretWithVersion: Tests `secret get --version` flag functionality
|
||||||
// - TestGetSecretWritesBinaryValue: Tests `secret get` output of binary values
|
// - TestGetSecretWritesBinaryValue: Tests `secret get` output of binary values
|
||||||
// - TestGetSecretReturnsWriteError: Tests `secret get` when stdout fails
|
// - TestGetSecretReturnsWriteError: Tests `secret get` when stdout fails
|
||||||
@@ -270,6 +272,23 @@ func TestPromoteNonExistentVersion(t *testing.T) {
|
|||||||
require.ErrorIs(t, err, vault.ErrVersionNotFound)
|
require.ErrorIs(t, err, vault.ErrVersionNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestPromoteHelpSaysCurrentFileIsRewritten is a regression test for
|
||||||
|
// https://git.eeqj.de/sneak/secret/issues/153: `secret version promote
|
||||||
|
// --help` said it updates a symlink, but promote rewrites the secret's
|
||||||
|
// `current` file, which is a plain file.
|
||||||
|
//
|
||||||
|
//nolint:paralleltest // executes the CLI in-process and sets the environment
|
||||||
|
func TestPromoteHelpSaysCurrentFileIsRewritten(t *testing.T) {
|
||||||
|
env := map[string]string{secret.EnvStateDir: t.TempDir()}
|
||||||
|
|
||||||
|
output, err := ExecuteCommandInProcess(
|
||||||
|
[]string{"version", "promote", "--help"}, "", env)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.NotContains(t, output, "symlink")
|
||||||
|
assert.Contains(t, output, "current file")
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetSecretWithVersion(t *testing.T) {
|
func TestGetSecretWithVersion(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
@@ -55,7 +55,10 @@ func ValidateVaultName(name string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ResolveVaultSymlink reads the currentvault file to get the path to the current vault
|
// ResolveVaultSymlink reads the currentvault file to get the path to the current vault
|
||||||
// The file contains just the vault name (e.g., "default")
|
// The file contains just the vault name (e.g., "default"). An empty or
|
||||||
|
// invalid name, or the name of a vault that does not exist, is an error
|
||||||
|
// wrapping ErrVaultNotFound that names the file and says to run
|
||||||
|
// 'secret vault select <name>'.
|
||||||
func ResolveVaultSymlink(fs afero.Fs, currentVaultPath string) (string, error) {
|
func ResolveVaultSymlink(fs afero.Fs, currentVaultPath string) (string, error) {
|
||||||
secret.Debug("resolveVaultSymlink starting", "path", currentVaultPath)
|
secret.Debug("resolveVaultSymlink starting", "path", currentVaultPath)
|
||||||
|
|
||||||
@@ -74,6 +77,21 @@ func ResolveVaultSymlink(fs afero.Fs, currentVaultPath string) (string, error) {
|
|||||||
stateDir := filepath.Dir(currentVaultPath)
|
stateDir := filepath.Dir(currentVaultPath)
|
||||||
absolutePath := filepath.Join(stateDir, "vaults.d", vaultName)
|
absolutePath := filepath.Join(stateDir, "vaults.d", vaultName)
|
||||||
|
|
||||||
|
// An empty name would make vaults.d itself the vault's directory
|
||||||
|
exists := false
|
||||||
|
if isValidVaultName(vaultName) {
|
||||||
|
exists, err = afero.DirExists(fs, absolutePath)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to check if vault exists: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
return "", fmt.Errorf("vault '%s' named in %s %w; run "+
|
||||||
|
"'secret vault select <name>' to select a vault",
|
||||||
|
vaultName, currentVaultPath, ErrVaultNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
secret.Debug("Resolved to absolute path", "absolute_path", absolutePath)
|
secret.Debug("Resolved to absolute path", "absolute_path", absolutePath)
|
||||||
|
|
||||||
return absolutePath, nil
|
return absolutePath, nil
|
||||||
|
|||||||
Reference in new issue
Block a user