Compare commits
3
Commits
f55d00225e
...
41965aa810
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
41965aa810 | ||
|
|
6bc95f7c69 | ||
|
|
fdfe2c92f6 |
@@ -142,7 +142,11 @@ value may be over 100 MiB, whatever the limit.
|
|||||||
|
|
||||||
#### `secret add <secret-name> [--force]`
|
#### `secret add <secret-name> [--force]`
|
||||||
|
|
||||||
Adds a secret to the current vault. Reads the secret value from stdin.
|
Adds a secret to the current vault. Reads the secret value from stdin. When the
|
||||||
|
value ends in a newline, that one newline is dropped, so
|
||||||
|
`echo value | secret add name` stores `value`; from `printf 'a\n\n'` it stores
|
||||||
|
`a` and one newline. To keep a file's final newline, as in a PEM key, use
|
||||||
|
`secret import`, which stores the file's bytes unchanged.
|
||||||
|
|
||||||
- `--force, -f`: Overwrite existing secret
|
- `--force, -f`: Overwrite existing secret
|
||||||
|
|
||||||
@@ -155,13 +159,13 @@ contain `//`, or have `..` as a path segment.
|
|||||||
|
|
||||||
#### `secret get <secret-name> [--version <version>]`
|
#### `secret get <secret-name> [--version <version>]`
|
||||||
|
|
||||||
Retrieves and outputs a secret value to stdout. A failed write of the value,
|
Writes the secret's value to stdout exactly as stored, with no newline added
|
||||||
such as to a file on a full disk, prints the error and exits 1, with two
|
after it. A failed write of the value, such as to a file on a full disk, prints
|
||||||
exceptions. If the program reading from a pipe has already exited, as in
|
the error and exits 1, with two exceptions. If the program reading from a pipe
|
||||||
`secret get s | true`, the broken-pipe signal (SIGPIPE) ends `secret` with no
|
has already exited, as in `secret get s | true`, the broken-pipe signal
|
||||||
message and the shell reports exit status 141. If the shell closed stdout before
|
(SIGPIPE) ends `secret` with no message and the shell reports exit status 141.
|
||||||
`secret` started (`secret get s >&-`), the value is written to `/dev/null` and
|
If the shell closed stdout before `secret` started (`secret get s >&-`), the
|
||||||
`secret get` exits 0.
|
value is written to `/dev/null` and `secret get` exits 0.
|
||||||
|
|
||||||
- `--version, -v`: Get a specific version (default: current)
|
- `--version, -v`: Get a specific version (default: current)
|
||||||
|
|
||||||
@@ -300,7 +304,7 @@ Selects an unlocker as the current default for operations.
|
|||||||
#### `secret import <secret-name> --source <filename>`
|
#### `secret import <secret-name> --source <filename>`
|
||||||
|
|
||||||
Imports a secret from a file and stores it in the current vault under the given
|
Imports a secret from a file and stores it in the current vault under the given
|
||||||
name.
|
name. The file's bytes are stored unchanged, a final newline included.
|
||||||
|
|
||||||
### Encryption Operations
|
### Encryption Operations
|
||||||
|
|
||||||
@@ -550,7 +554,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
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
//nolint:testpackage // white-box test of unexported internals
|
||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/afero"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestAddDropsOneTrailingNewlineImportKeepsIt pins the rule the README
|
||||||
|
// gives: `secret add` drops one trailing newline from stdin, and
|
||||||
|
// `secret import` stores the file's bytes unchanged.
|
||||||
|
func TestAddDropsOneTrailingNewlineImportKeepsIt(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
fs, vlt := newSizeTestVault(t)
|
||||||
|
value := []byte("a\n\n")
|
||||||
|
|
||||||
|
cmd := &cobra.Command{}
|
||||||
|
cmd.SetIn(bytes.NewReader(value))
|
||||||
|
|
||||||
|
cli := NewCLIInstanceWithStateDir(fs, testStateDir)
|
||||||
|
cli.cmd = cmd
|
||||||
|
|
||||||
|
require.NoError(t, cli.AddSecret("added", false))
|
||||||
|
|
||||||
|
sourceFile := "/test/value"
|
||||||
|
require.NoError(t, afero.WriteFile(fs, sourceFile, value, 0o600))
|
||||||
|
require.NoError(t, cli.ImportSecret(cmd, "imported", sourceFile, false))
|
||||||
|
|
||||||
|
added, err := vlt.GetSecret("added")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
defer added.Destroy()
|
||||||
|
|
||||||
|
imported, err := vlt.GetSecret("imported")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
defer imported.Destroy()
|
||||||
|
|
||||||
|
assert.Equal(t, []byte("a\n"), added.Bytes())
|
||||||
|
assert.Equal(t, []byte("a\n\n"), imported.Bytes())
|
||||||
|
}
|
||||||
@@ -388,7 +388,8 @@ func ResolveGPGKeyFingerprint(keyID string) (string, error) {
|
|||||||
return "", fmt.Errorf("%w: %s", ErrGPGKeyNotFound, keyID)
|
return "", fmt.Errorf("%w: %s", ErrGPGKeyNotFound, keyID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", fmt.Errorf("failed to resolve GPG key fingerprint: %w", err)
|
return "", fmt.Errorf(
|
||||||
|
"failed to resolve GPG key fingerprint: %w", withGPGStderr(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the output to extract the fingerprint
|
// Parse the output to extract the fingerprint
|
||||||
@@ -418,6 +419,24 @@ func checkGPGAvailable() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// withGPGStderr appends gpg's stderr, which says why gpg failed, to err
|
||||||
|
// from cmd.Output. cmd.Output keeps that stderr in the *exec.ExitError.
|
||||||
|
// err is returned unchanged when gpg wrote nothing to stderr.
|
||||||
|
func withGPGStderr(err error) error {
|
||||||
|
var exitErr *exec.ExitError
|
||||||
|
|
||||||
|
if !errors.As(err, &exitErr) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
stderr := strings.TrimSpace(string(exitErr.Stderr))
|
||||||
|
if stderr == "" {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("%w: %s", err, stderr)
|
||||||
|
}
|
||||||
|
|
||||||
// gpgEncryptDefault is the default implementation of GPG encryption
|
// gpgEncryptDefault is the default implementation of GPG encryption
|
||||||
func gpgEncryptDefault(data *memguard.LockedBuffer, keyID string) ([]byte, error) {
|
func gpgEncryptDefault(data *memguard.LockedBuffer, keyID string) ([]byte, error) {
|
||||||
if data == nil {
|
if data == nil {
|
||||||
@@ -437,7 +456,7 @@ func gpgEncryptDefault(data *memguard.LockedBuffer, keyID string) ([]byte, error
|
|||||||
|
|
||||||
output, err := cmd.Output()
|
output, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("GPG encryption failed: %w", err)
|
return nil, fmt.Errorf("GPG encryption failed: %w", withGPGStderr(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return output, nil
|
return output, nil
|
||||||
@@ -450,7 +469,7 @@ func gpgDecryptDefault(encryptedData []byte) (*memguard.LockedBuffer, error) {
|
|||||||
|
|
||||||
output, err := cmd.Output()
|
output, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("GPG decryption failed: %w", err)
|
return nil, fmt.Errorf("GPG decryption failed: %w", withGPGStderr(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a secure buffer for the decrypted data
|
// Create a secure buffer for the decrypted data
|
||||||
|
|||||||
@@ -102,3 +102,27 @@ func TestPGPUnlockerAddedTwiceKeepsFirst(t *testing.T) {
|
|||||||
assert.NotEqual(t, first.GetDirectory(), second.GetDirectory())
|
assert.NotEqual(t, first.GetDirectory(), second.GetDirectory())
|
||||||
assert.Equal(t, firstFiles, dirFiles(t, fs, first.GetDirectory()))
|
assert.Equal(t, firstFiles, dirFiles(t, fs, first.GetDirectory()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestGPGDecryptErrorHasGPGReason runs the real gpg on data that is not an
|
||||||
|
// OpenPGP message and checks that the error carries the reason gpg writes
|
||||||
|
// to stderr.
|
||||||
|
func TestGPGDecryptErrorHasGPGReason(t *testing.T) {
|
||||||
|
// The empty GNUPGHOME keeps gpg away from the user's own keyring;
|
||||||
|
// LC_ALL=C keeps gpg's message in English.
|
||||||
|
t.Setenv("GNUPGHOME", t.TempDir())
|
||||||
|
t.Setenv("LC_ALL", "C")
|
||||||
|
|
||||||
|
_, err := secret.GPGDecryptFunc([]byte("not an OpenPGP message"))
|
||||||
|
require.ErrorContains(t, err, "gpg: no valid OpenPGP data found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestGPGDecryptErrorWithoutGPGStderr checks that when gpg fails without
|
||||||
|
// writing to stderr, the error ends with the exit status and nothing after it.
|
||||||
|
//
|
||||||
|
//nolint:paralleltest // installFakeGPG uses t.Setenv
|
||||||
|
func TestGPGDecryptErrorWithoutGPGStderr(t *testing.T) {
|
||||||
|
installFakeGPG(t)
|
||||||
|
|
||||||
|
_, err := secret.GPGDecryptFunc([]byte("not an OpenPGP message"))
|
||||||
|
require.EqualError(t, err, "GPG decryption failed: exit status 1")
|
||||||
|
}
|
||||||
|
|||||||
@@ -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