Compare commits
2
Commits
8c82ce628f
...
ac1422f359
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac1422f359 | ||
|
|
df47ab386c |
No files matched your search
@@ -18,6 +18,23 @@ https://git.eeqj.de/sneak/secret/milestone/12
|
|||||||
|
|
||||||
# Completed Steps
|
# Completed Steps
|
||||||
|
|
||||||
|
- 2026-10-06: The tests run quickly with the race detector on
|
||||||
|
(https://git.eeqj.de/sneak/secret/issues/120). Most of their time went to
|
||||||
|
deriving keys from passphrases with scrypt, which is slow on purpose. The new
|
||||||
|
`secret.ScryptWorkFactor`, when not zero, replaces age's scrypt work factor
|
||||||
|
when a passphrase encrypts; the tests of `internal/secret`, `internal/vault`
|
||||||
|
and `internal/cli` set it to 1 before any test runs, and the program never
|
||||||
|
sets it. `TestRemovalAsksWithoutHoldingLock` and
|
||||||
|
`TestFailedCommandReleasesLock` no longer run in parallel with other tests:
|
||||||
|
each waits at most 10 seconds for the in-memory lock that every test in the
|
||||||
|
package shares, and other tests' commands held it longer. The `script/cibuild`
|
||||||
|
comment no longer says that tests are skipped without its memlock ulimit.
|
||||||
|
- 2026-10-05: No test stores a secret larger than 1 MiB
|
||||||
|
(https://git.eeqj.de/sneak/secret/issues/52). The size tests for `secret add`,
|
||||||
|
`secret import` and the stdin buffer no longer try 2 MB, 10 MB, 99 MB, 100 MB
|
||||||
|
or 101 MB secrets, and nothing tests that a secret over the 100 MB limit is
|
||||||
|
rejected; the limit itself is unchanged. With nothing large left, the size
|
||||||
|
tests no longer skip a case for want of locked memory.
|
||||||
- 2026-10-05: The Go module path is `sneak.berlin/go/secret`, as
|
- 2026-10-05: The Go module path is `sneak.berlin/go/secret`, as
|
||||||
`REPO_POLICIES.md` requires, not `git.eeqj.de/sneak/secret`
|
`REPO_POLICIES.md` requires, not `git.eeqj.de/sneak/secret`
|
||||||
(https://git.eeqj.de/sneak/secret/issues/43). Every import uses it, as do the
|
(https://git.eeqj.de/sneak/secret/issues/43). Every import uses it, as do the
|
||||||
|
|||||||
@@ -353,9 +353,9 @@ func TestRemovalWithoutTerminalFailsAtOnce(t *testing.T) {
|
|||||||
// for its answer, another command can take the state directory lock and
|
// for its answer, another command can take the state directory lock and
|
||||||
// change the secret, and that the removal then removes nothing, since the
|
// change the secret, and that the removal then removes nothing, since the
|
||||||
// secret is no longer what the question named.
|
// secret is no longer what the question named.
|
||||||
|
//
|
||||||
|
//nolint:paralleltest // times commands against the in-memory lock all tests share
|
||||||
func TestRemovalAsksWithoutHoldingLock(t *testing.T) {
|
func TestRemovalAsksWithoutHoldingLock(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
r := newRemoval(t, "rm")
|
r := newRemoval(t, "rm")
|
||||||
|
|
||||||
answers, answerWriter := io.Pipe()
|
answers, answerWriter := io.Pipe()
|
||||||
|
|||||||
@@ -52,8 +52,12 @@ func runSecretWithStdin(stdin string, env map[string]string, args ...string) (st
|
|||||||
return cli.ExecuteCommandInProcess(args, stdin, env)
|
return cli.ExecuteCommandInProcess(args, stdin, env)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestMain runs before all tests and ensures the binary is built
|
// TestMain runs before all tests and ensures the binary is built. It also
|
||||||
|
// makes passphrase encryption in the tests cheap (see
|
||||||
|
// secret.ScryptWorkFactor); the binary keeps age's work factor.
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
|
secret.ScryptWorkFactor = 1
|
||||||
|
|
||||||
// Get the current working directory
|
// Get the current working directory
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -235,9 +235,9 @@ func TestEncryptPipedIntoAdd(t *testing.T) {
|
|||||||
|
|
||||||
// TestFailedCommandReleasesLock checks that a command failing after it
|
// TestFailedCommandReleasesLock checks that a command failing after it
|
||||||
// took the state directory lock leaves the lock free for the next command.
|
// took the state directory lock leaves the lock free for the next command.
|
||||||
|
//
|
||||||
|
//nolint:paralleltest // times commands against the in-memory lock all tests share
|
||||||
func TestFailedCommandReleasesLock(t *testing.T) {
|
func TestFailedCommandReleasesLock(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
cli := NewCLIInstanceWithStateDir(fs, testStateDir)
|
cli := NewCLIInstanceWithStateDir(fs, testStateDir)
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
"sneak.berlin/go/secret/internal/vault"
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
"sneak.berlin/go/secret/pkg/agehd"
|
||||||
)
|
)
|
||||||
@@ -22,45 +21,6 @@ import (
|
|||||||
// testVaultName is the vault name used by the size tests.
|
// testVaultName is the vault name used by the size tests.
|
||||||
const testVaultName = "test-vault"
|
const testVaultName = "test-vault"
|
||||||
|
|
||||||
// lockedBytesPerSecretByte bounds the locked memory that storing a secret
|
|
||||||
// holds at once: the buffers it is read into reach up to 1.5 times its
|
|
||||||
// size, and they are then copied into one more buffer of its size.
|
|
||||||
const lockedBytesPerSecretByte = 3
|
|
||||||
|
|
||||||
// skipIfLockedMemoryTooLow skips the test when this process cannot lock
|
|
||||||
// the memory a secret of size bytes needs, found by locking a buffer of
|
|
||||||
// that size and releasing it. memguard panics, ending the whole test run,
|
|
||||||
// when it cannot lock a buffer, and a plain `docker build .` runs the
|
|
||||||
// tests under an 8 MiB locked-memory limit (RLIMIT_MEMLOCK). A process
|
|
||||||
// allowed to lock past that limit runs every case.
|
|
||||||
func skipIfLockedMemoryTooLow(t *testing.T, size int) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
need := lockedBytesPerSecretByte * size
|
|
||||||
|
|
||||||
buf, err := unix.Mmap(-1, 0, need,
|
|
||||||
unix.PROT_READ|unix.PROT_WRITE, unix.MAP_PRIVATE|unix.MAP_ANON)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
lockErr := unix.Mlock(buf)
|
|
||||||
|
|
||||||
// Unmapping the buffer also unlocks it.
|
|
||||||
err = unix.Munmap(buf)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
if lockErr != nil {
|
|
||||||
var limit unix.Rlimit
|
|
||||||
|
|
||||||
err = unix.Getrlimit(unix.RLIMIT_MEMLOCK, &limit)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
t.Skipf("a %d-byte secret needs up to %d bytes of locked memory, "+
|
|
||||||
"which could not be locked under the locked-memory limit "+
|
|
||||||
"(RLIMIT_MEMLOCK) of %d bytes: %v",
|
|
||||||
size, need, limit.Cur, lockErr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// newSizeTestVault creates an in-memory vault unlocked with the test
|
// newSizeTestVault creates an in-memory vault unlocked with the test
|
||||||
// mnemonic and returns the filesystem and vault.
|
// mnemonic and returns the filesystem and vault.
|
||||||
//
|
//
|
||||||
@@ -93,10 +53,9 @@ func newSizeTestVault(t *testing.T) (afero.Fs, *vault.Vault) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// runAddSecretSizeCase adds a secret of the given size through stdin and
|
// runAddSecretSizeCase adds a secret of the given size through stdin and
|
||||||
// verifies the outcome: wantErr, or the secret stored when wantErr is nil.
|
// verifies that it is stored.
|
||||||
func runAddSecretSizeCase(t *testing.T, size int, wantErr error) {
|
func runAddSecretSizeCase(t *testing.T, size int) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
skipIfLockedMemoryTooLow(t, size)
|
|
||||||
|
|
||||||
fs, vlt := newSizeTestVault(t)
|
fs, vlt := newSizeTestVault(t)
|
||||||
|
|
||||||
@@ -127,13 +86,6 @@ func runAddSecretSizeCase(t *testing.T, size int, wantErr error) {
|
|||||||
// Test adding the secret
|
// Test adding the secret
|
||||||
secretName := fmt.Sprintf("test-secret-%d", size)
|
secretName := fmt.Sprintf("test-secret-%d", size)
|
||||||
err = cli.AddSecret(secretName, false)
|
err = cli.AddSecret(secretName, false)
|
||||||
|
|
||||||
if wantErr != nil {
|
|
||||||
require.ErrorIs(t, err, wantErr)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Verify the secret was stored correctly
|
// Verify the secret was stored correctly
|
||||||
@@ -147,10 +99,9 @@ func runAddSecretSizeCase(t *testing.T, size int, wantErr error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// runImportSecretSizeCase imports a secret file of the given size and
|
// runImportSecretSizeCase imports a secret file of the given size and
|
||||||
// verifies the outcome: wantErr, or the secret stored when wantErr is nil.
|
// verifies that it is stored.
|
||||||
func runImportSecretSizeCase(t *testing.T, size int, wantErr error) {
|
func runImportSecretSizeCase(t *testing.T, size int) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
skipIfLockedMemoryTooLow(t, size)
|
|
||||||
|
|
||||||
fs, vlt := newSizeTestVault(t)
|
fs, vlt := newSizeTestVault(t)
|
||||||
|
|
||||||
@@ -179,13 +130,6 @@ func runImportSecretSizeCase(t *testing.T, size int, wantErr error) {
|
|||||||
// Test importing the secret
|
// Test importing the secret
|
||||||
secretName := fmt.Sprintf("imported-secret-%d", size)
|
secretName := fmt.Sprintf("imported-secret-%d", size)
|
||||||
err = cli.ImportSecret(cmd, secretName, testFile, false)
|
err = cli.ImportSecret(cmd, secretName, testFile, false)
|
||||||
|
|
||||||
if wantErr != nil {
|
|
||||||
require.ErrorIs(t, err, wantErr)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Verify the secret was stored correctly
|
// Verify the secret was stored correctly
|
||||||
@@ -200,12 +144,11 @@ func runImportSecretSizeCase(t *testing.T, size int, wantErr error) {
|
|||||||
|
|
||||||
// TestAddSecretVariousSizes tests adding secrets of various sizes through stdin
|
// TestAddSecretVariousSizes tests adding secrets of various sizes through stdin
|
||||||
//
|
//
|
||||||
//nolint:paralleltest // together the subtests lock more than the memlock limit
|
//nolint:paralleltest // in parallel, size tests could exceed the memlock limit
|
||||||
func TestAddSecretVariousSizes(t *testing.T) {
|
func TestAddSecretVariousSizes(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
size int
|
size int
|
||||||
wantErr error
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "1KB secret",
|
name: "1KB secret",
|
||||||
@@ -223,40 +166,22 @@ func TestAddSecretVariousSizes(t *testing.T) {
|
|||||||
name: "1MB secret",
|
name: "1MB secret",
|
||||||
size: 1024 * 1024,
|
size: 1024 * 1024,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "10MB secret",
|
|
||||||
size: 10 * 1024 * 1024,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "99MB secret",
|
|
||||||
size: 99 * 1024 * 1024,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "100MB secret minus 1 byte",
|
|
||||||
size: 100*1024*1024 - 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "101MB secret - should fail",
|
|
||||||
size: 101 * 1024 * 1024,
|
|
||||||
wantErr: errSecretTooLarge,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
runAddSecretSizeCase(t, tt.size, tt.wantErr)
|
runAddSecretSizeCase(t, tt.size)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestImportSecretVariousSizes tests importing secrets of various sizes from files
|
// TestImportSecretVariousSizes tests importing secrets of various sizes from files
|
||||||
//
|
//
|
||||||
//nolint:paralleltest // together the subtests lock more than the memlock limit
|
//nolint:paralleltest // in parallel, size tests could exceed the memlock limit
|
||||||
func TestImportSecretVariousSizes(t *testing.T) {
|
func TestImportSecretVariousSizes(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
size int
|
size int
|
||||||
wantErr error
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "1KB file",
|
name: "1KB file",
|
||||||
@@ -274,35 +199,18 @@ func TestImportSecretVariousSizes(t *testing.T) {
|
|||||||
name: "1MB file",
|
name: "1MB file",
|
||||||
size: 1024 * 1024,
|
size: 1024 * 1024,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "10MB file",
|
|
||||||
size: 10 * 1024 * 1024,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "99MB file",
|
|
||||||
size: 99 * 1024 * 1024,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "100MB file",
|
|
||||||
size: 100 * 1024 * 1024,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "101MB file - should fail",
|
|
||||||
size: 101 * 1024 * 1024,
|
|
||||||
wantErr: errSecretTooLarge,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
runImportSecretSizeCase(t, tt.size, tt.wantErr)
|
runImportSecretSizeCase(t, tt.size)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestAddSecretBufferGrowth tests that our buffer growth strategy works correctly
|
// TestAddSecretBufferGrowth tests that our buffer growth strategy works correctly
|
||||||
//
|
//
|
||||||
//nolint:paralleltest // together the subtests lock more than the memlock limit
|
//nolint:paralleltest // in parallel, size tests could exceed the memlock limit
|
||||||
func TestAddSecretBufferGrowth(t *testing.T) {
|
func TestAddSecretBufferGrowth(t *testing.T) {
|
||||||
// Test various sizes that should trigger buffer growth
|
// Test various sizes that should trigger buffer growth
|
||||||
sizes := []int{
|
sizes := []int{
|
||||||
@@ -321,13 +229,10 @@ func TestAddSecretBufferGrowth(t *testing.T) {
|
|||||||
131072, // 128KB
|
131072, // 128KB
|
||||||
524288, // 512KB
|
524288, // 512KB
|
||||||
1048576, // 1MB
|
1048576, // 1MB
|
||||||
2097152, // 2MB
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, size := range sizes {
|
for _, size := range sizes {
|
||||||
t.Run(fmt.Sprintf("size_%d", size), func(t *testing.T) {
|
t.Run(fmt.Sprintf("size_%d", size), func(t *testing.T) {
|
||||||
skipIfLockedMemoryTooLow(t, size)
|
|
||||||
|
|
||||||
fs, vlt := newSizeTestVault(t)
|
fs, vlt := newSizeTestVault(t)
|
||||||
|
|
||||||
// Create test data of exactly the specified size
|
// Create test data of exactly the specified size
|
||||||
|
|||||||
@@ -28,6 +28,17 @@ var (
|
|||||||
// 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")
|
||||||
|
|
||||||
|
// ScryptWorkFactor is, when not zero, the scrypt work factor that
|
||||||
|
// EncryptWithPassphrase uses instead of age's, 18: log2 of scrypt's cost
|
||||||
|
// parameter N. Deriving a key with age's takes about a second and 256 MiB, on
|
||||||
|
// purpose, since so does every guess at the passphrase. Only tests set it,
|
||||||
|
// lower, before any test runs, so that the passphrase unlockers they create
|
||||||
|
// cost nothing; the program leaves it zero. Decryption takes the work factor
|
||||||
|
// from the encrypted data, so it needs no setting.
|
||||||
|
//
|
||||||
|
//nolint:gochecknoglobals // set by the tests of the packages that use this one
|
||||||
|
var ScryptWorkFactor int
|
||||||
|
|
||||||
// EncryptToRecipient encrypts data to a recipient using age
|
// EncryptToRecipient encrypts data to a recipient using age
|
||||||
// The data parameter should be a LockedBuffer for secure memory handling
|
// The data parameter should be a LockedBuffer for secure memory handling
|
||||||
func EncryptToRecipient(
|
func EncryptToRecipient(
|
||||||
@@ -142,6 +153,10 @@ func EncryptWithPassphrase(
|
|||||||
return nil, fmt.Errorf("failed to create scrypt recipient: %w", err)
|
return nil, fmt.Errorf("failed to create scrypt recipient: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ScryptWorkFactor != 0 {
|
||||||
|
recipient.SetWorkFactor(ScryptWorkFactor)
|
||||||
|
}
|
||||||
|
|
||||||
return EncryptToRecipient(data, recipient)
|
return EncryptToRecipient(data, recipient)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,14 @@ var (
|
|||||||
errNotImplementedInMock = errors.New("not implemented in mock")
|
errNotImplementedInMock = errors.New("not implemented in mock")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TestMain makes passphrase encryption in the tests cheap; see
|
||||||
|
// ScryptWorkFactor.
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
ScryptWorkFactor = 1
|
||||||
|
|
||||||
|
os.Exit(m.Run())
|
||||||
|
}
|
||||||
|
|
||||||
// MockVault is a test implementation of the VaultInterface
|
// MockVault is a test implementation of the VaultInterface
|
||||||
type MockVault struct {
|
type MockVault struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package vault_test
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -28,6 +29,14 @@ const (
|
|||||||
testPassphrase = "test-passphrase"
|
testPassphrase = "test-passphrase"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TestMain makes passphrase encryption in the tests cheap; see
|
||||||
|
// secret.ScryptWorkFactor.
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
secret.ScryptWorkFactor = 1
|
||||||
|
|
||||||
|
os.Exit(m.Run())
|
||||||
|
}
|
||||||
|
|
||||||
// testMnemonicBuffer returns testMnemonic in a locked buffer that is
|
// testMnemonicBuffer returns testMnemonic in a locked buffer that is
|
||||||
// destroyed when the test ends.
|
// destroyed when the test ends.
|
||||||
func testMnemonicBuffer(t *testing.T) *memguard.LockedBuffer {
|
func testMnemonicBuffer(t *testing.T) *memguard.LockedBuffer {
|
||||||
|
|||||||
+3
-3
@@ -1,9 +1,9 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# script/cibuild: run the CI build. The Dockerfile runs script/check
|
# script/cibuild: run the CI build. The Dockerfile runs script/check
|
||||||
# (via make check), so a successful build implies all checks pass.
|
# (via make check), so a successful build implies all checks pass.
|
||||||
# The Gitea workflow runs this on push. The memlock ulimit lets the tests
|
# The Gitea workflow runs this on push. The memlock ulimit lifts the limit
|
||||||
# that lock large secrets in memory (memguard mlocks them) run; under the
|
# on memory the tests lock (memguard mlocks secrets); they also pass under
|
||||||
# lower limit of a plain `docker build .` they are skipped.
|
# the lower limit of a plain `docker build .`.
|
||||||
# A cached build checks nothing: a new CHECK_EPOCH on every run makes the
|
# A cached build checks nothing: a new CHECK_EPOCH on every run makes the
|
||||||
# Dockerfile's check steps run again on an unchanged tree, while its base
|
# Dockerfile's check steps run again on an unchanged tree, while its base
|
||||||
# images and module downloads stay cached.
|
# images and module downloads stay cached.
|
||||||
|
|||||||
Reference in new issue
Block a user