Compare commits
1
Commits
next
..
947f9b8411
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
947f9b8411 |
@@ -1,93 +1,95 @@
|
|||||||
# IMPORTANT RULES
|
# IMPORTANT RULES
|
||||||
|
|
||||||
- Claude is an inanimate tool. The spam that Claude attempts to insert into
|
* Claude is an inanimate tool. The spam that Claude attempts to insert into
|
||||||
commit messages (which it erroneously refers to as "attribution") is not
|
commit messages (which it erroneously refers to as "attribution") is not
|
||||||
attribution, as I am the sole author of code created using Claude. It is
|
attribution, as I am the sole author of code created using Claude. It is
|
||||||
corporate advertising for Anthropic and is therefore completely unacceptable
|
corporate advertising for Anthropic and is therefore completely
|
||||||
in commit messages.
|
unacceptable in commit messages.
|
||||||
|
|
||||||
- Tests should always be run before committing code. No commits should be made
|
* Tests should always be run before committing code. No commits should be
|
||||||
that do not pass tests.
|
made that do not pass tests.
|
||||||
|
|
||||||
- Code should always be formatted before committing. Do not commit unformatted
|
* Code should always be formatted before committing. Do not commit
|
||||||
code.
|
unformatted code.
|
||||||
|
|
||||||
- Code should always be linted and linter errors fixed before committing. NEVER
|
* Code should always be linted and linter errors fixed before committing.
|
||||||
commit code that does not pass the linter. DO NOT modify the linter config
|
NEVER commit code that does not pass the linter. DO NOT modify the linter
|
||||||
unless specifically instructed.
|
config unless specifically instructed.
|
||||||
|
|
||||||
- The test suite is fast and local. When running tests, NEVER run individual
|
* The test suite is fast and local. When running tests, NEVER run
|
||||||
parts of the test suite, always run the whole thing by running "make test".
|
individual parts of the test suite, always run the whole thing by running
|
||||||
|
"make test".
|
||||||
|
|
||||||
- Do not stop working on a task until you have reached the definition of done
|
* Do not stop working on a task until you have reached the definition of
|
||||||
provided to you in the initial instruction. Don't do part or most of the work,
|
done provided to you in the initial instruction. Don't do part or most of
|
||||||
do all of the work until the criteria for done are met.
|
the work, do all of the work until the criteria for done are met.
|
||||||
|
|
||||||
- When you complete each task, if the tests are passing and the code is
|
* When you complete each task, if the tests are passing and the code is
|
||||||
formatted and there are no linter errors, always commit and push your work.
|
formatted and there are no linter errors, always commit and push your
|
||||||
Use a good commit message and don't mention any author or co-author
|
work. Use a good commit message and don't mention any author or co-author
|
||||||
attribution.
|
attribution.
|
||||||
|
|
||||||
- Do not create additional files in the root directory of the project without
|
* Do not create additional files in the root directory of the project
|
||||||
asking permission first. Configuration files, documentation, and build files
|
without asking permission first. Configuration files, documentation, and
|
||||||
are acceptable in the root, but source code and other files should be
|
build files are acceptable in the root, but source code and other files
|
||||||
organized in appropriate subdirectories.
|
should be organized in appropriate subdirectories.
|
||||||
|
|
||||||
- Do not use bare strings or numbers in code, especially if they appear anywhere
|
* Do not use bare strings or numbers in code, especially if they appear
|
||||||
more than once. Always define a constant (usually at the top of the file) and
|
anywhere more than once. Always define a constant (usually at the top of
|
||||||
give it a descriptive name, then use that constant in the code instead of the
|
the file) and give it a descriptive name, then use that constant in the
|
||||||
bare string or number.
|
code instead of the bare string or number.
|
||||||
|
|
||||||
- If you are fixing a bug, write a test first that reproduces the bug and fails,
|
* If you are fixing a bug, write a test first that reproduces the bug and
|
||||||
and then fix the bug in the code, using the test to verify that the fix
|
fails, and then fix the bug in the code, using the test to verify that the
|
||||||
worked.
|
fix worked.
|
||||||
|
|
||||||
- When implementing new features, be aware of potential side-effects (such as
|
* When implementing new features, be aware of potential side-effects (such
|
||||||
state files on disk, data in the database, etc.) and ensure that it is
|
as state files on disk, data in the database, etc.) and ensure that it is
|
||||||
possible to mock or stub these side-effects in tests when designing an API.
|
possible to mock or stub these side-effects in tests when designing an
|
||||||
|
API.
|
||||||
|
|
||||||
- When dealing with dates and times or timestamps, always use, display, and
|
* When dealing with dates and times or timestamps, always use, display, and
|
||||||
store UTC. Set the local timezone to UTC on startup. If the user needs to see
|
store UTC. Set the local timezone to UTC on startup. If the user needs
|
||||||
the time in a different timezone, store the user's timezone in a separate
|
to see the time in a different timezone, store the user's timezone in a
|
||||||
field and convert the UTC time to the user's timezone when displaying it. For
|
separate field and convert the UTC time to the user's timezone when
|
||||||
internal use and internal applications and administrative purposes, always
|
displaying it. For internal use and internal applications and
|
||||||
display UTC.
|
administrative purposes, always display UTC.
|
||||||
|
|
||||||
- When implementing programs, put the main.go in ./cmd/<program_name>/main.go
|
* When implementing programs, put the main.go in
|
||||||
and put the program's code in ./internal/<program_name>/. This allows for
|
./cmd/<program_name>/main.go and put the program's code in
|
||||||
multiple programs to be implemented in the same repository without cluttering
|
./internal/<program_name>/. This allows for multiple programs to be
|
||||||
the root directory. main.go should simply import and call
|
implemented in the same repository without cluttering the root directory.
|
||||||
<program_name>.CLIEntry(). The full implementation should be in
|
main.go should simply import and call <program_name>.CLIEntry(). The
|
||||||
./internal/<program_name>/.
|
full implementation should be in ./internal/<program_name>/.
|
||||||
|
|
||||||
- When you are instructed to make the tests pass, DO NOT delete tests, skip
|
* When you are instructed to make the tests pass, DO NOT delete tests, skip
|
||||||
tests, or change the tests specifically to make them pass (unless there is a
|
tests, or change the tests specifically to make them pass (unless there
|
||||||
bug in the test). This is cheating, and it is bad. You should only be
|
is a bug in the test). This is cheating, and it is bad. You should only
|
||||||
modifying the test if it is incorrect or if the test is no longer relevant. In
|
be modifying the test if it is incorrect or if the test is no longer
|
||||||
almost all cases, you should be fixing the code that is being tested, or
|
relevant. In almost all cases, you should be fixing the code that is
|
||||||
updating the tests to match a refactored implementation.
|
being tested, or updating the tests to match a refactored implementation.
|
||||||
|
|
||||||
- Always write a `Makefile` with the default target being `test`, and with a
|
* Always write a `Makefile` with the default target being `test`, and with a
|
||||||
`fmt` target that formats the code. The `test` target should run all tests in
|
`fmt` target that formats the code. The `test` target should run all
|
||||||
the project, and the `fmt` target should format the code. `test` should also
|
tests in the project, and the `fmt` target should format the code. `test`
|
||||||
have a prerequisite target `lint` that should run any linters that are
|
should also have a prerequisite target `lint` that should run any linters
|
||||||
configured for the project.
|
that are configured for the project.
|
||||||
|
|
||||||
- After each completed bugfix or feature, the code must be committed. Do all of
|
* After each completed bugfix or feature, the code must be committed. Do
|
||||||
the pre-commit checks (test, lint, fmt) before committing, of course. After
|
all of the pre-commit checks (test, lint, fmt) before committing, of
|
||||||
each commit, push to the remote.
|
course. After each commit, push to the remote.
|
||||||
|
|
||||||
- Always write tests, even if they are extremely simple and just check for
|
* Always write tests, even if they are extremely simple and just check for
|
||||||
correct syntax (ability to compile/import). If you are writing a new feature,
|
correct syntax (ability to compile/import). If you are writing a new
|
||||||
write a test for it. You don't need to target complete coverage, but you
|
feature, write a test for it. You don't need to target complete coverage,
|
||||||
should at least test any new functionality you add.
|
but you should at least test any new functionality you add.
|
||||||
|
|
||||||
- Always use structured logging. Log any relevant state/context with the
|
* Always use structured logging. Log any relevant state/context with the
|
||||||
messages (but do not log secrets). If stdout is not a terminal, output the
|
messages (but do not log secrets). If stdout is not a terminal, output
|
||||||
structured logs in jsonl format. Use go's log/slog.
|
the structured logs in jsonl format. Use go's log/slog.
|
||||||
|
|
||||||
- You do not need to summarize your changes in the chat after making them.
|
* You do not need to summarize your changes in the chat after making them.
|
||||||
Making the changes and committing them is sufficient. If anything out of the
|
Making the changes and committing them is sufficient. If anything out of
|
||||||
ordinary happened, please explain it, but in the normal case where you found
|
the ordinary happened, please explain it, but in the normal case where you
|
||||||
and fixed the bug, or implemented the feature, there is no need for the
|
found and fixed the bug, or implemented the feature, there is no need for
|
||||||
end-of-change summary.
|
the end-of-change summary.
|
||||||
|
|||||||
+2
-14
@@ -1,22 +1,10 @@
|
|||||||
# node and yarn, copied into the lint stage for prettier, which checks the
|
|
||||||
# markdown formatting: node of the version script/bootstrap pins, built on
|
|
||||||
# Debian as the lint stage's image is.
|
|
||||||
# node:22.17.0-bookworm-slim, 2025-07-08
|
|
||||||
FROM node@sha256:b04ce4ae4e95b522112c2e5c52f781471a5cbc3b594527bcddedee9bc48c03a0 AS node
|
|
||||||
|
|
||||||
# Lint stage — fast feedback on formatting and lint issues
|
# Lint stage — fast feedback on formatting and lint issues
|
||||||
# golangci/golangci-lint:v2.12.2 (Debian-based), 2026-08-07
|
# golangci/golangci-lint:v2.12.2 (Debian-based), 2026-08-07
|
||||||
FROM golangci/golangci-lint:v2.12.2@sha256:5cceeef04e53efe1470638d4b4b4f5ceefd574955ab3941b2d9a68a8c9ad5240 AS lint
|
FROM golangci/golangci-lint:v2.12.2@sha256:5cceeef04e53efe1470638d4b4b4f5ceefd574955ab3941b2d9a68a8c9ad5240 AS lint
|
||||||
|
|
||||||
COPY --from=node /usr/local/bin/node /usr/local/bin/node
|
|
||||||
COPY --from=node /opt/yarn-v1.22.22 /opt/yarn-v1.22.22
|
|
||||||
ENV PATH="/opt/yarn-v1.22.22/bin:${PATH}"
|
|
||||||
|
|
||||||
# script/bootstrap downloads the Go modules and installs prettier
|
|
||||||
WORKDIR /src
|
WORKDIR /src
|
||||||
COPY script/ script/
|
COPY go.mod go.sum ./
|
||||||
COPY go.mod go.sum package.json yarn.lock ./
|
RUN go mod download
|
||||||
RUN script/bootstrap
|
|
||||||
|
|
||||||
# script/cibuild sets CHECK_EPOCH to the current time, so the RUN steps
|
# script/cibuild sets CHECK_EPOCH to the current time, so the RUN steps
|
||||||
# below run again on each build, an unchanged tree included, while the
|
# below run again on each build, an unchanged tree included, while the
|
||||||
|
|||||||
@@ -180,8 +180,8 @@ period.
|
|||||||
|
|
||||||
#### `secret version promote <secret-name> <version>`
|
#### `secret version promote <secret-name> <version>`
|
||||||
|
|
||||||
Promotes a specific version to current by rewriting the secret's `current` file
|
Promotes a specific version to current by updating the symlink. Does not modify
|
||||||
to name it. Does not modify any timestamps, allowing for rollback scenarios.
|
any timestamps, allowing for rollback scenarios.
|
||||||
|
|
||||||
#### `secret version remove <secret-name> <version> [--force]` / `secret version rm` ⚠️ 🛑
|
#### `secret version remove <secret-name> <version> [--force]` / `secret version rm` ⚠️ 🛑
|
||||||
|
|
||||||
@@ -211,9 +211,7 @@ Generates and stores a random secret.
|
|||||||
|
|
||||||
#### `secret unlocker list [--json]` / `secret unlocker ls`
|
#### `secret unlocker list [--json]` / `secret unlocker ls`
|
||||||
|
|
||||||
Lists all unlockers in the current vault with their metadata. An unlocker's ID,
|
Lists all unlockers in the current vault with their metadata.
|
||||||
which `secret unlocker select` and `secret unlocker remove` take, is the name of
|
|
||||||
its directory in `unlockers.d`.
|
|
||||||
|
|
||||||
#### `secret unlocker add <type> [options]`
|
#### `secret unlocker add <type> [options]`
|
||||||
|
|
||||||
@@ -280,13 +278,8 @@ Decrypts data using an Age key stored as a secret.
|
|||||||
|
|
||||||
### Directory Structure
|
### Directory Structure
|
||||||
|
|
||||||
The state directory is `berlin.sneak.pkg.secret` in the user's configuration
|
|
||||||
directory: on Linux `$XDG_CONFIG_HOME`, or `~/.config` when that is unset; on
|
|
||||||
macOS `~/Library/Application Support`. When `SB_SECRET_STATE_DIR` is set, it is
|
|
||||||
the state directory instead. On Linux:
|
|
||||||
|
|
||||||
```
|
```
|
||||||
~/.config/berlin.sneak.pkg.secret/
|
~/.local/share/secret/
|
||||||
├── vaults.d/
|
├── vaults.d/
|
||||||
│ ├── default/
|
│ ├── default/
|
||||||
│ │ ├── unlockers.d/
|
│ │ ├── unlockers.d/
|
||||||
@@ -299,12 +292,12 @@ the state directory instead. On Linux:
|
|||||||
│ │ │ │ │ │ ├── pub.age # Version public key
|
│ │ │ │ │ │ ├── pub.age # Version public key
|
||||||
│ │ │ │ │ │ ├── priv.age # Version private key (encrypted)
|
│ │ │ │ │ │ ├── priv.age # Version private key (encrypted)
|
||||||
│ │ │ │ │ │ ├── value.age # Encrypted value
|
│ │ │ │ │ │ ├── value.age # Encrypted value
|
||||||
│ │ │ │ │ │ └── metadata.age # Encrypted metadata
|
│ │ │ │ │ │ └── metadata.json # Unencrypted metadata
|
||||||
│ │ │ │ │ └── 20231216.001/ # Another version
|
│ │ │ │ │ └── 20231216.001/ # Another version
|
||||||
│ │ │ │ └── current # Current version's name: 20231216.001
|
│ │ │ │ └── current -> versions/20231216.001
|
||||||
│ │ │ └── database%password/ # Secret: database/password
|
│ │ │ └── database%password/ # Secret: database/password
|
||||||
│ │ │ ├── versions/
|
│ │ │ ├── versions/
|
||||||
│ │ │ └── current # Current version's name: 20231215.001
|
│ │ │ └── current -> versions/20231215.001
|
||||||
│ │ ├── vault-metadata.json # Vault metadata
|
│ │ ├── vault-metadata.json # Vault metadata
|
||||||
│ │ ├── pub.age # Long-term public key
|
│ │ ├── pub.age # Long-term public key
|
||||||
│ │ └── current-unlocker # Current unlocker's directory name
|
│ │ └── current-unlocker # Current unlocker's directory name
|
||||||
@@ -314,13 +307,9 @@ the state directory instead. On Linux:
|
|||||||
│ ├── vault-metadata.json
|
│ ├── vault-metadata.json
|
||||||
│ ├── pub.age
|
│ ├── pub.age
|
||||||
│ └── current-unlocker
|
│ └── current-unlocker
|
||||||
├── currentvault # Current vault's name: default
|
└── currentvault -> vaults.d/default
|
||||||
└── lock # Locked by each command that changes anything
|
|
||||||
```
|
```
|
||||||
|
|
||||||
`current`, `currentvault` and `current-unlocker` are plain files that each hold
|
|
||||||
one name. Changing one replaces it in one rename, so it is never half-written.
|
|
||||||
|
|
||||||
### Key Management and Encryption Flow
|
### Key Management and Encryption Flow
|
||||||
|
|
||||||
#### 1: Long-term Keys
|
#### 1: Long-term Keys
|
||||||
@@ -347,7 +336,7 @@ Unlockers provide different authentication methods to access the long-term keys:
|
|||||||
|
|
||||||
3. **Keychain Unlockers** (macOS only):
|
3. **Keychain Unlockers** (macOS only):
|
||||||
- Stores unlock keys in macOS Keychain
|
- Stores unlock keys in macOS Keychain
|
||||||
- Kept on this Mac only: the keychain item is never synced to other devices
|
- Protected by system authentication (Touch ID, password)
|
||||||
- Automatic unlocking when Keychain is unlocked
|
- Automatic unlocking when Keychain is unlocked
|
||||||
- Cross-application integration
|
- Cross-application integration
|
||||||
|
|
||||||
@@ -355,10 +344,8 @@ Unlockers provide different authentication methods to access the long-term keys:
|
|||||||
- Hardware-backed key storage using Apple Secure Enclave
|
- Hardware-backed key storage using Apple Secure Enclave
|
||||||
- Uses `sc_auth` / CryptoTokenKit for SE key management (no Apple Developer
|
- Uses `sc_auth` / CryptoTokenKit for SE key management (no Apple Developer
|
||||||
Program required)
|
Program required)
|
||||||
- ECIES encryption: the vault long-term key is encrypted directly to the SE
|
- ECIES encryption: vault long-term key encrypted directly by SE hardware
|
||||||
key, and only the SE can decrypt it
|
- Protected by biometric authentication (Touch ID) or system password
|
||||||
- The SE key cannot leave this Mac; using it asks for no Touch ID or
|
|
||||||
password
|
|
||||||
|
|
||||||
Each vault maintains its own set of unlockers and one long-term key. The
|
Each vault maintains its own set of unlockers and one long-term key. The
|
||||||
long-term key is encrypted to each unlocker, allowing any authorized unlocker to
|
long-term key is encrypted to each unlocker, allowing any authorized unlocker to
|
||||||
@@ -368,7 +355,7 @@ access vault secrets.
|
|||||||
|
|
||||||
- Each secret version has its own encryption key pair
|
- Each secret version has its own encryption key pair
|
||||||
- Private key encrypted to the vault's long-term key
|
- Private key encrypted to the vault's long-term key
|
||||||
- A version's private key decrypts only that version's value and metadata
|
- Provides forward secrecy and granular access control
|
||||||
|
|
||||||
### Environment Variables
|
### Environment Variables
|
||||||
|
|
||||||
@@ -518,21 +505,17 @@ secret decrypt encryption/mykey --input document.txt.age --output document.txt
|
|||||||
|
|
||||||
### File Formats
|
### File Formats
|
||||||
|
|
||||||
- **age Files**: Standard age encryption format (.age extension), except
|
- **age Files**: Standard age encryption format (.age extension)
|
||||||
`pub.age`, which holds an age public key as text
|
- **Metadata**: Unencrypted JSON format with timestamps and type information
|
||||||
- **Metadata**: `vault-metadata.json` and `unlocker-metadata.json` are
|
- **Vault Metadata**: JSON containing vault name, creation time, derivation
|
||||||
unencrypted JSON with a creation time, and `unlocker-metadata.json` also
|
index, and public key hash
|
||||||
records the unlocker's type; a version's `metadata.age` is JSON encrypted to
|
|
||||||
the version's public key
|
|
||||||
- **Vault Metadata**: JSON containing creation time, derivation index, and the
|
|
||||||
public key hashes described below
|
|
||||||
|
|
||||||
### 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
|
||||||
- **Public Key Hash**: Double SHA-256 hash of the vault's public key; the same
|
- **Public Key Hash**: Double SHA-256 hash of the index-0 public key identifies
|
||||||
hash of the index-0 public key identifies vaults from the same mnemonic
|
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
|
||||||
automatically derived
|
automatically derived
|
||||||
|
|
||||||
@@ -583,6 +566,8 @@ The project includes comprehensive tests:
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
make test # Run all tests
|
make test # Run all tests
|
||||||
|
go test ./... # Unit tests
|
||||||
|
go test -tags=integration -v ./internal/cli # Integration tests
|
||||||
```
|
```
|
||||||
|
|
||||||
## Entrypoints
|
## Entrypoints
|
||||||
@@ -593,10 +578,8 @@ standard: normalized scripts in `script/` are the entrypoints for the
|
|||||||
development workflow, and the Makefile targets are thin shims that call them. We
|
development workflow, and the Makefile targets are thin shims that call them. We
|
||||||
provide:
|
provide:
|
||||||
|
|
||||||
- `script/bootstrap` — install all dependencies (Go, Go module download, and
|
- `script/bootstrap` — install all dependencies (Go, Go module download),
|
||||||
node, yarn and prettier for formatting markdown), idempotently; prettier is
|
idempotently; golangci-lint is not installed, it runs in docker
|
||||||
pinned by hash in `package.json` and `yarn.lock`; golangci-lint is not
|
|
||||||
installed, it runs in docker
|
|
||||||
- `script/setup` — make a fresh clone ready for development: runs
|
- `script/setup` — make a fresh clone ready for development: runs
|
||||||
`script/bootstrap`, then `script/install-precommit`
|
`script/bootstrap`, then `script/install-precommit`
|
||||||
- `script/projectname` — output the project name (`secret`); used by other
|
- `script/projectname` — output the project name (`secret`); used by other
|
||||||
@@ -613,11 +596,8 @@ provide:
|
|||||||
compiles; cgo is off, so the keychain unlocker's calls into the keychain
|
compiles; cgo is off, so the keychain unlocker's calls into the keychain
|
||||||
(`internal/secret/keychainunlocker_cgo.go`, and `keychainunlocker_test.go`)
|
(`internal/secret/keychainunlocker_cgo.go`, and `keychainunlocker_test.go`)
|
||||||
and the Secure Enclave bindings (`internal/macse`) are not checked
|
and the Secure Enclave bindings (`internal/macse`) are not checked
|
||||||
- `script/fmt` — format all Go code with `go fmt` and every markdown file with
|
- `script/fmt` — format all Go code (writes)
|
||||||
prettier (4-space tabs, `proseWrap: always`) (writes)
|
- `script/fmt-check` — check formatting without writing
|
||||||
- `script/fmt-check` — check the same formatting without writing; the
|
|
||||||
`Dockerfile` lint stage runs it, so an unformatted Go or markdown file fails
|
|
||||||
the build
|
|
||||||
- `script/check` — run `script/test`, `script/lint`, `script/lint-darwin`, and
|
- `script/check` — run `script/test`, `script/lint`, `script/lint-darwin`, and
|
||||||
`script/fmt-check`
|
`script/fmt-check`
|
||||||
- `script/docker` — build the Docker image tagged with the project name
|
- `script/docker` — build the Docker image tagged with the project name
|
||||||
|
|||||||
@@ -18,459 +18,361 @@ https://git.eeqj.de/sneak/secret/milestone/12
|
|||||||
|
|
||||||
# Completed Steps
|
# Completed Steps
|
||||||
|
|
||||||
- 2026-10-05: The Go module path is `sneak.berlin/go/secret`, as
|
|
||||||
`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
|
|
||||||
`-X` flags in `script/build` that stamp the version and commit shown by
|
|
||||||
`secret info`, and the examples in `pkg/agehd/README.md` and
|
|
||||||
`pkg/bip85/README.md`. `go mod tidy` now lists `github.com/dustin/go-humanize`
|
|
||||||
and `github.com/fatih/color`, which `internal/cli` imports, as direct
|
|
||||||
requirements. Code that imported the old path must switch to the new one.
|
|
||||||
- 2026-10-04: `make fmt` formats every markdown file with prettier (4-space
|
|
||||||
tabs, `proseWrap: always`) as well as the Go code, and `make fmt-check` checks
|
|
||||||
both, as the model scripts in the `prompts` repo do
|
|
||||||
(https://git.eeqj.de/sneak/secret/issues/110). Prettier is pinned by hash in
|
|
||||||
`package.json` and `yarn.lock`, and `script/bootstrap` installs node, yarn and
|
|
||||||
prettier. The `Dockerfile` lint stage copies node and yarn from a node image
|
|
||||||
pinned by hash and runs `script/bootstrap`, so its `make fmt-check` fails the
|
|
||||||
build on an unformatted markdown file. Every markdown file was formatted once,
|
|
||||||
wording unchanged.
|
|
||||||
- 2026-10-04: A mnemonic that cannot be read, in `secret init` and
|
|
||||||
`secret vault create`, gives an error that names the mnemonic only
|
|
||||||
(https://git.eeqj.de/sneak/secret/issues/115). It is read with
|
|
||||||
`secret.ReadMnemonic`, whose every error wraps the new
|
|
||||||
`secret.ErrMnemonicNotRead`; before, it was read with `ReadPassphrase`, so the
|
|
||||||
message said "failed to read mnemonic: failed to read passphrase:" and advised
|
|
||||||
setting `SB_UNLOCK_PASSPHRASE`. Without a terminal it now says "failed to read
|
|
||||||
mnemonic: stdin is not a terminal (piped input or script). Please set the
|
|
||||||
SB_SECRET_MNEMONIC environment variable or run interactively". The passphrase
|
|
||||||
messages no longer repeat "cannot read passphrase" after "failed to read
|
|
||||||
passphrase:", and empty input gives "nothing was entered".
|
|
||||||
- 2026-10-04: A failure returns the same error value whichever command hits it
|
|
||||||
(https://git.eeqj.de/sneak/secret/issues/113). `internal/cli` no longer keeps
|
|
||||||
its own copies of `vault.ErrSecretNotFound`, `ErrVaultNotFound`,
|
|
||||||
`ErrVersionNotFound` and `ErrSecretExists`: `secret mv`, `rm`, `decrypt`,
|
|
||||||
`vault import`, `vault remove` and `version list`, `promote` and `rm` wrap the
|
|
||||||
`vault` errors. `errUnsupportedUnlockerType` is removed: `secret unlocker add`
|
|
||||||
gives `errInvalidUnlockerType` for an unknown type, whichever check rejects
|
|
||||||
it. Off macOS, adding a keychain or Secure Enclave unlocker returns the
|
|
||||||
`secret` package's error for it, not an `internal/cli` copy; on macOS, the
|
|
||||||
check that the system is macOS is gone, as it could never fail.
|
|
||||||
`secret vault import` gives `errInvalidMnemonicPhrase` for an invalid
|
|
||||||
mnemonic, as `init` and `vault create` do. `secret generate secret` gives
|
|
||||||
`errLengthTooSmall` for a length below 1 wherever it is checked, and
|
|
||||||
`errUnsupportedSecretType` for `--type mnemonic` too. `secret import` of a
|
|
||||||
file over 100MB wraps `errSecretTooLarge`, as `secret add` returns it.
|
|
||||||
`vault.ErrNilValueBuffer` is replaced by `secret.ErrNilValueBuffer`, which
|
|
||||||
`secret` already returned under another name. Messages are unchanged, except
|
|
||||||
that `secret decrypt` of a missing secret says "not found", as `secret get`
|
|
||||||
does, not "does not exist"; `vault import` of an invalid mnemonic says
|
|
||||||
"invalid BIP39 mnemonic phrase"; `--type mnemonic` says "unsupported type:
|
|
||||||
mnemonic (use 'secret generate mnemonic' instead)"; and a file too large to
|
|
||||||
import says
|
|
||||||
`failed to read secret from file <path>: secret too large: exceeds 100MB limit`.
|
|
||||||
Every error of `secret.ReadPassphrase` wraps `secret.ErrPassphraseNotRead`,
|
|
||||||
which supplies the words "failed to read passphrase" that its callers used to
|
|
||||||
add themselves; so two passphrases that differ now give only "passphrases do
|
|
||||||
not match", the words now follow "failed to read mnemonic:" and "failed to
|
|
||||||
read passphrase confirmation:", and a terminal read error no longer repeats
|
|
||||||
them. A GPG key the keyring does not hold gives `secret.ErrGPGKeyNotFound`,
|
|
||||||
found by gpg's status line for "No public key"; before, the message repeated
|
|
||||||
"failed to resolve GPG key fingerprint" and ended in gpg's exit status. The
|
|
||||||
keychain unlocker returns `errNilDataBuffer` for nil data; this and its test
|
|
||||||
build only on macOS with cgo and were only read. `bip85.ErrPasswordTooShort`
|
|
||||||
and `ErrEncodedTooShort` are removed with their checks: 64 bytes of entropy
|
|
||||||
always give 86 Base64 or 80 Base85 characters, the most a password length may
|
|
||||||
ask for. Tests that matched these errors' text use `errors.Is`.
|
|
||||||
- 2026-10-04: Tests check which error a failure returns with `errors.Is`, not by
|
|
||||||
matching words of its message (https://git.eeqj.de/sneak/secret/issues/49).
|
|
||||||
Every exported error that can be returned has a test that the function returns
|
|
||||||
it, and errors wrapping a cause are checked through the wrapping. Checks that
|
|
||||||
still match text, because the error has no exported value the test can name,
|
|
||||||
are listed on the issue.
|
|
||||||
- 2026-10-04: When a vault cannot be opened through its current unlocker,
|
|
||||||
because a file the unlocker needs is missing or damaged, its keychain item or
|
|
||||||
Secure Enclave key is gone, or the passphrase is wrong, the error now ends by
|
|
||||||
naming the vault, saying that it still opens with its mnemonic, and that
|
|
||||||
`secret unlocker add passphrase`, run with `SB_SECRET_MNEMONIC` set to it,
|
|
||||||
gives the vault a new unlocker; for a vault that is not the current one, as in
|
|
||||||
`secret move` between vaults, it says to run `secret vault select` first
|
|
||||||
(https://git.eeqj.de/sneak/secret/issues/47). Before, it ended with the bare
|
|
||||||
cause. The advice is given only when the vault metadata records the key the
|
|
||||||
mnemonic derives, so not for a vault created without a mnemonic, and not when
|
|
||||||
the passphrase could not be read at all. `secret vault import` is not named:
|
|
||||||
it refuses a vault that has a long-term key. `secret encrypt` and
|
|
||||||
`secret decrypt` now read the key secret through `vault.GetSecret`, as
|
|
||||||
`secret get` does, so they give the same advice; `Secret.GetValue`, the other
|
|
||||||
way to get the long-term key, is removed. When a secret's `current` file
|
|
||||||
cannot be read, the error says that `secret version list` lists its versions
|
|
||||||
and `secret version promote` makes one current. The causes stay wrapped.
|
|
||||||
- 2026-10-04: An unlocker's ID is the name of its directory in `unlockers.d`, so
|
|
||||||
no two unlockers of a vault share one
|
|
||||||
(https://git.eeqj.de/sneak/secret/issues/98). Before, a keychain or Secure
|
|
||||||
Enclave unlocker's ID was its creation time to the minute and the host name,
|
|
||||||
and a passphrase unlocker's the time to the minute, so two created within a
|
|
||||||
minute shared an ID, and `unlocker select`, `unlocker remove` and the
|
|
||||||
selection `unlocker add` makes acted on the older one. A PGP unlocker's ID was
|
|
||||||
`pgp-` and its key's fingerprint; a second PGP unlocker for a key is still
|
|
||||||
refused, now by comparing the fingerprint in the other unlockers' metadata.
|
|
||||||
`unlocker list` and the shell completion of `unlocker select` and
|
|
||||||
`unlocker remove` take each ID from the directory the unlocker was read from,
|
|
||||||
no longer by matching metadata, so two unlockers with the same metadata are
|
|
||||||
listed apart; an unlocker of an unknown type is listed under its directory
|
|
||||||
name, and completion now offers Secure Enclave unlockers too. The keychain and
|
|
||||||
Secure Enclave code was type-checked by `script/lint-darwin`, never run; a
|
|
||||||
test on Linux lists, completes, selects and removes each of two passphrase
|
|
||||||
unlockers with the same metadata by its own ID.
|
|
||||||
- 2026-10-04: README's Storage Architecture, `secret version promote`, Technical
|
|
||||||
Details and Testing text matches the code
|
|
||||||
(https://git.eeqj.de/sneak/secret/issues/102). `current` and `currentvault`
|
|
||||||
are plain files holding a name, not symbolic links; a version's metadata is
|
|
||||||
the encrypted `metadata.age`; the state directory is `berlin.sneak.pkg.secret`
|
|
||||||
in the user's configuration directory, not `~/.local/share/secret`, and holds
|
|
||||||
the `lock` file. Also corrected: the code sets up no Touch ID for the keychain
|
|
||||||
or Secure Enclave unlocker, and the Secure Enclave only decrypts; per-version
|
|
||||||
keys give no forward secrecy; `pub.age` is not age-encrypted; vault metadata
|
|
||||||
holds no vault name. Testing lists only `make test`.
|
|
||||||
- 2026-10-04: `secret init` and `secret vault create` create a vault whole or
|
|
||||||
not at all (https://git.eeqj.de/sneak/secret/issues/105). `vault.CreateVault`
|
|
||||||
now takes the unlocker passphrase too, writes the vault directory with its
|
|
||||||
metadata, long-term public key and passphrase unlocker, `longterm.age`
|
|
||||||
included, into a temporary directory, renames that into `vaults.d` once it is
|
|
||||||
complete, and only then makes the vault current. Before, either command killed
|
|
||||||
after the passphrase prompt but before the unlocker was written left a vault
|
|
||||||
with no unlocker, which `vault create` had already made current and which
|
|
||||||
neither command would create again. Killed part-way now, it leaves no vault,
|
|
||||||
and the next command that takes the lock deletes the temporary directory; or,
|
|
||||||
killed between the rename and making the vault current, a complete vault that
|
|
||||||
is not current, which `secret vault select` makes current.
|
|
||||||
- 2026-10-04: A failed `secret unlocker add keychain` or
|
- 2026-10-04: A failed `secret unlocker add keychain` or
|
||||||
`secret unlocker add secure-enclave` no longer leaves its keychain item or
|
`secret unlocker add secure-enclave` no longer leaves its keychain item or
|
||||||
Secure Enclave key behind (https://git.eeqj.de/sneak/secret/issues/89).
|
Secure Enclave key behind (https://git.eeqj.de/sneak/secret/issues/89).
|
||||||
`CreateSecureEnclaveUnlocker` gets the long-term key before it creates the
|
`CreateSecureEnclaveUnlocker` gets the long-term key before it creates the
|
||||||
Secure Enclave key, so that a wrong passphrase creates none, and deletes the
|
Secure Enclave key, so that a wrong passphrase creates none, and deletes the
|
||||||
key again if encrypting with it or writing the unlocker then fails.
|
key again if encrypting with it or writing the unlocker then fails.
|
||||||
`macse.CreateKey` finds the new key's hash right after `sc_auth` creates it,
|
`macse.CreateKey` finds the new key's hash right after `sc_auth` creates
|
||||||
and fails with an error naming the key's label if it cannot; it deletes the
|
it, and fails with an error naming the key's label if it cannot; it deletes
|
||||||
key again if getting its public key then fails. The Objective-C was only read,
|
the key again if getting its public key then fails. The Objective-C was only
|
||||||
never compiled or run, and so was `macse_darwin.go`, which is cgo only.
|
read, never compiled or run, and so was `macse_darwin.go`, which is cgo only.
|
||||||
`CreateKeychainUnlocker` writes all of the unlocker's files, the metadata
|
`CreateKeychainUnlocker` writes all of the unlocker's files, the metadata
|
||||||
among them, before it stores the item in the keychain, and deletes the item
|
among them, before it stores the item in the keychain, and deletes the item
|
||||||
again if moving the unlocker into place then fails. A failure to delete is
|
again if moving the unlocker into place then fails. A failure to delete is
|
||||||
reported along with the first error. The tests of this run only on macOS: the
|
reported along with the first error. The tests of this run only on macOS:
|
||||||
Secure Enclave one in a build with cgo on a Mac with a Secure Enclave, the
|
the Secure Enclave one in a build with cgo on a Mac with a Secure Enclave,
|
||||||
keychain one in a build with cgo.
|
the keychain one in a build with cgo.
|
||||||
- 2026-10-04: What a command killed part-way left under a `.tmp-` name
|
- 2026-10-04: What a command killed part-way left under a `.tmp-` name
|
||||||
(https://git.eeqj.de/sneak/secret/issues/75), the temporary directories of
|
(https://git.eeqj.de/sneak/secret/issues/75), the temporary directories
|
||||||
`secret.TempDirFor` and the temporary files of `secret.WriteFileAtomic`,
|
of `secret.TempDirFor` and the temporary files of
|
||||||
encrypted keys included, is deleted by the next command that takes the state
|
`secret.WriteFileAtomic`, encrypted keys included, is deleted by the next
|
||||||
directory lock. Before, it stayed until deleted by hand. A command writes
|
command that takes the state directory lock. Before, it stayed until
|
||||||
`finished` into the lock file just before it releases the lock; the next one
|
deleted by hand. A command writes `finished` into the lock file just
|
||||||
to take the lock searches only when it does not find that, so after a command
|
before it releases the lock; the next one to take the lock searches only
|
||||||
that finished nothing is searched, however many secrets and versions there
|
when it does not find that, so after a command that finished nothing is
|
||||||
are. The search looks in the state directory, each vault, each secret and each
|
searched, however many secrets and versions there are. The search looks
|
||||||
version, the only directories those helpers make them in. A command that only
|
in the state directory, each vault, each secret and each version, the
|
||||||
reads takes no lock and deletes nothing. A failure to delete is warned about
|
only directories those helpers make them in. A command that only reads
|
||||||
|
takes no lock and deletes nothing. A failure to delete is warned about
|
||||||
and the command goes on. An unlocker directory with no metadata file was
|
and the command goes on. An unlocker directory with no metadata file was
|
||||||
already removed by `secret unlocker remove` given its directory name; a test
|
already removed by `secret unlocker remove` given its directory name; a
|
||||||
now shows it.
|
test now shows it.
|
||||||
- 2026-10-04: An age identity's private key goes into a locked buffer through
|
- 2026-10-04: An age identity's private key goes into a locked buffer
|
||||||
`secret.IdentityToLockedBuffer` everywhere
|
through `secret.IdentityToLockedBuffer` everywhere
|
||||||
(https://git.eeqj.de/sneak/secret/issues/38): the vault's long-term key when a
|
(https://git.eeqj.de/sneak/secret/issues/38): the vault's long-term key
|
||||||
passphrase, PGP, keychain or Secure Enclave unlocker is created, the new
|
when a passphrase, PGP, keychain or Secure Enclave unlocker is created,
|
||||||
unlocker's own key, a new secret version's key, and the key `secret encrypt`
|
the new unlocker's own key, a new secret version's key, and the key
|
||||||
generates. Before, each place converted the string age returns to bytes and
|
`secret encrypt` generates. Before, each place converted the string age
|
||||||
left the string in ordinary memory. The function moves the string's own bytes
|
returns to bytes and left the string in ordinary memory. The function
|
||||||
into the buffer, which overwrites them; the copies age makes while writing the
|
moves the string's own bytes into the buffer, which overwrites them; the
|
||||||
string remain, as its comment says. The 1.0 memory-security entry below no
|
copies age makes while writing the string remain, as its comment says.
|
||||||
longer lists these places, `internal/cli/crypto.go` among them, nor
|
The 1.0 memory-security entry below no longer lists these places,
|
||||||
`version.go:155`, which was `internal/secret/version.go`, not
|
`internal/cli/crypto.go` among them, nor `version.go:155`, which was
|
||||||
`internal/cli/version.go`.
|
`internal/secret/version.go`, not `internal/cli/version.go`.
|
||||||
- 2026-10-04: `script/lint-darwin` (`make lint-darwin`) runs `go vet` and
|
- 2026-10-04: `script/lint-darwin` (`make lint-darwin`) runs `go vet` and
|
||||||
`golangci-lint` in docker on the code as a macOS build compiles it
|
`golangci-lint` in docker on the code as a macOS build compiles it
|
||||||
(`GOOS=darwin`), with cgo off (https://git.eeqj.de/sneak/secret/issues/50).
|
(`GOOS=darwin`), with cgo off
|
||||||
`script/check` runs it, and the `Dockerfile` lint stage runs its commands, so
|
(https://git.eeqj.de/sneak/secret/issues/50). `script/check` runs it, and
|
||||||
`script/cibuild` does too. Before, CI on Linux never compiled the files built
|
the `Dockerfile` lint stage runs its commands, so `script/cibuild` does too.
|
||||||
only for macOS. Compiling cgo code for macOS needs Apple's SDK headers, and
|
Before, CI on Linux never compiled the files built only for macOS. Compiling
|
||||||
both `internal/macse` and `github.com/keybase/go-keychain` are cgo on macOS.
|
cgo code for macOS needs Apple's SDK headers, and both `internal/macse` and
|
||||||
So the three functions that call `go-keychain` moved from
|
`github.com/keybase/go-keychain` are cgo on macOS. So the three functions
|
||||||
`keychainunlocker.go` to `keychainunlocker_cgo.go`, built only with cgo on
|
that call `go-keychain` moved from `keychainunlocker.go` to
|
||||||
macOS like `macse_darwin.go`. A macOS build without cgo, which before did not
|
`keychainunlocker_cgo.go`, built only with cgo on macOS like
|
||||||
compile, gets `keychainunlocker_nocgo.go` and the `macse` stub instead, whose
|
`macse_darwin.go`. A macOS build without cgo, which before did not compile,
|
||||||
errors say the keychain or Secure Enclave needs a macOS build with cgo. The
|
gets `keychainunlocker_nocgo.go` and the `macse` stub instead, whose errors
|
||||||
check covers the rest of the keychain unlocker, the Secure Enclave unlocker
|
say the keychain or Secure Enclave needs a macOS build with cgo. The check
|
||||||
and the macOS-only tests other than `keychainunlocker_test.go`, whose lint
|
covers the rest of the keychain unlocker, the Secure Enclave unlocker and
|
||||||
|
the macOS-only tests other than `keychainunlocker_test.go`, whose lint
|
||||||
findings are fixed. For the length and complexity limits, parts of
|
findings are fixed. For the length and complexity limits, parts of
|
||||||
`GetIdentity`, `getLongTermPrivateKey` and `CreateKeychainUnlocker` moved into
|
`GetIdentity`, `getLongTermPrivateKey` and `CreateKeychainUnlocker` moved
|
||||||
functions of their own, and the Secure Enclave unlocker derives the long-term
|
into functions of their own, and the Secure Enclave unlocker derives the
|
||||||
key from the mnemonic through the same function as the keychain unlocker
|
long-term key from the mnemonic through the same function as the keychain
|
||||||
instead of a copy of it. Lines over 88 columns in the files the check cannot
|
unlocker instead of a copy of it. Lines over 88 columns in the files the
|
||||||
see are wrapped.
|
check cannot see are wrapped.
|
||||||
- 2026-10-04: `secret rm`, `secret version rm`, `secret vault remove` and
|
- 2026-10-04: `secret rm`, `secret version rm`, `secret vault remove` and
|
||||||
`secret unlocker remove` ask `[y/N]` before removing anything
|
`secret unlocker remove` ask `[y/N]` before removing anything
|
||||||
(https://git.eeqj.de/sneak/secret/issues/39), naming what they remove: the
|
(https://git.eeqj.de/sneak/secret/issues/39), naming what they remove: the
|
||||||
secret, its vault and its version count; the version, secret and vault; the
|
secret, its vault and its version count; the version, secret and vault; the
|
||||||
vault and its secret count; the unlocker, its vault and whether it is the
|
vault and its secret count; the unlocker, its vault and whether it is the
|
||||||
last, and for the last the vault's secret count and that the vault then opens
|
last, and for the last the vault's secret count and that the vault then
|
||||||
only with its mnemonic. Only `y` or `yes` goes ahead. Without `--force`, a
|
opens only with its mnemonic. Only `y` or `yes` goes ahead. Without
|
||||||
command whose stdin is not a terminal fails at once. `--force` (now also on
|
`--force`, a command whose stdin is not a terminal fails at once. `--force`
|
||||||
`rm` and `version rm`) removes without asking; it replaces the old refusals to
|
(now also on `rm` and `version rm`) removes without asking; it replaces the
|
||||||
remove a vault with secrets or the last unlocker of one without `--force`,
|
old refusals to remove a vault with secrets or the last unlocker of one
|
||||||
which the question now covers. The checks run, and the question is asked,
|
without `--force`, which the question now covers. The checks run, and the
|
||||||
before the state directory lock is taken; under the lock the checks run again,
|
question is asked, before the state directory lock is taken; under the
|
||||||
and if they would ask a different question, nothing is removed. `secret rm`
|
lock the checks run again, and if they would ask a different question,
|
||||||
fails when it cannot count the versions.
|
nothing is removed. `secret rm` fails when it cannot count the versions.
|
||||||
- 2026-10-04: A crash while an unlocker is being replaced no longer leaves a
|
- 2026-10-04: A crash while an unlocker is being replaced no longer leaves a
|
||||||
current unlocker that cannot open the vault
|
current unlocker that cannot open the vault
|
||||||
(https://git.eeqj.de/sneak/secret/issues/71). Every new unlocker gets a
|
(https://git.eeqj.de/sneak/secret/issues/71). Every new unlocker gets a
|
||||||
directory of its own, named with the time to the nanosecond:
|
directory of its own, named with the time to the nanosecond:
|
||||||
`passphrase-<time>`, `<host>-pgp-<time>`, and for a keychain or Secure Enclave
|
`passphrase-<time>`, `<host>-pgp-<time>`, and for a keychain or Secure
|
||||||
unlocker the keychain item or Secure Enclave key, which names the directory,
|
Enclave unlocker the keychain item or Secure Enclave key, which names the
|
||||||
carries the time instead of the day. `secret.WriteDir` fails on a directory
|
directory, carries the time instead of the day. `secret.WriteDir` fails on a
|
||||||
that exists instead of writing into it. `unlocker add passphrase` writes the
|
directory that exists instead of writing into it. `unlocker add passphrase`
|
||||||
new unlocker, makes it current, and only then removes the vault's other
|
writes the new unlocker, makes it current, and only then removes the vault's
|
||||||
passphrase unlockers; a crash between the last two steps leaves the old one
|
other passphrase unlockers; a crash between the last two steps leaves the old
|
||||||
beside the new, and the old passphrase still opens the vault through it until
|
one beside the new, and the old passphrase still opens the vault through it
|
||||||
the next `unlocker add passphrase` or an `unlocker remove` removes it. A PGP,
|
until the next `unlocker add passphrase` or an `unlocker remove` removes it.
|
||||||
keychain or Secure Enclave unlocker added on the same host and day as another
|
A PGP, keychain or Secure Enclave unlocker added on the same host and day as
|
||||||
of its type is added beside it instead of replacing it.
|
another of its type is added beside it instead of replacing it.
|
||||||
- 2026-10-04: `SB_SECRET_MNEMONIC` and `SB_UNLOCK_PASSPHRASE` are read once per
|
- 2026-10-04: `SB_SECRET_MNEMONIC` and `SB_UNLOCK_PASSPHRASE` are read once
|
||||||
command, in its `RunE`, into locked buffers on the CLI `Instance`, and unset
|
per command, in its `RunE`, into locked buffers on the CLI `Instance`, and
|
||||||
at once, so that no program the command runs, `gpg` included, inherits them
|
unset at once, so that no program the command runs, `gpg` included,
|
||||||
(https://git.eeqj.de/sneak/secret/issues/60). Nothing below the command reads
|
inherits them (https://git.eeqj.de/sneak/secret/issues/60). Nothing below
|
||||||
the environment; the buffers are passed down: `vault.CreateVault` takes the
|
the command reads the environment; the buffers are passed down:
|
||||||
mnemonic (nil for none), a `Vault` derives its long-term key from its
|
`vault.CreateVault` takes the mnemonic (nil for none), a `Vault` derives its
|
||||||
`Mnemonic` and gives its `UnlockPassphrase` to a passphrase unlocker, and the
|
long-term key from its `Mnemonic` and gives its `UnlockPassphrase` to a
|
||||||
PGP, keychain and Secure Enclave unlocker constructors take both.
|
passphrase unlocker, and the PGP, keychain and Secure Enclave unlocker
|
||||||
`CreatePGPUnlocker` sets both on the vault it loads, through `SetMnemonic` and
|
constructors take both. `CreatePGPUnlocker` sets both on the vault it
|
||||||
`SetUnlockPassphrase`, now part of `VaultInterface`, before calling its
|
loads, through `SetMnemonic` and `SetUnlockPassphrase`, now part of
|
||||||
`GetOrDeriveLongTermKey`. `init` and `vault create` no longer put the mnemonic
|
`VaultInterface`, before calling its `GetOrDeriveLongTermKey`. `init` and
|
||||||
into the environment. Unsetting erases nothing: the starting environment
|
`vault create` no longer put the mnemonic into the environment. Unsetting
|
||||||
(`/proc/<pid>/environ`) and memory still hold the value. The README warns
|
erases nothing: the starting environment (`/proc/<pid>/environ`) and
|
||||||
against both variables.
|
memory still hold the value. The README warns against both variables.
|
||||||
- 2026-10-04: `.golangci.yml` is again the canonical file from `sneak/prompts`,
|
- 2026-10-04: `.golangci.yml` is again the canonical file from
|
||||||
byte for byte (https://git.eeqj.de/sneak/secret/issues/66). It runs
|
`sneak/prompts`, byte for byte
|
||||||
`gomodguard_v2` in place of the deprecated `gomodguard`, so the lint no longer
|
(https://git.eeqj.de/sneak/secret/issues/66). It runs `gomodguard_v2`
|
||||||
warns, and enables `depguard` with a rule that keeps `net/http/httptest` out
|
in place of the deprecated `gomodguard`, so the lint no longer warns,
|
||||||
of non-test files. Neither raised a finding in this repo.
|
and enables `depguard` with a rule that keeps `net/http/httptest` out of
|
||||||
|
non-test files. Neither raised a finding in this repo.
|
||||||
- 2026-10-04: `secret unlocker add pgp` works on Linux
|
- 2026-10-04: `secret unlocker add pgp` works on Linux
|
||||||
(https://git.eeqj.de/sneak/secret/issues/88). `CreatePGPUnlocker` gets the
|
(https://git.eeqj.de/sneak/secret/issues/88). `CreatePGPUnlocker` gets
|
||||||
vault's long-term key as adding a passphrase unlocker does, with the vault's
|
the vault's long-term key as adding a passphrase unlocker does, with the
|
||||||
`GetOrDeriveLongTermKey`, now part of `VaultInterface`: from the mnemonic,
|
vault's `GetOrDeriveLongTermKey`, now part of `VaultInterface`: from the
|
||||||
checked against the vault, or else from the current unlocker. Before, it used
|
mnemonic, checked against the vault, or else from the current unlocker.
|
||||||
the keychain unlocker's helper, which on every platform but macOS always
|
Before, it used the keychain unlocker's helper, which on every platform
|
||||||
failed. A test adds a PGP unlocker for a throwaway GPG key, getting the
|
but macOS always failed. A test adds a PGP unlocker for a throwaway GPG
|
||||||
long-term key once from the mnemonic and once from a passphrase unlocker, and
|
key, getting the long-term key once from the mnemonic and once from a
|
||||||
reads a secret through the new unlocker.
|
passphrase unlocker, and reads a secret through the new unlocker.
|
||||||
- 2026-10-04: A vault name may use only lowercase ASCII letters, digits, `.`,
|
- 2026-10-04: A vault name may use only lowercase ASCII letters, digits,
|
||||||
`-` and `_`, and must not be empty, `.` or `..`
|
`.`, `-` and `_`, and must not be empty, `.` or `..`
|
||||||
(https://git.eeqj.de/sneak/secret/issues/68); the error and `README.md` state
|
(https://git.eeqj.de/sneak/secret/issues/68); the error and `README.md`
|
||||||
the rule. `vault create`, `vault import`, `vault select`, `vault remove`, both
|
state the rule. `vault create`, `vault import`, `vault select`,
|
||||||
vault names of `mv` and shell completion of a `vault:secret` argument check
|
`vault remove`, both vault names of `mv` and shell completion of a
|
||||||
the name as typed with `vault.ValidateVaultName` before building any path from
|
`vault:secret` argument check the name as typed with
|
||||||
it. Before, `vault import ..` wrote a long-term key and an unlocker into the
|
`vault.ValidateVaultName` before building any path from it. Before,
|
||||||
state directory itself, and `vault select ..` made that the current vault.
|
`vault import ..` wrote a long-term key and an unlocker into the state
|
||||||
- 2026-10-04: `script/cibuild` runs the checks again on an unchanged tree
|
directory itself, and `vault select ..` made that the current vault.
|
||||||
(https://git.eeqj.de/sneak/secret/issues/54). It passes the current time as
|
- 2026-10-04: `script/cibuild` runs the checks again on an unchanged
|
||||||
the `CHECK_EPOCH` build argument, which both the lint and the build stage of
|
tree (https://git.eeqj.de/sneak/secret/issues/54). It passes the
|
||||||
the `Dockerfile` declare after their module download, so the `RUN` steps below
|
current time as the `CHECK_EPOCH` build argument, which both the lint
|
||||||
the argument run again on each build while the base images and module
|
and the build stage of the `Dockerfile` declare after their module
|
||||||
downloads stay cached. Before, a second run on the same tree took every check
|
download, so the `RUN` steps below the argument run again on each
|
||||||
from the build cache and reported success having run nothing.
|
build while the base images and module downloads stay cached. Before,
|
||||||
|
a second run on the same tree took every check from the build cache
|
||||||
|
and reported success having run nothing.
|
||||||
- 2026-10-04: A failed unlocker add no longer leaves a partial unlocker
|
- 2026-10-04: A failed unlocker add no longer leaves a partial unlocker
|
||||||
directory (https://git.eeqj.de/sneak/secret/issues/48).
|
directory (https://git.eeqj.de/sneak/secret/issues/48).
|
||||||
`secret unlocker add pgp` resolves the GPG key's fingerprint once, for its
|
`secret unlocker add pgp` resolves the GPG key's fingerprint once, for
|
||||||
duplicate check, and passes it to `CreatePGPUnlocker` to record.
|
its duplicate check, and passes it to `CreatePGPUnlocker` to record.
|
||||||
`CreatePGPUnlocker` and `CreateKeychainUnlocker` get the long-term key and
|
`CreatePGPUnlocker` and `CreateKeychainUnlocker` get the long-term key
|
||||||
encrypt everything before writing anything. All four unlocker types write
|
and encrypt everything before writing anything. All four unlocker
|
||||||
their files through `secret.WriteDir`: a new unlocker is built in a temporary
|
types write their files through `secret.WriteDir`: a new unlocker is
|
||||||
directory, renamed into place when complete and removed on a failure.
|
built in a temporary directory, renamed into place when complete and
|
||||||
- 2026-10-04: `secret unlocker select` and `secret unlocker remove` skip, with
|
removed on a failure.
|
||||||
the warning `unlocker list` gives, an unlocker directory whose metadata file
|
- 2026-10-04: `secret unlocker select` and `secret unlocker remove`
|
||||||
cannot be checked for, read or parsed, instead of failing when it sorts before
|
skip, with the warning `unlocker list` gives, an unlocker directory
|
||||||
the unlocker asked for. Such a directory, or one without a metadata file, is
|
whose metadata file cannot be checked for, read or parsed, instead of
|
||||||
removed by its directory name, the name the warning gives; only the directory
|
failing when it sorts before the unlocker asked for. Such a directory,
|
||||||
is removed, since its type is unknown. Removing one whose metadata file is
|
or one without a metadata file, is removed by its directory name, the
|
||||||
missing or corrupt never counts as removing the last unlocker. Removing one
|
name the warning gives; only the directory is removed, since its type
|
||||||
whose metadata file cannot be checked for or read always does, since it may be
|
is unknown. Removing one whose metadata file is missing or corrupt
|
||||||
the only working unlocker, so in a vault with secrets it needs `--force`.
|
never counts as removing the last unlocker. Removing one whose metadata
|
||||||
- 2026-10-04: A failed command prints its error once, without the usage text
|
file cannot be checked for or read always does, since it may be the
|
||||||
after it (https://git.eeqj.de/sneak/secret/issues/41). Usage is still printed
|
only working unlocker, so in a vault with secrets it needs `--force`.
|
||||||
for a command called wrongly: wrong number of arguments, unknown flag, bad
|
- 2026-10-04: A failed command prints its error once, without the usage
|
||||||
flag value, missing required flag, or flags that break a flag group (mutually
|
text after it (https://git.eeqj.de/sneak/secret/issues/41). Usage is
|
||||||
exclusive, required together, one required). The root command's
|
still printed for a command called wrongly: wrong number of arguments,
|
||||||
`PersistentPreRunE` turns usage off. Cobra checks arguments and flag values
|
unknown flag, bad flag value, missing required flag, or flags that
|
||||||
before that hook but required flags and flag groups only after it, so the hook
|
break a flag group (mutually exclusive, required together, one
|
||||||
checks those two first. Root `SilenceUsage` would have hidden usage for all of
|
required). The root command's `PersistentPreRunE` turns usage off.
|
||||||
these.
|
Cobra checks arguments and flag values before that hook but required
|
||||||
- 2026-10-04: `secret get` keeps the secret in locked memory until it writes it
|
flags and flag groups only after it, so the hook checks those two
|
||||||
out (https://git.eeqj.de/sneak/secret/issues/37): `Vault.GetSecret` and
|
first. Root `SilenceUsage` would have hidden usage for all of these.
|
||||||
`Vault.GetSecretVersion` return a `*memguard.LockedBuffer`, which every caller
|
- 2026-10-04: `secret get` keeps the secret in locked memory until it
|
||||||
destroys, and `secret get` writes its bytes straight to stdout, still with no
|
writes it out (https://git.eeqj.de/sneak/secret/issues/37):
|
||||||
trailing newline. Before, the value was copied into ordinary memory that
|
`Vault.GetSecret` and `Vault.GetSecretVersion` return a
|
||||||
nothing wiped, and `get --version` also wrote it to the debug log.
|
`*memguard.LockedBuffer`, which every caller destroys, and `secret get`
|
||||||
- 2026-10-04: The `Makefile` no longer sets `DOCKER_HOST`, so its docker targets
|
writes its bytes straight to stdout, still with no trailing newline.
|
||||||
use the local docker daemon, or whatever `DOCKER_HOST` the environment sets.
|
Before, the value was copied into ordinary memory that nothing wiped,
|
||||||
`make build` calls the new `script/build`, which stamps the version (`VERSION`
|
and `get --version` also wrote it to the debug log.
|
||||||
from the environment, else `git describe`) and the git commit as before.
|
- 2026-10-04: The `Makefile` no longer sets `DOCKER_HOST`, so its docker
|
||||||
`build`, `clean`, `install` and `docker-run` are in `.PHONY`; `make install`
|
targets use the local docker daemon, or whatever `DOCKER_HOST` the
|
||||||
depends on `build`. The `vet` target is gone: `script/test` runs `go vet`
|
environment sets. `make build` calls the new `script/build`, which
|
||||||
first.
|
stamps the version (`VERSION` from the environment, else
|
||||||
- 2026-10-04: `.gitignore` is the org's standard file, which ignores `.env`,
|
`git describe`) and the git commit as before. `build`, `clean`,
|
||||||
`.env.*`, `*.pem` and `*.key` and editor and OS files, plus this repo's
|
`install` and `docker-run` are in `.PHONY`; `make install` depends on
|
||||||
`/secret`, `*.log`, `*.test` and `settings.local.json`
|
`build`. The `vet` target is gone: `script/test` runs `go vet` first.
|
||||||
(https://git.eeqj.de/sneak/secret/issues/40). `.dockerignore` also leaves out
|
- 2026-10-04: `.gitignore` is the org's standard file, which ignores
|
||||||
`node_modules`; `.git` stays in the build context for the version stamp.
|
`.env`, `.env.*`, `*.pem` and `*.key` and editor and OS files, plus
|
||||||
|
this repo's `/secret`, `*.log`, `*.test` and `settings.local.json`
|
||||||
|
(https://git.eeqj.de/sneak/secret/issues/40). `.dockerignore` also
|
||||||
|
leaves out `node_modules`; `.git` stays in the build context for the
|
||||||
|
version stamp.
|
||||||
- 2026-10-04: `secret init` refuses when the default vault exists, and
|
- 2026-10-04: `secret init` refuses when the default vault exists, and
|
||||||
`secret vault create NAME` when `NAME` does, with "vault NAME already exists",
|
`secret vault create NAME` when `NAME` does, with "vault NAME already
|
||||||
before writing anything. The check is in `vault.CreateVault`, which both
|
exists", before writing anything. The check is in `vault.CreateVault`,
|
||||||
commands call while holding the state directory lock, so two creates of one
|
which both commands call while holding the state directory lock, so two
|
||||||
vault at once cannot both pass the check. Before, either command replaced the
|
creates of one vault at once cannot both pass the check. Before, either
|
||||||
vault's metadata, passphrase unlocker and `longterm.age`, so none of its
|
command replaced the vault's metadata, passphrase unlocker and
|
||||||
secrets could be decrypted any more. Both commands now ask for the unlocker
|
`longterm.age`, so none of its secrets could be decrypted any more. Both
|
||||||
passphrase before creating the vault, so one stopped at that prompt leaves no
|
commands now ask for the unlocker passphrase before creating the vault,
|
||||||
vault behind.
|
so one stopped at that prompt leaves no vault behind.
|
||||||
- 2026-10-04: The `internal/cli` tests are back to about their time before the
|
- 2026-10-04: The `internal/cli` tests are back to about their time
|
||||||
state directory lock (https://git.eeqj.de/sneak/secret/issues/80). The test
|
before the state directory lock
|
||||||
that each changing command waits for the lock releases it as soon as it sees
|
(https://git.eeqj.de/sneak/secret/issues/80). The test that each
|
||||||
the command waiting there, instead of after a fixed 100 ms. The two vaults
|
changing command waits for the lock releases it as soon as it sees the
|
||||||
with passphrase unlockers that the path and move tests start from are made
|
command waiting there, instead of after a fixed 100 ms. The two vaults
|
||||||
once and copied for each test.
|
with passphrase unlockers that the path and move tests start from are
|
||||||
- 2026-10-04: `secret mv` rejects a move whose destination is the source under
|
made once and copied for each test.
|
||||||
another name, such as `foo` for `Foo` on a case-insensitive filesystem (the
|
- 2026-10-04: `secret mv` rejects a move whose destination is the source
|
||||||
macOS default) or a name reached through a symbolic link, before changing
|
under another name, such as `foo` for `Foo` on a case-insensitive
|
||||||
anything, with or without `--force`, within a vault and between vaults;
|
filesystem (the macOS default) or a name reached through a symbolic
|
||||||
before, `--force` removed the destination and so deleted the secret. A rename
|
link, before changing anything, with or without `--force`, within a
|
||||||
that changes only letter case works on a case-sensitive filesystem as before.
|
vault and between vaults; before, `--force` removed the destination and
|
||||||
- 2026-10-04: Lint runs only in docker: `script/lint` builds `Dockerfile.lint`,
|
so deleted the secret. A rename that changes only letter case works on a
|
||||||
where golangci-lint is a build step rebuilt on every run
|
case-sensitive filesystem as before.
|
||||||
(`--no-cache-filter`), so an unchanged tree is linted too; the module download
|
- 2026-10-04: Lint runs only in docker: `script/lint` builds
|
||||||
stays cached. `script/bootstrap` no longer installs golangci-lint, and the
|
`Dockerfile.lint`, where golangci-lint is a build step rebuilt on
|
||||||
`Dockerfile` lint stage calls it directly instead of `make lint`.
|
every run (`--no-cache-filter`), so an unchanged tree is linted too;
|
||||||
`golangci-lint config verify` is not run: it fetches its schema live over
|
the module download stays cached. `script/bootstrap` no longer
|
||||||
unpinned HTTPS.
|
installs golangci-lint, and the `Dockerfile` lint stage calls it
|
||||||
- 2026-10-04: A PGP unlocker whose metadata has no usable GPG key ID no longer
|
directly instead of `make lint`. `golangci-lint config verify` is not
|
||||||
panics: `GetID()` warns with the unlocker's directory and returns
|
run: it fetches its schema live over unpinned HTTPS.
|
||||||
`pgp-unknown`. `ListUnlockers` skips, with a warning, an unlocker whose
|
- 2026-10-04: A PGP unlocker whose metadata has no usable GPG key ID
|
||||||
metadata file cannot be checked for, read or parsed instead of failing, so
|
no longer panics: `GetID()` warns with the unlocker's directory and
|
||||||
`secret unlocker list` still lists the others; the listing's ID lookup no
|
returns `pgp-unknown`. `ListUnlockers` skips, with a warning, an
|
||||||
longer warns about that directory again.
|
unlocker whose metadata file cannot be checked for, read or parsed
|
||||||
- 2026-10-03: `secret mv` rejects a move whose destination is the source
|
instead of failing, so `secret unlocker list` still lists the others;
|
||||||
(`mv --force x x`, `mv --force work:x work:`, or an empty destination, which
|
the listing's ID lookup no longer warns about that directory again.
|
||||||
defaults to the source name) before changing anything; before, `--force`
|
- 2026-10-03: `secret mv` rejects a move whose destination is the
|
||||||
removed the destination first and so deleted the secret. Every vault name
|
source (`mv --force x x`, `mv --force work:x work:`, or an empty
|
||||||
given with `vault:` must be one of the existing vaults by exact name, so
|
destination, which defaults to the source name) before changing
|
||||||
`work:x work/:x` is rejected instead of being taken for a move between two
|
anything; before, `--force` removed the destination first and so
|
||||||
vaults. A move within a named vault no longer makes that vault the current
|
deleted the secret. Every vault name given with `vault:` must be one
|
||||||
one, whether it succeeds or fails.
|
of the existing vaults by exact name, so `work:x work/:x` is rejected
|
||||||
- 2026-10-03: Commands that change the state directory hold one lock (`flock` on
|
instead of being taken for a move between two vaults. A move within a
|
||||||
`lock` in the state directory; a mutex on the in-memory test filesystem), so
|
named vault no longer makes that vault the current one, whether it
|
||||||
concurrent commands no longer lose versions or race on the current pointers.
|
succeeds or fails.
|
||||||
Every file is written through `secret.WriteFileAtomic` (temporary file, sync,
|
- 2026-10-03: Commands that change the state directory hold one lock
|
||||||
rename), so no file is ever half-written and `current`, `currentvault` and
|
(`flock` on `lock` in the state directory; a mutex on the in-memory
|
||||||
`current-unlocker` never go missing. New versions, new secrets and cross-vault
|
test filesystem), so concurrent commands no longer lose versions or
|
||||||
copies are built in a temporary directory and renamed into place, and removals
|
race on the current pointers. Every file is written through
|
||||||
rename out of the way first, so a version or secret is never half-added and
|
`secret.WriteFileAtomic` (temporary file, sync, rename), so no file
|
||||||
never half-removed.
|
is ever half-written and `current`, `currentvault` and
|
||||||
- 2026-10-03: The checks run before changing a vault now stop with an error
|
`current-unlocker` never go missing. New versions, new secrets and
|
||||||
naming the path and cause when they cannot read what they inspect, instead of
|
cross-vault copies are built in a temporary directory and renamed
|
||||||
reading the failure as "nothing there": the duplicate check before
|
into place, and removals rename out of the way first, so a version
|
||||||
`unlocker add pgp` (an unreadable `unlockers.d` or unlocker metadata file),
|
or secret is never half-added and never half-removed. An
|
||||||
the secret count that guards removing the last unlocker and removing a vault,
|
interrupted command can still leave, from `init` or `vault create`
|
||||||
and the existing long-term key check before `vault import`.
|
killed after the passphrase prompt but before the unlocker is
|
||||||
- 2026-10-03: `version rm`, `version promote` and `get --version` accept a
|
written, a vault with no unlocker, which `vault create` has already
|
||||||
version only if it is one of the versions `version list` lists for that
|
made the current vault.
|
||||||
secret, compared as typed before any path is built (`secret.VersionExists`),
|
- 2026-10-03: The checks run before changing a vault now stop with an
|
||||||
and touch nothing otherwise. An empty `--version` is rejected instead of
|
error naming the path and cause when they cannot read what they
|
||||||
meaning the current version. Before, `secret version rm x ../../..` deleted
|
inspect, instead of reading the failure as "nothing there": the
|
||||||
the whole vault, `secret version rm x ..` the secret, and `.` or `""` every
|
duplicate check before `unlocker add pgp` (an unreadable
|
||||||
version.
|
`unlockers.d` or unlocker metadata file), the secret count that
|
||||||
- 2026-10-03: Key material is wiped on every exit: `Entry()` returns the exit
|
guards removing the last unlocker and removing a vault, and the
|
||||||
code after its deferred `memguard.Purge()` has run, and only `main` calls
|
existing long-term key check before `vault import`.
|
||||||
`os.Exit`. SIGINT and SIGTERM go through memguard's handler, which wipes every
|
- 2026-10-03: `version rm`, `version promote` and `get --version`
|
||||||
buffer before exiting; when the process is in the terminal's foreground
|
accept a version only if it is one of the versions `version list`
|
||||||
process group it first restores the terminal settings from startup, so an
|
lists for that secret, compared as typed before any path is built
|
||||||
interrupted passphrase prompt no longer leaves echo off.
|
(`secret.VersionExists`), and touch nothing otherwise. An empty
|
||||||
- 2026-10-03: Every command that builds a path from a secret name checks the
|
`--version` is rejected instead of meaning the current version.
|
||||||
name first with `vault.ValidateSecretName` and touches nothing when it is
|
Before, `secret version rm x ../../..` deleted the whole vault,
|
||||||
invalid: `rm`, `mv` (both names, within a vault and between vaults, before
|
`secret version rm x ..` the secret, and `.` or `""` every version.
|
||||||
switching the current vault), `import`, `version list`/`promote`/`rm`,
|
- 2026-10-03: Key material is wiped on every exit: `Entry()` returns
|
||||||
`encrypt` and `decrypt`. The error and `README.md` state the naming rule.
|
the exit code after its deferred `memguard.Purge()` has run, and only
|
||||||
Before, `secret rm ..` deleted the whole vault and `secret rm .` every secret
|
`main` calls `os.Exit`. SIGINT and SIGTERM go through memguard's
|
||||||
in it.
|
handler, which wipes every buffer before exiting; when the process is
|
||||||
- 2026-10-03: The keychain unlocker's age key passphrase stays in locked memory:
|
in the terminal's foreground process group it first restores the
|
||||||
it is generated into a locked buffer, and the keychain JSON is written and
|
terminal settings from startup, so an interrupted passphrase prompt no
|
||||||
read by `KeychainData` code in `internal/secret/keychaindata.go` (tested on
|
longer leaves echo off.
|
||||||
Linux) without `encoding/json` holding it; the JSON field names are unchanged.
|
- 2026-10-03: Every command that builds a path from a secret name
|
||||||
- 2026-10-02: A plain `docker build .` builds again: the size tests skip a case
|
checks the name first with `vault.ValidateSecretName` and touches
|
||||||
that needs more locked memory than the process can lock, and run every case
|
nothing when it is invalid: `rm`, `mv` (both names, within a vault
|
||||||
under `script/cibuild`. The image stamps the `VERSION` build argument, else
|
and between vaults, before switching the current vault), `import`,
|
||||||
`git describe --tags --always`, into `Version`, and fails if `.git` is present
|
`version list`/`promote`/`rm`, `encrypt` and `decrypt`. The error
|
||||||
but yields no version; `make build` stamps `git describe` too, not a fixed
|
and `README.md` state the naming rule. Before, `secret rm ..`
|
||||||
`0.1.0`. `.dockerignore` keeps `.git/config` out; `script/docker` is the
|
deleted the whole vault and `secret rm .` every secret in it.
|
||||||
|
- 2026-10-03: The keychain unlocker's age key passphrase stays in
|
||||||
|
locked memory: it is generated into a locked buffer, and the
|
||||||
|
keychain JSON is written and read by `KeychainData` code in
|
||||||
|
`internal/secret/keychaindata.go` (tested on Linux) without
|
||||||
|
`encoding/json` holding it; the JSON field names are unchanged.
|
||||||
|
- 2026-10-02: A plain `docker build .` builds again: the size tests
|
||||||
|
skip a case that needs more locked memory than the process can
|
||||||
|
lock, and run every case under `script/cibuild`. The image stamps the
|
||||||
|
`VERSION` build argument, else `git describe --tags --always`, into
|
||||||
|
`Version`, and fails if `.git` is present but yields no version;
|
||||||
|
`make build` stamps `git describe` too, not a fixed `0.1.0`.
|
||||||
|
`.dockerignore` keeps `.git/config` out; `script/docker` is the
|
||||||
canonical copy.
|
canonical copy.
|
||||||
- 2026-08-07: Updated golangci-lint to v2.12.2 with the canonical
|
- 2026-08-07: Updated golangci-lint to v2.12.2 with the canonical
|
||||||
`.golangci.yml` (all linters enabled minus the standard disable list, `lll`
|
`.golangci.yml` (all linters enabled minus the standard disable
|
||||||
88, tests linted); bumped the `Dockerfile` lint-stage image to the tagged
|
list, `lll` 88, tests linted); bumped the `Dockerfile` lint-stage
|
||||||
v2.12.2 Debian digest; fixed all ~1550 new findings across `internal/` and
|
image to the tagged v2.12.2 Debian digest; fixed all ~1550 new
|
||||||
`pkg/` (line wrapping, `wsl_v5` blank lines, sentinel errors for `err113`,
|
findings across `internal/` and `pkg/` (line wrapping, `wsl_v5`
|
||||||
`t.Parallel()` where safe, `_test` package conversions, complexity/`dupl`
|
blank lines, sentinel errors for `err113`, `t.Parallel()` where
|
||||||
helper extraction) on branch `golangci-v2.12.2`. Reworked after review: the
|
safe, `_test` package conversions, complexity/`dupl` helper
|
||||||
`err113` sentinels in `internal/vault`, `internal/secret`, `internal/cli` and
|
extraction) on branch `golangci-v2.12.2`. Reworked after review:
|
||||||
`pkg/bip85` were reshaped so every composed error message is byte-identical to
|
the `err113` sentinels in `internal/vault`, `internal/secret`,
|
||||||
`main`, and `findUnlockerIDByMetadata` now returns an error so `unlocker list`
|
`internal/cli` and `pkg/bip85` were reshaped so every composed
|
||||||
skips an unreadable `unlockers.d` entry with a warning instead of emitting a
|
error message is byte-identical to `main`, and
|
||||||
fabricated fallback ID.
|
`findUnlockerIDByMetadata` now returns an error so `unlocker list`
|
||||||
|
skips an unreadable `unlockers.d` entry with a warning instead of
|
||||||
|
emitting a fabricated fallback ID.
|
||||||
- 2026-08-07: Added `.editorconfig`
|
- 2026-08-07: Added `.editorconfig`
|
||||||
(https://git.eeqj.de/sneak/secret/issues/27).
|
(https://git.eeqj.de/sneak/secret/issues/27).
|
||||||
- 2026-07-07 Adopted scripts-to-rule-them-all: `script/` entrypoints, Makefile
|
- 2026-07-07 Adopted scripts-to-rule-them-all: `script/` entrypoints,
|
||||||
shims, README Entrypoints section
|
Makefile shims, README Entrypoints section
|
||||||
- 2026-07-07: Added `REPO_POLICIES.md` and the `make hooks` target;
|
- 2026-07-07: Added `REPO_POLICIES.md` and the `make hooks` target;
|
||||||
`.gitea/workflows/check.yml` now runs `script/cibuild`.
|
`.gitea/workflows/check.yml` now runs `script/cibuild`.
|
||||||
- 2026-03-30: Added the `make fmt-check` target and
|
- 2026-03-30: Added the `make fmt-check` target and
|
||||||
`.gitea/workflows/check.yml`, which runs `docker build` on every push; the
|
`.gitea/workflows/check.yml`, which runs `docker build` on every push; the
|
||||||
`Dockerfile` base images are pinned by sha256.
|
`Dockerfile` base images are pinned by sha256.
|
||||||
- 2026-03-11: Secure Enclave unlocker for hardware-backed secret protection,
|
- 2026-03-11: Secure Enclave unlocker for hardware-backed secret
|
||||||
plus review fixes (stub panics, derivation index, tests, README) on branch
|
protection, plus review fixes (stub panics, derivation index, tests,
|
||||||
secure-enclave-unlocker.
|
README) on branch secure-enclave-unlocker.
|
||||||
- 2026-02-28: Repo cleanup, removed stale .cursorrules and coverage.out.
|
- 2026-02-28: Repo cleanup, removed stale .cursorrules and coverage.out.
|
||||||
- Audit fix wave (issues #1, #2, #3, #13, #14): skip unlockers with missing
|
- Audit fix wave (issues #1, #2, #3, #13, #14): skip unlockers with
|
||||||
metadata, allow uppercase secret names, fix hardcoded derivation index,
|
missing metadata, allow uppercase secret names, fix hardcoded
|
||||||
validate names in GetSecretVersion against path traversal, return errors
|
derivation index, validate names in GetSecretVersion against path
|
||||||
instead of panicking, add Warn() on silent anomalies.
|
traversal, return errors instead of panicking, add Warn() on silent
|
||||||
- Memory security hardening: LockedBuffer used through encrypt/decrypt paths
|
anomalies.
|
||||||
(Save/EncryptWithPassphrase/GetValue/gpg helpers), deprecated bare-[]byte APIs
|
- Memory security hardening: LockedBuffer used through encrypt/decrypt
|
||||||
removed.
|
paths (Save/EncryptWithPassphrase/GetValue/gpg helpers), deprecated
|
||||||
- Per-secret keypair architecture, vault package refactor, versioning with
|
bare-[]byte APIs removed.
|
||||||
--version, comprehensive test suite with in-memory filesystem.
|
- Per-secret keypair architecture, vault package refactor, versioning
|
||||||
|
with --version, comprehensive test suite with in-memory filesystem.
|
||||||
- Debug logging system (slog, GODEBUG flag, TTY-aware output).
|
- Debug logging system (slog, GODEBUG flag, TTY-aware output).
|
||||||
- Renamed SEP unlocker to Keychain, reorganized import commands.
|
- Renamed SEP unlocker to Keychain, reorganized import commands.
|
||||||
- 2025-05-28: Initial implementation (vault, age encryption, mnemonic, CLI).
|
- 2025-05-28: Initial implementation (vault, age encryption, mnemonic,
|
||||||
|
CLI).
|
||||||
|
|
||||||
# Future Steps
|
# Future Steps
|
||||||
|
|
||||||
- Implement version-number shell completion for the second arg of
|
- Implement version-number shell completion for the second arg of
|
||||||
`secret version promote` and `secret version rm` (`internal/cli/version.go`;
|
`secret version promote` and `secret version rm`
|
||||||
was an in-code TODO removed for godox).
|
(`internal/cli/version.go`; was an in-code TODO removed for godox).
|
||||||
- Cover mnemonic-vs-xprv identity consistency in `pkg/agehd/agehd_test.go`
|
- Cover mnemonic-vs-xprv identity consistency in
|
||||||
`TestMnemonicVsXPRVConsistency` (was an in-code FIXME removed for godox).
|
`pkg/agehd/agehd_test.go` `TestMnemonicVsXPRVConsistency` (was an
|
||||||
- CI does not compile, lint or test the files built only with cgo on macOS,
|
in-code FIXME removed for godox).
|
||||||
since compiling them needs Apple's SDK:
|
- CI does not compile, lint or test the files built only with cgo on
|
||||||
|
macOS, since compiling them needs Apple's SDK:
|
||||||
`internal/secret/keychainunlocker_cgo.go` (the three functions that call
|
`internal/secret/keychainunlocker_cgo.go` (the three functions that call
|
||||||
`go-keychain`) with `keychainunlocker_test.go`, and `internal/macse`
|
`go-keychain`) with `keychainunlocker_test.go`, and `internal/macse`
|
||||||
(`macse_darwin.go`, `macse_test.go`, the Objective-C sources). Lint has never
|
(`macse_darwin.go`, `macse_test.go`, the Objective-C sources). Lint has
|
||||||
run on them, so it would likely find more there than the line lengths. No
|
never run on them, so it would likely find more there than the line
|
||||||
macOS test runs in CI. A macOS runner would cover all of it (asked on
|
lengths. No macOS test runs in CI. A macOS runner would cover all of it
|
||||||
https://git.eeqj.de/sneak/secret/issues/50).
|
(asked on https://git.eeqj.de/sneak/secret/issues/50).
|
||||||
- 1.0 critical security blockers (from repo TODO.md):
|
- 1.0 critical security blockers (from repo TODO.md):
|
||||||
- Memory security: age writes an identity's private key out as a string in
|
- Memory security: age writes an identity's private key out as a string in
|
||||||
ordinary memory, and the copies it makes on the way stay there
|
ordinary memory, and the copies it makes on the way stay there
|
||||||
(`secret.IdentityToLockedBuffer` overwrites only the string itself).
|
(`secret.IdentityToLockedBuffer` overwrites only the string itself).
|
||||||
- Medium priority:
|
- Medium priority:
|
||||||
- Standardize error messages; stop leaking internals.
|
- Standardize error messages; stop leaking internals.
|
||||||
|
- Graceful handling of corrupted or missing key files with recovery
|
||||||
|
suggestions.
|
||||||
- Split oversized CLI functions.
|
- Split oversized CLI functions.
|
||||||
- Cleanups: read statedir from environment or default instead of passing it
|
- Cleanups: read statedir from environment or default instead of
|
||||||
around.
|
passing it around.
|
||||||
- Enhancements: help examples, colored output, --quiet flag, name suggestions on
|
- Enhancements: help examples, colored output, --quiet flag, name suggestions on
|
||||||
miss, audit logging, hardware integration tests (Keychain, GPG), naming
|
miss, audit logging, hardware integration tests (Keychain, GPG), naming
|
||||||
consistency, vault export/import, batch operations, search, secret metadata
|
consistency, vault export/import, batch operations, search, secret metadata
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"sneak.berlin/go/secret/internal/cli"
|
"git.eeqj.de/sneak/secret/internal/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
module sneak.berlin/go/secret
|
module git.eeqj.de/sneak/secret
|
||||||
|
|
||||||
go 1.24.1
|
go 1.24.1
|
||||||
|
|
||||||
@@ -10,8 +10,6 @@ require (
|
|||||||
github.com/btcsuite/btcd/btcutil v1.1.6
|
github.com/btcsuite/btcd/btcutil v1.1.6
|
||||||
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d
|
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d
|
||||||
github.com/creack/pty v1.1.24
|
github.com/creack/pty v1.1.24
|
||||||
github.com/dustin/go-humanize v1.0.1
|
|
||||||
github.com/fatih/color v1.18.0
|
|
||||||
github.com/keybase/go-keychain v0.0.0-20230307172405-3e4884637dd1
|
github.com/keybase/go-keychain v0.0.0-20230307172405-3e4884637dd1
|
||||||
github.com/oklog/ulid/v2 v2.1.1
|
github.com/oklog/ulid/v2 v2.1.1
|
||||||
github.com/spf13/afero v1.14.0
|
github.com/spf13/afero v1.14.0
|
||||||
@@ -28,6 +26,8 @@ require (
|
|||||||
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
|
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
|
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
|
||||||
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
|
github.com/fatih/color v1.18.0 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
|||||||
+1
-1
@@ -6,10 +6,10 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Instance encapsulates all CLI functionality and state
|
// Instance encapsulates all CLI functionality and state
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/cli"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/cli"
|
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCLIInstanceStateDir(t *testing.T) {
|
func TestCLIInstanceStateDir(t *testing.T) {
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"maps"
|
"path/filepath"
|
||||||
"slices"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// getSecretNamesCompletionFunc returns a completion function that provides
|
// getSecretNamesCompletionFunc returns a completion function that provides
|
||||||
@@ -44,7 +44,7 @@ func getSecretNamesCompletionFunc(fs afero.Fs, stateDir string) func(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getUnlockerIDsCompletionFunc returns a completion function that provides
|
// getUnlockerIDsCompletionFunc returns a completion function that provides
|
||||||
// unlocker IDs, the names of the unlockers' directories in unlockers.d
|
// unlocker IDs
|
||||||
func getUnlockerIDsCompletionFunc(fs afero.Fs, stateDir string) func(
|
func getUnlockerIDsCompletionFunc(fs afero.Fs, stateDir string) func(
|
||||||
cmd *cobra.Command, args []string, toComplete string,
|
cmd *cobra.Command, args []string, toComplete string,
|
||||||
) ([]string, cobra.ShellCompDirective) {
|
) ([]string, cobra.ShellCompDirective) {
|
||||||
@@ -57,15 +57,38 @@ func getUnlockerIDsCompletionFunc(fs afero.Fs, stateDir string) func(
|
|||||||
return nil, cobra.ShellCompDirectiveNoFileComp
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||||
}
|
}
|
||||||
|
|
||||||
unlockerMetadata, err := vlt.ListUnlockers()
|
// Get unlocker metadata list
|
||||||
|
unlockerMetadataList, err := vlt.ListUnlockers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, cobra.ShellCompDirectiveNoFileComp
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get vault directory
|
||||||
|
vaultDir, err := vlt.GetDirectory()
|
||||||
|
if err != nil {
|
||||||
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect unlocker IDs
|
||||||
var completions []string
|
var completions []string
|
||||||
|
|
||||||
for _, id := range slices.Sorted(maps.Keys(unlockerMetadata)) {
|
unlockersDir := filepath.Join(vaultDir, "unlockers.d")
|
||||||
if strings.HasPrefix(id, toComplete) {
|
|
||||||
|
for _, metadata := range unlockerMetadataList {
|
||||||
|
// Get the actual unlocker ID by creating the unlocker instance
|
||||||
|
id, err := findUnlockerIDByMetadata(
|
||||||
|
fs, unlockersDir, metadata, false,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
secret.Warn(
|
||||||
|
"Could not read unlockers directory during completion, "+
|
||||||
|
"skipping unlocker",
|
||||||
|
"unlockers_dir", unlockersDir, "error", err)
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if id != "" && strings.HasPrefix(id, toComplete) {
|
||||||
completions = append(completions, id)
|
completions = append(completions, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Sentinel errors for asking the user to confirm a removal
|
// Sentinel errors for asking the user to confirm a removal
|
||||||
|
|||||||
@@ -24,12 +24,12 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"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"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -63,10 +63,10 @@ func newConfirmTestVaults(
|
|||||||
fs := &afero.MemMapFs{}
|
fs := &afero.MemMapFs{}
|
||||||
mnemonic := testMnemonicBuffer(t)
|
mnemonic := testMnemonicBuffer(t)
|
||||||
|
|
||||||
_, err := vault.CreateVault(fs, testStateDir, "other", mnemonic, nil)
|
_, err := vault.CreateVault(fs, testStateDir, "other", mnemonic)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(fs, testStateDir, "work", mnemonic, nil)
|
vlt, err := vault.CreateVault(fs, testStateDir, "work", mnemonic)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
addTestSecret(t, vlt, []byte("older"), false)
|
addTestSecret(t, vlt, []byte("older"), false)
|
||||||
@@ -101,8 +101,7 @@ func newRemoval(t *testing.T, command string) removal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fs, workDir, older := newConfirmTestVaults(t, unlockers)
|
fs, workDir, older := newConfirmTestVaults(t, unlockers)
|
||||||
// The first unlocker's directory name, written by newConfirmTestVaults
|
unlockerID := "pgp-" + listTestGPGKeyID + "A"
|
||||||
unlockerID := "pgp-0"
|
|
||||||
|
|
||||||
removeFirstUnlocker := func(cli *Instance, cmd *cobra.Command, force bool) error {
|
removeFirstUnlocker := func(cli *Instance, cmd *cobra.Command, force bool) error {
|
||||||
return cli.UnlockersRemove(unlockerID, force, cmd)
|
return cli.UnlockersRemove(unlockerID, force, cmd)
|
||||||
@@ -143,7 +142,7 @@ func newRemoval(t *testing.T, command string) removal {
|
|||||||
return removal{
|
return removal{
|
||||||
fs: fs,
|
fs: fs,
|
||||||
run: removeFirstUnlocker,
|
run: removeFirstUnlocker,
|
||||||
removed: filepath.Join(workDir, "unlockers.d", unlockerID),
|
removed: filepath.Join(workDir, "unlockers.d", "pgp-0"),
|
||||||
question: "Permanently remove unlocker '" + unlockerID +
|
question: "Permanently remove unlocker '" + unlockerID +
|
||||||
"' from vault 'work'? It is not the vault's last unlocker.",
|
"' from vault 'work'? It is not the vault's last unlocker.",
|
||||||
}
|
}
|
||||||
@@ -151,7 +150,7 @@ func newRemoval(t *testing.T, command string) removal {
|
|||||||
return removal{
|
return removal{
|
||||||
fs: fs,
|
fs: fs,
|
||||||
run: removeFirstUnlocker,
|
run: removeFirstUnlocker,
|
||||||
removed: filepath.Join(workDir, "unlockers.d", unlockerID),
|
removed: filepath.Join(workDir, "unlockers.d", "pgp-0"),
|
||||||
question: "Permanently remove unlocker '" + unlockerID +
|
question: "Permanently remove unlocker '" + unlockerID +
|
||||||
"', the last unlocker of vault 'work', which holds 1 " +
|
"', the last unlocker of vault 'work', which holds 1 " +
|
||||||
"secret(s)? Without an unlocker the vault opens only " +
|
"secret(s)? Without an unlocker the vault opens only " +
|
||||||
|
|||||||
@@ -2,21 +2,16 @@ package cli_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
|
||||||
"maps"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/cli"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/cli"
|
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestCreateExistingVaultChangesNothing is a regression test for
|
// TestCreateExistingVaultChangesNothing is a regression test for
|
||||||
@@ -67,18 +62,22 @@ func TestCreateExistingVaultChangesNothing(t *testing.T) {
|
|||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
command string
|
command string
|
||||||
|
want string
|
||||||
run func(c *cli.Instance) error
|
run func(c *cli.Instance) error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"init",
|
"init",
|
||||||
|
"failed to create default vault: vault default already exists",
|
||||||
func(c *cli.Instance) error { return c.Init(cmd) },
|
func(c *cli.Instance) error { return c.Init(cmd) },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"vault create default",
|
"vault create default",
|
||||||
|
"vault default already exists",
|
||||||
func(c *cli.Instance) error { return c.CreateVault(cmd, "default") },
|
func(c *cli.Instance) error { return c.CreateVault(cmd, "default") },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"vault create work",
|
"vault create work",
|
||||||
|
"vault work already exists",
|
||||||
func(c *cli.Instance) error { return c.CreateVault(cmd, "work") },
|
func(c *cli.Instance) error { return c.CreateVault(cmd, "work") },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -89,7 +88,7 @@ func TestCreateExistingVaultChangesNothing(t *testing.T) {
|
|||||||
|
|
||||||
err := tt.run(newCLI(fs))
|
err := tt.run(newCLI(fs))
|
||||||
|
|
||||||
require.ErrorIs(t, err, vault.ErrVaultExists)
|
require.EqualError(t, err, tt.want)
|
||||||
require.Equal(t, before, snapshotStateDir(t, fs))
|
require.Equal(t, before, snapshotStateDir(t, fs))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -156,7 +155,7 @@ func TestStopAtPassphrasePromptLeavesNothing(t *testing.T) {
|
|||||||
require.NoError(t, empty.MkdirAll(testStateDir, secret.DirPerms))
|
require.NoError(t, empty.MkdirAll(testStateDir, secret.DirPerms))
|
||||||
|
|
||||||
withDefault := afero.NewMemMapFs()
|
withDefault := afero.NewMemMapFs()
|
||||||
_, err := vault.CreateVault(withDefault, testStateDir, "default", mnemonic, nil)
|
_, err := vault.CreateVault(withDefault, testStateDir, "default", mnemonic)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
cmd := &cobra.Command{}
|
cmd := &cobra.Command{}
|
||||||
@@ -189,226 +188,8 @@ func TestStopAtPassphrasePromptLeavesNothing(t *testing.T) {
|
|||||||
|
|
||||||
err := tt.run(c)
|
err := tt.run(c)
|
||||||
|
|
||||||
require.ErrorIs(t, err, secret.ErrPassphraseNotRead)
|
require.ErrorContains(t, err, "failed to read passphrase")
|
||||||
require.Equal(t, before, snapshotStateDir(t, tt.fs))
|
require.Equal(t, before, snapshotStateDir(t, tt.fs))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestMnemonicNotReadNamesOnlyMnemonic is a regression test for
|
|
||||||
// https://git.eeqj.de/sneak/secret/issues/115: `secret init` without
|
|
||||||
// SB_SECRET_MNEMONIC and with a stdin that is not a terminal said "failed to
|
|
||||||
// read mnemonic: failed to read passphrase: ...". The error must wrap
|
|
||||||
// secret.ErrMnemonicNotRead and name the mnemonic only. The message is
|
|
||||||
// pinned on the built binary, whose stdin is surely not a terminal.
|
|
||||||
func TestMnemonicNotReadNamesOnlyMnemonic(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
c := cli.NewCLIInstanceWithStateDir(afero.NewMemMapFs(), testStateDir)
|
|
||||||
require.ErrorIs(t, c.Init(discardCmd()), secret.ErrMnemonicNotRead)
|
|
||||||
|
|
||||||
stateDir := t.TempDir()
|
|
||||||
|
|
||||||
//nolint:gosec // G204: test executes the freshly built secret binary
|
|
||||||
cmd := exec.CommandContext(t.Context(), secretBinaryPath(t), "init")
|
|
||||||
cmd.Env = []string{
|
|
||||||
secret.EnvStateDir + "=" + stateDir,
|
|
||||||
"PATH=" + os.Getenv("PATH"),
|
|
||||||
"HOME=" + os.Getenv("HOME"),
|
|
||||||
}
|
|
||||||
|
|
||||||
output, err := cmd.CombinedOutput()
|
|
||||||
require.Error(t, err)
|
|
||||||
|
|
||||||
require.Equal(t, "Initialized secrets manager at: "+stateDir+"\n"+
|
|
||||||
"Error: failed to read mnemonic: stdin is not a terminal (piped input "+
|
|
||||||
"or script). Please set the SB_SECRET_MNEMONIC environment variable "+
|
|
||||||
"or run interactively\n", string(output))
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestStopDuringCreateLeavesWholeVaultOrNone is a regression test for
|
|
||||||
// https://git.eeqj.de/sneak/secret/issues/105: `secret init` or `secret vault
|
|
||||||
// create` killed after the passphrase prompt but before the unlocker was
|
|
||||||
// written left a vault with no unlocker, which neither command would then
|
|
||||||
// create again. After the prompt, each command changes the state directory
|
|
||||||
// only through vault.CreateVault. The test makes that call as the command
|
|
||||||
// does and records the state directory before each change it makes, and once
|
|
||||||
// after it returns: what a stop at that point leaves. Each must hold either
|
|
||||||
// no vault, and not name it current, or exactly the finished vault, which
|
|
||||||
// opens with the passphrase through its current unlocker. The command run
|
|
||||||
// again after a stop first takes the lock, which must delete what the stop
|
|
||||||
// left under a temporary name. Running the command is slow, so it runs once
|
|
||||||
// on each different state the lock leaves, and must create the vault there,
|
|
||||||
// or refuse the one there.
|
|
||||||
//
|
|
||||||
//nolint:paralleltest // commands on the in-memory filesystem share one lock
|
|
||||||
func TestStopDuringCreateLeavesWholeVaultOrNone(t *testing.T) {
|
|
||||||
mnemonic := testMnemonicBuffer(t)
|
|
||||||
passphrase := memguard.NewBufferFromBytes([]byte(testPassphrase))
|
|
||||||
t.Cleanup(passphrase.Destroy)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{}
|
|
||||||
cmd.SetOut(io.Discard)
|
|
||||||
|
|
||||||
t.Run("init", func(t *testing.T) {
|
|
||||||
// From an empty state directory
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
require.NoError(t, fs.MkdirAll(testStateDir, secret.DirPerms))
|
|
||||||
|
|
||||||
requireStopsLeaveWholeVaultOrNone(t, fs, "default", mnemonic, passphrase,
|
|
||||||
func(c *cli.Instance) error { return c.Init(cmd) })
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("vault create work", func(t *testing.T) {
|
|
||||||
// From a state directory holding the vault "default"
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
_, err := vault.CreateVault(fs, testStateDir, "default", mnemonic, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
requireStopsLeaveWholeVaultOrNone(t, fs, "work", mnemonic, passphrase,
|
|
||||||
func(c *cli.Instance) error { return c.CreateVault(cmd, "work") })
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// requireStopsLeaveWholeVaultOrNone checks, as
|
|
||||||
// TestStopDuringCreateLeavesWholeVaultOrNone describes, the stops of the
|
|
||||||
// command run, creating the vault name on fs with mnemonic and passphrase.
|
|
||||||
// Run again where the vault is there, the command must fail with
|
|
||||||
// vault.ErrVaultExists.
|
|
||||||
func requireStopsLeaveWholeVaultOrNone(
|
|
||||||
t *testing.T, fs afero.Fs, name string,
|
|
||||||
mnemonic, passphrase *memguard.LockedBuffer,
|
|
||||||
run func(c *cli.Instance) error,
|
|
||||||
) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
var stops []map[string]string
|
|
||||||
|
|
||||||
record := func() { stops = append(stops, snapshotStateDir(t, fs)) }
|
|
||||||
|
|
||||||
_, err := vault.CreateVault(hookFs{Fs: fs, before: record},
|
|
||||||
testStateDir, name, mnemonic, passphrase)
|
|
||||||
require.NoError(t, err)
|
|
||||||
record()
|
|
||||||
|
|
||||||
vaultDir := testStateDir + "/vaults.d/" + name
|
|
||||||
require.NotContains(t, stops[0], vaultDir+"/", "no stop before the vault")
|
|
||||||
|
|
||||||
finished := entriesUnder(stops[len(stops)-1], vaultDir)
|
|
||||||
|
|
||||||
opener := vault.NewVault(fs, testStateDir, name)
|
|
||||||
opener.UnlockPassphrase = passphrase
|
|
||||||
|
|
||||||
key, err := opener.UnlockVault()
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, finished[vaultDir+"/pub.age"], key.Recipient().String())
|
|
||||||
|
|
||||||
// Each different state the command run again finds once it holds the lock
|
|
||||||
var locked []map[string]string
|
|
||||||
|
|
||||||
for i, stop := range stops {
|
|
||||||
if _, there := stop[vaultDir+"/"]; there {
|
|
||||||
require.Equal(t, finished, entriesUnder(stop, vaultDir),
|
|
||||||
"stop %d left a partial vault", i)
|
|
||||||
} else {
|
|
||||||
require.NotEqual(t, name, stop[testStateDir+"/currentvault"],
|
|
||||||
"stop %d made a missing vault current", i)
|
|
||||||
}
|
|
||||||
|
|
||||||
stopped := newFsFromSnapshot(t, stop)
|
|
||||||
release, err := vault.LockStateDir(stopped, testStateDir)
|
|
||||||
require.NoError(t, err)
|
|
||||||
release()
|
|
||||||
|
|
||||||
state := snapshotStateDir(t, stopped)
|
|
||||||
for path := range state {
|
|
||||||
require.NotContains(t, path, ".tmp-", "stop %d", i)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !slices.ContainsFunc(locked, func(s map[string]string) bool {
|
|
||||||
return maps.Equal(s, state)
|
|
||||||
}) {
|
|
||||||
locked = append(locked, state)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, state := range locked {
|
|
||||||
c := cli.NewCLIInstanceWithStateDir(newFsFromSnapshot(t, state), testStateDir)
|
|
||||||
c.Mnemonic = mnemonic
|
|
||||||
c.UnlockPassphrase = passphrase
|
|
||||||
|
|
||||||
if _, there := state[vaultDir+"/"]; there {
|
|
||||||
require.ErrorIs(t, run(c), vault.ErrVaultExists)
|
|
||||||
} else {
|
|
||||||
require.NoError(t, run(c))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// entriesUnder returns the entries of a tree recorded by snapshotStateDir
|
|
||||||
// that are under dir.
|
|
||||||
func entriesUnder(tree map[string]string, dir string) map[string]string {
|
|
||||||
entries := map[string]string{}
|
|
||||||
|
|
||||||
for path, content := range tree {
|
|
||||||
if strings.HasPrefix(path, dir+"/") {
|
|
||||||
entries[path] = content
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return entries
|
|
||||||
}
|
|
||||||
|
|
||||||
// hookFs passes every call through to Fs, but first calls before for each
|
|
||||||
// call that can change the filesystem.
|
|
||||||
type hookFs struct {
|
|
||||||
afero.Fs
|
|
||||||
|
|
||||||
before func()
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint:ireturn // implements afero.Fs
|
|
||||||
func (h hookFs) Create(name string) (afero.File, error) {
|
|
||||||
h.before()
|
|
||||||
|
|
||||||
return h.Fs.Create(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint:ireturn // implements afero.Fs
|
|
||||||
func (h hookFs) OpenFile(
|
|
||||||
name string, flag int, perm os.FileMode,
|
|
||||||
) (afero.File, error) {
|
|
||||||
h.before()
|
|
||||||
|
|
||||||
return h.Fs.OpenFile(name, flag, perm)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h hookFs) Mkdir(name string, perm os.FileMode) error {
|
|
||||||
h.before()
|
|
||||||
|
|
||||||
return h.Fs.Mkdir(name, perm)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h hookFs) MkdirAll(path string, perm os.FileMode) error {
|
|
||||||
h.before()
|
|
||||||
|
|
||||||
return h.Fs.MkdirAll(path, perm)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h hookFs) Remove(name string) error {
|
|
||||||
h.before()
|
|
||||||
|
|
||||||
return h.Fs.Remove(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h hookFs) RemoveAll(path string) error {
|
|
||||||
h.before()
|
|
||||||
|
|
||||||
return h.Fs.RemoveAll(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h hookFs) Rename(oldname, newname string) error {
|
|
||||||
h.before()
|
|
||||||
|
|
||||||
return h.Fs.Rename(oldname, newname)
|
|
||||||
}
|
|
||||||
|
|||||||
+23
-5
@@ -7,16 +7,17 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Sentinel errors for encrypt/decrypt operations
|
// Sentinel errors for encrypt/decrypt operations
|
||||||
var (
|
var (
|
||||||
errNotAgeSecretKey = errors.New(
|
errNotAgeSecretKey = errors.New(
|
||||||
"does not contain a valid age secret key")
|
"does not contain a valid age secret key")
|
||||||
|
errSecretDoesNotExist = errors.New("does not exist")
|
||||||
)
|
)
|
||||||
|
|
||||||
// newCryptoCmd builds an encrypt/decrypt command with input/output flags
|
// newCryptoCmd builds an encrypt/decrypt command with input/output flags
|
||||||
@@ -129,7 +130,7 @@ func (cli *Instance) resolveEncryptionKey(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Secret exists, get the age secret key from it
|
// Secret exists, get the age secret key from it
|
||||||
secretBuffer, err := vlt.GetSecret(secretName)
|
secretBuffer, err := cli.getSecretValue(vlt, secretObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get secret value: %w", err)
|
return nil, fmt.Errorf("failed to get secret value: %w", err)
|
||||||
}
|
}
|
||||||
@@ -244,11 +245,11 @@ func (cli *Instance) Decrypt(secretName, inputFile, outputFile string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return fmt.Errorf("secret '%s' %w", secretName, vault.ErrSecretNotFound)
|
return fmt.Errorf("secret '%s' %w", secretName, errSecretDoesNotExist)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the age secret key from the secret
|
// Get the age secret key from the secret
|
||||||
secretBuffer, err := vlt.GetSecret(secretName)
|
secretBuffer, err := cli.getSecretValue(vlt, secretObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get secret value: %w", err)
|
return fmt.Errorf("failed to get secret value: %w", err)
|
||||||
}
|
}
|
||||||
@@ -312,3 +313,20 @@ func isValidAgeSecretKey(key string) bool {
|
|||||||
|
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getSecretValue retrieves the value of a secret with the vault's mnemonic
|
||||||
|
// when it has one, else with the current unlocker
|
||||||
|
func (cli *Instance) getSecretValue(
|
||||||
|
vlt *vault.Vault, secretObj *secret.Secret,
|
||||||
|
) (*memguard.LockedBuffer, error) {
|
||||||
|
if vlt.Mnemonic != nil {
|
||||||
|
return secretObj.GetValue(nil, vlt.Mnemonic)
|
||||||
|
}
|
||||||
|
|
||||||
|
unlocker, err := vlt.GetCurrentUnlocker()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get current unlocker: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return secretObj.GetValue(unlocker, nil)
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/cli"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/cli"
|
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Entry must return its exit code rather than exit, so that its deferred
|
// Entry must return its exit code rather than exit, so that its deferred
|
||||||
|
|||||||
@@ -1,62 +0,0 @@
|
|||||||
package cli_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"sneak.berlin/go/secret/internal/cli"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestMissingSecretOrVaultErrors checks that a command that finds no such
|
|
||||||
// secret or vault returns the vault package's error for it, as `secret get`
|
|
||||||
// does, and leaves the vaults unchanged. "default" is the current vault, and
|
|
||||||
// both vaults hold the secret "x".
|
|
||||||
func TestMissingSecretOrVaultErrors(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
before := snapshotStateDir(t, newTwoVaultFs(t))
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
command string
|
|
||||||
want error
|
|
||||||
run func(c *cli.Instance) error
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
"rm --force nosuch", vault.ErrSecretNotFound,
|
|
||||||
func(c *cli.Instance) error {
|
|
||||||
return c.RemoveSecret(&cobra.Command{}, "nosuch", true)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"version rm --force nosuch", vault.ErrSecretNotFound,
|
|
||||||
func(c *cli.Instance) error {
|
|
||||||
return c.RemoveVersion(&cobra.Command{}, "nosuch", "20260101.001", true)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"mv --force work:nosuch default", vault.ErrSecretNotFound,
|
|
||||||
func(c *cli.Instance) error {
|
|
||||||
return c.MoveSecret(&cobra.Command{}, "work:nosuch", "default", true)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"decrypt nosuch", vault.ErrSecretNotFound,
|
|
||||||
func(c *cli.Instance) error { return c.Decrypt("nosuch", "", "") },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"vault rm --force nosuch", vault.ErrVaultNotFound,
|
|
||||||
func(c *cli.Instance) error {
|
|
||||||
return c.RemoveVault(&cobra.Command{}, "nosuch", true)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.command, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
requireRejectedAndUnchanged(t, before, tt.want, tt.run)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,10 +7,10 @@ import (
|
|||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/tyler-smith/go-bip39"
|
"github.com/tyler-smith/go-bip39"
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -20,7 +20,11 @@ const (
|
|||||||
|
|
||||||
// Sentinel errors for secret generation
|
// Sentinel errors for secret generation
|
||||||
var (
|
var (
|
||||||
errLengthTooSmall = errors.New("length must be at least 1")
|
errLengthTooSmall = errors.New("length must be at least 1")
|
||||||
|
errLengthNotPositive = errors.New("length must be positive")
|
||||||
|
errMnemonicTypeNotSupported = errors.New(
|
||||||
|
"mnemonic type not supported for secret generation, " +
|
||||||
|
"use 'secret generate mnemonic' instead")
|
||||||
errUnsupportedSecretType = errors.New("unsupported type")
|
errUnsupportedSecretType = errors.New("unsupported type")
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -144,8 +148,7 @@ func (cli *Instance) GenerateSecret(
|
|||||||
case "alnum":
|
case "alnum":
|
||||||
secretValue, err = generateRandomAlnum(length)
|
secretValue, err = generateRandomAlnum(length)
|
||||||
case "mnemonic":
|
case "mnemonic":
|
||||||
return fmt.Errorf("%w: mnemonic (use 'secret generate mnemonic' instead)",
|
return errMnemonicTypeNotSupported
|
||||||
errUnsupportedSecretType)
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("%w: %s (supported: base58, alnum)",
|
return fmt.Errorf("%w: %s (supported: base58, alnum)",
|
||||||
errUnsupportedSecretType, secretType)
|
errUnsupportedSecretType, secretType)
|
||||||
@@ -201,8 +204,8 @@ func generateRandomAlnum(length int) (string, error) {
|
|||||||
// generateRandomString generates a random string of the specified length
|
// generateRandomString generates a random string of the specified length
|
||||||
// using the given character set
|
// using the given character set
|
||||||
func generateRandomString(length int, charset string) (string, error) {
|
func generateRandomString(length int, charset string) (string, error) {
|
||||||
if length < 1 {
|
if length <= 0 {
|
||||||
return "", errLengthTooSmall
|
return "", errLengthNotPositive
|
||||||
}
|
}
|
||||||
|
|
||||||
result := make([]byte, length)
|
result := make([]byte, length)
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Version info - these are set at build time
|
// Version info - these are set at build time
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// vaultStats accumulates statistics while walking vault directories
|
// vaultStats accumulates statistics while walking vault directories
|
||||||
|
|||||||
+66
-23
@@ -6,13 +6,16 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/tyler-smith/go-bip39"
|
"github.com/tyler-smith/go-bip39"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// errPassphraseMismatch is returned when passphrase confirmation fails
|
// errPassphraseMismatch is returned when passphrase confirmation fails
|
||||||
@@ -55,11 +58,11 @@ func (cli *Instance) promptMnemonic() (*memguard.LockedBuffer, func(), error) {
|
|||||||
secret.Debug("Prompting user for mnemonic phrase")
|
secret.Debug("Prompting user for mnemonic phrase")
|
||||||
|
|
||||||
// Read mnemonic securely without echo
|
// Read mnemonic securely without echo
|
||||||
mnemonicBuffer, err := secret.ReadMnemonic("Enter your BIP39 mnemonic phrase: ")
|
mnemonicBuffer, err := secret.ReadPassphrase("Enter your BIP39 mnemonic phrase: ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
secret.Debug("Failed to read mnemonic from stdin", "error", err)
|
secret.Debug("Failed to read mnemonic from stdin", "error", err)
|
||||||
|
|
||||||
return nil, nil, err
|
return nil, nil, fmt.Errorf("failed to read mnemonic: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintln(os.Stderr) // Add newline after hidden input
|
fmt.Fprintln(os.Stderr) // Add newline after hidden input
|
||||||
@@ -67,6 +70,43 @@ func (cli *Instance) promptMnemonic() (*memguard.LockedBuffer, func(), error) {
|
|||||||
return mnemonicBuffer, mnemonicBuffer.Destroy, nil
|
return mnemonicBuffer, mnemonicBuffer.Destroy, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setupDefaultVault creates the default vault and derives its long-term
|
||||||
|
// identity from the mnemonic
|
||||||
|
func (cli *Instance) setupDefaultVault(
|
||||||
|
stateDir string, mnemonic *memguard.LockedBuffer,
|
||||||
|
) (*vault.Vault, *age.X25519Identity, error) {
|
||||||
|
// Create the default vault - it will handle key derivation internally
|
||||||
|
secret.Debug("Creating default vault")
|
||||||
|
|
||||||
|
vlt, err := vault.CreateVault(cli.fs, cli.stateDir, "default", mnemonic)
|
||||||
|
if err != nil {
|
||||||
|
secret.Debug("Failed to create default vault", "error", err)
|
||||||
|
|
||||||
|
return nil, nil, fmt.Errorf("failed to create default vault: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the vault metadata to retrieve the derivation index
|
||||||
|
vaultDir := filepath.Join(stateDir, "vaults.d", "default")
|
||||||
|
|
||||||
|
metadata, err := vault.LoadVaultMetadata(cli.fs, vaultDir)
|
||||||
|
if err != nil {
|
||||||
|
secret.Debug("Failed to load vault metadata", "error", err)
|
||||||
|
|
||||||
|
return nil, nil, fmt.Errorf("failed to load vault metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Derive the long-term key using the same index that CreateVault used
|
||||||
|
ltIdentity, err := agehd.DeriveIdentity(mnemonic.String(), metadata.DerivationIndex)
|
||||||
|
if err != nil {
|
||||||
|
secret.Debug("Failed to derive long-term key", "error", err)
|
||||||
|
|
||||||
|
return nil, nil, fmt.Errorf(
|
||||||
|
"failed to derive long-term key from mnemonic: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return vlt, ltIdentity, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Init initializes the secret manager, holding the state directory lock
|
// Init initializes the secret manager, holding the state directory lock
|
||||||
// while initialize runs
|
// while initialize runs
|
||||||
func (cli *Instance) Init(cmd *cobra.Command) error {
|
func (cli *Instance) Init(cmd *cobra.Command) error {
|
||||||
@@ -133,31 +173,34 @@ func (cli *Instance) initialize(cmd *cobra.Command) error {
|
|||||||
}
|
}
|
||||||
defer cleanupPassphrase()
|
defer cleanupPassphrase()
|
||||||
|
|
||||||
// Create the default vault with its passphrase unlocker
|
// Create the default vault and derive its long-term key
|
||||||
secret.Debug("Creating default vault")
|
vlt, ltIdentity, err := cli.setupDefaultVault(stateDir, mnemonic)
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(cli.fs, cli.stateDir, "default",
|
|
||||||
mnemonic, passphraseBuffer)
|
|
||||||
if err != nil {
|
|
||||||
secret.Debug("Failed to create default vault", "error", err)
|
|
||||||
|
|
||||||
return fmt.Errorf("failed to create default vault: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ltIdentity, err := vlt.GetOrDeriveLongTermKey()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get long-term key: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
unlocker, err := vlt.GetCurrentUnlocker()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ltPubKey := ltIdentity.Recipient().String()
|
||||||
|
|
||||||
|
// Unlock the vault with the derived long-term key
|
||||||
|
vlt.Unlock(ltIdentity)
|
||||||
|
|
||||||
|
// Create passphrase-protected unlocker
|
||||||
|
secret.Debug("Creating passphrase-protected unlocker")
|
||||||
|
|
||||||
|
passphraseUnlocker, err := vlt.CreatePassphraseUnlocker(passphraseBuffer)
|
||||||
|
if err != nil {
|
||||||
|
secret.Debug("Failed to create unlocker", "error", err)
|
||||||
|
|
||||||
|
return fmt.Errorf("failed to create unlocker: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: CreatePassphraseUnlocker already encrypts and writes the long-term
|
||||||
|
// private key to longterm.age, so no need to do it again here.
|
||||||
|
|
||||||
if cmd != nil {
|
if cmd != nil {
|
||||||
cmd.Printf("\nDefault vault created and configured\n")
|
cmd.Printf("\nDefault vault created and configured\n")
|
||||||
cmd.Printf("Long-term public key: %s\n", ltIdentity.Recipient().String())
|
cmd.Printf("Long-term public key: %s\n", ltPubKey)
|
||||||
cmd.Printf("Unlocker ID: %s\n", unlocker.GetID())
|
cmd.Printf("Unlocker ID: %s\n", passphraseUnlocker.GetID())
|
||||||
cmd.Println("\nYour secret manager is ready to use!")
|
cmd.Println("\nYour secret manager is ready to use!")
|
||||||
cmd.Println("Note: When using SB_SECRET_MNEMONIC environment variable,")
|
cmd.Println("Note: When using SB_SECRET_MNEMONIC environment variable,")
|
||||||
cmd.Println("unlockers are not required for secret operations.")
|
cmd.Println("unlockers are not required for secret operations.")
|
||||||
|
|||||||
@@ -1,67 +0,0 @@
|
|||||||
//nolint:testpackage // white-box test of unexported internals
|
|
||||||
package cli
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/awnumar/memguard"
|
|
||||||
"github.com/spf13/afero"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestInvalidMnemonicError checks that every command that takes a mnemonic
|
|
||||||
// returns errInvalidMnemonicPhrase for one that is not valid BIP39. The vault
|
|
||||||
// "other" has no long-term key, as vault import needs.
|
|
||||||
func TestInvalidMnemonicError(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
command string
|
|
||||||
run func(c *Instance) error
|
|
||||||
}{
|
|
||||||
{"secret init", func(c *Instance) error { return c.Init(c.cmd) }},
|
|
||||||
{"secret vault create work", func(c *Instance) error {
|
|
||||||
return c.CreateVault(c.cmd, "work")
|
|
||||||
}},
|
|
||||||
{"secret vault import other", func(c *Instance) error {
|
|
||||||
return c.VaultImport(c.cmd, "other")
|
|
||||||
}},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.command, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
_, err := vault.CreateVault(fs, listTestStateDir, "other", nil, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
instance, _ := newTestInstance(fs)
|
|
||||||
instance.Mnemonic = memguard.NewBufferFromBytes([]byte("not a mnemonic"))
|
|
||||||
t.Cleanup(instance.Mnemonic.Destroy)
|
|
||||||
|
|
||||||
require.ErrorIs(t, tt.run(instance), errInvalidMnemonicPhrase)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestGenerateSecretErrors checks that `secret generate secret` gives one
|
|
||||||
// error for a length below 1 and one for a type it cannot generate.
|
|
||||||
func TestGenerateSecretErrors(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
instance, cmd := newTestInstance(afero.NewMemMapFs())
|
|
||||||
|
|
||||||
err := instance.GenerateSecret(cmd, "x", 0, "base58", false)
|
|
||||||
require.ErrorIs(t, err, errLengthTooSmall)
|
|
||||||
|
|
||||||
_, err = generateRandomString(0, "ab")
|
|
||||||
require.ErrorIs(t, err, errLengthTooSmall)
|
|
||||||
|
|
||||||
err = instance.GenerateSecret(cmd, "x", defaultSecretLength, "mnemonic", false)
|
|
||||||
require.ErrorIs(t, err, errUnsupportedSecretType)
|
|
||||||
|
|
||||||
err = instance.GenerateSecret(cmd, "x", defaultSecretLength, "hex", false)
|
|
||||||
require.ErrorIs(t, err, errUnsupportedSecretType)
|
|
||||||
}
|
|
||||||
@@ -17,15 +17,15 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/cli"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/creack/pty"
|
"github.com/creack/pty"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/cli"
|
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -680,10 +680,10 @@ func test06GetSecret(t *testing.T, testMnemonic string, runSecret func(...string
|
|||||||
require.NoError(t, err, "get secret should succeed")
|
require.NoError(t, err, "get secret should succeed")
|
||||||
assert.Equal(t, "password123", strings.TrimSpace(output), "should return correct secret value")
|
assert.Equal(t, "password123", strings.TrimSpace(output), "should return correct secret value")
|
||||||
|
|
||||||
// Test that without mnemonic, we get an error: the passphrase unlocker
|
// Test that without mnemonic, we get an error
|
||||||
// cannot ask for its passphrase, as the tests have no terminal
|
output, err = runSecret("get", "database/password")
|
||||||
_, err = runSecret("get", "database/password")
|
require.Error(t, err, "get should fail without unlock method")
|
||||||
require.ErrorIs(t, err, secret.ErrPassphraseNotRead, "get should fail without unlock method")
|
assert.Contains(t, output, "failed to unlock vault", "should indicate unlock failure")
|
||||||
}
|
}
|
||||||
|
|
||||||
func test07AddSecretVersion(t *testing.T, tempDir, testMnemonic string, runSecret func(...string) (string, error), runSecretWithEnv func(map[string]string, ...string) (string, error), runSecretWithStdin func(string, map[string]string, ...string) (string, error)) {
|
func test07AddSecretVersion(t *testing.T, tempDir, testMnemonic string, runSecret func(...string) (string, error), runSecretWithEnv func(map[string]string, ...string) (string, error), runSecretWithStdin func(string, map[string]string, ...string) (string, error)) {
|
||||||
@@ -839,11 +839,12 @@ func test09GetSpecificVersion(t *testing.T, tempDir, testMnemonic string, runSec
|
|||||||
assert.Equal(t, "newpassword456", strings.TrimSpace(output), "should return new secret value without --version")
|
assert.Equal(t, "newpassword456", strings.TrimSpace(output), "should return new secret value without --version")
|
||||||
|
|
||||||
// An empty --version is not a version; it does not mean the current one
|
// An empty --version is not a version; it does not mean the current one
|
||||||
_, err = runSecretWithEnv(map[string]string{
|
output, err = runSecretWithEnv(map[string]string{
|
||||||
secret.EnvMnemonic: testMnemonic,
|
secret.EnvMnemonic: testMnemonic,
|
||||||
}, "get", "--version", "", "database/password")
|
}, "get", "--version", "", "database/password")
|
||||||
|
|
||||||
require.ErrorIs(t, err, vault.ErrVersionNotFound, "should reject the empty version")
|
require.Error(t, err, "get with an empty version should fail")
|
||||||
|
assert.Contains(t, output, "version '' not found", "should reject the empty version")
|
||||||
}
|
}
|
||||||
|
|
||||||
func test10PromoteVersion(t *testing.T, tempDir, testMnemonic string, runSecret func(...string) (string, error), runSecretWithEnv func(map[string]string, ...string) (string, error)) {
|
func test10PromoteVersion(t *testing.T, tempDir, testMnemonic string, runSecret func(...string) (string, error), runSecretWithEnv func(map[string]string, ...string) (string, error)) {
|
||||||
@@ -1157,7 +1158,11 @@ func testInvalidSecretNames(t *testing.T, testMnemonic string, runSecretWithStdi
|
|||||||
shouldFail := slices.Contains(definitelyInvalid, invalidName)
|
shouldFail := slices.Contains(definitelyInvalid, invalidName)
|
||||||
|
|
||||||
if shouldFail {
|
if shouldFail {
|
||||||
require.ErrorIs(t, err, vault.ErrInvalidSecretName, "add '%s' should fail", invalidName)
|
require.Error(t, err, "add '%s' should fail", invalidName)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
assert.Contains(t, output, "invalid secret name", "should indicate invalid name for '%s'", invalidName)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// For the slash cases and .hidden, they might succeed
|
// For the slash cases and .hidden, they might succeed
|
||||||
// Just log what happened
|
// Just log what happened
|
||||||
@@ -1216,8 +1221,9 @@ func test12bMoveSecret(t *testing.T, testMnemonic string, runSecret func(...stri
|
|||||||
|
|
||||||
// Test error cases
|
// Test error cases
|
||||||
// Try to move non-existent secret
|
// Try to move non-existent secret
|
||||||
_, err = runSecret("move", "test/nonexistent", "test/destination")
|
output, err = runSecret("move", "test/nonexistent", "test/destination")
|
||||||
require.ErrorIs(t, err, vault.ErrSecretNotFound, "move non-existent should fail")
|
require.Error(t, err, "move non-existent should fail")
|
||||||
|
assert.Contains(t, output, "not found", "should indicate source not found")
|
||||||
|
|
||||||
// Try to move to existing destination
|
// Try to move to existing destination
|
||||||
_, err = runSecretWithStdin("dest-value", map[string]string{
|
_, err = runSecretWithStdin("dest-value", map[string]string{
|
||||||
@@ -1225,8 +1231,9 @@ func test12bMoveSecret(t *testing.T, testMnemonic string, runSecret func(...stri
|
|||||||
}, "add", "test/existing-dest")
|
}, "add", "test/existing-dest")
|
||||||
require.NoError(t, err, "add test/existing-dest should succeed")
|
require.NoError(t, err, "add test/existing-dest should succeed")
|
||||||
|
|
||||||
_, err = runSecret("move", "test/renamed", "test/existing-dest")
|
output, err = runSecret("move", "test/renamed", "test/existing-dest")
|
||||||
require.ErrorIs(t, err, vault.ErrSecretExists, "move to existing destination should fail")
|
require.Error(t, err, "move to existing destination should fail")
|
||||||
|
assert.Contains(t, output, "already exists", "should indicate destination exists")
|
||||||
|
|
||||||
// Verify the source wasn't removed since move failed
|
// Verify the source wasn't removed since move failed
|
||||||
getOutput, err = runSecretWithEnv(map[string]string{
|
getOutput, err = runSecretWithEnv(map[string]string{
|
||||||
@@ -1303,8 +1310,9 @@ func test12cCrossVaultMove(t *testing.T, testMnemonic string, runSecretWithEnv f
|
|||||||
require.NoError(t, err, "add force/test in work should succeed")
|
require.NoError(t, err, "add force/test in work should succeed")
|
||||||
|
|
||||||
// Move without force should fail
|
// Move without force should fail
|
||||||
_, err = runSecretWithEnv(env, "move", "work:force/test", "default")
|
output, err = runSecretWithEnv(env, "move", "work:force/test", "default")
|
||||||
require.ErrorIs(t, err, vault.ErrSecretExists, "move without force should fail when dest exists")
|
require.Error(t, err, "move without force should fail when dest exists")
|
||||||
|
assert.Contains(t, output, "already exists", "should indicate destination exists")
|
||||||
|
|
||||||
// Move with force should succeed
|
// Move with force should succeed
|
||||||
output, err = runSecretWithEnv(env, "move", "--force", "work:force/test", "default")
|
output, err = runSecretWithEnv(env, "move", "--force", "work:force/test", "default")
|
||||||
@@ -1419,8 +1427,9 @@ func test14SwitchVault(t *testing.T, tempDir string, runSecret func(...string) (
|
|||||||
require.NoError(t, err, "vault select default should succeed")
|
require.NoError(t, err, "vault select default should succeed")
|
||||||
|
|
||||||
// Test selecting non-existent vault
|
// Test selecting non-existent vault
|
||||||
_, err = runSecret("vault", "select", "nonexistent")
|
output, err := runSecret("vault", "select", "nonexistent")
|
||||||
require.ErrorIs(t, err, vault.ErrVaultNotFound, "selecting non-existent vault should fail")
|
require.Error(t, err, "selecting non-existent vault should fail")
|
||||||
|
assert.Contains(t, output, "does not exist", "should indicate vault doesn't exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
func test15VaultIsolation(t *testing.T, testMnemonic string, runSecret func(...string) (string, error), runSecretWithEnv func(map[string]string, ...string) (string, error), runSecretWithStdin func(string, map[string]string, ...string) (string, error)) {
|
func test15VaultIsolation(t *testing.T, testMnemonic string, runSecret func(...string) (string, error), runSecretWithEnv func(map[string]string, ...string) (string, error), runSecretWithStdin func(string, map[string]string, ...string) (string, error)) {
|
||||||
@@ -1441,10 +1450,11 @@ func test15VaultIsolation(t *testing.T, testMnemonic string, runSecret func(...s
|
|||||||
require.NoError(t, err, "vault select work should succeed")
|
require.NoError(t, err, "vault select work should succeed")
|
||||||
|
|
||||||
// Try to get the default-only secret (should fail)
|
// Try to get the default-only secret (should fail)
|
||||||
_, err = runSecretWithEnv(map[string]string{
|
output, err := runSecretWithEnv(map[string]string{
|
||||||
secret.EnvMnemonic: testMnemonic,
|
secret.EnvMnemonic: testMnemonic,
|
||||||
}, "get", "default-only/secret")
|
}, "get", "default-only/secret")
|
||||||
require.ErrorIs(t, err, vault.ErrSecretNotFound, "should not be able to get default vault secret from work vault")
|
require.Error(t, err, "should not be able to get default vault secret from work vault")
|
||||||
|
assert.Contains(t, output, "not found", "should indicate secret not found")
|
||||||
|
|
||||||
// Add a unique secret to work vault
|
// Add a unique secret to work vault
|
||||||
_, err = runSecretWithStdin("work-vault-secret", map[string]string{
|
_, err = runSecretWithStdin("work-vault-secret", map[string]string{
|
||||||
@@ -1457,13 +1467,14 @@ func test15VaultIsolation(t *testing.T, testMnemonic string, runSecret func(...s
|
|||||||
require.NoError(t, err, "vault select default should succeed")
|
require.NoError(t, err, "vault select default should succeed")
|
||||||
|
|
||||||
// Try to get the work-only secret (should fail)
|
// Try to get the work-only secret (should fail)
|
||||||
_, err = runSecretWithEnv(map[string]string{
|
output, err = runSecretWithEnv(map[string]string{
|
||||||
secret.EnvMnemonic: testMnemonic,
|
secret.EnvMnemonic: testMnemonic,
|
||||||
}, "get", "work-only/secret")
|
}, "get", "work-only/secret")
|
||||||
require.ErrorIs(t, err, vault.ErrSecretNotFound, "should not be able to get work vault secret from default vault")
|
require.Error(t, err, "should not be able to get work vault secret from default vault")
|
||||||
|
assert.Contains(t, output, "not found", "should indicate secret not found")
|
||||||
|
|
||||||
// Verify we can still get the default-only secret
|
// Verify we can still get the default-only secret
|
||||||
output, err := runSecretWithEnv(map[string]string{
|
output, err = runSecretWithEnv(map[string]string{
|
||||||
secret.EnvMnemonic: testMnemonic,
|
secret.EnvMnemonic: testMnemonic,
|
||||||
}, "get", "default-only/secret")
|
}, "get", "default-only/secret")
|
||||||
require.NoError(t, err, "get default-only secret should succeed")
|
require.NoError(t, err, "get default-only secret should succeed")
|
||||||
@@ -1575,10 +1586,11 @@ func test17ImportFromFile(t *testing.T, tempDir, testMnemonic string, runSecretW
|
|||||||
// Just verify the import succeeded
|
// Just verify the import succeeded
|
||||||
|
|
||||||
// Test importing non-existent file
|
// Test importing non-existent file
|
||||||
_, err = runSecretWithEnv(map[string]string{
|
output, err = runSecretWithEnv(map[string]string{
|
||||||
secret.EnvMnemonic: testMnemonic,
|
secret.EnvMnemonic: testMnemonic,
|
||||||
}, "import", "imported/nonexistent", "--source", "/nonexistent/file")
|
}, "import", "imported/nonexistent", "--source", "/nonexistent/file")
|
||||||
require.ErrorIs(t, err, os.ErrNotExist, "importing non-existent file should fail")
|
require.Error(t, err, "importing non-existent file should fail")
|
||||||
|
assert.Contains(t, output, "failed", "should indicate failure")
|
||||||
|
|
||||||
// Verify filesystem structure
|
// Verify filesystem structure
|
||||||
defaultVaultDir := filepath.Join(tempDir, "vaults.d", "default")
|
defaultVaultDir := filepath.Join(tempDir, "vaults.d", "default")
|
||||||
@@ -1893,10 +1905,11 @@ func test23ErrorHandling(t *testing.T, tempDir, secretPath, testMnemonic string,
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
// Get non-existent secret
|
// Get non-existent secret
|
||||||
_, err := runSecretWithEnv(map[string]string{
|
output, err := runSecretWithEnv(map[string]string{
|
||||||
secret.EnvMnemonic: testMnemonic,
|
secret.EnvMnemonic: testMnemonic,
|
||||||
}, "get", "nonexistent/secret")
|
}, "get", "nonexistent/secret")
|
||||||
require.ErrorIs(t, err, vault.ErrSecretNotFound, "get non-existent secret should fail")
|
require.Error(t, err, "get non-existent secret should fail")
|
||||||
|
assert.Contains(t, output, "not found", "should indicate secret not found")
|
||||||
|
|
||||||
// Add secret without mnemonic or unlocker
|
// Add secret without mnemonic or unlocker
|
||||||
unsetMnemonic := os.Getenv(secret.EnvMnemonic)
|
unsetMnemonic := os.Getenv(secret.EnvMnemonic)
|
||||||
@@ -1926,28 +1939,32 @@ func test23ErrorHandling(t *testing.T, tempDir, secretPath, testMnemonic string,
|
|||||||
// Invalid secret names (already tested in test 12)
|
// Invalid secret names (already tested in test 12)
|
||||||
|
|
||||||
// Non-existent vault operations
|
// Non-existent vault operations
|
||||||
_, err = runSecret("vault", "select", "nonexistent")
|
output, err = runSecret("vault", "select", "nonexistent")
|
||||||
require.ErrorIs(t, err, vault.ErrVaultNotFound, "select non-existent vault should fail")
|
require.Error(t, err, "select non-existent vault should fail")
|
||||||
|
assert.Contains(t, output, "does not exist", "should indicate vault doesn't exist")
|
||||||
|
|
||||||
// Import to non-existent vault with test passphrase
|
// Import to non-existent vault with test passphrase
|
||||||
testPassphrase := "test-passphrase-123" // Define testPassphrase locally
|
testPassphrase := "test-passphrase-123" // Define testPassphrase locally
|
||||||
_, err = runSecretWithEnv(map[string]string{
|
output, err = runSecretWithEnv(map[string]string{
|
||||||
secret.EnvMnemonic: testMnemonic,
|
secret.EnvMnemonic: testMnemonic,
|
||||||
secret.EnvUnlockPassphrase: testPassphrase,
|
secret.EnvUnlockPassphrase: testPassphrase,
|
||||||
}, "vault", "import", "nonexistent")
|
}, "vault", "import", "nonexistent")
|
||||||
require.ErrorIs(t, err, vault.ErrVaultNotFound, "import to non-existent vault should fail")
|
require.Error(t, err, "import to non-existent vault should fail")
|
||||||
|
assert.Contains(t, output, "does not exist", "should indicate vault doesn't exist")
|
||||||
|
|
||||||
// Get specific version that doesn't exist
|
// Get specific version that doesn't exist
|
||||||
_, err = runSecretWithEnv(map[string]string{
|
output, err = runSecretWithEnv(map[string]string{
|
||||||
secret.EnvMnemonic: testMnemonic,
|
secret.EnvMnemonic: testMnemonic,
|
||||||
}, "get", "--version", "99999999.999", "database/password")
|
}, "get", "--version", "99999999.999", "database/password")
|
||||||
require.ErrorIs(t, err, vault.ErrVersionNotFound, "get non-existent version should fail")
|
require.Error(t, err, "get non-existent version should fail")
|
||||||
|
assert.Contains(t, output, "not found", "should indicate version not found")
|
||||||
|
|
||||||
// Promote non-existent version
|
// Promote non-existent version
|
||||||
_, err = runSecretWithEnv(map[string]string{
|
output, err = runSecretWithEnv(map[string]string{
|
||||||
secret.EnvMnemonic: testMnemonic,
|
secret.EnvMnemonic: testMnemonic,
|
||||||
}, "version", "promote", "database/password", "99999999.999")
|
}, "version", "promote", "database/password", "99999999.999")
|
||||||
require.ErrorIs(t, err, vault.ErrVersionNotFound, "promote non-existent version should fail")
|
require.Error(t, err, "promote non-existent version should fail")
|
||||||
|
assert.Contains(t, output, "not found", "should indicate version not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func test24EnvironmentVariables(t *testing.T, tempDir, secretPath, testMnemonic, testPassphrase string) {
|
func test24EnvironmentVariables(t *testing.T, tempDir, secretPath, testMnemonic, testPassphrase string) {
|
||||||
@@ -2350,10 +2367,11 @@ func test30BackupRestore(t *testing.T, tempDir, secretPath, testMnemonic string,
|
|||||||
assert.NotEmpty(t, output, "restored secret should have value")
|
assert.NotEmpty(t, output, "restored secret should have value")
|
||||||
|
|
||||||
// Verify post-backup secret is gone
|
// Verify post-backup secret is gone
|
||||||
_, err = runSecretWithEnv(map[string]string{
|
output, err = runSecretWithEnv(map[string]string{
|
||||||
secret.EnvMnemonic: testMnemonic,
|
secret.EnvMnemonic: testMnemonic,
|
||||||
}, "get", "post-backup/secret")
|
}, "get", "post-backup/secret")
|
||||||
require.ErrorIs(t, err, vault.ErrSecretNotFound, "post-backup secret should not exist after restore")
|
require.Error(t, err, "post-backup secret should not exist after restore")
|
||||||
|
assert.Contains(t, output, "not found", "should indicate secret not found")
|
||||||
|
|
||||||
t.Log("Backup and restore completed successfully")
|
t.Log("Backup and restore completed successfully")
|
||||||
}
|
}
|
||||||
@@ -2418,7 +2436,8 @@ func test31EnvMnemonicUsesVaultDerivationIndex(t *testing.T, tempDir, secretPath
|
|||||||
t.Logf("Output: %s", getOutput)
|
t.Logf("Output: %s", getOutput)
|
||||||
|
|
||||||
// This is the expected behavior with the current bug
|
// This is the expected behavior with the current bug
|
||||||
require.ErrorIs(t, err, vault.ErrMnemonicMismatch, "get should fail due to wrong derivation index")
|
require.Error(t, err, "get should fail due to wrong derivation index")
|
||||||
|
assert.Contains(t, getOutput, "derived public key does not match vault", "should indicate key derivation failure")
|
||||||
|
|
||||||
// Document what should happen when the bug is fixed
|
// Document what should happen when the bug is fixed
|
||||||
t.Log("When the bug is fixed, GetValue should read vault metadata and use derivation index 1")
|
t.Log("When the bug is fixed, GetValue should read vault metadata and use derivation index 1")
|
||||||
@@ -2544,7 +2563,7 @@ func secretRmCommand(ctx context.Context, t *testing.T) (*exec.Cmd, string) {
|
|||||||
mnemonic := memguard.NewBufferFromBytes([]byte(testMnemonic))
|
mnemonic := memguard.NewBufferFromBytes([]byte(testMnemonic))
|
||||||
defer mnemonic.Destroy()
|
defer mnemonic.Destroy()
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(afero.NewOsFs(), stateDir, "default", mnemonic, nil)
|
vlt, err := vault.CreateVault(afero.NewOsFs(), stateDir, "default", mnemonic)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
value := memguard.NewBufferFromBytes([]byte("value"))
|
value := memguard.NewBufferFromBytes([]byte("value"))
|
||||||
|
|||||||
@@ -4,12 +4,12 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/cli"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/cli"
|
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestLeftoversRemovedByNextChangingCommand is a regression test for
|
// TestLeftoversRemovedByNextChangingCommand is a regression test for
|
||||||
@@ -28,7 +28,7 @@ func TestLeftoversRemovedByNextChangingCommand(t *testing.T) {
|
|||||||
|
|
||||||
fs := newTwoVaultFs(t)
|
fs := newTwoVaultFs(t)
|
||||||
|
|
||||||
_, err := vault.CreateVault(fs, testStateDir, ".tmp-0", nil, nil)
|
_, err := vault.CreateVault(fs, testStateDir, ".tmp-0", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, vault.SelectVault(fs, testStateDir, "default"))
|
require.NoError(t, vault.SelectVault(fs, testStateDir, "default"))
|
||||||
|
|
||||||
|
|||||||
@@ -13,13 +13,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"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"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -110,7 +110,7 @@ func TestConcurrentAddsKeepEveryVersion(t *testing.T) {
|
|||||||
{"real", afero.NewOsFs(), t.TempDir()},
|
{"real", afero.NewOsFs(), t.TempDir()},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
_, err := vault.CreateVault(tc.fs, tc.stateDir, "default", mnemonic, nil)
|
_, err := vault.CreateVault(tc.fs, tc.stateDir, "default", mnemonic)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// One add creates the secret; the others find that it exists
|
// One add creates the secret; the others find that it exists
|
||||||
@@ -185,7 +185,7 @@ func (r *readNotifier) Read(p []byte) (int, error) {
|
|||||||
//nolint:paralleltest // times commands against the in-memory lock all tests share
|
//nolint:paralleltest // times commands against the in-memory lock all tests share
|
||||||
func TestEncryptPipedIntoAdd(t *testing.T) {
|
func TestEncryptPipedIntoAdd(t *testing.T) {
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
_, err := vault.CreateVault(fs, testStateDir, "default", testMnemonicBuffer(t), nil)
|
_, err := vault.CreateVault(fs, testStateDir, "default", testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, afero.WriteFile(fs, testInput, []byte("piped"), 0o600))
|
require.NoError(t, afero.WriteFile(fs, testInput, []byte("piped"), 0o600))
|
||||||
|
|
||||||
@@ -292,14 +292,14 @@ func setupEveryCommand(
|
|||||||
|
|
||||||
mnemonic := testMnemonicBuffer(t)
|
mnemonic := testMnemonicBuffer(t)
|
||||||
|
|
||||||
other, err := vault.CreateVault(fs, testStateDir, "other", mnemonic, nil)
|
other, err := vault.CreateVault(fs, testStateDir, "other", mnemonic)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
otherDir, err := other.GetDirectory()
|
otherDir, err := other.GetDirectory()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, fs.Remove(filepath.Join(otherDir, "pub.age")))
|
require.NoError(t, fs.Remove(filepath.Join(otherDir, "pub.age")))
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(fs, testStateDir, "work", mnemonic, nil)
|
vlt, err := vault.CreateVault(fs, testStateDir, "work", mnemonic)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
addTestSecret(t, vlt, []byte("older"), false)
|
addTestSecret(t, vlt, []byte("older"), false)
|
||||||
@@ -487,7 +487,7 @@ func TestEncryptWithExistingKeyTakesNoLock(t *testing.T) {
|
|||||||
mnemonic := testMnemonicBuffer(t)
|
mnemonic := testMnemonicBuffer(t)
|
||||||
|
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
_, err := vault.CreateVault(fs, testStateDir, "default", mnemonic, nil)
|
_, err := vault.CreateVault(fs, testStateDir, "default", mnemonic)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, afero.WriteFile(fs, testInput, []byte("input"), 0o600))
|
require.NoError(t, afero.WriteFile(fs, testInput, []byte("input"), 0o600))
|
||||||
|
|
||||||
@@ -525,7 +525,7 @@ func TestEncryptWithExistingKeyTakesNoLock(t *testing.T) {
|
|||||||
//nolint:paralleltest // times commands against the in-memory lock all tests share
|
//nolint:paralleltest // times commands against the in-memory lock all tests share
|
||||||
func TestEncryptStreamsUnlocked(t *testing.T) {
|
func TestEncryptStreamsUnlocked(t *testing.T) {
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
_, err := vault.CreateVault(fs, testStateDir, "default", testMnemonicBuffer(t), nil)
|
_, err := vault.CreateVault(fs, testStateDir, "default", testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, afero.WriteFile(fs, testInput, []byte("streamed"), 0o600))
|
require.NoError(t, afero.WriteFile(fs, testInput, []byte("streamed"), 0o600))
|
||||||
|
|
||||||
|
|||||||
+29
-54
@@ -5,12 +5,12 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/cli"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/cli"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestRejectedMoveWithinVaultLeavesStateUnchanged is a regression test for
|
// TestRejectedMoveWithinVaultLeavesStateUnchanged is a regression test for
|
||||||
@@ -30,8 +30,6 @@ func TestRejectedMoveWithinVaultLeavesStateUnchanged(t *testing.T) {
|
|||||||
workX = "work:x"
|
workX = "work:x"
|
||||||
)
|
)
|
||||||
|
|
||||||
// internal/cli declares these errors itself and does not export them, so
|
|
||||||
// only their text can be compared.
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
command string
|
command string
|
||||||
source, dest string
|
source, dest string
|
||||||
@@ -45,6 +43,30 @@ func TestRejectedMoveWithinVaultLeavesStateUnchanged(t *testing.T) {
|
|||||||
{`mv --force work:x ""`, workX, "", true, ontoItself},
|
{`mv --force work:x ""`, workX, "", true, ontoItself},
|
||||||
// "work" is a vault name, so the destination is work:x.
|
// "work" is a vault name, so the destination is work:x.
|
||||||
{"mv --force work:x work", workX, "work", true, ontoItself},
|
{"mv --force work:x work", workX, "work", true, ontoItself},
|
||||||
|
{
|
||||||
|
"mv work:nosuch work:y", "work:nosuch", "work:y", false,
|
||||||
|
"secret 'nosuch' not found",
|
||||||
|
},
|
||||||
|
// Only an existing vault is used.
|
||||||
|
{
|
||||||
|
"mv --force nosuch:x nosuch:y", "nosuch:x", "nosuch:y", true,
|
||||||
|
"vault 'nosuch' does not exist",
|
||||||
|
},
|
||||||
|
// Each of these spells "work" a second way. The spelling is not a
|
||||||
|
// valid vault name, so the move is not taken for a move between two
|
||||||
|
// vaults, which would delete the destination, here the source.
|
||||||
|
{
|
||||||
|
"mv --force work:x work/:x", workX, "work/:x", true,
|
||||||
|
vault.ValidateVaultName("work/").Error(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mv --force work/:x work:", "work/:x", "work:", true,
|
||||||
|
vault.ValidateVaultName("work/").Error(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"mv --force work:x ./work:x", workX, "./work:x", true,
|
||||||
|
vault.ValidateVaultName("./work").Error(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@@ -60,53 +82,6 @@ func TestRejectedMoveWithinVaultLeavesStateUnchanged(t *testing.T) {
|
|||||||
require.EqualError(t, err, tt.wantErr)
|
require.EqualError(t, err, tt.wantErr)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
missing := []struct {
|
|
||||||
command string
|
|
||||||
source, dest string
|
|
||||||
force bool
|
|
||||||
want error
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
"mv work:nosuch work:y", "work:nosuch", "work:y", false,
|
|
||||||
vault.ErrSecretNotFound,
|
|
||||||
},
|
|
||||||
// Only an existing vault is used.
|
|
||||||
{
|
|
||||||
"mv --force nosuch:x nosuch:y", "nosuch:x", "nosuch:y", true,
|
|
||||||
vault.ErrVaultNotFound,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range missing {
|
|
||||||
t.Run(tt.command, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
requireRejectedAndUnchanged(t, before, tt.want, func(c *cli.Instance) error {
|
|
||||||
return c.MoveSecret(&cobra.Command{}, tt.source, tt.dest, tt.force)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Each of these spells "work" a second way. The spelling is not a valid
|
|
||||||
// vault name, so the move is not taken for a move between two vaults,
|
|
||||||
// which would delete the destination, here the source.
|
|
||||||
invalidNames := []struct{ source, dest string }{
|
|
||||||
{workX, "work/:x"},
|
|
||||||
{"work/:x", "work:"},
|
|
||||||
{workX, "./work:x"},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range invalidNames {
|
|
||||||
t.Run("mv --force "+tt.source+" "+tt.dest, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
requireRejectedAndUnchanged(t, before, vault.ErrInvalidVaultName,
|
|
||||||
func(c *cli.Instance) error {
|
|
||||||
return c.MoveSecret(&cobra.Command{}, tt.source, tt.dest, true)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestMoveWithinOtherVaultKeepsCurrentVault checks that `secret mv work:x
|
// TestMoveWithinOtherVaultKeepsCurrentVault checks that `secret mv work:x
|
||||||
@@ -181,10 +156,10 @@ func TestMoveOntoSameSecretUnderAnotherNameIsRejected(t *testing.T) {
|
|||||||
vaultsDir := filepath.Join(stateDir, "vaults.d")
|
vaultsDir := filepath.Join(stateDir, "vaults.d")
|
||||||
|
|
||||||
// "default" is created last, so it is the current vault.
|
// "default" is created last, so it is the current vault.
|
||||||
_, err := vault.CreateVault(fs, stateDir, "other", testMnemonicBuffer(t), nil)
|
_, err := vault.CreateVault(fs, stateDir, "other", testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(fs, stateDir, "default", testMnemonicBuffer(t), nil)
|
vlt, err := vault.CreateVault(fs, stateDir, "default", testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
err = vlt.AddSecret("x", memguard.NewBufferFromBytes([]byte("value")), false)
|
err = vlt.AddSecret("x", memguard.NewBufferFromBytes([]byte("value")), false)
|
||||||
@@ -230,7 +205,7 @@ func TestForcedCaseOnlyMoveOnCaseSensitiveFilesystem(t *testing.T) {
|
|||||||
fs := afero.NewOsFs()
|
fs := afero.NewOsFs()
|
||||||
stateDir := t.TempDir()
|
stateDir := t.TempDir()
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(fs, stateDir, "default", testMnemonicBuffer(t), nil)
|
vlt, err := vault.CreateVault(fs, stateDir, "default", testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
err = vlt.AddSecret("Foo", memguard.NewBufferFromBytes([]byte("upper")), false)
|
err = vlt.AddSecret("Foo", memguard.NewBufferFromBytes([]byte("upper")), false)
|
||||||
|
|||||||
@@ -9,13 +9,13 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/cli"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/cli"
|
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -68,7 +68,7 @@ func newTwoVaultFs(t *testing.T) afero.Fs {
|
|||||||
mnemonic := testMnemonicBuffer(t)
|
mnemonic := testMnemonicBuffer(t)
|
||||||
|
|
||||||
for _, name := range []string{"work", "default"} {
|
for _, name := range []string{"work", "default"} {
|
||||||
vlt, err := vault.CreateVault(fs, testStateDir, name, mnemonic, nil)
|
vlt, err := vault.CreateVault(fs, testStateDir, name, mnemonic)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
err = vlt.AddSecret("x", memguard.NewBufferFromBytes([]byte("value")), false)
|
err = vlt.AddSecret("x", memguard.NewBufferFromBytes([]byte("value")), false)
|
||||||
@@ -154,10 +154,11 @@ func newFsFromSnapshot(t *testing.T, tree map[string]string) afero.Fs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// requireRejectedAndUnchanged runs a command on a copy of the state
|
// requireRejectedAndUnchanged runs a command on a copy of the state
|
||||||
// directory recorded in before. It requires the error want, so that a later
|
// directory recorded in before. It requires an error with exactly the
|
||||||
// check rejecting the argument does not count, and everything under the
|
// message of want, so that a later check rejecting the argument does not
|
||||||
// state directory as it was: the error alone proves nothing, since it could
|
// count, and everything under the state directory as it was: the error
|
||||||
// come after the vault had already been deleted.
|
// alone proves nothing, since it could come after the vault had already
|
||||||
|
// been deleted.
|
||||||
func requireRejectedAndUnchanged(
|
func requireRejectedAndUnchanged(
|
||||||
t *testing.T, before map[string]string, want error,
|
t *testing.T, before map[string]string, want error,
|
||||||
run func(c *cli.Instance) error,
|
run func(c *cli.Instance) error,
|
||||||
@@ -169,7 +170,7 @@ func requireRejectedAndUnchanged(
|
|||||||
err := run(cli.NewCLIInstanceWithStateDir(fs, testStateDir))
|
err := run(cli.NewCLIInstanceWithStateDir(fs, testStateDir))
|
||||||
|
|
||||||
require.Equal(t, before, snapshotStateDir(t, fs))
|
require.Equal(t, before, snapshotStateDir(t, fs))
|
||||||
require.ErrorIs(t, err, want)
|
require.EqualError(t, err, want.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestInvalidSecretNameLeavesVaultsUnchanged is a regression test for
|
// TestInvalidSecretNameLeavesVaultsUnchanged is a regression test for
|
||||||
@@ -192,76 +193,77 @@ func TestInvalidSecretNameLeavesVaultsUnchanged(t *testing.T) {
|
|||||||
cmd := &cobra.Command{}
|
cmd := &cobra.Command{}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
command string
|
command string
|
||||||
run func(c *cli.Instance) error
|
rejected string // the secret name the command must reject
|
||||||
|
run func(c *cli.Instance) error
|
||||||
}{
|
}{
|
||||||
{"rm --force ..", func(c *cli.Instance) error {
|
{"rm --force ..", "..", func(c *cli.Instance) error {
|
||||||
return c.RemoveSecret(cmd, "..", true)
|
return c.RemoveSecret(cmd, "..", true)
|
||||||
}},
|
}},
|
||||||
{"rm --force .", func(c *cli.Instance) error {
|
{"rm --force .", ".", func(c *cli.Instance) error {
|
||||||
return c.RemoveSecret(cmd, ".", true)
|
return c.RemoveSecret(cmd, ".", true)
|
||||||
}},
|
}},
|
||||||
{`rm --force ""`, func(c *cli.Instance) error {
|
{`rm --force ""`, "", func(c *cli.Instance) error {
|
||||||
return c.RemoveSecret(cmd, "", true)
|
return c.RemoveSecret(cmd, "", true)
|
||||||
}},
|
}},
|
||||||
{"rm --force ../../etc", func(c *cli.Instance) error {
|
{"rm --force ../../etc", "../../etc", func(c *cli.Instance) error {
|
||||||
return c.RemoveSecret(cmd, "../../etc", true)
|
return c.RemoveSecret(cmd, "../../etc", true)
|
||||||
}},
|
}},
|
||||||
{"mv --force .. x", func(c *cli.Instance) error {
|
{"mv --force .. x", "..", func(c *cli.Instance) error {
|
||||||
return c.MoveSecret(cmd, "..", "x", true)
|
return c.MoveSecret(cmd, "..", "x", true)
|
||||||
}},
|
}},
|
||||||
{"mv --force x ..", func(c *cli.Instance) error {
|
{"mv --force x ..", "..", func(c *cli.Instance) error {
|
||||||
return c.MoveSecret(cmd, "x", "..", true)
|
return c.MoveSecret(cmd, "x", "..", true)
|
||||||
}},
|
}},
|
||||||
{`mv --force x ""`, func(c *cli.Instance) error {
|
{`mv --force x ""`, "", func(c *cli.Instance) error {
|
||||||
return c.MoveSecret(cmd, "x", "", true)
|
return c.MoveSecret(cmd, "x", "", true)
|
||||||
}},
|
}},
|
||||||
// "work" is not the current vault: a move within it must not
|
// "work" is not the current vault: a move within it must not
|
||||||
// select it when a name is rejected.
|
// select it when a name is rejected.
|
||||||
{"mv --force work:.. work:x", func(c *cli.Instance) error {
|
{"mv --force work:.. work:x", "..", func(c *cli.Instance) error {
|
||||||
return c.MoveSecret(cmd, "work:..", "work:x", true)
|
return c.MoveSecret(cmd, "work:..", "work:x", true)
|
||||||
}},
|
}},
|
||||||
{"mv --force work:x work:..", func(c *cli.Instance) error {
|
{"mv --force work:x work:..", "..", func(c *cli.Instance) error {
|
||||||
return c.MoveSecret(cmd, "work:x", "work:..", true)
|
return c.MoveSecret(cmd, "work:x", "work:..", true)
|
||||||
}},
|
}},
|
||||||
{"mv --force default:.. work", func(c *cli.Instance) error {
|
{"mv --force default:.. work", "..", func(c *cli.Instance) error {
|
||||||
return c.MoveSecret(cmd, "default:..", "work", true)
|
return c.MoveSecret(cmd, "default:..", "work", true)
|
||||||
}},
|
}},
|
||||||
{"mv --force default:.. work:y", func(c *cli.Instance) error {
|
{"mv --force default:.. work:y", "..", func(c *cli.Instance) error {
|
||||||
return c.MoveSecret(cmd, "default:..", "work:y", true)
|
return c.MoveSecret(cmd, "default:..", "work:y", true)
|
||||||
}},
|
}},
|
||||||
{"mv --force default:x work:..", func(c *cli.Instance) error {
|
{"mv --force default:x work:..", "..", func(c *cli.Instance) error {
|
||||||
return c.MoveSecret(cmd, "default:x", "work:..", true)
|
return c.MoveSecret(cmd, "default:x", "work:..", true)
|
||||||
}},
|
}},
|
||||||
{"import --force ..", func(c *cli.Instance) error {
|
{"import --force ..", "..", func(c *cli.Instance) error {
|
||||||
return c.ImportSecret(cmd, "..", missingFile, true)
|
return c.ImportSecret(cmd, "..", missingFile, true)
|
||||||
}},
|
}},
|
||||||
{"import --force .", func(c *cli.Instance) error {
|
{"import --force .", ".", func(c *cli.Instance) error {
|
||||||
return c.ImportSecret(cmd, ".", missingFile, true)
|
return c.ImportSecret(cmd, ".", missingFile, true)
|
||||||
}},
|
}},
|
||||||
{"import --force ../../etc", func(c *cli.Instance) error {
|
{"import --force ../../etc", "../../etc", func(c *cli.Instance) error {
|
||||||
return c.ImportSecret(cmd, "../../etc", missingFile, true)
|
return c.ImportSecret(cmd, "../../etc", missingFile, true)
|
||||||
}},
|
}},
|
||||||
{"version list ..", func(c *cli.Instance) error {
|
{"version list ..", "..", func(c *cli.Instance) error {
|
||||||
return c.ListVersions(cmd, "..")
|
return c.ListVersions(cmd, "..")
|
||||||
}},
|
}},
|
||||||
{"version promote ..", func(c *cli.Instance) error {
|
{"version promote ..", "..", func(c *cli.Instance) error {
|
||||||
return c.PromoteVersion(cmd, "..", testVersion)
|
return c.PromoteVersion(cmd, "..", testVersion)
|
||||||
}},
|
}},
|
||||||
{"version rm --force ..", func(c *cli.Instance) error {
|
{"version rm --force ..", "..", func(c *cli.Instance) error {
|
||||||
return c.RemoveVersion(cmd, "..", testVersion, true)
|
return c.RemoveVersion(cmd, "..", testVersion, true)
|
||||||
}},
|
}},
|
||||||
{"encrypt ..", func(c *cli.Instance) error {
|
{"encrypt ..", "..", func(c *cli.Instance) error {
|
||||||
return c.Encrypt("..", "", "")
|
return c.Encrypt("..", "", "")
|
||||||
}},
|
}},
|
||||||
{"decrypt ..", func(c *cli.Instance) error {
|
{"decrypt ..", "..", func(c *cli.Instance) error {
|
||||||
return c.Decrypt("..", "", "")
|
return c.Decrypt("..", "", "")
|
||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.command, func(t *testing.T) {
|
t.Run(tt.command, func(t *testing.T) {
|
||||||
requireRejectedAndUnchanged(t, before, vault.ErrInvalidSecretName, tt.run)
|
requireRejectedAndUnchanged(t, before, vault.ValidateSecretName(tt.rejected), tt.run)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -297,7 +299,9 @@ func TestInvalidVersionLeavesVaultsUnchanged(t *testing.T) {
|
|||||||
for _, tt := range commands {
|
for _, tt := range commands {
|
||||||
for _, version := range []string{"", ".", "..", "../../..", "a/b"} {
|
for _, version := range []string{"", ".", "..", "../../..", "a/b"} {
|
||||||
t.Run(fmt.Sprintf("%s %q", tt.command, version), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%s %q", tt.command, version), func(t *testing.T) {
|
||||||
requireRejectedAndUnchanged(t, before, vault.ErrVersionNotFound,
|
want := fmt.Errorf("version '%s' %w '%s'",
|
||||||
|
version, vault.ErrVersionNotFound, "x")
|
||||||
|
requireRejectedAndUnchanged(t, before, want,
|
||||||
func(c *cli.Instance) error { return tt.run(c, version) })
|
func(c *cli.Instance) error { return tt.run(c, version) })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -351,7 +355,7 @@ func TestInvalidVaultNameLeavesStateUnchanged(t *testing.T) {
|
|||||||
for _, tt := range commands {
|
for _, tt := range commands {
|
||||||
for _, name := range []string{"", ".", "..", "a/b"} {
|
for _, name := range []string{"", ".", "..", "a/b"} {
|
||||||
t.Run(fmt.Sprintf(tt.command, name), func(t *testing.T) {
|
t.Run(fmt.Sprintf(tt.command, name), func(t *testing.T) {
|
||||||
requireRejectedAndUnchanged(t, before, vault.ErrInvalidVaultName,
|
requireRejectedAndUnchanged(t, before, vault.ValidateVaultName(name),
|
||||||
func(c *cli.Instance) error {
|
func(c *cli.Instance) error {
|
||||||
c.Mnemonic = mnemonic
|
c.Mnemonic = mnemonic
|
||||||
c.UnlockPassphrase = passphrase
|
c.UnlockPassphrase = passphrase
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Entry runs the secret CLI and returns the process exit code. It wipes
|
// Entry runs the secret CLI and returns the process exit code. It wipes
|
||||||
|
|||||||
+18
-9
@@ -11,11 +11,11 @@ import (
|
|||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -32,7 +32,13 @@ const (
|
|||||||
|
|
||||||
// Sentinel errors for secret operations
|
// Sentinel errors for secret operations
|
||||||
var (
|
var (
|
||||||
errSecretTooLarge = errors.New("secret too large: exceeds 100MB limit")
|
errSecretTooLarge = errors.New("secret too large: exceeds 100MB limit")
|
||||||
|
errSecretFileTooLarge = errors.New(
|
||||||
|
"secret file too large: exceeds 100MB limit")
|
||||||
|
errSecretNotFound = errors.New("not found")
|
||||||
|
errSecretExistsNoForce = errors.New(
|
||||||
|
"already exists (use --force to overwrite)")
|
||||||
|
errVaultDoesNotExist = errors.New("does not exist")
|
||||||
errCrossVaultSourceUnqualified = errors.New(
|
errCrossVaultSourceUnqualified = errors.New(
|
||||||
"source must specify vault (e.g., vault:secret) for cross-vault move")
|
"source must specify vault (e.g., vault:secret) for cross-vault move")
|
||||||
errMoveOntoItself = errors.New("cannot be moved onto itself")
|
errMoveOntoItself = errors.New("cannot be moved onto itself")
|
||||||
@@ -667,6 +673,10 @@ func (cli *Instance) ImportSecret(
|
|||||||
|
|
||||||
buffers, totalSize, err := readSecretFromReader(file)
|
buffers, totalSize, err := readSecretFromReader(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, errSecretTooLarge) {
|
||||||
|
return errSecretFileTooLarge
|
||||||
|
}
|
||||||
|
|
||||||
return fmt.Errorf("failed to read secret from file %s: %w", sourceFile, err)
|
return fmt.Errorf("failed to read secret from file %s: %w", sourceFile, err)
|
||||||
}
|
}
|
||||||
defer destroyBuffers(buffers)
|
defer destroyBuffers(buffers)
|
||||||
@@ -766,7 +776,7 @@ func (cli *Instance) findSecretToRemove(
|
|||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return secretToRemove{},
|
return secretToRemove{},
|
||||||
fmt.Errorf("secret '%s' %w", secretName, vault.ErrSecretNotFound)
|
fmt.Errorf("secret '%s' %w", secretName, errSecretNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
// A secret without a versions directory has no versions, and can
|
// A secret without a versions directory has no versions, and can
|
||||||
@@ -897,7 +907,7 @@ func (cli *Instance) existingVault(name string) (*vault.Vault, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !slices.Contains(vaults, name) {
|
if !slices.Contains(vaults, name) {
|
||||||
return nil, fmt.Errorf("vault '%s' %w", name, vault.ErrVaultNotFound)
|
return nil, fmt.Errorf("vault '%s' %w", name, errVaultDoesNotExist)
|
||||||
}
|
}
|
||||||
|
|
||||||
return vault.NewVault(cli.fs, cli.stateDir, name), nil
|
return vault.NewVault(cli.fs, cli.stateDir, name), nil
|
||||||
@@ -928,7 +938,7 @@ func (cli *Instance) moveSecretWithinVault(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return fmt.Errorf("secret '%s' %w", source, vault.ErrSecretNotFound)
|
return fmt.Errorf("secret '%s' %w", source, errSecretNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
destEncoded := strings.ReplaceAll(dest, "/", "%")
|
destEncoded := strings.ReplaceAll(dest, "/", "%")
|
||||||
@@ -953,8 +963,7 @@ func (cli *Instance) moveSecretWithinVault(
|
|||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
if !force {
|
if !force {
|
||||||
return fmt.Errorf("secret '%s' %w (use --force to overwrite)",
|
return fmt.Errorf("secret '%s' %w", dest, errSecretExistsNoForce)
|
||||||
dest, vault.ErrSecretExists)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = secret.RemoveDirAtomic(cli.fs, destDir)
|
err = secret.RemoveDirAtomic(cli.fs, destDir)
|
||||||
@@ -1019,7 +1028,7 @@ func (cli *Instance) moveSecretCrossVault(
|
|||||||
exists, err := afero.DirExists(cli.fs, srcSecretDir)
|
exists, err := afero.DirExists(cli.fs, srcSecretDir)
|
||||||
if err != nil || !exists {
|
if err != nil || !exists {
|
||||||
return fmt.Errorf("secret '%s' %w in vault '%s'",
|
return fmt.Errorf("secret '%s' %w in vault '%s'",
|
||||||
srcSecretName, vault.ErrSecretNotFound, srcVault.Name)
|
srcSecretName, errSecretNotFound, srcVault.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// The source is removed after the copy, so a destination that is the
|
// The source is removed after the copy, so a destination that is the
|
||||||
|
|||||||
@@ -10,13 +10,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"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"
|
"golang.org/x/sys/unix"
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// testVaultName is the vault name used by the size tests.
|
// testVaultName is the vault name used by the size tests.
|
||||||
@@ -71,8 +71,7 @@ func newSizeTestVault(t *testing.T) (afero.Fs, *vault.Vault) {
|
|||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
// Create vault
|
// Create vault
|
||||||
_, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
_, err := vault.CreateVault(fs, testStateDir, testVaultName, testMnemonicBuffer(t))
|
||||||
testMnemonicBuffer(t), nil)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Set current vault
|
// Set current vault
|
||||||
@@ -93,8 +92,8 @@ 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 the outcome.
|
||||||
func runAddSecretSizeCase(t *testing.T, size int, wantErr error) {
|
func runAddSecretSizeCase(t *testing.T, size int, wantErr bool, errMsg string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
skipIfLockedMemoryTooLow(t, size)
|
skipIfLockedMemoryTooLow(t, size)
|
||||||
|
|
||||||
@@ -128,8 +127,9 @@ func runAddSecretSizeCase(t *testing.T, size int, wantErr error) {
|
|||||||
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 {
|
if wantErr {
|
||||||
require.ErrorIs(t, err, wantErr)
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), errMsg)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -147,8 +147,8 @@ 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 the outcome.
|
||||||
func runImportSecretSizeCase(t *testing.T, size int, wantErr error) {
|
func runImportSecretSizeCase(t *testing.T, size int, wantErr bool, errMsg string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
skipIfLockedMemoryTooLow(t, size)
|
skipIfLockedMemoryTooLow(t, size)
|
||||||
|
|
||||||
@@ -180,8 +180,9 @@ func runImportSecretSizeCase(t *testing.T, size int, wantErr error) {
|
|||||||
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 {
|
if wantErr {
|
||||||
require.ErrorIs(t, err, wantErr)
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), errMsg)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -203,48 +204,57 @@ func runImportSecretSizeCase(t *testing.T, size int, wantErr error) {
|
|||||||
//nolint:paralleltest // together the subtests lock more than the memlock limit
|
//nolint:paralleltest // together the subtests lock more than 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
|
shouldError bool
|
||||||
|
errorMsg string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "1KB secret",
|
name: "1KB secret",
|
||||||
size: 1024,
|
size: 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "10KB secret",
|
name: "10KB secret",
|
||||||
size: 10 * 1024,
|
size: 10 * 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "100KB secret",
|
name: "100KB secret",
|
||||||
size: 100 * 1024,
|
size: 100 * 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "1MB secret",
|
name: "1MB secret",
|
||||||
size: 1024 * 1024,
|
size: 1024 * 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "10MB secret",
|
name: "10MB secret",
|
||||||
size: 10 * 1024 * 1024,
|
size: 10 * 1024 * 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "99MB secret",
|
name: "99MB secret",
|
||||||
size: 99 * 1024 * 1024,
|
size: 99 * 1024 * 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "100MB secret minus 1 byte",
|
name: "100MB secret minus 1 byte",
|
||||||
size: 100*1024*1024 - 1,
|
size: 100*1024*1024 - 1,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "101MB secret - should fail",
|
name: "101MB secret - should fail",
|
||||||
size: 101 * 1024 * 1024,
|
size: 101 * 1024 * 1024,
|
||||||
wantErr: errSecretTooLarge,
|
shouldError: true,
|
||||||
|
errorMsg: "secret too large: exceeds 100MB limit",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
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, tt.shouldError, tt.errorMsg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -254,48 +264,57 @@ func TestAddSecretVariousSizes(t *testing.T) {
|
|||||||
//nolint:paralleltest // together the subtests lock more than the memlock limit
|
//nolint:paralleltest // together the subtests lock more than 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
|
shouldError bool
|
||||||
|
errorMsg string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "1KB file",
|
name: "1KB file",
|
||||||
size: 1024,
|
size: 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "10KB file",
|
name: "10KB file",
|
||||||
size: 10 * 1024,
|
size: 10 * 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "100KB file",
|
name: "100KB file",
|
||||||
size: 100 * 1024,
|
size: 100 * 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "1MB file",
|
name: "1MB file",
|
||||||
size: 1024 * 1024,
|
size: 1024 * 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "10MB file",
|
name: "10MB file",
|
||||||
size: 10 * 1024 * 1024,
|
size: 10 * 1024 * 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "99MB file",
|
name: "99MB file",
|
||||||
size: 99 * 1024 * 1024,
|
size: 99 * 1024 * 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "100MB file",
|
name: "100MB file",
|
||||||
size: 100 * 1024 * 1024,
|
size: 100 * 1024 * 1024,
|
||||||
|
shouldError: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "101MB file - should fail",
|
name: "101MB file - should fail",
|
||||||
size: 101 * 1024 * 1024,
|
size: 101 * 1024 * 1024,
|
||||||
wantErr: errSecretTooLarge,
|
shouldError: true,
|
||||||
|
errorMsg: "secret file too large: exceeds 100MB limit",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
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, tt.shouldError, tt.errorMsg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestGetCommandOutputsToStdout tests that 'secret get' outputs the secret
|
// TestGetCommandOutputsToStdout tests that 'secret get' outputs the secret
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExecuteCommandInProcess executes a CLI command in-process for testing
|
// ExecuteCommandInProcess executes a CLI command in-process for testing
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package cli_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/cli"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//nolint:paralleltest // executes the CLI in-process against shared state
|
//nolint:paralleltest // executes the CLI in-process against shared state
|
||||||
|
|||||||
@@ -1,374 +0,0 @@
|
|||||||
// Unlock Failure Tests
|
|
||||||
//
|
|
||||||
// When a vault cannot be opened through its current unlocker, because a
|
|
||||||
// file the unlocker needs is missing or the passphrase is wrong, the error
|
|
||||||
// keeps its cause and ends by saying that the mnemonic still opens that
|
|
||||||
// vault, but only for a vault that the mnemonic does open, and not when the
|
|
||||||
// passphrase could not be read at all. When a secret's current file is
|
|
||||||
// missing, the error says how to make a version current again. Each test
|
|
||||||
// that pins such advice also follows it.
|
|
||||||
|
|
||||||
package cli_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"filippo.io/age"
|
|
||||||
"github.com/awnumar/memguard"
|
|
||||||
"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"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// mnemonicAdvice ends the error when the current vault "default", which
|
|
||||||
// its mnemonic opens, cannot be opened through its current unlocker.
|
|
||||||
mnemonicAdvice = "; the vault 'default' still opens with its mnemonic: " +
|
|
||||||
"run 'secret unlocker add passphrase' with SB_SECRET_MNEMONIC set " +
|
|
||||||
"to the mnemonic to give it a new unlocker"
|
|
||||||
|
|
||||||
// versionAdvice ends the error when a secret's current file cannot be
|
|
||||||
// read.
|
|
||||||
versionAdvice = "; this file only names the current version: " +
|
|
||||||
"'secret version list' lists the secret's versions, and " +
|
|
||||||
"'secret version promote' makes one of them current"
|
|
||||||
|
|
||||||
// unlockTestVaultDir is the directory of the vault "default" of
|
|
||||||
// newTwoVaultFs, the current vault, whose secret "x" is "value".
|
|
||||||
unlockTestVaultDir = testStateDir + "/vaults.d/default"
|
|
||||||
)
|
|
||||||
|
|
||||||
// currentUnlockerDir returns the directory of the current unlocker of the
|
|
||||||
// vault in vaultDir on fs.
|
|
||||||
func currentUnlockerDir(t *testing.T, fs afero.Fs, vaultDir string) string {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
unlockerName, err := afero.ReadFile(fs,
|
|
||||||
filepath.Join(vaultDir, "current-unlocker"))
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
return filepath.Join(vaultDir, "unlockers.d", string(unlockerName))
|
|
||||||
}
|
|
||||||
|
|
||||||
// newUnlockTestCLI returns the directory of the current unlocker of the
|
|
||||||
// vault "default" on fs, a copy of the vaults of newTwoVaultFs, and a CLI
|
|
||||||
// instance on fs that has the unlock passphrase, as from the environment,
|
|
||||||
// but not the mnemonic.
|
|
||||||
func newUnlockTestCLI(t *testing.T, fs afero.Fs) (string, *cli.Instance) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
c := cli.NewCLIInstanceWithStateDir(fs, testStateDir)
|
|
||||||
c.UnlockPassphrase = memguard.NewBufferFromBytes([]byte(testPassphrase))
|
|
||||||
t.Cleanup(c.UnlockPassphrase.Destroy)
|
|
||||||
|
|
||||||
return currentUnlockerDir(t, fs, unlockTestVaultDir), c
|
|
||||||
}
|
|
||||||
|
|
||||||
// discardCmd returns a command whose output is discarded.
|
|
||||||
func discardCmd() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{}
|
|
||||||
cmd.SetOut(io.Discard)
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
// getSecret returns what `secret get name` prints.
|
|
||||||
func getSecret(t *testing.T, c *cli.Instance, name string) string {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
var out bytes.Buffer
|
|
||||||
|
|
||||||
cmd := &cobra.Command{}
|
|
||||||
cmd.SetOut(&out)
|
|
||||||
require.NoError(t, c.GetSecret(cmd, name))
|
|
||||||
|
|
||||||
return out.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestUnlockFailureNamesMnemonic checks the error of `secret get` when a
|
|
||||||
// file that opening the vault through its current unlocker needs is
|
|
||||||
// missing: it keeps the cause, which names the file, and ends with the
|
|
||||||
// advice that the mnemonic still opens the vault. The test then follows
|
|
||||||
// that advice: `secret unlocker add passphrase`, with the mnemonic, gives
|
|
||||||
// the vault a new unlocker, which opens it.
|
|
||||||
func TestUnlockFailureNamesMnemonic(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
file string // the file removed
|
|
||||||
inVaultDir bool // the file is the vault's, not the unlocker's
|
|
||||||
want string // the message before the cause
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
file: "current-unlocker",
|
|
||||||
inVaultDir: true,
|
|
||||||
want: "failed to unlock vault: failed to get long-term key: " +
|
|
||||||
"failed to get current unlocker: " +
|
|
||||||
"failed to read current unlocker: ",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
file: "priv.age",
|
|
||||||
want: "failed to unlock vault: failed to get long-term key: " +
|
|
||||||
"failed to get unlocker identity: " +
|
|
||||||
"failed to read unlocker private key: ",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
file: "longterm.age",
|
|
||||||
want: "failed to unlock vault: failed to get long-term key: " +
|
|
||||||
"failed to read encrypted long-term private key: ",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.file, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
fs := newTwoVaultFs(t)
|
|
||||||
unlockerDir, c := newUnlockTestCLI(t, fs)
|
|
||||||
|
|
||||||
path := filepath.Join(unlockerDir, tt.file)
|
|
||||||
|
|
||||||
if tt.inVaultDir {
|
|
||||||
path = filepath.Join(unlockTestVaultDir, tt.file)
|
|
||||||
}
|
|
||||||
|
|
||||||
require.NoError(t, fs.Remove(path))
|
|
||||||
|
|
||||||
err := c.GetSecret(discardCmd(), "x")
|
|
||||||
|
|
||||||
var cause *os.PathError
|
|
||||||
|
|
||||||
require.ErrorAs(t, err, &cause)
|
|
||||||
require.ErrorIs(t, err, os.ErrNotExist)
|
|
||||||
assert.Equal(t, path, cause.Path)
|
|
||||||
|
|
||||||
require.EqualError(t, err, tt.want+cause.Error()+mnemonicAdvice)
|
|
||||||
|
|
||||||
c.Mnemonic = testMnemonicBuffer(t)
|
|
||||||
require.NoError(t, c.UnlockersAdd("passphrase", discardCmd()))
|
|
||||||
|
|
||||||
c.Mnemonic = nil
|
|
||||||
assert.Equal(t, "value", getSecret(t, c, "x"))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestWrongPassphraseNamesMnemonic checks the error of `secret get` given a
|
|
||||||
// passphrase that does not decrypt the passphrase unlocker: it keeps age's
|
|
||||||
// error and ends with the advice that the mnemonic still opens the vault.
|
|
||||||
func TestWrongPassphraseNamesMnemonic(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
_, c := newUnlockTestCLI(t, newTwoVaultFs(t))
|
|
||||||
c.UnlockPassphrase = memguard.NewBufferFromBytes([]byte("wrong passphrase"))
|
|
||||||
t.Cleanup(c.UnlockPassphrase.Destroy)
|
|
||||||
|
|
||||||
err := c.GetSecret(discardCmd(), "x")
|
|
||||||
|
|
||||||
var noMatch *age.NoIdentityMatchError
|
|
||||||
|
|
||||||
require.ErrorAs(t, err, &noMatch)
|
|
||||||
|
|
||||||
require.EqualError(t, err, "failed to unlock vault: "+
|
|
||||||
"failed to get long-term key: failed to get unlocker identity: "+
|
|
||||||
"failed to decrypt unlocker private key: failed to create decryptor: "+
|
|
||||||
noMatch.Error()+mnemonicAdvice)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestMoveUnlockFailureNamesVault checks the error of `secret move` into
|
|
||||||
// the vault "work", which is not the current vault, when "work" cannot be
|
|
||||||
// opened through its current unlocker: the advice names "work" and says to
|
|
||||||
// select it first, since `secret unlocker add` acts on the current vault.
|
|
||||||
// The test then follows that advice, and the move succeeds.
|
|
||||||
func TestMoveUnlockFailureNamesVault(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
fs := newTwoVaultFs(t)
|
|
||||||
_, c := newUnlockTestCLI(t, fs)
|
|
||||||
|
|
||||||
path := filepath.Join(
|
|
||||||
currentUnlockerDir(t, fs, testStateDir+"/vaults.d/work"), "priv.age")
|
|
||||||
require.NoError(t, fs.Remove(path))
|
|
||||||
|
|
||||||
err := c.MoveSecret(discardCmd(), "default:x", "work:y", false)
|
|
||||||
|
|
||||||
var cause *os.PathError
|
|
||||||
|
|
||||||
require.ErrorAs(t, err, &cause)
|
|
||||||
assert.Equal(t, path, cause.Path)
|
|
||||||
|
|
||||||
require.EqualError(t, err, "failed to unlock destination vault 'work': "+
|
|
||||||
"failed to get unlocker identity: failed to read unlocker private key: "+
|
|
||||||
cause.Error()+"; the vault 'work' still opens with its mnemonic: "+
|
|
||||||
"run 'secret vault select work', then 'secret unlocker add passphrase' "+
|
|
||||||
"with SB_SECRET_MNEMONIC set to the mnemonic to give it a new unlocker")
|
|
||||||
|
|
||||||
require.NoError(t, c.SelectVault(discardCmd(), "work"))
|
|
||||||
|
|
||||||
c.Mnemonic = testMnemonicBuffer(t)
|
|
||||||
require.NoError(t, c.UnlockersAdd("passphrase", discardCmd()))
|
|
||||||
|
|
||||||
c.Mnemonic = nil
|
|
||||||
require.NoError(t, c.MoveSecret(discardCmd(), "default:x", "work:y", false))
|
|
||||||
assert.Equal(t, "value", getSecret(t, c, "y"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestPassphraseNotReadNamesNoMnemonic runs `secret get x` on the built
|
|
||||||
// binary without SB_UNLOCK_PASSPHRASE and with a stdin that is not a
|
|
||||||
// terminal, so the passphrase cannot be read. The unlocker was not tried,
|
|
||||||
// and adding one would need a passphrase read the same way, so the error
|
|
||||||
// is the cause alone, without the advice to use the mnemonic.
|
|
||||||
func TestPassphraseNotReadNamesNoMnemonic(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
stateDir := t.TempDir()
|
|
||||||
|
|
||||||
mnemonic := memguard.NewBufferFromBytes([]byte(testMnemonic))
|
|
||||||
defer mnemonic.Destroy()
|
|
||||||
|
|
||||||
passphrase := memguard.NewBufferFromBytes([]byte(testPassphrase))
|
|
||||||
defer passphrase.Destroy()
|
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(
|
|
||||||
afero.NewOsFs(), stateDir, "default", mnemonic, passphrase)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
value := memguard.NewBufferFromBytes([]byte("value"))
|
|
||||||
defer value.Destroy()
|
|
||||||
|
|
||||||
require.NoError(t, vlt.AddSecret("x", value, false))
|
|
||||||
|
|
||||||
//nolint:gosec // G204: test executes the freshly built secret binary
|
|
||||||
cmd := exec.CommandContext(t.Context(), secretBinaryPath(t), "get", "x")
|
|
||||||
cmd.Env = []string{
|
|
||||||
secret.EnvStateDir + "=" + stateDir,
|
|
||||||
"PATH=" + os.Getenv("PATH"),
|
|
||||||
"HOME=" + os.Getenv("HOME"),
|
|
||||||
}
|
|
||||||
|
|
||||||
output, err := cmd.CombinedOutput()
|
|
||||||
require.Error(t, err)
|
|
||||||
|
|
||||||
assert.Equal(t, "Error: failed to unlock vault: "+
|
|
||||||
"failed to get long-term key: failed to get unlocker identity: "+
|
|
||||||
"failed to read passphrase: stdin is not a terminal (piped input or "+
|
|
||||||
"script). Please set the SB_UNLOCK_PASSPHRASE environment variable or "+
|
|
||||||
"run interactively\n", string(output))
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestCryptoUnlockFailureNamesMnemonic checks that `secret encrypt` and
|
|
||||||
// `secret decrypt`, reading the key secret, end with the same advice as
|
|
||||||
// `secret get` when the vault cannot be opened through its current
|
|
||||||
// unlocker.
|
|
||||||
func TestCryptoUnlockFailureNamesMnemonic(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
command string
|
|
||||||
run func(c *cli.Instance) error
|
|
||||||
}{
|
|
||||||
{"encrypt", func(c *cli.Instance) error { return c.Encrypt("x", "", "") }},
|
|
||||||
{"decrypt", func(c *cli.Instance) error { return c.Decrypt("x", "", "") }},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.command, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
fs := newTwoVaultFs(t)
|
|
||||||
unlockerDir, c := newUnlockTestCLI(t, fs)
|
|
||||||
|
|
||||||
path := filepath.Join(unlockerDir, "priv.age")
|
|
||||||
require.NoError(t, fs.Remove(path))
|
|
||||||
|
|
||||||
err := tt.run(c)
|
|
||||||
|
|
||||||
var cause *os.PathError
|
|
||||||
|
|
||||||
require.ErrorAs(t, err, &cause)
|
|
||||||
assert.Equal(t, path, cause.Path)
|
|
||||||
|
|
||||||
require.EqualError(t, err, "failed to get secret value: "+
|
|
||||||
"failed to unlock vault: failed to get long-term key: "+
|
|
||||||
"failed to get unlocker identity: "+
|
|
||||||
"failed to read unlocker private key: "+cause.Error()+
|
|
||||||
mnemonicAdvice)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestMissingCurrentFileNamesVersionCommands checks the error of `secret
|
|
||||||
// get` when the secret's current file is missing: it keeps the cause, which
|
|
||||||
// names the file, and ends with the advice that says how to make a version
|
|
||||||
// current again. The test then follows that advice.
|
|
||||||
func TestMissingCurrentFileNamesVersionCommands(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
fs := newTwoVaultFs(t)
|
|
||||||
_, c := newUnlockTestCLI(t, fs)
|
|
||||||
|
|
||||||
secretDir := filepath.Join(unlockTestVaultDir, "secrets.d", "x")
|
|
||||||
path := filepath.Join(secretDir, "current")
|
|
||||||
require.NoError(t, fs.Remove(path))
|
|
||||||
|
|
||||||
err := c.GetSecret(discardCmd(), "x")
|
|
||||||
|
|
||||||
var cause *os.PathError
|
|
||||||
|
|
||||||
require.ErrorAs(t, err, &cause)
|
|
||||||
require.ErrorIs(t, err, os.ErrNotExist)
|
|
||||||
assert.Equal(t, path, cause.Path)
|
|
||||||
|
|
||||||
require.EqualError(t, err, "failed to get current version: "+
|
|
||||||
"failed to read current version file: "+cause.Error()+versionAdvice)
|
|
||||||
|
|
||||||
versions, err := afero.ReadDir(fs, filepath.Join(secretDir, "versions"))
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Len(t, versions, 1)
|
|
||||||
|
|
||||||
var out bytes.Buffer
|
|
||||||
|
|
||||||
cmd := &cobra.Command{}
|
|
||||||
cmd.SetOut(&out)
|
|
||||||
require.NoError(t, c.ListVersions(cmd, "x"))
|
|
||||||
assert.Contains(t, out.String(), versions[0].Name())
|
|
||||||
|
|
||||||
require.NoError(t, c.PromoteVersion(cmd, "x", versions[0].Name()))
|
|
||||||
assert.Equal(t, "value", getSecret(t, c, "x"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestUnlockFailureWithoutLongTermKeyNamesNoMnemonic checks that a vault
|
|
||||||
// created without a mnemonic, which no mnemonic opens, gets no advice to
|
|
||||||
// use one: `secret unlocker add passphrase` there fails with the cause
|
|
||||||
// alone.
|
|
||||||
func TestUnlockFailureWithoutLongTermKeyNamesNoMnemonic(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
|
|
||||||
_, err := vault.CreateVault(fs, testStateDir, "keyless", nil, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
c := cli.NewCLIInstanceWithStateDir(fs, testStateDir)
|
|
||||||
c.UnlockPassphrase = memguard.NewBufferFromBytes([]byte(testPassphrase))
|
|
||||||
t.Cleanup(c.UnlockPassphrase.Destroy)
|
|
||||||
|
|
||||||
err = c.UnlockersAdd("passphrase", discardCmd())
|
|
||||||
|
|
||||||
var cause *os.PathError
|
|
||||||
|
|
||||||
require.ErrorAs(t, err, &cause)
|
|
||||||
|
|
||||||
require.EqualError(t, err, "failed to get long-term key: "+
|
|
||||||
"failed to get current unlocker: failed to read current unlocker: "+
|
|
||||||
cause.Error())
|
|
||||||
}
|
|
||||||
+163
-25
@@ -6,7 +6,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"maps"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -15,10 +14,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Unlocker type names and platform identifiers shared across the CLI
|
// Unlocker type names and platform identifiers shared across the CLI
|
||||||
@@ -39,10 +38,15 @@ var (
|
|||||||
errInvalidUnlockerType = errors.New("invalid unlocker type")
|
errInvalidUnlockerType = errors.New("invalid unlocker type")
|
||||||
errKeyIDOnlyForPGP = errors.New(
|
errKeyIDOnlyForPGP = errors.New(
|
||||||
"--keyid flag is only valid for PGP unlockers")
|
"--keyid flag is only valid for PGP unlockers")
|
||||||
|
errKeychainMacOSOnly = errors.New(
|
||||||
|
"keychain unlockers are only supported on macOS")
|
||||||
|
errSecureEnclaveMacOSOnly = errors.New(
|
||||||
|
"secure enclave unlockers are only supported on macOS")
|
||||||
// errGPGKeyAlreadyUnlocker carries only the message tail; the caller
|
// errGPGKeyAlreadyUnlocker carries only the message tail; the caller
|
||||||
// composes "GPG key <id> is already added as an unlocker".
|
// composes "GPG key <id> is already added as an unlocker".
|
||||||
errGPGKeyAlreadyUnlocker = errors.New(
|
errGPGKeyAlreadyUnlocker = errors.New(
|
||||||
"is already added as an unlocker")
|
"is already added as an unlocker")
|
||||||
|
errUnsupportedUnlockerType = errors.New("unsupported unlocker type")
|
||||||
)
|
)
|
||||||
|
|
||||||
// UnlockerInfo represents unlocker information for display
|
// UnlockerInfo represents unlocker information for display
|
||||||
@@ -309,8 +313,91 @@ func newUnlockerSelectCmd() *cobra.Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnlockersList lists unlockers in the current vault, each under its ID,
|
// unlockerIDFromDir constructs an unlocker of the given metadata type
|
||||||
// the name of its directory in unlockers.d
|
// rooted at unlockerDir and returns its ID. Returns "" for unknown types
|
||||||
|
// and, when includeSecureEnclave is false, for secure enclave unlockers.
|
||||||
|
func unlockerIDFromDir(
|
||||||
|
fs afero.Fs, unlockerDir string, metadata secret.UnlockerMetadata,
|
||||||
|
includeSecureEnclave bool,
|
||||||
|
) string {
|
||||||
|
// Create the appropriate unlocker instance
|
||||||
|
var unlocker secret.Unlocker
|
||||||
|
|
||||||
|
switch metadata.Type {
|
||||||
|
case unlockerTypePassphrase:
|
||||||
|
unlocker = secret.NewPassphraseUnlocker(fs, unlockerDir, metadata)
|
||||||
|
case unlockerTypeKeychain:
|
||||||
|
unlocker = secret.NewKeychainUnlocker(fs, unlockerDir, metadata)
|
||||||
|
case unlockerTypePGP:
|
||||||
|
unlocker = secret.NewPGPUnlocker(fs, unlockerDir, metadata)
|
||||||
|
case unlockerTypeSecureEnclave:
|
||||||
|
if includeSecureEnclave {
|
||||||
|
unlocker = secret.NewSecureEnclaveUnlocker(fs, unlockerDir, metadata)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if unlocker == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return unlocker.GetID()
|
||||||
|
}
|
||||||
|
|
||||||
|
// findUnlockerIDByMetadata scans unlockersDir for the directory whose
|
||||||
|
// stored metadata matches the given type and creation time and returns
|
||||||
|
// the matching unlocker's ID. It returns ("", nil) when the directory is
|
||||||
|
// readable but holds no match, and a non-nil error when the directory
|
||||||
|
// itself cannot be read. Callers must distinguish the two: an unreadable
|
||||||
|
// directory means the unlocker's real ID is unknowable, so the entry has
|
||||||
|
// to be skipped rather than reported under a synthesized ID.
|
||||||
|
//
|
||||||
|
// A metadata file that cannot be read or parsed is skipped without a
|
||||||
|
// warning: every caller gets metadata from vault.ListUnlockers first,
|
||||||
|
// which has already warned about that directory.
|
||||||
|
func findUnlockerIDByMetadata(
|
||||||
|
fs afero.Fs, unlockersDir string, metadata secret.UnlockerMetadata,
|
||||||
|
includeSecureEnclave bool,
|
||||||
|
) (string, error) {
|
||||||
|
files, err := afero.ReadDir(fs, unlockersDir)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf(
|
||||||
|
"failed to read unlockers directory %s: %w", unlockersDir, err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
if !file.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
unlockerDir := filepath.Join(unlockersDir, file.Name())
|
||||||
|
metadataPath := filepath.Join(unlockerDir, "unlocker-metadata.json")
|
||||||
|
|
||||||
|
// Check if this is the right unlocker by comparing metadata
|
||||||
|
metadataBytes, err := afero.ReadFile(fs, metadataPath)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var diskMetadata secret.UnlockerMetadata
|
||||||
|
|
||||||
|
err = json.Unmarshal(metadataBytes, &diskMetadata)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match by type and creation time
|
||||||
|
if diskMetadata.Type == metadata.Type &&
|
||||||
|
diskMetadata.CreatedAt.Equal(metadata.CreatedAt) {
|
||||||
|
return unlockerIDFromDir(fs, unlockerDir, diskMetadata,
|
||||||
|
includeSecureEnclave), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnlockersList lists unlockers in the current vault
|
||||||
func (cli *Instance) UnlockersList(jsonOutput bool) error {
|
func (cli *Instance) UnlockersList(jsonOutput bool) error {
|
||||||
// Get current vault
|
// Get current vault
|
||||||
vlt, err := vault.GetCurrentVault(cli.fs, cli.stateDir)
|
vlt, err := vault.GetCurrentVault(cli.fs, cli.stateDir)
|
||||||
@@ -326,23 +413,58 @@ func (cli *Instance) UnlockersList(jsonOutput bool) error {
|
|||||||
currentUnlockerID = currentUnlocker.GetID()
|
currentUnlockerID = currentUnlocker.GetID()
|
||||||
}
|
}
|
||||||
|
|
||||||
unlockerMetadata, err := vlt.ListUnlockers()
|
// Get the metadata first
|
||||||
|
unlockerMetadataList, err := vlt.ListUnlockers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load actual unlocker objects to get the proper IDs
|
||||||
var unlockers []UnlockerInfo
|
var unlockers []UnlockerInfo
|
||||||
|
|
||||||
for _, unlockerID := range slices.Sorted(maps.Keys(unlockerMetadata)) {
|
for _, metadata := range unlockerMetadataList {
|
||||||
metadata := unlockerMetadata[unlockerID]
|
// Create unlocker instance to get the proper ID
|
||||||
|
vaultDir, err := vlt.GetDirectory()
|
||||||
|
if err != nil {
|
||||||
|
secret.Warn("Could not get vault directory while listing unlockers",
|
||||||
|
"error", err)
|
||||||
|
|
||||||
unlockers = append(unlockers, UnlockerInfo{
|
continue
|
||||||
ID: unlockerID,
|
}
|
||||||
|
|
||||||
|
// Find the unlocker directory by type and created time
|
||||||
|
unlockersDir := filepath.Join(vaultDir, "unlockers.d")
|
||||||
|
|
||||||
|
unlockerID, err := findUnlockerIDByMetadata(
|
||||||
|
cli.fs, unlockersDir, metadata, true,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
secret.Warn("Could not read unlockers directory, skipping unlocker",
|
||||||
|
"unlockers_dir", unlockersDir, "error", err)
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the proper ID using the unlocker's ID() method
|
||||||
|
var properID string
|
||||||
|
if unlockerID != "" {
|
||||||
|
properID = unlockerID
|
||||||
|
} else {
|
||||||
|
// Generate ID as fallback
|
||||||
|
properID = fmt.Sprintf("%s-%s",
|
||||||
|
metadata.CreatedAt.Format("2006-01-02.15.04"), metadata.Type)
|
||||||
|
secret.Warn("Could not create unlocker instance, using fallback ID",
|
||||||
|
"fallback_id", properID, "type", metadata.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
unlockerInfo := UnlockerInfo{
|
||||||
|
ID: properID,
|
||||||
Type: metadata.Type,
|
Type: metadata.Type,
|
||||||
CreatedAt: metadata.CreatedAt,
|
CreatedAt: metadata.CreatedAt,
|
||||||
Flags: metadata.Flags,
|
Flags: metadata.Flags,
|
||||||
IsCurrent: unlockerID == currentUnlockerID,
|
IsCurrent: properID == currentUnlockerID,
|
||||||
})
|
}
|
||||||
|
unlockers = append(unlockers, unlockerInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
if jsonOutput {
|
if jsonOutput {
|
||||||
@@ -434,7 +556,7 @@ func (cli *Instance) UnlockersAdd(unlockerType string, cmd *cobra.Command) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf("%w: %s (supported: %s)",
|
return fmt.Errorf("%w: %s (supported: %s)",
|
||||||
errInvalidUnlockerType, unlockerType, supportedTypes)
|
errUnsupportedUnlockerType, unlockerType, supportedTypes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -469,7 +591,7 @@ func (cli *Instance) addPassphraseUnlocker(cmd *cobra.Command) error {
|
|||||||
// Use secure passphrase input with confirmation
|
// Use secure passphrase input with confirmation
|
||||||
passphraseBuffer, err = readSecurePassphrase("Enter passphrase for unlocker: ")
|
passphraseBuffer, err = readSecurePassphrase("Enter passphrase for unlocker: ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to read passphrase: %w", err)
|
||||||
}
|
}
|
||||||
defer passphraseBuffer.Destroy()
|
defer passphraseBuffer.Destroy()
|
||||||
}
|
}
|
||||||
@@ -489,6 +611,10 @@ func (cli *Instance) addPassphraseUnlocker(cmd *cobra.Command) error {
|
|||||||
|
|
||||||
// addKeychainUnlocker creates a macOS Keychain unlocker in the current vault
|
// addKeychainUnlocker creates a macOS Keychain unlocker in the current vault
|
||||||
func (cli *Instance) addKeychainUnlocker(cmd *cobra.Command) error {
|
func (cli *Instance) addKeychainUnlocker(cmd *cobra.Command) error {
|
||||||
|
if runtime.GOOS != platformDarwin {
|
||||||
|
return errKeychainMacOSOnly
|
||||||
|
}
|
||||||
|
|
||||||
keychainUnlocker, err := secret.CreateKeychainUnlocker(
|
keychainUnlocker, err := secret.CreateKeychainUnlocker(
|
||||||
cli.fs, cli.stateDir, cli.Mnemonic, cli.UnlockPassphrase)
|
cli.fs, cli.stateDir, cli.Mnemonic, cli.UnlockPassphrase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -516,6 +642,10 @@ func (cli *Instance) addKeychainUnlocker(cmd *cobra.Command) error {
|
|||||||
// addSecureEnclaveUnlocker creates a Secure Enclave unlocker in the
|
// addSecureEnclaveUnlocker creates a Secure Enclave unlocker in the
|
||||||
// current vault
|
// current vault
|
||||||
func (cli *Instance) addSecureEnclaveUnlocker(cmd *cobra.Command) error {
|
func (cli *Instance) addSecureEnclaveUnlocker(cmd *cobra.Command) error {
|
||||||
|
if runtime.GOOS != platformDarwin {
|
||||||
|
return errSecureEnclaveMacOSOnly
|
||||||
|
}
|
||||||
|
|
||||||
seUnlocker, err := secret.CreateSecureEnclaveUnlocker(
|
seUnlocker, err := secret.CreateSecureEnclaveUnlocker(
|
||||||
cli.fs, cli.stateDir, cli.Mnemonic, cli.UnlockPassphrase)
|
cli.fs, cli.stateDir, cli.Mnemonic, cli.UnlockPassphrase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -567,7 +697,9 @@ func (cli *Instance) addPGPUnlocker(cmd *cobra.Command) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if this GPG key is already added
|
// Check if this GPG key is already added
|
||||||
exists, err := cli.pgpUnlockerExists(vlt, fingerprint)
|
expectedID := "pgp-" + fingerprint
|
||||||
|
|
||||||
|
exists, err := cli.checkUnlockerExists(vlt, expectedID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"could not check whether GPG key %s is already an unlocker: %w",
|
"could not check whether GPG key %s is already an unlocker: %w",
|
||||||
@@ -672,7 +804,13 @@ func (cli *Instance) findUnlockerToRemove(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(unlockers) == 1 {
|
if len(unlockers) == 1 {
|
||||||
_, found.last = unlockers[unlockerID]
|
lastID, err := findUnlockerIDByMetadata(
|
||||||
|
cli.fs, unlockersDir, unlockers[0], true)
|
||||||
|
if err != nil {
|
||||||
|
return unlockerToRemove{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
found.last = lastID == unlockerID
|
||||||
}
|
}
|
||||||
|
|
||||||
// unlockerID may instead name a directory left out of the list. If its
|
// unlockerID may instead name a directory left out of the list. If its
|
||||||
@@ -751,16 +889,16 @@ func (cli *Instance) UnlockerSelect(unlockerID string) error {
|
|||||||
return vlt.SelectUnlocker(unlockerID)
|
return vlt.SelectUnlocker(unlockerID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pgpUnlockerExists reports whether the vault already has a PGP unlocker
|
// checkUnlockerExists reports whether the vault already has an unlocker
|
||||||
// for the GPG key with the given fingerprint. It returns an error, and no
|
// with the given ID. It returns an error, and no answer, when unlockers.d
|
||||||
// answer, when unlockers.d or an unlocker's metadata file cannot be read;
|
// or an unlocker's metadata file cannot be read; the caller must then not
|
||||||
// the caller must then not create the unlocker. It reads unlockers.d itself
|
// create the unlocker. It reads unlockers.d itself because
|
||||||
// because vault.ListUnlockers skips an unlocker it cannot read, which suits
|
// vault.ListUnlockers skips an unlocker it cannot read, which suits
|
||||||
// `unlocker list` but not this check: the skipped unlocker may be the
|
// `unlocker list` but not this check: the skipped unlocker may be the
|
||||||
// duplicate. A directory whose metadata file is missing or corrupt is not
|
// duplicate. A directory whose metadata file is missing or corrupt is not
|
||||||
// a working unlocker and is passed over.
|
// a working unlocker and is passed over.
|
||||||
func (cli *Instance) pgpUnlockerExists(
|
func (cli *Instance) checkUnlockerExists(
|
||||||
vlt *vault.Vault, fingerprint string,
|
vlt *vault.Vault, unlockerID string,
|
||||||
) (bool, error) {
|
) (bool, error) {
|
||||||
vaultDir, err := vlt.GetDirectory()
|
vaultDir, err := vlt.GetDirectory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -799,14 +937,14 @@ func (cli *Instance) pgpUnlockerExists(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
var metadata secret.PGPUnlockerMetadata
|
var metadata secret.UnlockerMetadata
|
||||||
|
|
||||||
err = json.Unmarshal(metadataBytes, &metadata)
|
err = json.Unmarshal(metadataBytes, &metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if metadata.Type == unlockerTypePGP && metadata.GPGKeyID == fingerprint {
|
if unlockerIDFromDir(cli.fs, unlockerDir, metadata, true) == unlockerID {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,11 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// unknownTestGPGUserID is a GPG user ID that no key in the test keyring has.
|
// unknownTestGPGUserID is a GPG user ID that no key in the test keyring has.
|
||||||
@@ -48,7 +47,7 @@ func TestAddPGPUnlocker(t *testing.T) {
|
|||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
vlt, err := vault.CreateVault(fs, listTestStateDir, listTestVaultName,
|
vlt, err := vault.CreateVault(fs, listTestStateDir, listTestVaultName,
|
||||||
testMnemonicBuffer(t), nil)
|
testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
err = vlt.AddSecret(addTestSecretName,
|
err = vlt.AddSecret(addTestSecretName,
|
||||||
@@ -99,7 +98,7 @@ func TestAddPGPUnlockerUnknownKey(t *testing.T) {
|
|||||||
|
|
||||||
err := instance.addPGPUnlocker(cmd)
|
err := instance.addPGPUnlocker(cmd)
|
||||||
|
|
||||||
require.ErrorIs(t, err, secret.ErrGPGKeyNotFound)
|
require.ErrorContains(t, err, "failed to resolve GPG key fingerprint")
|
||||||
assertDirEntries(t, base,
|
assertDirEntries(t, base,
|
||||||
filepath.Join(testVaultDir(listTestVaultName), listTestUnlockersDirName),
|
filepath.Join(testVaultDir(listTestVaultName), listTestUnlockersDirName),
|
||||||
listTestUnlockerDirOne)
|
listTestUnlockerDirOne)
|
||||||
|
|||||||
@@ -17,10 +17,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// newCorruptUnlockerVault returns the two-unlocker test vault with the
|
// newCorruptUnlockerVault returns the two-unlocker test vault with the
|
||||||
@@ -46,7 +46,7 @@ func TestUnlockerSelectSkipsCorruptUnlocker(t *testing.T) {
|
|||||||
fs := newCorruptUnlockerVault(t)
|
fs := newCorruptUnlockerVault(t)
|
||||||
instance, _ := newTestInstance(fs)
|
instance, _ := newTestInstance(fs)
|
||||||
|
|
||||||
require.NoError(t, instance.UnlockerSelect(listTestUnlockerDirTwo))
|
require.NoError(t, instance.UnlockerSelect("pgp-"+listTestGPGKeyID+"B"))
|
||||||
|
|
||||||
current, err := afero.ReadFile(fs,
|
current, err := afero.ReadFile(fs,
|
||||||
filepath.Join(testVaultDir(listTestVaultName), "current-unlocker"))
|
filepath.Join(testVaultDir(listTestVaultName), "current-unlocker"))
|
||||||
@@ -72,7 +72,7 @@ func TestUnlockerRemoveWithCorruptUnlocker(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "the other unlocker",
|
name: "the other unlocker",
|
||||||
unlockerID: listTestUnlockerDirTwo,
|
unlockerID: "pgp-" + listTestGPGKeyID + "B",
|
||||||
wantLast: true,
|
wantLast: true,
|
||||||
wantEntries: []string{listTestUnlockerDirOne},
|
wantEntries: []string{listTestUnlockerDirOne},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,79 +0,0 @@
|
|||||||
//nolint:testpackage // white-box test of unexported internals
|
|
||||||
package cli
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"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"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestSameMetadataUnlockersHaveTheirOwnIDs writes two passphrase unlockers
|
|
||||||
// side by side whose metadata is the same, creation time included, as
|
|
||||||
// copying an unlocker directory leaves them. It asserts that `unlocker
|
|
||||||
// list` and the shell completion of `unlocker select` and `unlocker remove`
|
|
||||||
// give each its own ID, and that each is selected and removed by its ID
|
|
||||||
// alone. Keychain and Secure Enclave unlockers, which only macOS can add,
|
|
||||||
// get their IDs the same way.
|
|
||||||
func TestSameMetadataUnlockersHaveTheirOwnIDs(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
_, err := vault.CreateVault(fs, listTestStateDir, listTestVaultName,
|
|
||||||
testMnemonicBuffer(t), nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
vaultDir := testVaultDir(listTestVaultName)
|
|
||||||
unlockersDir := filepath.Join(vaultDir, listTestUnlockersDirName)
|
|
||||||
dirNames := []string{
|
|
||||||
"passphrase-2026-10-04.12.30.00.000000000",
|
|
||||||
"passphrase-2026-10-04.12.30.00.000000000-copy",
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata, err := json.Marshal(secret.UnlockerMetadata{
|
|
||||||
Type: unlockerTypePassphrase,
|
|
||||||
CreatedAt: time.Date(2026, time.October, 4, 12, 30, 0, 0, time.UTC),
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
for _, dirName := range dirNames {
|
|
||||||
dir := filepath.Join(unlockersDir, dirName)
|
|
||||||
require.NoError(t, fs.MkdirAll(dir, listTestDirPerm))
|
|
||||||
require.NoError(t, afero.WriteFile(fs,
|
|
||||||
filepath.Join(dir, listTestMetadataFileName), metadata,
|
|
||||||
listTestFilePerm))
|
|
||||||
}
|
|
||||||
|
|
||||||
listed := listUnlockersJSON(t, fs)
|
|
||||||
require.Len(t, listed, len(dirNames))
|
|
||||||
|
|
||||||
completed, _ := getUnlockerIDsCompletionFunc(fs, listTestStateDir)(
|
|
||||||
nil, nil, "")
|
|
||||||
assert.Equal(t, dirNames, completed)
|
|
||||||
|
|
||||||
instance, cmd := newTestInstance(fs)
|
|
||||||
|
|
||||||
for i, unlocker := range listed {
|
|
||||||
assert.Equal(t, dirNames[i], unlocker.ID)
|
|
||||||
|
|
||||||
require.NoError(t, instance.UnlockerSelect(unlocker.ID))
|
|
||||||
|
|
||||||
current, err := afero.ReadFile(fs,
|
|
||||||
filepath.Join(vaultDir, "current-unlocker"))
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, dirNames[i], string(current))
|
|
||||||
}
|
|
||||||
|
|
||||||
// The second one first: an ID both shared would remove the first one
|
|
||||||
require.NoError(t, instance.UnlockersRemove(listed[1].ID, true, cmd))
|
|
||||||
assertDirEntries(t, fs, unlockersDir, dirNames[0])
|
|
||||||
|
|
||||||
require.NoError(t, instance.UnlockersRemove(listed[0].ID, true, cmd))
|
|
||||||
assertDirEntries(t, fs, unlockersDir)
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,25 @@
|
|||||||
// Unlocker List Tests
|
// Unlocker List Tests
|
||||||
//
|
//
|
||||||
// Tests for `secret unlocker list` behavior when an unlocker's metadata
|
// Tests for `secret unlocker list` behavior when the unlockers.d directory,
|
||||||
// cannot be read or used:
|
// or an unlocker's metadata in it, cannot be read while the listing is
|
||||||
|
// being rendered:
|
||||||
//
|
//
|
||||||
|
// - TestUnlockersListSkipsUnreadableUnlockersDir: an unreadable
|
||||||
|
// unlockers.d yields no rows rather than rows bearing synthesized IDs.
|
||||||
|
// - TestUnlockersListSkipsOnlyUnreadableEntries: a readable entry is
|
||||||
|
// still listed, with its real ID and its current-unlocker marker,
|
||||||
|
// when a later entry's scan fails.
|
||||||
// - TestUnlockersListToleratesCorruptMetadata: one unlocker's corrupt
|
// - TestUnlockersListToleratesCorruptMetadata: one unlocker's corrupt
|
||||||
// metadata does not stop the others from being listed.
|
// metadata does not stop the others from being listed.
|
||||||
// - TestUnlockersListSkipsUnreadableMetadata: an unlocker whose metadata
|
// - TestUnlockersListSkipsUnreadableMetadata: an unlocker whose metadata
|
||||||
// file cannot be checked for or read is left out, and the other is
|
// file cannot be checked for or read is left out, and the other is
|
||||||
// still listed.
|
// still listed.
|
||||||
|
//
|
||||||
|
// The listing resolves each unlocker's real ID by rescanning unlockers.d
|
||||||
|
// after the vault has already enumerated it. If that rescan fails the ID
|
||||||
|
// is unknowable, so the entry must be skipped: a synthesized ID matches
|
||||||
|
// no `unlocker remove` or `unlocker select` argument and would also
|
||||||
|
// suppress the current-unlocker marker.
|
||||||
|
|
||||||
//nolint:testpackage // white-box test of unexported internals
|
//nolint:testpackage // white-box test of unexported internals
|
||||||
package cli
|
package cli
|
||||||
@@ -21,11 +33,11 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"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"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -36,16 +48,18 @@ const (
|
|||||||
// listTestVaultName is the name of that synthetic vault.
|
// listTestVaultName is the name of that synthetic vault.
|
||||||
listTestVaultName = "default"
|
listTestVaultName = "default"
|
||||||
|
|
||||||
// listTestGPGKeyID is the GPG key ID recorded, with a letter appended,
|
// listTestGPGKeyID is the GPG key ID recorded in the readable PGP
|
||||||
// in the PGP unlockers' metadata.
|
// unlocker's metadata. The unlocker's real ID is derived from it, and
|
||||||
|
// differs from the timestamp-derived fallback ID.
|
||||||
listTestGPGKeyID = "DEADBEEFDEADBEEF"
|
listTestGPGKeyID = "DEADBEEFDEADBEEF"
|
||||||
|
|
||||||
// listTestUnlockerDirOne and listTestUnlockerDirTwo are the unlocker
|
// listTestUnlockerDirOne and listTestUnlockerDirTwo are the unlocker
|
||||||
// directory names under unlockers.d, and so the unlockers' IDs.
|
// directory names under unlockers.d.
|
||||||
listTestUnlockerDirOne = "host-pgp-2026-08-09"
|
listTestUnlockerDirOne = "host-pgp-2026-08-09"
|
||||||
listTestUnlockerDirTwo = "host-pgp-2026-08-10"
|
listTestUnlockerDirTwo = "host-pgp-2026-08-10"
|
||||||
|
|
||||||
// listTestUnlockersDirName is the directory holding the unlockers.
|
// listTestUnlockersDirName is the directory the listing rescans to
|
||||||
|
// resolve unlocker IDs.
|
||||||
listTestUnlockersDirName = "unlockers.d"
|
listTestUnlockersDirName = "unlockers.d"
|
||||||
|
|
||||||
// listTestMetadataFileName is the per-unlocker metadata file name.
|
// listTestMetadataFileName is the per-unlocker metadata file name.
|
||||||
@@ -60,16 +74,25 @@ const (
|
|||||||
// a successful open of unlockers.d.
|
// a successful open of unlockers.d.
|
||||||
var errUnlockersDirUnreadable = errors.New("permission denied")
|
var errUnlockersDirUnreadable = errors.New("permission denied")
|
||||||
|
|
||||||
// unlockersDirFailFs fails every open of unlockers.d, as when the
|
// unlockersDirFailFs makes unlockers.d unreadable once it has been opened
|
||||||
// directory cannot be read.
|
// successfully openBudget times. This reproduces the directory becoming
|
||||||
|
// unreadable (permission change, partially restored backup, EIO) between
|
||||||
|
// the vault's own enumeration and the per-entry rescan that resolves
|
||||||
|
// unlocker IDs.
|
||||||
type unlockersDirFailFs struct {
|
type unlockersDirFailFs struct {
|
||||||
afero.Fs
|
afero.Fs
|
||||||
|
|
||||||
|
openBudget int
|
||||||
|
opens int
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:ireturn // afero.File is the interface required by afero.Fs
|
//nolint:ireturn // afero.File is the interface required by afero.Fs
|
||||||
func (f *unlockersDirFailFs) Open(name string) (afero.File, error) {
|
func (f *unlockersDirFailFs) Open(name string) (afero.File, error) {
|
||||||
if filepath.Base(name) == listTestUnlockersDirName {
|
if filepath.Base(name) == listTestUnlockersDirName {
|
||||||
return nil, errUnlockersDirUnreadable
|
f.opens++
|
||||||
|
if f.opens > f.openBudget {
|
||||||
|
return nil, errUnlockersDirUnreadable
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:wrapcheck // test double must return the wrapped Fs error as-is
|
//nolint:wrapcheck // test double must return the wrapped Fs error as-is
|
||||||
@@ -119,8 +142,8 @@ func (f *metadataStatFailFs) Stat(name string) (os.FileInfo, error) {
|
|||||||
return f.Fs.Stat(name)
|
return f.Fs.Stat(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// writePGPUnlocker writes a PGP unlocker directory named dirName, with
|
// writePGPUnlocker writes a PGP unlocker directory with metadata that
|
||||||
// metadata recording the GPG key ID keyID.
|
// yields the real ID "pgp-<keyID>".
|
||||||
func writePGPUnlocker(
|
func writePGPUnlocker(
|
||||||
t *testing.T, fs afero.Fs, unlockersDir, dirName string,
|
t *testing.T, fs afero.Fs, unlockersDir, dirName string,
|
||||||
createdAt time.Time, keyID string,
|
createdAt time.Time, keyID string,
|
||||||
@@ -201,6 +224,44 @@ func listUnlockersJSON(t *testing.T, fs afero.Fs) []UnlockerInfo {
|
|||||||
return decoded.Unlockers
|
return decoded.Unlockers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestUnlockersListSkipsUnreadableUnlockersDir asserts that an unlockers.d
|
||||||
|
// which becomes unreadable after the vault enumerated it produces no rows,
|
||||||
|
// rather than rows carrying fabricated fallback IDs.
|
||||||
|
func TestUnlockersListSkipsUnreadableUnlockersDir(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
base := newListTestVault(t, 1)
|
||||||
|
// Budget of one: the vault's own ListUnlockers scan succeeds, the
|
||||||
|
// per-entry rescan that resolves the ID fails.
|
||||||
|
fs := &unlockersDirFailFs{Fs: base, openBudget: 1}
|
||||||
|
|
||||||
|
unlockers := listUnlockersJSON(t, fs)
|
||||||
|
|
||||||
|
assert.Empty(t, unlockers,
|
||||||
|
"an unreadable unlockers.d must yield no rows, not fabricated IDs")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestUnlockersListSkipsOnlyUnreadableEntries asserts that a readable
|
||||||
|
// entry survives with its real ID and current-unlocker marker when a later
|
||||||
|
// entry's rescan fails.
|
||||||
|
func TestUnlockersListSkipsOnlyUnreadableEntries(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
base := newListTestVault(t, 2)
|
||||||
|
// Budget of two: ListUnlockers plus the first entry's rescan succeed,
|
||||||
|
// the second entry's rescan fails.
|
||||||
|
fs := &unlockersDirFailFs{Fs: base, openBudget: 2}
|
||||||
|
|
||||||
|
unlockers := listUnlockersJSON(t, fs)
|
||||||
|
|
||||||
|
require.Len(t, unlockers, 1,
|
||||||
|
"only the entry whose directory was readable may be listed")
|
||||||
|
assert.Equal(t, "pgp-"+listTestGPGKeyID+"A", unlockers[0].ID,
|
||||||
|
"the surviving row must carry the real unlocker ID")
|
||||||
|
assert.True(t, unlockers[0].IsCurrent,
|
||||||
|
"the current-unlocker marker must survive the skip")
|
||||||
|
}
|
||||||
|
|
||||||
// TestUnlockersListReadableEntriesAreListed is the control case: with a
|
// TestUnlockersListReadableEntriesAreListed is the control case: with a
|
||||||
// fully readable unlockers.d every entry is listed with its real ID.
|
// fully readable unlockers.d every entry is listed with its real ID.
|
||||||
func TestUnlockersListReadableEntriesAreListed(t *testing.T) {
|
func TestUnlockersListReadableEntriesAreListed(t *testing.T) {
|
||||||
@@ -211,21 +272,20 @@ func TestUnlockersListReadableEntriesAreListed(t *testing.T) {
|
|||||||
unlockers := listUnlockersJSON(t, base)
|
unlockers := listUnlockersJSON(t, base)
|
||||||
|
|
||||||
require.Len(t, unlockers, 2)
|
require.Len(t, unlockers, 2)
|
||||||
assert.Equal(t, listTestUnlockerDirOne, unlockers[0].ID)
|
assert.Equal(t, "pgp-"+listTestGPGKeyID+"A", unlockers[0].ID)
|
||||||
assert.Equal(t, listTestUnlockerDirTwo, unlockers[1].ID)
|
assert.Equal(t, "pgp-"+listTestGPGKeyID+"B", unlockers[1].ID)
|
||||||
assert.True(t, unlockers[0].IsCurrent)
|
assert.True(t, unlockers[0].IsCurrent)
|
||||||
assert.False(t, unlockers[1].IsCurrent)
|
assert.False(t, unlockers[1].IsCurrent)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestUnlockersListToleratesCorruptMetadata asserts that one unlocker with
|
// TestUnlockersListToleratesCorruptMetadata asserts that one unlocker with
|
||||||
// corrupt metadata does not stop the listing. Metadata that is not JSON
|
// corrupt metadata does not stop the listing. Metadata that is not JSON
|
||||||
// leaves that unlocker out; PGP metadata without a usable GPG key ID, and
|
// leaves that unlocker out; PGP metadata without a usable GPG key ID lists
|
||||||
// metadata of an unknown type, are still listed, under the directory name
|
// it as "pgp-unknown". The healthy unlocker is listed with its real ID.
|
||||||
// like any other. The healthy unlocker is listed with its real ID.
|
|
||||||
func TestUnlockersListToleratesCorruptMetadata(t *testing.T) {
|
func TestUnlockersListToleratesCorruptMetadata(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
healthyID := listTestUnlockerDirOne
|
healthyID := "pgp-" + listTestGPGKeyID + "A"
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -240,17 +300,12 @@ func TestUnlockersListToleratesCorruptMetadata(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "GPG key ID of the wrong type",
|
name: "GPG key ID of the wrong type",
|
||||||
metadata: `{"type": "pgp", "gpgKeyId": 42}`,
|
metadata: `{"type": "pgp", "gpgKeyId": 42}`,
|
||||||
wantIDs: []string{healthyID, listTestUnlockerDirTwo},
|
wantIDs: []string{healthyID, "pgp-unknown"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "GPG key ID missing",
|
name: "GPG key ID missing",
|
||||||
metadata: `{"type": "pgp"}`,
|
metadata: `{"type": "pgp"}`,
|
||||||
wantIDs: []string{healthyID, listTestUnlockerDirTwo},
|
wantIDs: []string{healthyID, "pgp-unknown"},
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "unknown type",
|
|
||||||
metadata: `{"type": "unknown"}`,
|
|
||||||
wantIDs: []string{healthyID, listTestUnlockerDirTwo},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,7 +371,7 @@ func TestUnlockersListSkipsUnreadableMetadata(t *testing.T) {
|
|||||||
|
|
||||||
require.Len(t, unlockers, 1,
|
require.Len(t, unlockers, 1,
|
||||||
"only the unlocker with usable metadata may be listed")
|
"only the unlocker with usable metadata may be listed")
|
||||||
assert.Equal(t, listTestUnlockerDirTwo, unlockers[0].ID,
|
assert.Equal(t, "pgp-"+listTestGPGKeyID+"B", unlockers[0].ID,
|
||||||
"the listed row must carry the real unlocker ID")
|
"the listed row must carry the real unlocker ID")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,11 +28,11 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"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"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -290,7 +290,7 @@ func TestRemoveLastUnlockerAbortsWhenSecretsUnreadable(t *testing.T) {
|
|||||||
writeTestSecret(t, base, vaultDir)
|
writeTestSecret(t, base, vaultDir)
|
||||||
instance, _ := newTestInstance(&statFailFs{Fs: base, path: path})
|
instance, _ := newTestInstance(&statFailFs{Fs: base, path: path})
|
||||||
|
|
||||||
_, err := instance.findUnlockerToRemove(listTestUnlockerDirOne)
|
_, err := instance.findUnlockerToRemove("pgp-" + listTestGPGKeyID + "A")
|
||||||
|
|
||||||
require.ErrorIs(t, err, errStatFailed)
|
require.ErrorIs(t, err, errStatFailed)
|
||||||
assertDirEntries(t, base, unlockersDir, listTestUnlockerDirOne)
|
assertDirEntries(t, base, unlockersDir, listTestUnlockerDirOne)
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/cli"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/cli"
|
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// usageHeading starts the usage text cobra prints after an error.
|
// usageHeading starts the usage text cobra prints after an error.
|
||||||
|
|||||||
+30
-15
@@ -10,19 +10,20 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/tyler-smith/go-bip39"
|
"github.com/tyler-smith/go-bip39"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Sentinel errors for vault operations
|
// Sentinel errors for vault operations
|
||||||
var (
|
var (
|
||||||
errMnemonicEmpty = errors.New("mnemonic cannot be empty")
|
errMnemonicEmpty = errors.New("mnemonic cannot be empty")
|
||||||
errInvalidMnemonicPhrase = errors.New("invalid BIP39 mnemonic phrase")
|
errInvalidMnemonicPhrase = errors.New("invalid BIP39 mnemonic phrase")
|
||||||
|
errInvalidMnemonic = errors.New("invalid BIP39 mnemonic")
|
||||||
errVaultHasLongTermKey = errors.New(
|
errVaultHasLongTermKey = errors.New(
|
||||||
"already has a long-term key configured")
|
"already has a long-term key configured")
|
||||||
errMnemonicEnvNotSet = errors.New(
|
errMnemonicEnvNotSet = errors.New(
|
||||||
@@ -249,7 +250,7 @@ func (cli *Instance) resolvePassphrase() (*memguard.LockedBuffer, func(), error)
|
|||||||
// Use secure passphrase input with confirmation
|
// Use secure passphrase input with confirmation
|
||||||
passphraseBuffer, err := readSecurePassphrase("Enter passphrase for unlocker: ")
|
passphraseBuffer, err := readSecurePassphrase("Enter passphrase for unlocker: ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, fmt.Errorf("failed to read passphrase: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return passphraseBuffer, passphraseBuffer.Destroy, nil
|
return passphraseBuffer, passphraseBuffer.Destroy, nil
|
||||||
@@ -292,26 +293,40 @@ func (cli *Instance) CreateVault(cmd *cobra.Command, name string) error {
|
|||||||
}
|
}
|
||||||
defer cleanupPassphrase()
|
defer cleanupPassphrase()
|
||||||
|
|
||||||
// Create the vault with its passphrase unlocker
|
// Create the vault - it will handle key derivation internally
|
||||||
vlt, err := vault.CreateVault(cli.fs, cli.stateDir, name,
|
vlt, err := vault.CreateVault(cli.fs, cli.stateDir, name, mnemonic)
|
||||||
mnemonic, passphraseBuffer)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ltIdentity, err := vlt.GetOrDeriveLongTermKey()
|
// Get the vault metadata to retrieve the derivation index
|
||||||
|
vaultDir := filepath.Join(cli.stateDir, "vaults.d", name)
|
||||||
|
|
||||||
|
metadata, err := vault.LoadVaultMetadata(cli.fs, vaultDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get long-term key: %w", err)
|
return fmt.Errorf("failed to load vault metadata: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
unlocker, err := vlt.GetCurrentUnlocker()
|
// Derive the long-term key using the same index that CreateVault used
|
||||||
|
ltIdentity, err := agehd.DeriveIdentity(mnemonicStr, metadata.DerivationIndex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to derive long-term key from mnemonic: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unlock the vault with the derived long-term key
|
||||||
|
vlt.Unlock(ltIdentity)
|
||||||
|
|
||||||
|
// Create passphrase-protected unlocker
|
||||||
|
secret.Debug("Creating passphrase-protected unlocker")
|
||||||
|
|
||||||
|
passphraseUnlocker, err := vlt.CreatePassphraseUnlocker(passphraseBuffer)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create unlocker: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Printf("Created vault '%s'\n", vlt.GetName())
|
cmd.Printf("Created vault '%s'\n", vlt.GetName())
|
||||||
cmd.Printf("Long-term public key: %s\n", ltIdentity.Recipient().String())
|
cmd.Printf("Long-term public key: %s\n", ltIdentity.Recipient().String())
|
||||||
cmd.Printf("Unlocker ID: %s\n", unlocker.GetID())
|
cmd.Printf("Unlocker ID: %s\n", passphraseUnlocker.GetID())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -352,7 +367,7 @@ func (cli *Instance) vaultImportPreflight(
|
|||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return "", "", "", fmt.Errorf("vault '%s' %w",
|
return "", "", "", fmt.Errorf("vault '%s' %w",
|
||||||
vaultName, vault.ErrVaultNotFound)
|
vaultName, errVaultDoesNotExist)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if vault already has a public key
|
// Check if vault already has a public key
|
||||||
@@ -380,7 +395,7 @@ func (cli *Instance) vaultImportPreflight(
|
|||||||
secret.Debug("Validating BIP39 mnemonic", "word_count", len(mnemonicWords))
|
secret.Debug("Validating BIP39 mnemonic", "word_count", len(mnemonicWords))
|
||||||
|
|
||||||
if !bip39.IsMnemonicValid(mnemonic) {
|
if !bip39.IsMnemonicValid(mnemonic) {
|
||||||
return "", "", "", errInvalidMnemonicPhrase
|
return "", "", "", errInvalidMnemonic
|
||||||
}
|
}
|
||||||
|
|
||||||
return vaultDir, pubKeyPath, mnemonic, nil
|
return vaultDir, pubKeyPath, mnemonic, nil
|
||||||
@@ -643,7 +658,7 @@ func (cli *Instance) findVaultToRemove(name string) (vaultToRemove, error) {
|
|||||||
|
|
||||||
if !slices.Contains(vaults, name) {
|
if !slices.Contains(vaults, name) {
|
||||||
return vaultToRemove{},
|
return vaultToRemove{},
|
||||||
fmt.Errorf("vault '%s' %w", name, vault.ErrVaultNotFound)
|
fmt.Errorf("vault '%s' %w", name, errVaultDoesNotExist)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(vaults) == 1 {
|
if len(vaults) == 1 {
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -23,6 +23,7 @@ const (
|
|||||||
|
|
||||||
// Sentinel errors for version operations
|
// Sentinel errors for version operations
|
||||||
var (
|
var (
|
||||||
|
errVersionNotFound = errors.New("not found for secret")
|
||||||
errCannotRemoveCurrentVersion = errors.New("promote another version first")
|
errCannotRemoveCurrentVersion = errors.New("promote another version first")
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -155,7 +156,7 @@ func (cli *Instance) ListVersions(cmd *cobra.Command, secretName string) error {
|
|||||||
if !exists {
|
if !exists {
|
||||||
secret.Debug("Secret not found", "secret_name", secretName)
|
secret.Debug("Secret not found", "secret_name", secretName)
|
||||||
|
|
||||||
return fmt.Errorf("secret '%s' %w", secretName, vault.ErrSecretNotFound)
|
return fmt.Errorf("secret '%s' %w", secretName, errSecretNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
// List all versions
|
// List all versions
|
||||||
@@ -288,7 +289,7 @@ func (cli *Instance) PromoteVersion(
|
|||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return fmt.Errorf("version '%s' %w '%s'",
|
return fmt.Errorf("version '%s' %w '%s'",
|
||||||
version, vault.ErrVersionNotFound, secretName)
|
version, errVersionNotFound, secretName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the current symlink using the proper function
|
// Update the current symlink using the proper function
|
||||||
@@ -373,7 +374,7 @@ func (cli *Instance) findVersionToRemove(
|
|||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return versionToRemove{},
|
return versionToRemove{},
|
||||||
fmt.Errorf("secret '%s' %w", secretName, vault.ErrSecretNotFound)
|
fmt.Errorf("secret '%s' %w", secretName, errSecretNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if version exists
|
// Check if version exists
|
||||||
@@ -385,7 +386,7 @@ func (cli *Instance) findVersionToRemove(
|
|||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return versionToRemove{}, fmt.Errorf("version '%s' %w '%s'",
|
return versionToRemove{}, fmt.Errorf("version '%s' %w '%s'",
|
||||||
version, vault.ErrVersionNotFound, secretName)
|
version, errVersionNotFound, secretName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get current version
|
// Get current version
|
||||||
|
|||||||
@@ -26,13 +26,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -73,8 +73,7 @@ func setupTestVault(t *testing.T, fs afero.Fs) {
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
// Create vault
|
// Create vault
|
||||||
vlt, err := vault.CreateVault(fs, testStateDir, "default",
|
vlt, err := vault.CreateVault(fs, testStateDir, "default", testMnemonicBuffer(t))
|
||||||
testMnemonicBuffer(t), nil)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Derive and store long-term key from mnemonic
|
// Derive and store long-term key from mnemonic
|
||||||
@@ -171,7 +170,8 @@ func TestListVersionsNonExistentSecret(t *testing.T) {
|
|||||||
|
|
||||||
// Try to list versions of non-existent secret
|
// Try to list versions of non-existent secret
|
||||||
err := cli.ListVersions(cmd, "nonexistent/secret")
|
err := cli.ListVersions(cmd, "nonexistent/secret")
|
||||||
require.ErrorIs(t, err, vault.ErrSecretNotFound)
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPromoteVersionCommand(t *testing.T) {
|
func TestPromoteVersionCommand(t *testing.T) {
|
||||||
@@ -265,7 +265,8 @@ func TestPromoteNonExistentVersion(t *testing.T) {
|
|||||||
|
|
||||||
// Try to promote non-existent version
|
// Try to promote non-existent version
|
||||||
err = cli.PromoteVersion(cmd, "test/secret", "20991231.999")
|
err = cli.PromoteVersion(cmd, "test/secret", "20991231.999")
|
||||||
require.ErrorIs(t, err, vault.ErrVersionNotFound)
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetSecretWithVersion(t *testing.T) {
|
func TestGetSecretWithVersion(t *testing.T) {
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/macse"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/macse"
|
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var errInjected = errors.New("injected failure")
|
var errInjected = errors.New("injected failure")
|
||||||
@@ -236,7 +236,7 @@ func newVaultWithSecret(
|
|||||||
) *vault.Vault {
|
) *vault.Vault {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(fs, stateDir, name, testMnemonicBuffer(t), nil)
|
vlt, err := vault.CreateVault(fs, stateDir, name, testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
buffer := memguard.NewBufferFromBytes([]byte(value))
|
buffer := memguard.NewBufferFromBytes([]byte(value))
|
||||||
@@ -353,7 +353,7 @@ func TestLongestNames(t *testing.T) {
|
|||||||
fs := afero.NewOsFs()
|
fs := afero.NewOsFs()
|
||||||
name := strings.Repeat("a", longestName)
|
name := strings.Repeat("a", longestName)
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(fs, t.TempDir(), name, testMnemonicBuffer(t), nil)
|
vlt, err := vault.CreateVault(fs, t.TempDir(), name, testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
value := memguard.NewBufferFromBytes([]byte("long"))
|
value := memguard.NewBufferFromBytes([]byte("long"))
|
||||||
@@ -647,7 +647,7 @@ func TestPassphraseUnlockerGetsKeyFirst(t *testing.T) {
|
|||||||
|
|
||||||
// No mnemonic, and no current unlocker to get the key from
|
// No mnemonic, and no current unlocker to get the key from
|
||||||
base := afero.NewMemMapFs()
|
base := afero.NewMemMapFs()
|
||||||
_, err := vault.CreateVault(base, testVaultStateDir, testVaultName, nil, nil)
|
_, err := vault.CreateVault(base, testVaultStateDir, testVaultName, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fs := hookFs{Fs: base, before: func(_, path string) error {
|
fs := hookFs{Fs: base, before: func(_, path string) error {
|
||||||
@@ -679,7 +679,7 @@ func TestPassphraseUnlockerIsWholeOrAbsent(t *testing.T) {
|
|||||||
|
|
||||||
base, stateDir := tfs.open(t)
|
base, stateDir := tfs.open(t)
|
||||||
vlt, err := vault.CreateVault(base, stateDir, testVaultName,
|
vlt, err := vault.CreateVault(base, stateDir, testVaultName,
|
||||||
testMnemonicBuffer(t), nil)
|
testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
vaultDir, err := vlt.GetDirectory()
|
vaultDir, err := vlt.GetDirectory()
|
||||||
@@ -728,7 +728,7 @@ func TestPassphraseUnlockerReplacementKeepsVaultOpen(t *testing.T) {
|
|||||||
|
|
||||||
base, stateDir := tfs.open(t)
|
base, stateDir := tfs.open(t)
|
||||||
vlt, err := vault.CreateVault(base, stateDir, testVaultName,
|
vlt, err := vault.CreateVault(base, stateDir, testVaultName,
|
||||||
testMnemonicBuffer(t), nil)
|
testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
ltIdentity, err := vlt.GetOrDeriveLongTermKey()
|
ltIdentity, err := vlt.GetOrDeriveLongTermKey()
|
||||||
@@ -911,7 +911,7 @@ func TestSecureEnclaveUnlockerFailureDeletesKey(t *testing.T) {
|
|||||||
|
|
||||||
mnemonic := testMnemonicBuffer(t)
|
mnemonic := testMnemonicBuffer(t)
|
||||||
base := afero.NewMemMapFs()
|
base := afero.NewMemMapFs()
|
||||||
_, err := vault.CreateVault(base, testVaultStateDir, testVaultName, mnemonic, nil)
|
_, err := vault.CreateVault(base, testVaultStateDir, testVaultName, mnemonic)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// The unlocker's directory is named se-<label of its Secure Enclave key>
|
// The unlocker's directory is named se-<label of its Secure Enclave key>
|
||||||
|
|||||||
+19
-39
@@ -17,17 +17,16 @@ import (
|
|||||||
var (
|
var (
|
||||||
errNilPassphraseBuffer = errors.New("passphrase buffer is nil")
|
errNilPassphraseBuffer = errors.New("passphrase buffer is nil")
|
||||||
errStdinNotTerminal = errors.New(
|
errStdinNotTerminal = errors.New(
|
||||||
"stdin is not a terminal (piped input or script)")
|
"cannot read passphrase from non-terminal stdin " +
|
||||||
|
"(piped input or script). Please set the SB_UNLOCK_PASSPHRASE " +
|
||||||
|
"environment variable or run interactively")
|
||||||
errStderrNotTerminal = errors.New(
|
errStderrNotTerminal = errors.New(
|
||||||
"stderr is not a terminal (running in non-interactive mode)")
|
"cannot prompt for passphrase: stderr is not a terminal " +
|
||||||
errNothingEntered = errors.New("nothing was entered")
|
"(running in non-interactive mode). Please set the " +
|
||||||
|
"SB_UNLOCK_PASSPHRASE environment variable")
|
||||||
errEmptyPassphrase = errors.New("passphrase cannot be empty")
|
errEmptyPassphrase = errors.New("passphrase cannot be empty")
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrMnemonicNotRead is wrapped in every error of ReadMnemonic: there is no
|
|
||||||
// terminal to read the mnemonic from, reading it failed, or it was empty.
|
|
||||||
var ErrMnemonicNotRead = errors.New("failed to read mnemonic")
|
|
||||||
|
|
||||||
// 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(
|
||||||
@@ -167,61 +166,42 @@ func DecryptWithPassphrase(
|
|||||||
|
|
||||||
// ReadPassphrase reads a passphrase securely from the terminal without echoing
|
// ReadPassphrase reads a passphrase securely from the terminal without echoing
|
||||||
// This version is for unlocking and doesn't require confirmation
|
// This version is for unlocking and doesn't require confirmation
|
||||||
// Returns a LockedBuffer containing the passphrase for secure memory handling.
|
// Returns a LockedBuffer containing the passphrase for secure memory handling
|
||||||
// Every error it returns wraps ErrPassphraseNotRead.
|
|
||||||
func ReadPassphrase(prompt string) (*memguard.LockedBuffer, error) {
|
func ReadPassphrase(prompt string) (*memguard.LockedBuffer, error) {
|
||||||
return readFromTerminal(prompt, ErrPassphraseNotRead, EnvUnlockPassphrase)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadMnemonic reads a mnemonic from the terminal as ReadPassphrase reads a
|
|
||||||
// passphrase. Every error it returns wraps ErrMnemonicNotRead.
|
|
||||||
func ReadMnemonic(prompt string) (*memguard.LockedBuffer, error) {
|
|
||||||
return readFromTerminal(prompt, ErrMnemonicNotRead, EnvMnemonic)
|
|
||||||
}
|
|
||||||
|
|
||||||
// readFromTerminal reads input from the terminal without echoing it. Every
|
|
||||||
// error it returns wraps notRead; without a terminal, the error says to set
|
|
||||||
// envVar instead.
|
|
||||||
func readFromTerminal(
|
|
||||||
prompt string, notRead error, envVar string,
|
|
||||||
) (*memguard.LockedBuffer, error) {
|
|
||||||
// Check if stdin is a terminal
|
// Check if stdin is a terminal
|
||||||
if !term.IsTerminal(syscall.Stdin) {
|
if !term.IsTerminal(syscall.Stdin) {
|
||||||
// Not a terminal - never read secrets from piped input
|
// Not a terminal - never read passphrases from piped input
|
||||||
// for security reasons
|
// for security reasons
|
||||||
return nil, fmt.Errorf(
|
return nil, errStdinNotTerminal
|
||||||
"%w: %w. Please set the %s environment variable or run interactively",
|
|
||||||
notRead, errStdinNotTerminal, envVar)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// stdin is a terminal, check if stderr is also a terminal for
|
// stdin is a terminal, check if stderr is also a terminal for
|
||||||
// interactive prompting
|
// interactive prompting
|
||||||
if !term.IsTerminal(syscall.Stderr) {
|
if !term.IsTerminal(syscall.Stderr) {
|
||||||
return nil, fmt.Errorf("%w: %w. Please set the %s environment variable",
|
return nil, errStderrNotTerminal
|
||||||
notRead, errStderrNotTerminal, envVar)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Both stdin and stderr are terminals - use secure password reading
|
// Both stdin and stderr are terminals - use secure password reading
|
||||||
fmt.Fprint(os.Stderr, prompt) // Write prompt to stderr, not stdout
|
fmt.Fprint(os.Stderr, prompt) // Write prompt to stderr, not stdout
|
||||||
|
|
||||||
input, err := term.ReadPassword(syscall.Stdin)
|
passphrase, err := term.ReadPassword(syscall.Stdin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%w: %w", notRead, err)
|
return nil, fmt.Errorf("failed to read passphrase: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print newline to stderr since ReadPassword doesn't echo
|
// Print newline to stderr since ReadPassword doesn't echo
|
||||||
fmt.Fprintln(os.Stderr)
|
fmt.Fprintln(os.Stderr)
|
||||||
|
|
||||||
if len(input) == 0 {
|
if len(passphrase) == 0 {
|
||||||
return nil, fmt.Errorf("%w: %w", notRead, errNothingEntered)
|
return nil, errEmptyPassphrase
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a secure buffer and copy the input
|
// Create a secure buffer and copy the passphrase
|
||||||
secureBuffer := memguard.NewBufferFromBytes(input)
|
secureBuffer := memguard.NewBufferFromBytes(passphrase)
|
||||||
|
|
||||||
// Clear the original input slice
|
// Clear the original passphrase slice
|
||||||
for i := range input {
|
for i := range passphrase {
|
||||||
input[i] = 0
|
passphrase[i] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
return secureBuffer, nil
|
return secureBuffer, nil
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestIdentityToLockedBuffer checks that the buffer holds the identity's
|
// TestIdentityToLockedBuffer checks that the buffer holds the identity's
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// realVault is a minimal VaultInterface backed by a real afero filesystem,
|
// realVault is a minimal VaultInterface backed by a real afero filesystem,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package secret_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDetermineStateDir_ErrorsWhenHomeDirUnavailable(t *testing.T) {
|
func TestDetermineStateDir_ErrorsWhenHomeDirUnavailable(t *testing.T) {
|
||||||
|
|||||||
@@ -11,12 +11,13 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -38,6 +39,8 @@ const (
|
|||||||
var keychainItemNameRegex = regexp.MustCompile(`^[A-Za-z0-9._-]+$`)
|
var keychainItemNameRegex = regexp.MustCompile(`^[A-Za-z0-9._-]+$`)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
errNotMacOS = errors.New(
|
||||||
|
"keychain unlockers are only supported on macOS")
|
||||||
errKeychainItemNameEmpty = errors.New("keychain item name cannot be empty")
|
errKeychainItemNameEmpty = errors.New("keychain item name cannot be empty")
|
||||||
errInvalidKeychainItemName = errors.New("invalid keychain item name format")
|
errInvalidKeychainItemName = errors.New("invalid keychain item name format")
|
||||||
errUnsupportedCurrentUnlocker = errors.New(
|
errUnsupportedCurrentUnlocker = errors.New(
|
||||||
@@ -153,9 +156,20 @@ func (k *KeychainUnlocker) GetDirectory() string {
|
|||||||
return k.Directory
|
return k.Directory
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetID implements Unlocker interface: the name of the unlocker's directory
|
// GetID implements Unlocker interface - generates ID from keychain item name
|
||||||
func (k *KeychainUnlocker) GetID() string {
|
func (k *KeychainUnlocker) GetID() string {
|
||||||
return filepath.Base(k.Directory)
|
// Generate ID in the format YYYY-MM-DD.HH.mm-hostname-keychain
|
||||||
|
// This matches the passphrase unlocker format
|
||||||
|
hostname, err := os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
hostname = "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the creation timestamp from metadata
|
||||||
|
createdAt := k.Metadata.CreatedAt
|
||||||
|
timestamp := createdAt.Format("2006-01-02.15.04")
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s-%s-keychain", timestamp, hostname)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove implements Unlocker interface - removes the keychain unlocker
|
// Remove implements Unlocker interface - removes the keychain unlocker
|
||||||
@@ -391,6 +405,12 @@ func deriveLongTermPrivateKey(
|
|||||||
func CreateKeychainUnlocker(
|
func CreateKeychainUnlocker(
|
||||||
fs afero.Fs, stateDir string, mnemonic, passphrase *memguard.LockedBuffer,
|
fs afero.Fs, stateDir string, mnemonic, passphrase *memguard.LockedBuffer,
|
||||||
) (*KeychainUnlocker, error) {
|
) (*KeychainUnlocker, error) {
|
||||||
|
// Check if we're on macOS
|
||||||
|
err := checkMacOSAvailable()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// Get current vault using the GetCurrentVault function from the same package
|
// Get current vault using the GetCurrentVault function from the same package
|
||||||
vault, err := GetCurrentVault(fs, stateDir)
|
vault, err := GetCurrentVault(fs, stateDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -546,6 +566,15 @@ func writeKeychainUnlocker(
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkMacOSAvailable verifies that we're running on macOS
|
||||||
|
func checkMacOSAvailable() error {
|
||||||
|
if runtime.GOOS != "darwin" {
|
||||||
|
return fmt.Errorf("%w, current OS: %s", errNotMacOS, runtime.GOOS)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// validateKeychainItemName validates that a keychain item name is safe for
|
// validateKeychainItemName validates that a keychain item name is safe for
|
||||||
// command execution
|
// command execution
|
||||||
func validateKeychainItemName(itemName string) error {
|
func validateKeychainItemName(itemName string) error {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
// storeInKeychain stores data in the macOS keychain using keybase/go-keychain
|
// storeInKeychain stores data in the macOS keychain using keybase/go-keychain
|
||||||
func storeInKeychain(itemName string, data *memguard.LockedBuffer) error {
|
func storeInKeychain(itemName string, data *memguard.LockedBuffer) error {
|
||||||
if data == nil {
|
if data == nil {
|
||||||
return errNilDataBuffer
|
return fmt.Errorf("data buffer is nil")
|
||||||
}
|
}
|
||||||
if err := validateKeychainItemName(itemName); err != nil {
|
if err := validateKeychainItemName(itemName); err != nil {
|
||||||
return fmt.Errorf("invalid keychain item name: %w", err)
|
return fmt.Errorf("invalid keychain item name: %w", err)
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ package secret
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
@@ -61,9 +60,9 @@ func (k *KeychainUnlocker) GetDirectory() string {
|
|||||||
return k.Directory
|
return k.Directory
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetID returns the unlocker ID, the name of the unlocker's directory
|
// GetID returns the unlocker ID
|
||||||
func (k *KeychainUnlocker) GetID() string {
|
func (k *KeychainUnlocker) GetID() string {
|
||||||
return filepath.Base(k.Directory)
|
return k.Metadata.CreatedAt.Format("2006-01-02.15.04") + "-keychain"
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetKeychainItemName returns an error on non-Darwin platforms
|
// GetKeychainItemName returns an error on non-Darwin platforms
|
||||||
|
|||||||
@@ -72,12 +72,9 @@ func TestKeychainInvalidItemName(t *testing.T) {
|
|||||||
testData := memguard.NewBufferFromBytes([]byte("test"))
|
testData := memguard.NewBufferFromBytes([]byte("test"))
|
||||||
defer testData.Destroy()
|
defer testData.Destroy()
|
||||||
|
|
||||||
// Test an empty item name
|
|
||||||
err := storeInKeychain("", testData)
|
|
||||||
require.ErrorIs(t, err, errKeychainItemNameEmpty)
|
|
||||||
|
|
||||||
// Test invalid item names
|
// Test invalid item names
|
||||||
invalidNames := []string{
|
invalidNames := []string{
|
||||||
|
"", // Empty name
|
||||||
"test space", // Contains space
|
"test space", // Contains space
|
||||||
"test/slash", // Contains slash
|
"test/slash", // Contains slash
|
||||||
"test\\backslash", // Contains backslash
|
"test\\backslash", // Contains backslash
|
||||||
@@ -99,8 +96,9 @@ func TestKeychainInvalidItemName(t *testing.T) {
|
|||||||
|
|
||||||
for _, name := range invalidNames {
|
for _, name := range invalidNames {
|
||||||
err := storeInKeychain(name, testData)
|
err := storeInKeychain(name, testData)
|
||||||
require.ErrorIs(t, err, errInvalidKeychainItemName,
|
assert.Error(t, err, "Expected error for invalid name: %s", name)
|
||||||
"Expected error for invalid name: %s", name)
|
assert.Contains(t, err.Error(), "invalid keychain item name",
|
||||||
|
"Error should mention invalid name for: %s", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test valid names (should not error on validation)
|
// Test valid names (should not error on validation)
|
||||||
@@ -130,7 +128,8 @@ func TestKeychainNilData(t *testing.T) {
|
|||||||
|
|
||||||
// Test storing nil data
|
// Test storing nil data
|
||||||
err := storeInKeychain("test-item", nil)
|
err := storeInKeychain("test-item", nil)
|
||||||
require.ErrorIs(t, err, errNilDataBuffer)
|
assert.Error(t, err, "Expected error when storing nil data")
|
||||||
|
assert.Contains(t, err.Error(), "data buffer is nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKeychainLargeData(t *testing.T) {
|
func TestKeychainLargeData(t *testing.T) {
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// testMnemonic is the standard BIP39 test vector mnemonic.
|
// testMnemonic is the standard BIP39 test vector mnemonic.
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package secret
|
package secret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -11,11 +10,6 @@ import (
|
|||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrPassphraseNotRead is wrapped in every error of ReadPassphrase: there
|
|
||||||
// is no terminal to read the passphrase from, reading it failed, or it was
|
|
||||||
// empty. A passphrase unlocker that fails with it was not tried.
|
|
||||||
var ErrPassphraseNotRead = errors.New("failed to read passphrase")
|
|
||||||
|
|
||||||
// PassphraseUnlocker represents a passphrase-protected unlocker
|
// PassphraseUnlocker represents a passphrase-protected unlocker
|
||||||
type PassphraseUnlocker struct {
|
type PassphraseUnlocker struct {
|
||||||
Directory string
|
Directory string
|
||||||
@@ -115,9 +109,12 @@ func (p *PassphraseUnlocker) GetDirectory() string {
|
|||||||
return p.Directory
|
return p.Directory
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetID implements Unlocker interface: the name of the unlocker's directory
|
// GetID implements Unlocker interface - generates ID from creation timestamp
|
||||||
func (p *PassphraseUnlocker) GetID() string {
|
func (p *PassphraseUnlocker) GetID() string {
|
||||||
return filepath.Base(p.Directory)
|
// Generate ID using creation timestamp: YYYY-MM-DD.HH.mm-passphrase
|
||||||
|
createdAt := p.Metadata.CreatedAt
|
||||||
|
|
||||||
|
return createdAt.Format("2006-01-02.15.04") + "-passphrase"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove implements Unlocker interface - removes the passphrase unlocker
|
// Remove implements Unlocker interface - removes the passphrase unlocker
|
||||||
@@ -155,7 +152,7 @@ func (p *PassphraseUnlocker) getPassphrase() (*memguard.LockedBuffer, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
Debug("Failed to read passphrase", "error", err, "unlocker_id", p.GetID())
|
Debug("Failed to read passphrase", "error", err, "unlocker_id", p.GetID())
|
||||||
|
|
||||||
return nil, err
|
return nil, fmt.Errorf("failed to read passphrase: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return secureBuffer, nil
|
return secureBuffer, nil
|
||||||
|
|||||||
@@ -17,11 +17,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// pgpUnlockerType is the type of a PGP unlocker.
|
// pgpUnlockerType is the type of a PGP unlocker.
|
||||||
@@ -297,6 +297,11 @@ func TestPGPUnlockerWithRealFS(t *testing.T) {
|
|||||||
// Create a PGP unlocker for the remaining tests
|
// Create a PGP unlocker for the remaining tests
|
||||||
unlocker := secret.NewPGPUnlocker(fs, unlockerDir, metadata)
|
unlocker := secret.NewPGPUnlocker(fs, unlockerDir, metadata)
|
||||||
|
|
||||||
|
// Test getting GPG key ID
|
||||||
|
t.Run("GetGPGKeyID", func(t *testing.T) {
|
||||||
|
testGetGPGKeyID(t, fs, unlocker, unlockerDir, metadata, fingerprint)
|
||||||
|
})
|
||||||
|
|
||||||
// Test getting identity from PGP unlocker
|
// Test getting identity from PGP unlocker
|
||||||
t.Run("GetIdentity", func(t *testing.T) {
|
t.Run("GetIdentity", func(t *testing.T) {
|
||||||
testPGPUnlockerGetIdentity(t, fs, unlocker, unlockerDir, keyID)
|
testPGPUnlockerGetIdentity(t, fs, unlocker, unlockerDir, keyID)
|
||||||
@@ -325,7 +330,7 @@ func testCreatePGPUnlocker(
|
|||||||
mnemonic := testMnemonicBuffer(t)
|
mnemonic := testMnemonicBuffer(t)
|
||||||
|
|
||||||
// Create a test vault directory structure
|
// Create a test vault directory structure
|
||||||
vlt, err := vault.CreateVault(fs, stateDir, vaultName, mnemonic, nil)
|
vlt, err := vault.CreateVault(fs, stateDir, vaultName, mnemonic)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create vault: %v", err)
|
t.Fatalf("Failed to create vault: %v", err)
|
||||||
}
|
}
|
||||||
@@ -391,10 +396,10 @@ func testCreatePGPUnlocker(
|
|||||||
t.Errorf("Expected PGP unlock key type 'pgp', got '%s'", pgpUnlocker.GetType())
|
t.Errorf("Expected PGP unlock key type 'pgp', got '%s'", pgpUnlocker.GetType())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the ID is the name of the unlocker's directory
|
// Check if the key ID includes the GPG fingerprint
|
||||||
if pgpUnlocker.GetID() != filepath.Base(pgpUnlocker.GetDirectory()) {
|
if !strings.Contains(pgpUnlocker.GetID(), fingerprint) {
|
||||||
t.Errorf("PGP unlock key ID '%s' is not its directory name '%s'",
|
t.Errorf("PGP unlock key ID '%s' does not contain GPG fingerprint '%s'",
|
||||||
pgpUnlocker.GetID(), filepath.Base(pgpUnlocker.GetDirectory()))
|
pgpUnlocker.GetID(), fingerprint)
|
||||||
}
|
}
|
||||||
|
|
||||||
checkPGPUnlockerFiles(t, fs, pgpUnlocker.GetDirectory())
|
checkPGPUnlockerFiles(t, fs, pgpUnlocker.GetDirectory())
|
||||||
@@ -499,6 +504,51 @@ func checkPGPUnlockerMetadata(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// testGetGPGKeyID writes PGP unlocker metadata holding the GPG fingerprint
|
||||||
|
// into unlockerDir and checks that unlocker reads it back.
|
||||||
|
func testGetGPGKeyID(
|
||||||
|
t *testing.T, fs afero.Fs, unlocker *secret.PGPUnlocker,
|
||||||
|
unlockerDir string, metadata secret.UnlockerMetadata, fingerprint string,
|
||||||
|
) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
// Create PGP metadata with GPG key ID
|
||||||
|
type PGPUnlockerMetadata struct {
|
||||||
|
secret.UnlockerMetadata
|
||||||
|
|
||||||
|
GPGKeyID string `json:"gpgKeyId"`
|
||||||
|
}
|
||||||
|
|
||||||
|
pgpMetadata := PGPUnlockerMetadata{
|
||||||
|
UnlockerMetadata: metadata,
|
||||||
|
GPGKeyID: fingerprint,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write metadata file
|
||||||
|
metadataPath := filepath.Join(unlockerDir, unlockerMetadataFile)
|
||||||
|
|
||||||
|
metadataBytes, err := json.MarshalIndent(pgpMetadata, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to marshal metadata: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = afero.WriteFile(fs, metadataPath, metadataBytes, secret.FilePerms)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to write metadata: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get GPG key ID
|
||||||
|
retrievedKeyID, err := unlocker.GetGPGKeyID()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to get GPG key ID: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify key ID (should be the fingerprint)
|
||||||
|
if retrievedKeyID != fingerprint {
|
||||||
|
t.Errorf("Expected GPG fingerprint '%s', got '%s'", fingerprint, retrievedKeyID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// testPGPUnlockerGetIdentity writes an age identity encrypted to the GPG key
|
// testPGPUnlockerGetIdentity writes an age identity encrypted to the GPG key
|
||||||
// keyID into unlockerDir and checks that unlocker decrypts it.
|
// keyID into unlockerDir and checks that unlocker decrypts it.
|
||||||
func testPGPUnlockerGetIdentity(
|
func testPGPUnlockerGetIdentity(
|
||||||
|
|||||||
@@ -18,10 +18,6 @@ import (
|
|||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
)
|
)
|
||||||
|
|
||||||
// gpgNoPublicKeyStatus is the status line gpg writes when it has no key for
|
|
||||||
// the ID it was asked to list: 9 is gpg's error code for "No public key".
|
|
||||||
const gpgNoPublicKeyStatus = "[GNUPG:] ERROR keylist.getkey 9\n"
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
errGPGKeyIDEmpty = errors.New("GPG key ID cannot be empty")
|
errGPGKeyIDEmpty = errors.New("GPG key ID cannot be empty")
|
||||||
errInvalidGPGKeyID = errors.New("invalid GPG key ID format")
|
errInvalidGPGKeyID = errors.New("invalid GPG key ID format")
|
||||||
@@ -29,10 +25,6 @@ var (
|
|||||||
errNilDataBuffer = errors.New("data buffer is nil")
|
errNilDataBuffer = errors.New("data buffer is nil")
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrGPGKeyNotFound is returned by ResolveGPGKeyFingerprint for a key ID
|
|
||||||
// that matches no key in the GPG keyring.
|
|
||||||
var ErrGPGKeyNotFound = errors.New("GPG key not found")
|
|
||||||
|
|
||||||
// Variables to allow overriding in tests
|
// Variables to allow overriding in tests
|
||||||
var (
|
var (
|
||||||
// GPGEncryptFunc is the function used for GPG encryption
|
// GPGEncryptFunc is the function used for GPG encryption
|
||||||
@@ -163,9 +155,21 @@ func (p *PGPUnlocker) GetDirectory() string {
|
|||||||
return p.Directory
|
return p.Directory
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetID implements Unlocker interface: the name of the unlocker's directory
|
// GetID implements Unlocker interface - generates ID from GPG key ID.
|
||||||
|
// If the metadata has no usable GPG key ID, it warns with the unlocker's
|
||||||
|
// directory and returns "pgp-unknown", so listing the other unlockers
|
||||||
|
// still works.
|
||||||
func (p *PGPUnlocker) GetID() string {
|
func (p *PGPUnlocker) GetID() string {
|
||||||
return filepath.Base(p.Directory)
|
// Generate ID using GPG key ID: pgp-<keyid>
|
||||||
|
gpgKeyID, err := p.GetGPGKeyID()
|
||||||
|
if err != nil {
|
||||||
|
Warn("PGP unlocker metadata is corrupt or missing its GPG key ID",
|
||||||
|
"directory", p.Directory, "error", err)
|
||||||
|
|
||||||
|
return "pgp-unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
return "pgp-" + gpgKeyID
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove implements Unlocker interface - removes the PGP unlocker
|
// Remove implements Unlocker interface - removes the PGP unlocker
|
||||||
@@ -180,6 +184,30 @@ func (p *PGPUnlocker) Remove() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetGPGKeyID returns the GPG key ID from metadata
|
||||||
|
func (p *PGPUnlocker) GetGPGKeyID() (string, error) {
|
||||||
|
// Load the metadata
|
||||||
|
metadataPath := filepath.Join(p.Directory, "unlocker-metadata.json")
|
||||||
|
|
||||||
|
metadataData, err := afero.ReadFile(p.fs, metadataPath)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to read PGP metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var pgpMetadata PGPUnlockerMetadata
|
||||||
|
|
||||||
|
err = json.Unmarshal(metadataData, &pgpMetadata)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to parse PGP metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if pgpMetadata.GPGKeyID == "" {
|
||||||
|
return "", fmt.Errorf("PGP metadata: %w", errGPGKeyIDEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
|
return pgpMetadata.GPGKeyID, nil
|
||||||
|
}
|
||||||
|
|
||||||
// generatePGPUnlockerName generates a unique name for the PGP unlocker
|
// generatePGPUnlockerName generates a unique name for the PGP unlocker
|
||||||
// based on hostname and time
|
// based on hostname and time
|
||||||
func generatePGPUnlockerName() (string, error) {
|
func generatePGPUnlockerName() (string, error) {
|
||||||
@@ -375,20 +403,14 @@ func ResolveGPGKeyFingerprint(keyID string) (string, error) {
|
|||||||
return "", fmt.Errorf("invalid GPG key ID: %w", err)
|
return "", fmt.Errorf("invalid GPG key ID: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use GPG to get the full fingerprint for the key. --status-fd 1 adds
|
// Use GPG to get the full fingerprint for the key
|
||||||
// gpg's status lines to the output.
|
|
||||||
cmd := exec.CommandContext( //nolint:gosec // G204: keyID validated above
|
cmd := exec.CommandContext( //nolint:gosec // G204: keyID validated above
|
||||||
context.Background(),
|
context.Background(),
|
||||||
"gpg", "--status-fd", "1",
|
"gpg", "--list-keys", "--with-colons", "--fingerprint", keyID,
|
||||||
"--list-keys", "--with-colons", "--fingerprint", keyID,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
output, err := cmd.Output()
|
output, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if strings.Contains(string(output), gpgNoPublicKeyStatus) {
|
|
||||||
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", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// The GPG key ID and fingerprint passed to CreatePGPUnlocker.
|
// The GPG key ID and fingerprint passed to CreatePGPUnlocker.
|
||||||
@@ -48,7 +48,7 @@ func TestCreatePGPUnlockerFailureWritesNothing(t *testing.T) {
|
|||||||
installFakeGPG(t)
|
installFakeGPG(t)
|
||||||
|
|
||||||
base := afero.NewMemMapFs()
|
base := afero.NewMemMapFs()
|
||||||
vlt, err := vault.CreateVault(base, testVaultStateDir, testVaultName, nil, nil)
|
vlt, err := vault.CreateVault(base, testVaultStateDir, testVaultName, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fs := hookFs{Fs: base, before: func(_, path string) error {
|
fs := hookFs{Fs: base, before: func(_, path string) error {
|
||||||
@@ -87,7 +87,7 @@ func TestPGPUnlockerAddedTwiceKeepsFirst(t *testing.T) {
|
|||||||
|
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
mnemonic := testMnemonicBuffer(t)
|
mnemonic := testMnemonicBuffer(t)
|
||||||
_, err := vault.CreateVault(fs, testVaultStateDir, testVaultName, mnemonic, nil)
|
_, err := vault.CreateVault(fs, testVaultStateDir, testVaultName, mnemonic)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
first, err := secret.CreatePGPUnlocker(
|
first, err := secret.CreatePGPUnlocker(
|
||||||
|
|||||||
@@ -1,18 +1,26 @@
|
|||||||
package secret
|
package secret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// errSecretNotFound carries only the message tail; callers compose
|
||||||
|
// "secret <name> not found" around it so the emitted text is
|
||||||
|
// unchanged.
|
||||||
|
errSecretNotFound = errors.New("not found")
|
||||||
|
errUnlockerRequired = errors.New("unlocker required to decrypt secret")
|
||||||
errGetEncryptedDataDeprecated = errors.New(
|
errGetEncryptedDataDeprecated = errors.New(
|
||||||
"GetEncryptedData is deprecated - use version-specific methods")
|
"GetEncryptedData is deprecated - use version-specific methods")
|
||||||
errGetCurrentVaultNotRegistered = errors.New(
|
errGetCurrentVaultNotRegistered = errors.New(
|
||||||
@@ -73,6 +81,73 @@ func NewSecret(vault VaultInterface, name string) *Secret {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetValue retrieves and decrypts the current version's value, with the
|
||||||
|
// vault's long-term key derived from mnemonic when it is not nil, else
|
||||||
|
// obtained through unlocker
|
||||||
|
func (s *Secret) GetValue(
|
||||||
|
unlocker Unlocker, mnemonic *memguard.LockedBuffer,
|
||||||
|
) (*memguard.LockedBuffer, error) {
|
||||||
|
DebugWith("Getting secret value",
|
||||||
|
slog.String("secret_name", s.Name),
|
||||||
|
slog.String("vault_name", s.vault.GetName()),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Check if secret exists
|
||||||
|
exists, err := s.Exists()
|
||||||
|
if err != nil {
|
||||||
|
Debug("Failed to check if secret exists during GetValue",
|
||||||
|
"error", err, "secret_name", s.Name)
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("failed to check if secret exists: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
Debug("Secret not found during GetValue",
|
||||||
|
"secret_name", s.Name, "vault_name", s.vault.GetName())
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("secret %s %w", s.Name, errSecretNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
Debug("Secret exists, getting current version", "secret_name", s.Name)
|
||||||
|
|
||||||
|
// Get current version
|
||||||
|
currentVersion, err := GetCurrentVersion(s.vault.GetFilesystem(), s.Directory)
|
||||||
|
if err != nil {
|
||||||
|
Debug("Failed to get current version", "error", err, "secret_name", s.Name)
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("failed to get current version: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create version object
|
||||||
|
version := NewVersion(s.vault, s.Name, currentVersion)
|
||||||
|
|
||||||
|
if mnemonic != nil {
|
||||||
|
return s.getValueViaMnemonic(version, mnemonic.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
Debug("Using unlocker for vault access", "secret_name", s.Name)
|
||||||
|
|
||||||
|
// Use the provided unlocker to get the vault's long-term private key
|
||||||
|
if unlocker == nil {
|
||||||
|
Debug("No unlocker provided for secret decryption", "secret_name", s.Name)
|
||||||
|
|
||||||
|
return nil, errUnlockerRequired
|
||||||
|
}
|
||||||
|
|
||||||
|
ltIdentity, err := s.getLongTermIdentityFromUnlocker(unlocker)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
DebugWith("Successfully obtained vault's long-term key",
|
||||||
|
slog.String("secret_name", s.Name),
|
||||||
|
slog.String("public_key", ltIdentity.Recipient().String()),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Use the long-term key to decrypt the version
|
||||||
|
return version.GetValue(ltIdentity)
|
||||||
|
}
|
||||||
|
|
||||||
// LoadMetadata is deprecated - metadata is now per-version and encrypted
|
// LoadMetadata is deprecated - metadata is now per-version and encrypted
|
||||||
func (s *Secret) LoadMetadata() error {
|
func (s *Secret) LoadMetadata() error {
|
||||||
Debug("LoadMetadata called but is deprecated in versioned model",
|
Debug("LoadMetadata called but is deprecated in versioned model",
|
||||||
@@ -140,6 +215,124 @@ func (s *Secret) Exists() (bool, error) {
|
|||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getValueViaMnemonic derives the vault's long-term key from the
|
||||||
|
// mnemonic and decrypts the version value with it.
|
||||||
|
func (s *Secret) getValueViaMnemonic(
|
||||||
|
version *Version, mnemonic string,
|
||||||
|
) (*memguard.LockedBuffer, error) {
|
||||||
|
Debug("Using mnemonic for direct long-term key derivation",
|
||||||
|
"secret_name", s.Name)
|
||||||
|
|
||||||
|
// Get vault directory to read metadata
|
||||||
|
vaultDir, err := s.vault.GetDirectory()
|
||||||
|
if err != nil {
|
||||||
|
Debug("Failed to get vault directory", "error", err, "secret_name", s.Name)
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("failed to get vault directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load vault metadata to get the correct derivation index
|
||||||
|
metadataPath := filepath.Join(vaultDir, "vault-metadata.json")
|
||||||
|
|
||||||
|
metadataBytes, err := afero.ReadFile(s.vault.GetFilesystem(), metadataPath)
|
||||||
|
if err != nil {
|
||||||
|
Debug("Failed to read vault metadata", "error", err, "path", metadataPath)
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("failed to read vault metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var metadata VaultMetadata
|
||||||
|
|
||||||
|
err = json.Unmarshal(metadataBytes, &metadata)
|
||||||
|
if err != nil {
|
||||||
|
Debug("Failed to parse vault metadata", "error", err, "secret_name", s.Name)
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("failed to parse vault metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
DebugWith("Using vault derivation index from metadata",
|
||||||
|
slog.String("secret_name", s.Name),
|
||||||
|
slog.String("vault_name", s.vault.GetName()),
|
||||||
|
slog.Uint64("derivation_index", uint64(metadata.DerivationIndex)),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Use mnemonic with the vault's derivation index from metadata
|
||||||
|
ltIdentity, err := agehd.DeriveIdentity(mnemonic, metadata.DerivationIndex)
|
||||||
|
if err != nil {
|
||||||
|
Debug("Failed to derive long-term key from mnemonic for secret",
|
||||||
|
"error", err, "secret_name", s.Name)
|
||||||
|
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"failed to derive long-term key from mnemonic: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
Debug("Successfully derived long-term key from mnemonic", "secret_name", s.Name)
|
||||||
|
|
||||||
|
// Use the long-term key to decrypt the version
|
||||||
|
return version.GetValue(ltIdentity)
|
||||||
|
}
|
||||||
|
|
||||||
|
// getLongTermIdentityFromUnlocker uses the unlocker to obtain and parse
|
||||||
|
// the vault's long-term private key.
|
||||||
|
func (s *Secret) getLongTermIdentityFromUnlocker(
|
||||||
|
unlocker Unlocker,
|
||||||
|
) (*age.X25519Identity, error) {
|
||||||
|
DebugWith("Getting vault's long-term key using unlocker",
|
||||||
|
slog.String("secret_name", s.Name),
|
||||||
|
slog.String("unlocker_type", unlocker.GetType()),
|
||||||
|
slog.String("unlocker_id", unlocker.GetID()),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Step 1: Use the unlocker to get the vault's long-term private key
|
||||||
|
unlockIdentity, err := unlocker.GetIdentity()
|
||||||
|
if err != nil {
|
||||||
|
Debug("Failed to get unlocker identity",
|
||||||
|
"error", err, "secret_name", s.Name,
|
||||||
|
"unlocker_type", unlocker.GetType())
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("failed to get unlocker identity: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the encrypted long-term private key from the unlocker directory
|
||||||
|
encryptedLtPrivKeyPath := filepath.Join(unlocker.GetDirectory(), "longterm.age")
|
||||||
|
Debug("Reading encrypted long-term private key", "path", encryptedLtPrivKeyPath)
|
||||||
|
|
||||||
|
encryptedLtPrivKey, err := afero.ReadFile(
|
||||||
|
s.vault.GetFilesystem(), encryptedLtPrivKeyPath)
|
||||||
|
if err != nil {
|
||||||
|
Debug("Failed to read encrypted long-term private key",
|
||||||
|
"error", err, "path", encryptedLtPrivKeyPath)
|
||||||
|
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"failed to read encrypted long-term private key: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt the encrypted long-term private key using the unlocker
|
||||||
|
Debug("Decrypting long-term private key using unlocker", "secret_name", s.Name)
|
||||||
|
|
||||||
|
ltPrivKeyBuffer, err := DecryptWithIdentity(encryptedLtPrivKey, unlockIdentity)
|
||||||
|
if err != nil {
|
||||||
|
Debug("Failed to decrypt long-term private key",
|
||||||
|
"error", err, "secret_name", s.Name)
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("failed to decrypt long-term private key: %w", err)
|
||||||
|
}
|
||||||
|
defer ltPrivKeyBuffer.Destroy()
|
||||||
|
|
||||||
|
// Parse the long-term private key
|
||||||
|
Debug("Parsing long-term private key", "secret_name", s.Name)
|
||||||
|
|
||||||
|
ltIdentity, err := age.ParseX25519Identity(ltPrivKeyBuffer.String())
|
||||||
|
if err != nil {
|
||||||
|
Debug("Failed to parse long-term private key",
|
||||||
|
"error", err, "secret_name", s.Name)
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("failed to parse long-term private key: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ltIdentity, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetCurrentVault gets the current vault from the file system
|
// GetCurrentVault gets the current vault from the file system
|
||||||
// This function is a wrapper around the actual implementation in the vault package
|
// This function is a wrapper around the actual implementation in the vault package
|
||||||
// and exists to break the import cycle.
|
// and exists to break the import cycle.
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
package secret
|
package secret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -9,9 +10,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// testMnemonicValue is the standard BIP39 test vector mnemonic.
|
// testMnemonicValue is the standard BIP39 test vector mnemonic.
|
||||||
@@ -319,3 +321,46 @@ func TestPerSecretKeyFunctionality(t *testing.T) {
|
|||||||
t.Logf("Secret.Exists() works correctly")
|
t.Logf("Secret.Exists() works correctly")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestSecretGetValueWithMnemonicUsesVaultDerivationIndex checks that
|
||||||
|
// GetValue, given the mnemonic, derives the long-term key at the derivation
|
||||||
|
// index in the vault's metadata. At index 0 it could not decrypt the secret,
|
||||||
|
// which was encrypted to the key at index 1.
|
||||||
|
func TestSecretGetValueWithMnemonicUsesVaultDerivationIndex(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
vaultDir := "/test-config/vaults.d/test-vault"
|
||||||
|
|
||||||
|
mnemonic := memguard.NewBufferFromBytes([]byte(testMnemonicValue))
|
||||||
|
defer mnemonic.Destroy()
|
||||||
|
|
||||||
|
vlt := &MockVault{
|
||||||
|
name: "test-vault",
|
||||||
|
fs: fs,
|
||||||
|
directory: vaultDir,
|
||||||
|
derivationIndex: 1,
|
||||||
|
mnemonic: mnemonic,
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata, err := json.Marshal(VaultMetadata{DerivationIndex: vlt.derivationIndex})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, fs.MkdirAll(vaultDir, DirPerms))
|
||||||
|
|
||||||
|
err = afero.WriteFile(
|
||||||
|
fs, filepath.Join(vaultDir, "vault-metadata.json"), metadata, FilePerms)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
secretName, secretValue := "x", "value"
|
||||||
|
|
||||||
|
err = vlt.AddSecret(secretName,
|
||||||
|
memguard.NewBufferFromBytes([]byte(secretValue)), false)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
value, err := NewSecret(vlt, secretName).GetValue(nil, mnemonic)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
defer value.Destroy()
|
||||||
|
|
||||||
|
require.Equal(t, secretValue, value.String())
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/macse"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/macse"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -130,9 +130,17 @@ func (s *SecureEnclaveUnlocker) GetDirectory() string {
|
|||||||
return s.Directory
|
return s.Directory
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetID implements Unlocker interface: the name of the unlocker's directory.
|
// GetID implements Unlocker interface.
|
||||||
func (s *SecureEnclaveUnlocker) GetID() string {
|
func (s *SecureEnclaveUnlocker) GetID() string {
|
||||||
return filepath.Base(s.Directory)
|
hostname, err := os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
hostname = "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
createdAt := s.Metadata.CreatedAt
|
||||||
|
timestamp := createdAt.Format("2006-01-02.15.04")
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s-%s-%s", timestamp, hostname, seUnlockerType)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove implements Unlocker interface.
|
// Remove implements Unlocker interface.
|
||||||
@@ -216,6 +224,11 @@ func CreateSecureEnclaveUnlocker(
|
|||||||
stateDir string,
|
stateDir string,
|
||||||
mnemonic, passphrase *memguard.LockedBuffer,
|
mnemonic, passphrase *memguard.LockedBuffer,
|
||||||
) (*SecureEnclaveUnlocker, error) {
|
) (*SecureEnclaveUnlocker, error) {
|
||||||
|
err := checkMacOSAvailable()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
vault, err := GetCurrentVault(fs, stateDir)
|
vault, err := GetCurrentVault(fs, stateDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get current vault: %w", err)
|
return nil, fmt.Errorf("failed to get current vault: %w", err)
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ package secret
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
@@ -68,9 +67,9 @@ func (s *SecureEnclaveUnlocker) GetDirectory() string {
|
|||||||
return s.Directory
|
return s.Directory
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetID returns the unlocker ID, the name of the unlocker's directory.
|
// GetID returns the unlocker ID.
|
||||||
func (s *SecureEnclaveUnlocker) GetID() string {
|
func (s *SecureEnclaveUnlocker) GetID() string {
|
||||||
return filepath.Base(s.Directory)
|
return s.Metadata.CreatedAt.Format("2006-01-02.15.04") + "-" + seUnlockerType
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove returns an error on non-Darwin platforms.
|
// Remove returns an error on non-Darwin platforms.
|
||||||
|
|||||||
@@ -35,8 +35,9 @@ func TestNewSecureEnclaveUnlocker(t *testing.T) {
|
|||||||
// Test GetDirectory returns the directory we passed in
|
// Test GetDirectory returns the directory we passed in
|
||||||
assert.Equal(t, dir, unlocker.GetDirectory())
|
assert.Equal(t, dir, unlocker.GetDirectory())
|
||||||
|
|
||||||
// Test GetID returns the name of the unlocker's directory
|
// Test GetID returns a formatted string with the creation timestamp
|
||||||
assert.Equal(t, "test-se-unlocker", unlocker.GetID())
|
expectedID := "2026-01-15.10.30-secure-enclave"
|
||||||
|
assert.Equal(t, expectedID, unlocker.GetID())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSecureEnclaveUnlockerGetIdentityReturnsError(t *testing.T) {
|
func TestSecureEnclaveUnlockerGetIdentityReturnsError(t *testing.T) {
|
||||||
|
|||||||
@@ -4,8 +4,6 @@
|
|||||||
package secret
|
package secret
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -63,9 +61,11 @@ func TestSecureEnclaveUnlockerGetIDFormat(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unlocker := NewSecureEnclaveUnlocker(fs, "/tmp/test", metadata)
|
unlocker := NewSecureEnclaveUnlocker(fs, "/tmp/test", metadata)
|
||||||
|
id := unlocker.GetID()
|
||||||
|
|
||||||
// The ID is the name of the unlocker's directory
|
// ID should contain the timestamp and "secure-enclave" type
|
||||||
assert.Equal(t, "test", unlocker.GetID())
|
assert.Contains(t, id, "2026-03-10.14.30")
|
||||||
|
assert.Contains(t, id, seUnlockerType)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGenerateSEKeyLabel(t *testing.T) {
|
func TestGenerateSEKeyLabel(t *testing.T) {
|
||||||
@@ -108,10 +108,6 @@ func TestSecureEnclaveUnlockerGetIdentityMissingFile(t *testing.T) {
|
|||||||
// GetIdentity should fail because the encrypted longterm key file is missing
|
// GetIdentity should fail because the encrypted longterm key file is missing
|
||||||
identity, err := unlocker.GetIdentity()
|
identity, err := unlocker.GetIdentity()
|
||||||
assert.Nil(t, identity)
|
assert.Nil(t, identity)
|
||||||
|
require.Error(t, err)
|
||||||
var cause *os.PathError
|
assert.Contains(t, err.Error(), "failed to read SE-encrypted long-term key")
|
||||||
|
|
||||||
require.ErrorAs(t, err, &cause)
|
|
||||||
require.ErrorIs(t, err, os.ErrNotExist)
|
|
||||||
assert.Equal(t, filepath.Join(dir, seLongtermFilename), cause.Path)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,6 @@ type Unlocker interface {
|
|||||||
GetType() string
|
GetType() string
|
||||||
GetMetadata() UnlockerMetadata
|
GetMetadata() UnlockerMetadata
|
||||||
GetDirectory() string
|
GetDirectory() string
|
||||||
GetID() string // The name of the unlocker's directory, unique in its vault
|
GetID() string // Generate ID based on unlocker type and data
|
||||||
Remove() error // Remove the unlocker and any associated resources
|
Remove() error // Remove the unlocker and any associated resources
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,10 +22,10 @@ const (
|
|||||||
maxVersionsPerDay = 999
|
maxVersionsPerDay = 999
|
||||||
)
|
)
|
||||||
|
|
||||||
var errMaxVersionsPerDay = errors.New("exceeded maximum versions per day (999)")
|
var (
|
||||||
|
errMaxVersionsPerDay = errors.New("exceeded maximum versions per day (999)")
|
||||||
// ErrNilValueBuffer is returned when a secret's value is given as nil.
|
errNilValueBuffer = errors.New("value buffer is nil")
|
||||||
var ErrNilValueBuffer = errors.New("value buffer is nil")
|
)
|
||||||
|
|
||||||
// VersionMetadata contains information about a secret version
|
// VersionMetadata contains information about a secret version
|
||||||
type VersionMetadata struct {
|
type VersionMetadata struct {
|
||||||
@@ -138,7 +138,7 @@ func GenerateVersionName(fs afero.Fs, secretDir string) (string, error) {
|
|||||||
// process dies part-way.
|
// process dies part-way.
|
||||||
func (sv *Version) Save(value *memguard.LockedBuffer) error {
|
func (sv *Version) Save(value *memguard.LockedBuffer) error {
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return ErrNilValueBuffer
|
return errNilValueBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
DebugWith("Saving secret version",
|
DebugWith("Saving secret version",
|
||||||
@@ -557,18 +557,13 @@ func VersionExists(fs afero.Fs, secretDir string, version string) (bool, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetCurrentVersion returns the version that the "current" file points to
|
// GetCurrentVersion returns the version that the "current" file points to
|
||||||
// The file contains just the version name (e.g., "20231215.001"). If it
|
// The file contains just the version name (e.g., "20231215.001")
|
||||||
// cannot be read, the error says how to make a version current again: the
|
|
||||||
// versions themselves are not in the file.
|
|
||||||
func GetCurrentVersion(fs afero.Fs, secretDir string) (string, error) {
|
func GetCurrentVersion(fs afero.Fs, secretDir string) (string, error) {
|
||||||
currentPath := filepath.Join(secretDir, "current")
|
currentPath := filepath.Join(secretDir, "current")
|
||||||
|
|
||||||
fileData, err := afero.ReadFile(fs, currentPath)
|
fileData, err := afero.ReadFile(fs, currentPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to read current version file: %w; "+
|
return "", fmt.Errorf("failed to read current version file: %w", err)
|
||||||
"this file only names the current version: 'secret version list' "+
|
|
||||||
"lists the secret's versions, and 'secret version promote' makes "+
|
|
||||||
"one of them current", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
version := strings.TrimSpace(string(fileData))
|
version := strings.TrimSpace(string(fileData))
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
package secret
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGenerateVersionNameMaxSerial(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
secretDir := "/test/secret"
|
|
||||||
versionsDir := filepath.Join(secretDir, "versions")
|
|
||||||
|
|
||||||
// Create 999 versions
|
|
||||||
today := time.Now().Format("20060102")
|
|
||||||
for i := 1; i <= 999; i++ {
|
|
||||||
versionName := fmt.Sprintf("%s.%03d", today, i)
|
|
||||||
err := fs.MkdirAll(filepath.Join(versionsDir, versionName), 0o755)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to create one more - should fail
|
|
||||||
_, err := GenerateVersionName(fs, secretDir)
|
|
||||||
require.ErrorIs(t, err, errMaxVersionsPerDay)
|
|
||||||
}
|
|
||||||
@@ -36,16 +36,17 @@ package secret_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -126,6 +127,27 @@ func TestGenerateVersionName(t *testing.T) {
|
|||||||
assert.NotEqual(t, version1, version2)
|
assert.NotEqual(t, version1, version2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenerateVersionNameMaxSerial(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
secretDir := testSecretDir
|
||||||
|
versionsDir := filepath.Join(secretDir, "versions")
|
||||||
|
|
||||||
|
// Create 999 versions
|
||||||
|
today := time.Now().Format("20060102")
|
||||||
|
for i := 1; i <= 999; i++ {
|
||||||
|
versionName := fmt.Sprintf("%s.%03d", today, i)
|
||||||
|
err := fs.MkdirAll(filepath.Join(versionsDir, versionName), 0o755)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to create one more - should fail
|
||||||
|
_, err := secret.GenerateVersionName(fs, secretDir)
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "exceeded maximum versions per day")
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewVersion(t *testing.T) {
|
func TestNewVersion(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
@@ -31,10 +31,8 @@ var (
|
|||||||
// Composed as "vault <name> already exists".
|
// Composed as "vault <name> already exists".
|
||||||
ErrVaultExists = errors.New("already exists")
|
ErrVaultExists = errors.New("already exists")
|
||||||
|
|
||||||
// ErrUnlockerWithoutMnemonic indicates that CreateVault was given a
|
// ErrNilValueBuffer indicates a nil value buffer was supplied.
|
||||||
// passphrase for an unlocker but no mnemonic to derive the long-term key
|
ErrNilValueBuffer = errors.New("value buffer is nil")
|
||||||
// it unlocks. Composed as "vault <name> needs a mnemonic for an unlocker".
|
|
||||||
ErrUnlockerWithoutMnemonic = errors.New("needs a mnemonic for an unlocker")
|
|
||||||
|
|
||||||
// ErrInvalidSecretName indicates a secret name that breaks the naming
|
// ErrInvalidSecretName indicates a secret name that breaks the naming
|
||||||
// rule: only ASCII letters, digits, '.', '-', '_' and '/'; not empty;
|
// rule: only ASCII letters, digits, '.', '-', '_' and '/'; not empty;
|
||||||
|
|||||||
@@ -1,138 +0,0 @@
|
|||||||
package vault_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/awnumar/memguard"
|
|
||||||
"github.com/spf13/afero"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// otherMnemonic is a valid BIP39 mnemonic other than testMnemonic.
|
|
||||||
otherMnemonic = "legal winner thank year wave sausage worth useful " +
|
|
||||||
"legal winner thank yellow"
|
|
||||||
|
|
||||||
// missingName names no vault, secret or unlocker.
|
|
||||||
missingName = "missing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// newErrorTestVault creates the vault testVaultName, with the secret
|
|
||||||
// testSecretName in it, on a new in-memory filesystem.
|
|
||||||
func newErrorTestVault(t *testing.T) *vault.Vault {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(afero.NewMemMapFs(), testStateDir,
|
|
||||||
testVaultName, testMnemonicBuffer(t), nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
value := memguard.NewBufferFromBytes([]byte("value"))
|
|
||||||
t.Cleanup(value.Destroy)
|
|
||||||
|
|
||||||
require.NoError(t, vlt.AddSecret(testSecretName, value, false))
|
|
||||||
|
|
||||||
return vlt
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestVaultErrors checks that each failure returns its exported error,
|
|
||||||
// wrapped or not, so that errors.Is tells it apart from the others.
|
|
||||||
func TestVaultErrors(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
vaultDir := filepath.Join(testStateDir, "vaults.d", testVaultName)
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
run func(vlt *vault.Vault) error
|
|
||||||
want error
|
|
||||||
}{
|
|
||||||
{"create an existing vault", func(vlt *vault.Vault) error {
|
|
||||||
_, err := vault.CreateVault(vlt.GetFilesystem(), testStateDir,
|
|
||||||
testVaultName, nil, nil)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}, vault.ErrVaultExists},
|
|
||||||
{"select a missing vault", func(vlt *vault.Vault) error {
|
|
||||||
return vault.SelectVault(vlt.GetFilesystem(), testStateDir, missingName)
|
|
||||||
}, vault.ErrVaultNotFound},
|
|
||||||
{"add a nil value", func(vlt *vault.Vault) error {
|
|
||||||
return vlt.AddSecret(missingName, nil, false)
|
|
||||||
}, secret.ErrNilValueBuffer},
|
|
||||||
{"get a missing secret", func(vlt *vault.Vault) error {
|
|
||||||
_, err := vlt.GetSecret(missingName)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}, vault.ErrSecretNotFound},
|
|
||||||
{"copy onto an existing secret", func(vlt *vault.Vault) error {
|
|
||||||
return vlt.CopySecretAllVersions(vlt, testSecretName, testSecretName, false)
|
|
||||||
}, vault.ErrSecretExists},
|
|
||||||
{"copy a secret without versions", func(vlt *vault.Vault) error {
|
|
||||||
const versionless = "versionless"
|
|
||||||
|
|
||||||
err := vlt.GetFilesystem().MkdirAll(
|
|
||||||
filepath.Join(vaultDir, "secrets.d", versionless), secret.DirPerms)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return vlt.CopySecretAllVersions(vlt, versionless, "copy", false)
|
|
||||||
}, vault.ErrNoVersions},
|
|
||||||
{"remove a missing unlocker", func(vlt *vault.Vault) error {
|
|
||||||
return vlt.RemoveUnlocker(missingName)
|
|
||||||
}, vault.ErrUnlockerNotFound},
|
|
||||||
{"select a missing unlocker", func(vlt *vault.Vault) error {
|
|
||||||
return vlt.SelectUnlocker(missingName)
|
|
||||||
}, vault.ErrUnlockerNotFound},
|
|
||||||
{"unlocker of an unknown type", func(vlt *vault.Vault) error {
|
|
||||||
fs := vlt.GetFilesystem()
|
|
||||||
|
|
||||||
err := afero.WriteFile(fs,
|
|
||||||
filepath.Join(vaultDir, "unlockers.d", "odd", "unlocker-metadata.json"),
|
|
||||||
[]byte(`{"type":"odd"}`), secret.FilePerms)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = afero.WriteFile(fs, filepath.Join(vaultDir, "current-unlocker"),
|
|
||||||
[]byte("odd"), secret.FilePerms)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = vlt.GetCurrentUnlocker()
|
|
||||||
|
|
||||||
return err
|
|
||||||
}, vault.ErrUnsupportedUnlockerType},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
require.ErrorIs(t, tt.run(newErrorTestVault(t)), tt.want)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestGetSecretWithWrongMnemonic checks that getting a secret that exists,
|
|
||||||
// from a vault the given mnemonic does not open, fails with
|
|
||||||
// ErrMnemonicMismatch through GetSecret's wrapping, and not with
|
|
||||||
// ErrSecretNotFound.
|
|
||||||
func TestGetSecretWithWrongMnemonic(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
created := newErrorTestVault(t)
|
|
||||||
|
|
||||||
mnemonic := memguard.NewBufferFromBytes([]byte(otherMnemonic))
|
|
||||||
t.Cleanup(mnemonic.Destroy)
|
|
||||||
|
|
||||||
vlt := vault.NewVault(created.GetFilesystem(), testStateDir, testVaultName)
|
|
||||||
vlt.SetMnemonic(mnemonic)
|
|
||||||
|
|
||||||
_, err := vlt.GetSecret(testSecretName)
|
|
||||||
require.ErrorIs(t, err, vault.ErrMnemonicMismatch)
|
|
||||||
require.NotErrorIs(t, err, vault.ErrSecretNotFound)
|
|
||||||
}
|
|
||||||
@@ -2,17 +2,16 @@ package vault_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// deriveVaultIdentity derives the long-term identity for the given vault
|
// deriveVaultIdentity derives the long-term identity for the given vault
|
||||||
@@ -100,7 +99,7 @@ func testCurrentVaultFileHandling(t *testing.T, fs afero.Fs, tempDir string) {
|
|||||||
|
|
||||||
// Create a test vault
|
// Create a test vault
|
||||||
vlt, err := vault.CreateVault(fs, stateDir, testVaultName,
|
vlt, err := vault.CreateVault(fs, stateDir, testVaultName,
|
||||||
testMnemonicBuffer(t), nil)
|
testMnemonicBuffer(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create vault: %v", err)
|
t.Fatalf("Failed to create vault: %v", err)
|
||||||
}
|
}
|
||||||
@@ -148,7 +147,7 @@ func testDeepPathSecrets(t *testing.T, fs afero.Fs, tempDir string) {
|
|||||||
// Create a test vault - CreateVault writes the public key derived from
|
// Create a test vault - CreateVault writes the public key derived from
|
||||||
// the mnemonic
|
// the mnemonic
|
||||||
vlt, err := vault.CreateVault(fs, stateDir, testVaultName,
|
vlt, err := vault.CreateVault(fs, stateDir, testVaultName,
|
||||||
testMnemonicBuffer(t), nil)
|
testMnemonicBuffer(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create vault: %v", err)
|
t.Fatalf("Failed to create vault: %v", err)
|
||||||
}
|
}
|
||||||
@@ -224,7 +223,7 @@ func testKeyCaching(t *testing.T, fs afero.Fs, tempDir string) {
|
|||||||
// Create a test vault - CreateVault writes the public key derived from
|
// Create a test vault - CreateVault writes the public key derived from
|
||||||
// the mnemonic
|
// the mnemonic
|
||||||
vlt, err := vault.CreateVault(fs, stateDir, testVaultName,
|
vlt, err := vault.CreateVault(fs, stateDir, testVaultName,
|
||||||
testMnemonicBuffer(t), nil)
|
testMnemonicBuffer(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create vault: %v", err)
|
t.Fatalf("Failed to create vault: %v", err)
|
||||||
}
|
}
|
||||||
@@ -325,7 +324,7 @@ func testVaultNameValidation(t *testing.T, fs afero.Fs, tempDir string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, name := range validNames {
|
for _, name := range validNames {
|
||||||
_, err := vault.CreateVault(fs, stateDir, name, testMnemonicBuffer(t), nil)
|
_, err := vault.CreateVault(fs, stateDir, name, testMnemonicBuffer(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to create vault with valid name %q: %v", name, err)
|
t.Errorf("Failed to create vault with valid name %q: %v", name, err)
|
||||||
}
|
}
|
||||||
@@ -341,10 +340,10 @@ func testVaultNameValidation(t *testing.T, fs afero.Fs, tempDir string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, name := range invalidNames {
|
for _, name := range invalidNames {
|
||||||
_, err := vault.CreateVault(fs, stateDir, name, testMnemonicBuffer(t), nil)
|
_, err := vault.CreateVault(fs, stateDir, name, testMnemonicBuffer(t))
|
||||||
if !errors.Is(err, vault.ErrInvalidVaultName) {
|
if err == nil {
|
||||||
t.Errorf("Expected ErrInvalidVaultName creating vault with "+
|
t.Errorf("Expected error creating vault with invalid name %q, "+
|
||||||
"invalid name %q, got %v", name, err)
|
"but got none", name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -362,7 +361,7 @@ func testMultipleVaults(t *testing.T, fs afero.Fs, tempDir string) {
|
|||||||
// Create three vaults
|
// Create three vaults
|
||||||
vaultNames := []string{"vault1", "vault2", "vault3"}
|
vaultNames := []string{"vault1", "vault2", "vault3"}
|
||||||
for _, name := range vaultNames {
|
for _, name := range vaultNames {
|
||||||
_, err := vault.CreateVault(fs, stateDir, name, testMnemonicBuffer(t), nil)
|
_, err := vault.CreateVault(fs, stateDir, name, testMnemonicBuffer(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create vault %s: %v", name, err)
|
t.Fatalf("Failed to create vault %s: %v", name, err)
|
||||||
}
|
}
|
||||||
@@ -412,12 +411,12 @@ func testVaultIsolation(t *testing.T, fs afero.Fs, tempDir string) {
|
|||||||
|
|
||||||
// Create two vaults - CreateVault writes the public key derived from
|
// Create two vaults - CreateVault writes the public key derived from
|
||||||
// the mnemonic
|
// the mnemonic
|
||||||
vault1, err := vault.CreateVault(fs, stateDir, "vault1", testMnemonicBuffer(t), nil)
|
vault1, err := vault.CreateVault(fs, stateDir, "vault1", testMnemonicBuffer(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create vault1: %v", err)
|
t.Fatalf("Failed to create vault1: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
vault2, err := vault.CreateVault(fs, stateDir, "vault2", testMnemonicBuffer(t), nil)
|
vault2, err := vault.CreateVault(fs, stateDir, "vault2", testMnemonicBuffer(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create vault2: %v", err)
|
t.Fatalf("Failed to create vault2: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,12 +30,12 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// errUnexpectedValue is returned by concurrent readers when a secret value
|
// errUnexpectedValue is returned by concurrent readers when a secret value
|
||||||
@@ -49,7 +49,7 @@ func TestVersionIntegrationWorkflow(t *testing.T) {
|
|||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
// Create vault without a long-term key, which is set up below
|
// Create vault without a long-term key, which is set up below
|
||||||
vault, err := CreateVault(fs, testStateDir, "test", nil, nil)
|
vault, err := CreateVault(fs, testStateDir, "test", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Derive and store long-term key from mnemonic
|
// Derive and store long-term key from mnemonic
|
||||||
@@ -320,10 +320,10 @@ func testVersionSerialLimits(
|
|||||||
err = fs.MkdirAll(filepath.Join(secretDir, versionName), 0o755)
|
err = fs.MkdirAll(filepath.Join(secretDir, versionName), 0o755)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Should fail to create 1000th version. The error is unexported in
|
// Should fail to create 1000th version
|
||||||
// package secret, whose own test checks that it is the one returned.
|
|
||||||
_, err = secret.GenerateVersionName(fs, filepath.Dir(secretDir))
|
_, err = secret.GenerateVersionName(fs, filepath.Dir(secretDir))
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "exceeded maximum versions per day")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testVersionErrorCases(t *testing.T, vault *Vault, secretName string) {
|
func testVersionErrorCases(t *testing.T, vault *Vault, secretName string) {
|
||||||
@@ -331,18 +331,20 @@ func testVersionErrorCases(t *testing.T, vault *Vault, secretName string) {
|
|||||||
|
|
||||||
// Try to get non-existent version
|
// Try to get non-existent version
|
||||||
_, err := vault.GetSecretVersion(secretName, "99991231.999")
|
_, err := vault.GetSecretVersion(secretName, "99991231.999")
|
||||||
require.ErrorIs(t, err, ErrVersionNotFound)
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "not found")
|
||||||
|
|
||||||
// Try to get version of non-existent secret
|
// Try to get version of non-existent secret
|
||||||
_, err = vault.GetSecretVersion("nonexistent/secret", "")
|
_, err = vault.GetSecretVersion("nonexistent/secret", "")
|
||||||
require.ErrorIs(t, err, ErrSecretNotFound)
|
require.Error(t, err)
|
||||||
|
|
||||||
// Try to add secret without force when it exists
|
// Try to add secret without force when it exists
|
||||||
failBuffer := memguard.NewBufferFromBytes([]byte("should-fail"))
|
failBuffer := memguard.NewBufferFromBytes([]byte("should-fail"))
|
||||||
defer failBuffer.Destroy()
|
defer failBuffer.Destroy()
|
||||||
|
|
||||||
err = vault.AddSecret(secretName, failBuffer, false)
|
err = vault.AddSecret(secretName, failBuffer, false)
|
||||||
require.ErrorIs(t, err, ErrSecretExists)
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "already exists")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestVersionConcurrency tests concurrent version operations
|
// TestVersionConcurrency tests concurrent version operations
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// lockFileName is the file in the state directory that LockStateDir locks.
|
// lockFileName is the file in the state directory that LockStateDir locks.
|
||||||
|
|||||||
@@ -5,11 +5,11 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@@ -8,11 +8,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Register the GetCurrentVault function with the secret package
|
// Register the GetCurrentVault function with the secret package
|
||||||
@@ -153,17 +152,16 @@ func ListVaults(fs afero.Fs, stateDir string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// processMnemonicForVault handles mnemonic processing for vault creation.
|
// processMnemonicForVault handles mnemonic processing for vault creation.
|
||||||
// It returns the long-term key, nil when there is no mnemonic, and the
|
// It returns the derivation index, public key hash, and family hash.
|
||||||
// derivation index, public key hash, and family hash.
|
|
||||||
func processMnemonicForVault(
|
func processMnemonicForVault(
|
||||||
fs afero.Fs, stateDir, vaultDir, vaultName string,
|
fs afero.Fs, stateDir, vaultDir, vaultName string,
|
||||||
mnemonicBuffer *memguard.LockedBuffer,
|
mnemonicBuffer *memguard.LockedBuffer,
|
||||||
) (*age.X25519Identity, uint32, string, string, error) {
|
) (uint32, string, string, error) {
|
||||||
if mnemonicBuffer == nil {
|
if mnemonicBuffer == nil {
|
||||||
secret.Debug("No mnemonic given, vault created without long-term key",
|
secret.Debug("No mnemonic given, vault created without long-term key",
|
||||||
"vault", vaultName)
|
"vault", vaultName)
|
||||||
// Use 0 for derivation index when no mnemonic is provided
|
// Use 0 for derivation index when no mnemonic is provided
|
||||||
return nil, 0, "", "", nil
|
return 0, "", "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
mnemonic := mnemonicBuffer.String()
|
mnemonic := mnemonicBuffer.String()
|
||||||
@@ -173,14 +171,13 @@ func processMnemonicForVault(
|
|||||||
// Get the next available derivation index for this mnemonic
|
// Get the next available derivation index for this mnemonic
|
||||||
derivationIndex, err := GetNextDerivationIndex(fs, stateDir, mnemonic)
|
derivationIndex, err := GetNextDerivationIndex(fs, stateDir, mnemonic)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, "", "",
|
return 0, "", "", fmt.Errorf("failed to get next derivation index: %w", err)
|
||||||
fmt.Errorf("failed to get next derivation index: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Derive the long-term key using the actual derivation index
|
// Derive the long-term key using the actual derivation index
|
||||||
ltIdentity, err := agehd.DeriveIdentity(mnemonic, derivationIndex)
|
ltIdentity, err := agehd.DeriveIdentity(mnemonic, derivationIndex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, "", "", fmt.Errorf("failed to derive long-term key: %w", err)
|
return 0, "", "", fmt.Errorf("failed to derive long-term key: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the public key
|
// Write the public key
|
||||||
@@ -190,8 +187,7 @@ func processMnemonicForVault(
|
|||||||
|
|
||||||
err = secret.WriteFileAtomic(fs, ltPubKeyPath, []byte(ltPubKey))
|
err = secret.WriteFileAtomic(fs, ltPubKeyPath, []byte(ltPubKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, "", "",
|
return 0, "", "", fmt.Errorf("failed to write long-term public key: %w", err)
|
||||||
fmt.Errorf("failed to write long-term public key: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
secret.Debug("Wrote long-term public key", "path", ltPubKeyPath)
|
secret.Debug("Wrote long-term public key", "path", ltPubKeyPath)
|
||||||
@@ -203,33 +199,24 @@ func processMnemonicForVault(
|
|||||||
// This is used to identify which vaults belong to the same mnemonic family
|
// This is used to identify which vaults belong to the same mnemonic family
|
||||||
identity0, err := agehd.DeriveIdentity(mnemonic, 0)
|
identity0, err := agehd.DeriveIdentity(mnemonic, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, "", "",
|
return 0, "", "", fmt.Errorf("failed to derive identity for index 0: %w", err)
|
||||||
fmt.Errorf("failed to derive identity for index 0: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
familyHash := ComputeDoubleSHA256([]byte(identity0.Recipient().String()))
|
familyHash := ComputeDoubleSHA256([]byte(identity0.Recipient().String()))
|
||||||
|
|
||||||
return ltIdentity, derivationIndex, publicKeyHash, familyHash, nil
|
return derivationIndex, publicKeyHash, familyHash, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateVault creates a new vault and selects it as the current vault. When
|
// CreateVault creates a new vault and selects it as the current vault. When
|
||||||
// mnemonic is not nil, the vault's long-term key is derived from it, and the
|
// mnemonic is not nil, the vault's long-term key is derived from it, and the
|
||||||
// returned vault has it as its Mnemonic; when it is nil, the vault has no
|
// returned vault has it as its Mnemonic; when it is nil, the vault has no
|
||||||
// long-term key until one is imported. When passphrase is not nil, the vault
|
// long-term key until one is imported. It refuses a vault that already
|
||||||
// gets a passphrase unlocker protected by it, as its current unlocker; that
|
// exists before writing anything: creating it again would replace its keys,
|
||||||
// needs a mnemonic. It refuses a vault that already exists before writing
|
// and its secrets could no longer be decrypted. The commands that call it
|
||||||
// anything: creating it again would replace its keys, and its secrets could
|
// hold the state directory lock, so no other command can create the vault
|
||||||
// no longer be decrypted. The commands that call it hold the state directory
|
// between the check and the writes.
|
||||||
// lock, so no other command can create the vault between the check and the
|
|
||||||
// writes.
|
|
||||||
//
|
|
||||||
// The vault is written whole into a temporary directory, which is renamed
|
|
||||||
// into vaults.d only once complete, and only then selected: a crash at any
|
|
||||||
// point leaves either no vault or a complete one. The next command that
|
|
||||||
// takes the lock deletes what the crash left under a temporary name.
|
|
||||||
func CreateVault(
|
func CreateVault(
|
||||||
fs afero.Fs, stateDir string, name string,
|
fs afero.Fs, stateDir string, name string, mnemonic *memguard.LockedBuffer,
|
||||||
mnemonic, passphrase *memguard.LockedBuffer,
|
|
||||||
) (*Vault, error) {
|
) (*Vault, error) {
|
||||||
secret.Debug("Creating new vault", "name", name, "state_dir", stateDir)
|
secret.Debug("Creating new vault", "name", name, "state_dir", stateDir)
|
||||||
|
|
||||||
@@ -253,19 +240,51 @@ func CreateVault(
|
|||||||
return nil, fmt.Errorf("vault %s %w", name, ErrVaultExists)
|
return nil, fmt.Errorf("vault %s %w", name, ErrVaultExists)
|
||||||
}
|
}
|
||||||
|
|
||||||
if passphrase != nil && mnemonic == nil {
|
// Create vault directory structure
|
||||||
return nil, fmt.Errorf("vault %s %w", name, ErrUnlockerWithoutMnemonic)
|
|
||||||
}
|
|
||||||
|
|
||||||
secret.Debug("Creating vault directory structure", "vault_dir", vaultDir)
|
secret.Debug("Creating vault directory structure", "vault_dir", vaultDir)
|
||||||
|
|
||||||
err = secret.WriteDir(fs, vaultDir, func(dir string) error {
|
// Create main vault directory
|
||||||
return writeVaultFiles(fs, stateDir, dir, name, mnemonic, passphrase)
|
err = fs.MkdirAll(vaultDir, secret.DirPerms)
|
||||||
})
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create vault directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create secrets directory
|
||||||
|
secretsDir := filepath.Join(vaultDir, "secrets.d")
|
||||||
|
|
||||||
|
err = fs.MkdirAll(secretsDir, secret.DirPerms)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create secrets directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create unlockers directory
|
||||||
|
unlockersDir := filepath.Join(vaultDir, "unlockers.d")
|
||||||
|
|
||||||
|
err = fs.MkdirAll(unlockersDir, secret.DirPerms)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create unlockers directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process mnemonic if available
|
||||||
|
derivationIndex, publicKeyHash, familyHash, err := processMnemonicForVault(
|
||||||
|
fs, stateDir, vaultDir, name, mnemonic)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save vault metadata
|
||||||
|
metadata := &Metadata{
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
DerivationIndex: derivationIndex,
|
||||||
|
PublicKeyHash: publicKeyHash,
|
||||||
|
MnemonicFamilyHash: familyHash,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = SaveVaultMetadata(fs, vaultDir, metadata)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to save vault metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Select the newly created vault as current
|
// Select the newly created vault as current
|
||||||
secret.Debug("Selecting newly created vault as current", "name", name)
|
secret.Debug("Selecting newly created vault as current", "name", name)
|
||||||
|
|
||||||
@@ -283,47 +302,6 @@ func CreateVault(
|
|||||||
return vlt, nil
|
return vlt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeVaultFiles writes the files of the new vault name into vaultDir: its
|
|
||||||
// secrets and unlockers directories, its long-term public key and metadata,
|
|
||||||
// and, when passphrase is not nil, a passphrase unlocker as its current one.
|
|
||||||
func writeVaultFiles(
|
|
||||||
fs afero.Fs, stateDir, vaultDir, name string,
|
|
||||||
mnemonic, passphrase *memguard.LockedBuffer,
|
|
||||||
) error {
|
|
||||||
for _, subdir := range []string{"secrets.d", "unlockers.d"} {
|
|
||||||
err := fs.MkdirAll(filepath.Join(vaultDir, subdir), secret.DirPerms)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create %s directory: %w", subdir, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ltIdentity, derivationIndex, publicKeyHash, familyHash, err :=
|
|
||||||
processMnemonicForVault(fs, stateDir, vaultDir, name, mnemonic)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata := &Metadata{
|
|
||||||
CreatedAt: time.Now(),
|
|
||||||
DerivationIndex: derivationIndex,
|
|
||||||
PublicKeyHash: publicKeyHash,
|
|
||||||
MnemonicFamilyHash: familyHash,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = SaveVaultMetadata(fs, vaultDir, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to save vault metadata: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if passphrase == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = writePassphraseUnlocker(fs, vaultDir, ltIdentity, passphrase)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// SelectVault selects the given vault as the current vault
|
// SelectVault selects the given vault as the current vault
|
||||||
func SelectVault(fs afero.Fs, stateDir string, name string) error {
|
func SelectVault(fs afero.Fs, stateDir string, name string) error {
|
||||||
secret.Debug("Selecting vault", "vault_name", name, "state_dir", stateDir)
|
secret.Debug("Selecting vault", "vault_name", name, "state_dir", stateDir)
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Metadata is an alias for secret.VaultMetadata
|
// Metadata is an alias for secret.VaultMetadata
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//nolint:paralleltest // subtests share an in-memory filesystem sequentially
|
//nolint:paralleltest // subtests share an in-memory filesystem sequentially
|
||||||
@@ -304,7 +304,7 @@ func TestWorkflowMismatch(t *testing.T) {
|
|||||||
fs := afero.NewOsFs()
|
fs := afero.NewOsFs()
|
||||||
|
|
||||||
// Test Case 1: Create vault WITH mnemonic (like init command)
|
// Test Case 1: Create vault WITH mnemonic (like init command)
|
||||||
_, err := vault.CreateVault(fs, tempDir, "default", testMnemonicBuffer(t), nil)
|
_, err := vault.CreateVault(fs, tempDir, "default", testMnemonicBuffer(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create vault with mnemonic: %v", err)
|
t.Fatalf("Failed to create vault with mnemonic: %v", err)
|
||||||
}
|
}
|
||||||
@@ -321,7 +321,7 @@ func TestWorkflowMismatch(t *testing.T) {
|
|||||||
metadata1.DerivationIndex, metadata1.PublicKeyHash)
|
metadata1.DerivationIndex, metadata1.PublicKeyHash)
|
||||||
|
|
||||||
// Test Case 2: Create vault WITHOUT mnemonic, then import (work vault)
|
// Test Case 2: Create vault WITHOUT mnemonic, then import (work vault)
|
||||||
_, err = vault.CreateVault(fs, tempDir, "work", nil, nil)
|
_, err = vault.CreateVault(fs, tempDir, "work", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create vault without mnemonic: %v", err)
|
t.Fatalf("Failed to create vault without mnemonic: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ package vault_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestGetSecretVersionRejectsPathTraversal verifies that GetSecretVersion
|
// TestGetSecretVersionRejectsPathTraversal verifies that GetSecretVersion
|
||||||
@@ -18,7 +18,7 @@ func TestGetSecretVersionRejectsPathTraversal(t *testing.T) {
|
|||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
vlt, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
||||||
testMnemonicBuffer(t), nil)
|
testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Add a legitimate secret so the vault is set up
|
// Add a legitimate secret so the vault is set up
|
||||||
@@ -41,8 +41,10 @@ func TestGetSecretVersionRejectsPathTraversal(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
_, err := vlt.GetSecretVersion(name, "")
|
_, err := vlt.GetSecretVersion(name, "")
|
||||||
require.ErrorIs(t, err, vault.ErrInvalidSecretName,
|
require.Error(t, err,
|
||||||
"GetSecretVersion should reject malicious name: %s", name)
|
"GetSecretVersion should reject malicious name: %s", name)
|
||||||
|
require.Contains(t, err.Error(), "invalid secret name",
|
||||||
|
"error should indicate invalid name for: %s", name)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -55,11 +57,12 @@ func TestGetSecretRejectsPathTraversal(t *testing.T) {
|
|||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
vlt, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
||||||
testMnemonicBuffer(t), nil)
|
testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
_, err = vlt.GetSecret("../../../etc/passwd")
|
_, err = vlt.GetSecret("../../../etc/passwd")
|
||||||
require.ErrorIs(t, err, vault.ErrInvalidSecretName)
|
require.Error(t, err)
|
||||||
|
require.Contains(t, err.Error(), "invalid secret name")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestGetSecretObjectRejectsPathTraversal verifies GetSecretObject
|
// TestGetSecretObjectRejectsPathTraversal verifies GetSecretObject
|
||||||
@@ -70,7 +73,7 @@ func TestGetSecretObjectRejectsPathTraversal(t *testing.T) {
|
|||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
vlt, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
||||||
testMnemonicBuffer(t), nil)
|
testMnemonicBuffer(t))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
maliciousNames := []string{
|
maliciousNames := []string{
|
||||||
@@ -84,8 +87,8 @@ func TestGetSecretObjectRejectsPathTraversal(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
_, err := vlt.GetSecretObject(name)
|
_, err := vlt.GetSecretObject(name)
|
||||||
require.ErrorIs(t, err, vault.ErrInvalidSecretName,
|
require.Error(t, err, "GetSecretObject should reject: %s", name)
|
||||||
"GetSecretObject should reject: %s", name)
|
require.Contains(t, err.Error(), "invalid secret name")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ListSecrets returns a list of secret names in this vault
|
// ListSecrets returns a list of secret names in this vault
|
||||||
@@ -130,7 +130,7 @@ func ValidateSecretName(name string) error {
|
|||||||
// AddSecret adds a secret to this vault
|
// AddSecret adds a secret to this vault
|
||||||
func (v *Vault) AddSecret(name string, value *memguard.LockedBuffer, force bool) error {
|
func (v *Vault) AddSecret(name string, value *memguard.LockedBuffer, force bool) error {
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return secret.ErrNilValueBuffer
|
return ErrNilValueBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
secret.DebugWith("Adding secret to vault",
|
secret.DebugWith("Adding secret to vault",
|
||||||
|
|||||||
@@ -27,12 +27,12 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// testMnemonic is the mnemonic used to derive the vault long-term key.
|
// testMnemonic is the mnemonic used to derive the vault long-term key.
|
||||||
@@ -66,7 +66,7 @@ func createTestVaultWithKey(t *testing.T, fs afero.Fs) *Vault {
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
// Create vault without a long-term key, which is set up below
|
// Create vault without a long-term key, which is set up below
|
||||||
vault, err := CreateVault(fs, testStateDir, "test", nil, nil)
|
vault, err := CreateVault(fs, testStateDir, "test", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Derive and store long-term key from mnemonic
|
// Derive and store long-term key from mnemonic
|
||||||
@@ -143,7 +143,8 @@ func TestVaultAddSecretMultipleVersions(t *testing.T) {
|
|||||||
defer failBuffer.Destroy()
|
defer failBuffer.Destroy()
|
||||||
|
|
||||||
err := vault.AddSecret(testSecretPath, failBuffer, false)
|
err := vault.AddSecret(testSecretPath, failBuffer, false)
|
||||||
require.ErrorIs(t, err, ErrSecretExists)
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "already exists")
|
||||||
|
|
||||||
// Add with force - should create new version
|
// Add with force - should create new version
|
||||||
addTestSecretToVault(t, vault, testSecretPath, []byte("version-2"), true)
|
addTestSecretToVault(t, vault, testSecretPath, []byte("version-2"), true)
|
||||||
@@ -308,7 +309,8 @@ func TestVaultGetNonExistentVersion(t *testing.T) {
|
|||||||
|
|
||||||
// Try to get non-existent version
|
// Try to get non-existent version
|
||||||
_, err := vault.GetSecretVersion(testSecretPath, "20991231.999")
|
_, err := vault.GetSecretVersion(testSecretPath, "20991231.999")
|
||||||
require.ErrorIs(t, err, ErrVersionNotFound)
|
require.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateVersionMetadata(t *testing.T) {
|
func TestUpdateVersionMetadata(t *testing.T) {
|
||||||
|
|||||||
+26
-42
@@ -11,9 +11,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Unlocker metadata type strings.
|
// Unlocker metadata type strings.
|
||||||
@@ -188,9 +188,8 @@ func (v *Vault) findUnlockerByID(
|
|||||||
return nil, skippedDirPath, nil
|
return nil, skippedDirPath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListUnlockers returns the metadata of each unlocker of this vault, keyed
|
// ListUnlockers returns a list of available unlockers for this vault
|
||||||
// by the unlocker's ID, the name of its directory in unlockers.d
|
func (v *Vault) ListUnlockers() ([]UnlockerMetadata, error) {
|
||||||
func (v *Vault) ListUnlockers() (map[string]UnlockerMetadata, error) {
|
|
||||||
vaultDir, err := v.GetDirectory()
|
vaultDir, err := v.GetDirectory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -205,7 +204,7 @@ func (v *Vault) ListUnlockers() (map[string]UnlockerMetadata, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return map[string]UnlockerMetadata{}, nil
|
return []UnlockerMetadata{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// List directories in unlockers.d
|
// List directories in unlockers.d
|
||||||
@@ -214,7 +213,7 @@ func (v *Vault) ListUnlockers() (map[string]UnlockerMetadata, error) {
|
|||||||
return nil, fmt.Errorf("failed to read unlockers directory: %w", err)
|
return nil, fmt.Errorf("failed to read unlockers directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
unlockers := map[string]UnlockerMetadata{}
|
var unlockers []UnlockerMetadata
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if !file.IsDir() {
|
if !file.IsDir() {
|
||||||
@@ -223,7 +222,7 @@ func (v *Vault) ListUnlockers() (map[string]UnlockerMetadata, error) {
|
|||||||
|
|
||||||
metadata, ok := v.readUnlockerMetadataOrWarn(unlockersDir, file.Name())
|
metadata, ok := v.readUnlockerMetadataOrWarn(unlockersDir, file.Name())
|
||||||
if ok {
|
if ok {
|
||||||
unlockers[file.Name()] = metadata
|
unlockers = append(unlockers, metadata)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,31 +390,8 @@ func (v *Vault) CreatePassphraseUnlocker(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
unlocker, err := writePassphraseUnlocker(v.fs, vaultDir, ltIdentity, passphrase)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, oldDir := range oldDirs {
|
|
||||||
err = secret.RemoveDirAtomic(v.fs, oldDir)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf(
|
|
||||||
"created and selected the new passphrase unlocker: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return unlocker, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// writePassphraseUnlocker writes a new passphrase unlocker of the long-term
|
|
||||||
// key ltIdentity into the vault directory vaultDir, in a directory of its own,
|
|
||||||
// and makes it the vault's current unlocker.
|
|
||||||
func writePassphraseUnlocker(
|
|
||||||
fs afero.Fs, vaultDir string, ltIdentity *age.X25519Identity,
|
|
||||||
passphrase *memguard.LockedBuffer,
|
|
||||||
) (*secret.PassphraseUnlocker, error) {
|
|
||||||
createdAt := time.Now()
|
createdAt := time.Now()
|
||||||
unlockerDir := filepath.Join(vaultDir, "unlockers.d", unlockerTypePassphrase+"-"+
|
unlockerDir := filepath.Join(unlockersDir, unlockerTypePassphrase+"-"+
|
||||||
createdAt.UTC().Format(secret.UnlockerTimeFormat))
|
createdAt.UTC().Format(secret.UnlockerTimeFormat))
|
||||||
|
|
||||||
// Generate new age keypair for unlocker
|
// Generate new age keypair for unlocker
|
||||||
@@ -446,24 +422,33 @@ func writePassphraseUnlocker(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write the unlocker's files, the metadata last
|
// Write the unlocker's files, the metadata last
|
||||||
err = secret.WriteDir(fs, unlockerDir, func(dir string) error {
|
err = secret.WriteDir(v.fs, unlockerDir, func(dir string) error {
|
||||||
return writeUnlockerFiles(fs, dir, unlockerIdentity, passphrase,
|
return v.writeUnlockerFiles(dir, unlockerIdentity, passphrase,
|
||||||
encryptedLtPrivKey, metadataBytes)
|
encryptedLtPrivKey, metadataBytes)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the new unlocker the current one
|
// Select the new unlocker by its directory, not by its ID: an old
|
||||||
|
// passphrase unlocker created in the same minute has the same ID.
|
||||||
currentUnlockerPath := filepath.Join(vaultDir, "current-unlocker")
|
currentUnlockerPath := filepath.Join(vaultDir, "current-unlocker")
|
||||||
|
|
||||||
err = secret.WriteFileAtomic(fs, currentUnlockerPath,
|
err = secret.WriteFileAtomic(v.fs, currentUnlockerPath,
|
||||||
[]byte(filepath.Base(unlockerDir)))
|
[]byte(filepath.Base(unlockerDir)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to select new unlocker: %w", err)
|
return nil, fmt.Errorf("failed to select new unlocker: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return secret.NewPassphraseUnlocker(fs, unlockerDir, metadata), nil
|
for _, oldDir := range oldDirs {
|
||||||
|
err = secret.RemoveDirAtomic(v.fs, oldDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"created and selected the new passphrase unlocker: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return secret.NewPassphraseUnlocker(v.fs, unlockerDir, metadata), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// passphraseUnlockerDirs returns the directories in unlockersDir that hold
|
// passphraseUnlockerDirs returns the directories in unlockersDir that hold
|
||||||
@@ -529,8 +514,7 @@ func (v *Vault) readUnlockerMetadata(unlockerDir string) (UnlockerMetadata, erro
|
|||||||
// writeUnlockerFiles writes the files of a passphrase unlocker into
|
// writeUnlockerFiles writes the files of a passphrase unlocker into
|
||||||
// unlockerDir: its public key, its passphrase-encrypted private key, the
|
// unlockerDir: its public key, its passphrase-encrypted private key, the
|
||||||
// long-term private key encrypted to it, and its metadata, last.
|
// long-term private key encrypted to it, and its metadata, last.
|
||||||
func writeUnlockerFiles(
|
func (v *Vault) writeUnlockerFiles(
|
||||||
fs afero.Fs,
|
|
||||||
unlockerDir string,
|
unlockerDir string,
|
||||||
unlockerIdentity *age.X25519Identity,
|
unlockerIdentity *age.X25519Identity,
|
||||||
passphrase *memguard.LockedBuffer,
|
passphrase *memguard.LockedBuffer,
|
||||||
@@ -539,7 +523,7 @@ func writeUnlockerFiles(
|
|||||||
// Write public key
|
// Write public key
|
||||||
pubKeyPath := filepath.Join(unlockerDir, "pub.age")
|
pubKeyPath := filepath.Join(unlockerDir, "pub.age")
|
||||||
|
|
||||||
err := secret.WriteFileAtomic(fs, pubKeyPath,
|
err := secret.WriteFileAtomic(v.fs, pubKeyPath,
|
||||||
[]byte(unlockerIdentity.Recipient().String()))
|
[]byte(unlockerIdentity.Recipient().String()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write unlocker public key: %w", err)
|
return fmt.Errorf("failed to write unlocker public key: %w", err)
|
||||||
@@ -557,18 +541,18 @@ func writeUnlockerFiles(
|
|||||||
// Write encrypted private key
|
// Write encrypted private key
|
||||||
privKeyPath := filepath.Join(unlockerDir, "priv.age")
|
privKeyPath := filepath.Join(unlockerDir, "priv.age")
|
||||||
|
|
||||||
err = secret.WriteFileAtomic(fs, privKeyPath, encryptedPrivKey)
|
err = secret.WriteFileAtomic(v.fs, privKeyPath, encryptedPrivKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write encrypted unlocker private key: %w", err)
|
return fmt.Errorf("failed to write encrypted unlocker private key: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = secret.WriteFileAtomic(fs,
|
err = secret.WriteFileAtomic(v.fs,
|
||||||
filepath.Join(unlockerDir, "longterm.age"), encryptedLtPrivKey)
|
filepath.Join(unlockerDir, "longterm.age"), encryptedLtPrivKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write encrypted long-term private key: %w", err)
|
return fmt.Errorf("failed to write encrypted long-term private key: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = secret.WriteFileAtomic(fs,
|
err = secret.WriteFileAtomic(v.fs,
|
||||||
filepath.Join(unlockerDir, "unlocker-metadata.json"), metadataBytes)
|
filepath.Join(unlockerDir, "unlocker-metadata.json"), metadataBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write unlocker metadata: %w", err)
|
return fmt.Errorf("failed to write unlocker metadata: %w", err)
|
||||||
|
|||||||
+4
-39
@@ -1,16 +1,15 @@
|
|||||||
package vault
|
package vault
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Vault represents a secrets vault
|
// Vault represents a secrets vault
|
||||||
@@ -99,8 +98,7 @@ func (v *Vault) GetOrDeriveLongTermKey() (*age.X25519Identity, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
secret.Debug("Failed to get current unlocker", "error", err, "vault_name", v.Name)
|
secret.Debug("Failed to get current unlocker", "error", err, "vault_name", v.Name)
|
||||||
|
|
||||||
return nil, v.withMnemonicAdvice(
|
return nil, fmt.Errorf("failed to get current unlocker: %w", err)
|
||||||
fmt.Errorf("failed to get current unlocker: %w", err))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
secret.DebugWith("Retrieved current unlocker for vault unlock",
|
secret.DebugWith("Retrieved current unlocker for vault unlock",
|
||||||
@@ -114,7 +112,7 @@ func (v *Vault) GetOrDeriveLongTermKey() (*age.X25519Identity, error) {
|
|||||||
// Other unlockers return their own identity, used to decrypt longterm.age.
|
// Other unlockers return their own identity, used to decrypt longterm.age.
|
||||||
ltIdentity, err := v.unlockLongTermKey(unlocker)
|
ltIdentity, err := v.unlockLongTermKey(unlocker)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, v.withMnemonicAdvice(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
secret.DebugWith("Successfully obtained long-term identity via unlocker",
|
secret.DebugWith("Successfully obtained long-term identity via unlocker",
|
||||||
@@ -297,36 +295,3 @@ func (v *Vault) unlockLongTermKey(
|
|||||||
|
|
||||||
return ltIdentity, nil
|
return ltIdentity, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// withMnemonicAdvice returns err, a failure to get the long-term key through
|
|
||||||
// the current unlocker, with advice added: that the mnemonic still opens the
|
|
||||||
// vault, and how to give it a new unlocker. The advice is added only when the
|
|
||||||
// vault metadata records the key that the mnemonic derives; a vault created
|
|
||||||
// without a mnemonic records none, and without its metadata the key cannot
|
|
||||||
// be derived. It is not added when the passphrase could not be read: the
|
|
||||||
// unlocker was not tried, and adding one would need a passphrase read the
|
|
||||||
// same way.
|
|
||||||
func (v *Vault) withMnemonicAdvice(err error) error {
|
|
||||||
if errors.Is(err, secret.ErrPassphraseNotRead) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
vaultDir, _ := v.GetDirectory()
|
|
||||||
|
|
||||||
metadata, metadataErr := LoadVaultMetadata(v.fs, vaultDir)
|
|
||||||
if metadataErr != nil || metadata.PublicKeyHash == "" {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 'secret unlocker add' acts on the current vault only.
|
|
||||||
steps := "'secret unlocker add passphrase'"
|
|
||||||
|
|
||||||
current, currentErr := GetCurrentVault(v.fs, v.stateDir)
|
|
||||||
if currentErr != nil || current.Name != v.Name {
|
|
||||||
steps = fmt.Sprintf("'secret vault select %s', then %s", v.Name, steps)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("%w; the vault '%s' still opens with its mnemonic: run "+
|
|
||||||
"%s with %s set to the mnemonic to give it a new unlocker",
|
|
||||||
err, v.Name, steps, secret.EnvMnemonic)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
package vault_test
|
package vault_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAddSecretFailsWithMissingPublicKey(t *testing.T) {
|
func TestAddSecretFailsWithMissingPublicKey(t *testing.T) {
|
||||||
@@ -37,13 +36,8 @@ func TestAddSecretFailsWithMissingPublicKey(t *testing.T) {
|
|||||||
defer value.Destroy()
|
defer value.Destroy()
|
||||||
|
|
||||||
err := vlt.AddSecret(testSecretName, value, false)
|
err := vlt.AddSecret(testSecretName, value, false)
|
||||||
|
require.Error(t, err, "AddSecret should fail when public key is missing")
|
||||||
var cause *os.PathError
|
assert.Contains(t, err.Error(), "failed to read long-term public key")
|
||||||
|
|
||||||
require.ErrorAs(t, err, &cause)
|
|
||||||
require.ErrorIs(t, err, os.ErrNotExist,
|
|
||||||
"AddSecret should fail when public key is missing")
|
|
||||||
assert.Equal(t, filepath.Join(vaultDir, "pub.age"), cause.Path)
|
|
||||||
|
|
||||||
// Verify that the secret directory was NOT created
|
// Verify that the secret directory was NOT created
|
||||||
secretDir := filepath.Join(vaultDir, "secrets.d", testSecretName)
|
secretDir := filepath.Join(vaultDir, "secrets.d", testSecretName)
|
||||||
|
|||||||
@@ -2,16 +2,15 @@ package vault_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/internal/secret"
|
||||||
|
"git.eeqj.de/sneak/secret/internal/vault"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
"github.com/awnumar/memguard"
|
"github.com/awnumar/memguard"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"sneak.berlin/go/secret/internal/secret"
|
|
||||||
"sneak.berlin/go/secret/internal/vault"
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// testMnemonic is the shared BIP39 test mnemonic for tests in this package.
|
// testMnemonic is the shared BIP39 test mnemonic for tests in this package.
|
||||||
@@ -73,7 +72,7 @@ func testCreateVault(t *testing.T, fs afero.Fs) {
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
vlt, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
vlt, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
||||||
testMnemonicBuffer(t), nil)
|
testMnemonicBuffer(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create vault: %v", err)
|
t.Fatalf("Failed to create vault: %v", err)
|
||||||
}
|
}
|
||||||
@@ -299,7 +298,7 @@ func TestListUnlockers_SkipsMissingMetadata(t *testing.T) {
|
|||||||
|
|
||||||
// Create vault
|
// Create vault
|
||||||
vlt, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
vlt, err := vault.CreateVault(fs, testStateDir, testVaultName,
|
||||||
testMnemonicBuffer(t), nil)
|
testMnemonicBuffer(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create vault: %v", err)
|
t.Fatalf("Failed to create vault: %v", err)
|
||||||
}
|
}
|
||||||
@@ -345,29 +344,3 @@ func TestListUnlockers_SkipsMissingMetadata(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestCreateVaultUnlockerNeedsMnemonic checks that CreateVault, given a
|
|
||||||
// passphrase for an unlocker but no mnemonic to derive the long-term key from,
|
|
||||||
// fails without writing anything.
|
|
||||||
func TestCreateVaultUnlockerNeedsMnemonic(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
fs := afero.NewMemMapFs()
|
|
||||||
|
|
||||||
passphrase := memguard.NewBufferFromBytes([]byte(testPassphrase))
|
|
||||||
defer passphrase.Destroy()
|
|
||||||
|
|
||||||
_, err := vault.CreateVault(fs, testStateDir, testVaultName, nil, passphrase)
|
|
||||||
if !errors.Is(err, vault.ErrUnlockerWithoutMnemonic) {
|
|
||||||
t.Fatalf("Expected ErrUnlockerWithoutMnemonic, got %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
exists, err := afero.Exists(fs, testStateDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to check for the state directory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if exists {
|
|
||||||
t.Errorf("CreateVault wrote the state directory")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"devDependencies": {
|
|
||||||
"prettier": "3.8.1"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+32
-49
@@ -1,21 +1,14 @@
|
|||||||
# agehd - Deterministic Age Identities from BIP85
|
# agehd - Deterministic Age Identities from BIP85
|
||||||
|
|
||||||
The `agehd` package derives deterministic X25519 age identities using BIP85
|
The `agehd` package derives deterministic X25519 age identities using BIP85 entropy derivation and a deterministic random number generator (DRNG). This package only supports proper BIP85 sources: BIP39 mnemonics and extended private keys (xprv).
|
||||||
entropy derivation and a deterministic random number generator (DRNG). This
|
|
||||||
package only supports proper BIP85 sources: BIP39 mnemonics and extended private
|
|
||||||
keys (xprv).
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **Deterministic key generation**: Same input always produces the same age
|
- **Deterministic key generation**: Same input always produces the same age identity
|
||||||
identity
|
|
||||||
- **BIP85 compliance**: Uses the BIP85 standard for entropy derivation
|
- **BIP85 compliance**: Uses the BIP85 standard for entropy derivation
|
||||||
- **Multiple key support**: Generate multiple keys from the same source using
|
- **Multiple key support**: Generate multiple keys from the same source using different indices
|
||||||
different indices
|
- **Two BIP85 input methods**: Support for BIP39 mnemonics and extended private keys (xprv)
|
||||||
- **Two BIP85 input methods**: Support for BIP39 mnemonics and extended private
|
- **Vendor/application scoped**: Uses vendor-specific derivation paths to avoid conflicts
|
||||||
keys (xprv)
|
|
||||||
- **Vendor/application scoped**: Uses vendor-specific derivation paths to avoid
|
|
||||||
conflicts
|
|
||||||
|
|
||||||
## Derivation Path
|
## Derivation Path
|
||||||
|
|
||||||
@@ -26,7 +19,6 @@ m/83696968'/592366788'/733482323'/n'
|
|||||||
```
|
```
|
||||||
|
|
||||||
Where:
|
Where:
|
||||||
|
|
||||||
- `83696968'` is the BIP85 root path ("bip" in ASCII)
|
- `83696968'` is the BIP85 root path ("bip" in ASCII)
|
||||||
- `592366788'` is the vendor ID (sha256("berlin.sneak") & 0x7fffffff)
|
- `592366788'` is the vendor ID (sha256("berlin.sneak") & 0x7fffffff)
|
||||||
- `733482323'` is the application ID (sha256("secret") & 0x7fffffff)
|
- `733482323'` is the application ID (sha256("secret") & 0x7fffffff)
|
||||||
@@ -42,19 +34,19 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
mnemonic := "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
|
mnemonic := "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
|
||||||
|
|
||||||
// Derive the first identity (index 0)
|
// Derive the first identity (index 0)
|
||||||
identity, err := agehd.DeriveIdentity(mnemonic, 0)
|
identity, err := agehd.DeriveIdentity(mnemonic, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Secret key: %s\n", identity.String())
|
fmt.Printf("Secret key: %s\n", identity.String())
|
||||||
fmt.Printf("Public key: %s\n", identity.Recipient().String())
|
fmt.Printf("Public key: %s\n", identity.Recipient().String())
|
||||||
}
|
}
|
||||||
@@ -68,19 +60,19 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
xprv := "xprv9s21ZrQH143K2LBWUUQRFXhucrQqBpKdRRxNVq2zBqsx8HVqFk2uYo8kmbaLLHRdqtQpUm98uKfu3vca1LqdGhUtyoFnCNkfmXRyPXLjbKb"
|
xprv := "xprv9s21ZrQH143K2LBWUUQRFXhucrQqBpKdRRxNVq2zBqsx8HVqFk2uYo8kmbaLLHRdqtQpUm98uKfu3vca1LqdGhUtyoFnCNkfmXRyPXLjbKb"
|
||||||
|
|
||||||
// Derive the first identity (index 0) from the xprv
|
// Derive the first identity (index 0) from the xprv
|
||||||
identity, err := agehd.DeriveIdentityFromXPRV(xprv, 0)
|
identity, err := agehd.DeriveIdentityFromXPRV(xprv, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Secret key: %s\n", identity.String())
|
fmt.Printf("Secret key: %s\n", identity.String())
|
||||||
fmt.Printf("Public key: %s\n", identity.Recipient().String())
|
fmt.Printf("Public key: %s\n", identity.Recipient().String())
|
||||||
}
|
}
|
||||||
@@ -94,20 +86,20 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
mnemonic := "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
|
mnemonic := "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
|
||||||
|
|
||||||
// Derive multiple identities with different indices
|
// Derive multiple identities with different indices
|
||||||
for i := uint32(0); i < 3; i++ {
|
for i := uint32(0); i < 3; i++ {
|
||||||
identity, err := agehd.DeriveIdentity(mnemonic, i)
|
identity, err := agehd.DeriveIdentity(mnemonic, i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Identity %d: %s\n", i, identity.Recipient().String())
|
fmt.Printf("Identity %d: %s\n", i, identity.Recipient().String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -121,25 +113,25 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"sneak.berlin/go/secret/pkg/agehd"
|
"git.eeqj.de/sneak/secret/pkg/agehd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
mnemonic := "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
|
mnemonic := "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
|
||||||
|
|
||||||
// First derive entropy using BIP85
|
// First derive entropy using BIP85
|
||||||
entropy, err := agehd.DeriveEntropy(mnemonic, 0)
|
entropy, err := agehd.DeriveEntropy(mnemonic, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then create identity from entropy
|
// Then create identity from entropy
|
||||||
identity, err := agehd.IdentityFromEntropy(entropy)
|
identity, err := agehd.IdentityFromEntropy(entropy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Secret key: %s\n", identity.String())
|
fmt.Printf("Secret key: %s\n", identity.String())
|
||||||
fmt.Printf("Public key: %s\n", identity.Recipient().String())
|
fmt.Printf("Public key: %s\n", identity.Recipient().String())
|
||||||
}
|
}
|
||||||
@@ -159,8 +151,7 @@ Derives a deterministic age identity from a BIP39 mnemonic and index.
|
|||||||
|
|
||||||
#### `DeriveIdentityFromXPRV(xprv string, n uint32) (*age.X25519Identity, error)`
|
#### `DeriveIdentityFromXPRV(xprv string, n uint32) (*age.X25519Identity, error)`
|
||||||
|
|
||||||
Derives a deterministic age identity from an extended private key (xprv) and
|
Derives a deterministic age identity from an extended private key (xprv) and index.
|
||||||
index.
|
|
||||||
|
|
||||||
- `xprv`: A valid extended private key in xprv format
|
- `xprv`: A valid extended private key in xprv format
|
||||||
- `n`: The derivation index (0, 1, 2, ...)
|
- `n`: The derivation index (0, 1, 2, ...)
|
||||||
@@ -176,8 +167,7 @@ Derives 32 bytes of entropy from a BIP39 mnemonic and index using BIP85.
|
|||||||
|
|
||||||
#### `DeriveEntropyFromXPRV(xprv string, n uint32) ([]byte, error)`
|
#### `DeriveEntropyFromXPRV(xprv string, n uint32) ([]byte, error)`
|
||||||
|
|
||||||
Derives 32 bytes of entropy from an extended private key (xprv) and index using
|
Derives 32 bytes of entropy from an extended private key (xprv) and index using BIP85.
|
||||||
BIP85.
|
|
||||||
|
|
||||||
- `xprv`: A valid extended private key in xprv format
|
- `xprv`: A valid extended private key in xprv format
|
||||||
- `n`: The derivation index
|
- `n`: The derivation index
|
||||||
@@ -192,27 +182,20 @@ Converts 32 bytes of entropy into an age X25519 identity.
|
|||||||
|
|
||||||
## Implementation Details
|
## Implementation Details
|
||||||
|
|
||||||
1. **BIP85 Entropy Derivation**: The package uses the BIP85 standard to derive
|
1. **BIP85 Entropy Derivation**: The package uses the BIP85 standard to derive 64 bytes of entropy from the input source
|
||||||
64 bytes of entropy from the input source
|
2. **DRNG**: A BIP85 DRNG (Deterministic Random Number Generator) using SHAKE256 is seeded with the 64-byte entropy
|
||||||
2. **DRNG**: A BIP85 DRNG (Deterministic Random Number Generator) using SHAKE256
|
3. **Key Generation**: 32 bytes are read from the DRNG to generate the age private key
|
||||||
is seeded with the 64-byte entropy
|
4. **RFC-7748 Clamping**: The private key is clamped according to RFC-7748 for X25519
|
||||||
3. **Key Generation**: 32 bytes are read from the DRNG to generate the age
|
5. **Bech32 Encoding**: The key is encoded using Bech32 with the "age-secret-key-" prefix
|
||||||
private key
|
|
||||||
4. **RFC-7748 Clamping**: The private key is clamped according to RFC-7748 for
|
|
||||||
X25519
|
|
||||||
5. **Bech32 Encoding**: The key is encoded using Bech32 with the
|
|
||||||
"age-secret-key-" prefix
|
|
||||||
|
|
||||||
## Security Considerations
|
## Security Considerations
|
||||||
|
|
||||||
- The same mnemonic/xprv and index will always produce the same identity
|
- The same mnemonic/xprv and index will always produce the same identity
|
||||||
- Different indices produce cryptographically independent identities
|
- Different indices produce cryptographically independent identities
|
||||||
- The vendor/application scoping prevents conflicts with other BIP85
|
- The vendor/application scoping prevents conflicts with other BIP85 applications
|
||||||
applications
|
|
||||||
- The DRNG ensures high-quality randomness for key generation
|
- The DRNG ensures high-quality randomness for key generation
|
||||||
- Private keys are properly clamped for X25519 usage
|
- Private keys are properly clamped for X25519 usage
|
||||||
- Only accepts proper BIP85 sources (mnemonics and xprv keys), not arbitrary
|
- Only accepts proper BIP85 sources (mnemonics and xprv keys), not arbitrary passphrases
|
||||||
passphrases
|
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
@@ -220,4 +203,4 @@ Run the tests with:
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
go test -v ./internal/agehd
|
go test -v ./internal/agehd
|
||||||
```
|
```
|
||||||
+1
-1
@@ -14,11 +14,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/bip85"
|
||||||
"github.com/btcsuite/btcd/btcutil/hdkeychain"
|
"github.com/btcsuite/btcd/btcutil/hdkeychain"
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
"github.com/btcsuite/btcd/chaincfg"
|
||||||
"github.com/btcsuite/btcutil/bech32"
|
"github.com/btcsuite/btcutil/bech32"
|
||||||
"github.com/tyler-smith/go-bip39"
|
"github.com/tyler-smith/go-bip39"
|
||||||
"sneak.berlin/go/secret/pkg/bip85"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
+18
-5
@@ -38,6 +38,7 @@ const (
|
|||||||
testMessageLargePattern = "A"
|
testMessageLargePattern = "A"
|
||||||
|
|
||||||
// Error messages for validation
|
// Error messages for validation
|
||||||
|
errorMsgNeed32Bytes = "need 32-byte scalar, got"
|
||||||
errorMsgInvalidXPRV = "invalid-xprv"
|
errorMsgInvalidXPRV = "invalid-xprv"
|
||||||
|
|
||||||
// Test constants for various scenarios
|
// Test constants for various scenarios
|
||||||
@@ -329,17 +330,24 @@ func TestClampFunction(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// requireIdentityError asserts that identity derivation failed with
|
// requireIdentityError asserts that identity derivation failed with an
|
||||||
// errInvalidScalarSize and returned no identity.
|
// error containing errorMsg and returned no identity.
|
||||||
func requireIdentityError(
|
func requireIdentityError(
|
||||||
t *testing.T,
|
t *testing.T,
|
||||||
identity *age.X25519Identity,
|
identity *age.X25519Identity,
|
||||||
err error,
|
err error,
|
||||||
|
errorMsg string,
|
||||||
) {
|
) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
if !errors.Is(err, errInvalidScalarSize) {
|
if err == nil {
|
||||||
t.Errorf("expected errInvalidScalarSize, got %v", err)
|
t.Errorf("expected error but got none")
|
||||||
|
} else if !strings.Contains(err.Error(), errorMsg) {
|
||||||
|
t.Errorf(
|
||||||
|
"expected error containing %q, got %q",
|
||||||
|
errorMsg,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if identity != nil {
|
if identity != nil {
|
||||||
@@ -355,26 +363,31 @@ func TestIdentityFromEntropyEdgeCases(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
entropy []byte
|
entropy []byte
|
||||||
expectError bool
|
expectError bool
|
||||||
|
errorMsg string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "nil entropy",
|
name: "nil entropy",
|
||||||
entropy: nil,
|
entropy: nil,
|
||||||
expectError: true,
|
expectError: true,
|
||||||
|
errorMsg: errorMsgNeed32Bytes + " 0",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty entropy",
|
name: "empty entropy",
|
||||||
entropy: []byte{},
|
entropy: []byte{},
|
||||||
expectError: true,
|
expectError: true,
|
||||||
|
errorMsg: errorMsgNeed32Bytes + " 0",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "too short entropy",
|
name: "too short entropy",
|
||||||
entropy: make([]byte, 31),
|
entropy: make([]byte, 31),
|
||||||
expectError: true,
|
expectError: true,
|
||||||
|
errorMsg: errorMsgNeed32Bytes + " 31",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "too long entropy",
|
name: "too long entropy",
|
||||||
entropy: make([]byte, 33),
|
entropy: make([]byte, 33),
|
||||||
expectError: true,
|
expectError: true,
|
||||||
|
errorMsg: errorMsgNeed32Bytes + " 33",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "valid 32-byte entropy",
|
name: "valid 32-byte entropy",
|
||||||
@@ -406,7 +419,7 @@ func TestIdentityFromEntropyEdgeCases(t *testing.T) {
|
|||||||
identity, err := IdentityFromEntropy(tt.entropy)
|
identity, err := IdentityFromEntropy(tt.entropy)
|
||||||
|
|
||||||
if tt.expectError {
|
if tt.expectError {
|
||||||
requireIdentityError(t, identity, err)
|
requireIdentityError(t, identity, err, tt.errorMsg)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-19
@@ -1,15 +1,10 @@
|
|||||||
# BIP85 - Deterministic Entropy From BIP32 Keychains
|
# BIP85 - Deterministic Entropy From BIP32 Keychains
|
||||||
|
|
||||||
This package implements
|
This package implements [BIP85](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki), which allows for deterministic derivation of entropy from a BIP32 master key. This enables a single seed to generate multiple wallet keys, mnemonics, and random values in a fully deterministic way.
|
||||||
[BIP85](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki), which
|
|
||||||
allows for deterministic derivation of entropy from a BIP32 master key. This
|
|
||||||
enables a single seed to generate multiple wallet keys, mnemonics, and random
|
|
||||||
values in a fully deterministic way.
|
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
BIP85 enables a variety of use cases:
|
BIP85 enables a variety of use cases:
|
||||||
|
|
||||||
- Generate multiple BIP39 mnemonic seeds from a single master key
|
- Generate multiple BIP39 mnemonic seeds from a single master key
|
||||||
- Derive Bitcoin HD wallet seeds (WIF format)
|
- Derive Bitcoin HD wallet seeds (WIF format)
|
||||||
- Create extended private keys (XPRV)
|
- Create extended private keys (XPRV)
|
||||||
@@ -22,8 +17,8 @@ BIP85 enables a variety of use cases:
|
|||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/bip85"
|
||||||
"github.com/btcsuite/btcd/btcutil/hdkeychain"
|
"github.com/btcsuite/btcd/btcutil/hdkeychain"
|
||||||
"sneak.berlin/go/secret/pkg/bip85"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parse an existing master key
|
// Parse an existing master key
|
||||||
@@ -119,16 +114,15 @@ m/83696968'/{app}'/{parameters}
|
|||||||
```
|
```
|
||||||
|
|
||||||
Where:
|
Where:
|
||||||
|
|
||||||
- `83696968'` is the BIP85 root path (BIP in ASCII)
|
- `83696968'` is the BIP85 root path (BIP in ASCII)
|
||||||
- `{app}'` is the application number:
|
- `{app}'` is the application number:
|
||||||
- `39'` for BIP39 mnemonics
|
- `39'` for BIP39 mnemonics
|
||||||
- `2'` for HD-WIF keys
|
- `2'` for HD-WIF keys
|
||||||
- `32'` for XPRV
|
- `32'` for XPRV
|
||||||
- `128169'` for HEX data
|
- `128169'` for HEX data
|
||||||
- `707764'` for Base64 passwords
|
- `707764'` for Base64 passwords
|
||||||
- `707785'` for Base85 passwords
|
- `707785'` for Base85 passwords
|
||||||
- `828365'` for RSA keys
|
- `828365'` for RSA keys
|
||||||
- `{parameters}` are application-specific parameters
|
- `{parameters}` are application-specific parameters
|
||||||
|
|
||||||
## Test Vectors
|
## Test Vectors
|
||||||
@@ -141,13 +135,12 @@ This implementation passes all the test vectors from the BIP85 specification:
|
|||||||
- XPRV
|
- XPRV
|
||||||
- SHAKE256 DRNG output
|
- SHAKE256 DRNG output
|
||||||
|
|
||||||
The implementation is also compatible with the Python reference implementation's
|
The implementation is also compatible with the Python reference implementation's test vectors for the DRNG functionality.
|
||||||
test vectors for the DRNG functionality.
|
|
||||||
|
|
||||||
Run the tests with verbose output to see the test vectors and results:
|
Run the tests with verbose output to see the test vectors and results:
|
||||||
|
|
||||||
```
|
```
|
||||||
go test -v sneak.berlin/go/secret/pkg/bip85
|
go test -v git.eeqj.de/sneak/secret/pkg/bip85
|
||||||
```
|
```
|
||||||
|
|
||||||
## References
|
## References
|
||||||
@@ -156,4 +149,4 @@ go test -v sneak.berlin/go/secret/pkg/bip85
|
|||||||
- [Python Reference Implementation](https://github.com/ethankosakovsky/bip85)
|
- [Python Reference Implementation](https://github.com/ethankosakovsky/bip85)
|
||||||
- [Bitcoin Core](https://github.com/bitcoin/bitcoin)
|
- [Bitcoin Core](https://github.com/bitcoin/bitcoin)
|
||||||
- [BIP32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)
|
- [BIP32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)
|
||||||
- [BIP39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki)
|
- [BIP39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki)
|
||||||
+26
-4
@@ -59,6 +59,16 @@ var (
|
|||||||
// ErrInvalidBase85PwdLen is returned when the Base85 password length
|
// ErrInvalidBase85PwdLen is returned when the Base85 password length
|
||||||
// is out of range.
|
// is out of range.
|
||||||
ErrInvalidBase85PwdLen = errors.New("pwdLen must be between 10 and 80")
|
ErrInvalidBase85PwdLen = errors.New("pwdLen must be between 10 and 80")
|
||||||
|
// ErrPasswordTooShort is returned when the derived material is
|
||||||
|
// shorter than the requested password length. It carries only the
|
||||||
|
// middle of the message, which the caller composes as
|
||||||
|
// "derived password length <n> is shorter than requested length <m>",
|
||||||
|
// so the emitted text is unchanged.
|
||||||
|
ErrPasswordTooShort = errors.New("is shorter than requested length")
|
||||||
|
// ErrEncodedTooShort is returned when the encoded material is shorter
|
||||||
|
// than the requested password length. Composed as
|
||||||
|
// "encoded length <n> is less than requested length <m>".
|
||||||
|
ErrEncodedTooShort = errors.New("is less than requested length")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Version bytes for extended keys
|
// Version bytes for extended keys
|
||||||
@@ -371,8 +381,14 @@ func DeriveBase64Password(
|
|||||||
// Remove any padding
|
// Remove any padding
|
||||||
encodedStr = strings.TrimRight(encodedStr, "=")
|
encodedStr = strings.TrimRight(encodedStr, "=")
|
||||||
|
|
||||||
// Slice to the desired password length: 64 bytes of entropy leave 86
|
// Slice to the desired password length
|
||||||
// characters, the most pwdLen allows
|
if len(encodedStr) < int(pwdLen) {
|
||||||
|
return "", fmt.Errorf(
|
||||||
|
"derived password length %d %w %d",
|
||||||
|
len(encodedStr), ErrPasswordTooShort, pwdLen,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return encodedStr[:pwdLen], nil
|
return encodedStr[:pwdLen], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -395,8 +411,14 @@ func DeriveBase85Password(
|
|||||||
// Base85 encode all 64 bytes of entropy using the RFC1924 character set
|
// Base85 encode all 64 bytes of entropy using the RFC1924 character set
|
||||||
encoded := encodeBase85WithRFC1924Charset(entropy)
|
encoded := encodeBase85WithRFC1924Charset(entropy)
|
||||||
|
|
||||||
// Slice to the desired password length: 64 bytes of entropy give 80
|
// Slice to the desired password length
|
||||||
// characters, the most pwdLen allows
|
if len(encoded) < int(pwdLen) {
|
||||||
|
return "", fmt.Errorf(
|
||||||
|
"encoded length %d %w %d",
|
||||||
|
len(encoded), ErrEncodedTooShort, pwdLen,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return encoded[:pwdLen], nil
|
return encoded[:pwdLen], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
-36
@@ -4,14 +4,13 @@ package bip85_test
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.eeqj.de/sneak/secret/pkg/bip85"
|
||||||
"github.com/btcsuite/btcd/btcutil/hdkeychain"
|
"github.com/btcsuite/btcd/btcutil/hdkeychain"
|
||||||
"github.com/tyler-smith/go-bip39"
|
"github.com/tyler-smith/go-bip39"
|
||||||
"sneak.berlin/go/secret/pkg/bip85"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -1014,13 +1013,14 @@ func TestHexDerivation(t *testing.T) {
|
|||||||
func TestInvalidParameters(t *testing.T) {
|
func TestInvalidParameters(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
logTestVector(t, "Invalid Parameters")
|
||||||
|
|
||||||
masterKey := mustParseTestMasterKey(t)
|
masterKey := mustParseTestMasterKey(t)
|
||||||
|
|
||||||
// Test cases for parameter validation
|
// Test cases for parameter validation
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
testFunc func() error
|
testFunc func() error
|
||||||
want error
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "BIP39 invalid word count",
|
name: "BIP39 invalid word count",
|
||||||
@@ -1030,7 +1030,6 @@ func TestInvalidParameters(t *testing.T) {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
want: bip85.ErrInvalidWordCount,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Base64 password too short",
|
name: "Base64 password too short",
|
||||||
@@ -1040,7 +1039,6 @@ func TestInvalidParameters(t *testing.T) {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
want: bip85.ErrInvalidBase64PwdLen,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Base64 password too long",
|
name: "Base64 password too long",
|
||||||
@@ -1050,7 +1048,6 @@ func TestInvalidParameters(t *testing.T) {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
want: bip85.ErrInvalidBase64PwdLen,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Base85 password too short",
|
name: "Base85 password too short",
|
||||||
@@ -1060,7 +1057,6 @@ func TestInvalidParameters(t *testing.T) {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
want: bip85.ErrInvalidBase85PwdLen,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Base85 password too long",
|
name: "Base85 password too long",
|
||||||
@@ -1070,7 +1066,6 @@ func TestInvalidParameters(t *testing.T) {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
want: bip85.ErrInvalidBase85PwdLen,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Hex data too small",
|
name: "Hex data too small",
|
||||||
@@ -1080,7 +1075,6 @@ func TestInvalidParameters(t *testing.T) {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
want: bip85.ErrInvalidNumBytes,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Hex data too large",
|
name: "Hex data too large",
|
||||||
@@ -1090,43 +1084,23 @@ func TestInvalidParameters(t *testing.T) {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
want: bip85.ErrInvalidNumBytes,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run all validation test cases
|
// Run all validation test cases
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
t.Logf("Testing: %s", tc.name)
|
||||||
|
|
||||||
err := tc.testFunc()
|
err := tc.testFunc()
|
||||||
if !errors.Is(err, tc.want) {
|
if err == nil {
|
||||||
t.Errorf("Expected %v for %s, got %v", tc.want, tc.name, err)
|
t.Errorf("Expected error for %s, but got nil", tc.name)
|
||||||
|
} else {
|
||||||
|
t.Logf("Got expected error: %v", err)
|
||||||
|
t.Logf("RESULT: PASS")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestDeriveBIP85EntropyErrors checks that DeriveBIP85Entropy returns
|
|
||||||
// ErrNotPrivateKey for a public master key, and ErrInvalidPathComponent,
|
|
||||||
// wrapped, for a path component that is not a number.
|
|
||||||
func TestDeriveBIP85EntropyErrors(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
masterKey := mustParseTestMasterKey(t)
|
|
||||||
|
|
||||||
publicKey, err := masterKey.Neuter()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to get the public key of the master key: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = bip85.DeriveBIP85Entropy(publicKey, testCase1Path)
|
|
||||||
if !errors.Is(err, bip85.ErrNotPrivateKey) {
|
|
||||||
t.Errorf("Expected ErrNotPrivateKey, got %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = bip85.DeriveBIP85Entropy(masterKey, bip85.BIP85_MASTER_PATH+"/x'")
|
|
||||||
if !errors.Is(err, bip85.ErrInvalidPathComponent) {
|
|
||||||
t.Errorf("Expected ErrInvalidPathComponent, got %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestAdditionalDeriveHex tests additional hex derivation scenarios
|
// TestAdditionalDeriveHex tests additional hex derivation scenarios
|
||||||
func TestAdditionalDeriveHex(t *testing.T) {
|
func TestAdditionalDeriveHex(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|||||||
+3
-4
@@ -131,10 +131,9 @@ main() {
|
|||||||
if missing make; then pkg_install gnumake make make make; fi
|
if missing make; then pkg_install gnumake make make make; fi
|
||||||
|
|
||||||
# ---- JS / docs repos ----
|
# ---- JS / docs repos ----
|
||||||
# prettier, pinned in package.json and yarn.lock, formats the markdown
|
# ensure_node
|
||||||
ensure_node
|
# ensure_yarn
|
||||||
ensure_yarn
|
# install_js_deps
|
||||||
install_js_deps
|
|
||||||
|
|
||||||
# ---- Go repos ----
|
# ---- Go repos ----
|
||||||
if missing go; then pkg_install go golang go go; fi
|
if missing go; then pkg_install go golang go go; fi
|
||||||
|
|||||||
+1
-1
@@ -17,7 +17,7 @@ main() {
|
|||||||
echo dev)"
|
echo dev)"
|
||||||
fi
|
fi
|
||||||
commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
|
commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
|
||||||
pkg=sneak.berlin/go/secret/internal/cli
|
pkg=git.eeqj.de/sneak/secret/internal/cli
|
||||||
# Build the file, not the package `./cmd/secret`: a package build
|
# Build the file, not the package `./cmd/secret`: a package build
|
||||||
# also stamps git status into the binary and fails where git cannot
|
# also stamps git status into the binary and fails where git cannot
|
||||||
# read the checkout, instead of falling back to `dev`/`unknown`.
|
# read the checkout, instead of falling back to `dev`/`unknown`.
|
||||||
|
|||||||
+1
-22
@@ -1,33 +1,12 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# script/fmt: format all files (writes): Go with go fmt, markdown with
|
# script/fmt: format all files (writes).
|
||||||
# prettier.
|
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
||||||
|
|
||||||
# Must match the pin in script/bootstrap.
|
|
||||||
NODE_VERSION="22.17.0"
|
|
||||||
|
|
||||||
# script/bootstrap installs node and yarn under nvm and leaves neither
|
|
||||||
# on the PATH of the shell that called it, so resolve the pinned
|
|
||||||
# toolchain here the way bootstrap's own install step does. nvm is a
|
|
||||||
# bash script, hence the subshell.
|
|
||||||
run_yarn() {
|
|
||||||
if command -v yarn >/dev/null 2>&1; then
|
|
||||||
exec yarn "$@"
|
|
||||||
fi
|
|
||||||
if [ ! -s "$HOME/.nvm/nvm.sh" ]; then
|
|
||||||
echo "fmt: no yarn; run script/bootstrap first" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
exec bash -c '. "$HOME/.nvm/nvm.sh" && nvm use "$1" >/dev/null &&
|
|
||||||
shift && exec yarn "$@"' bash "$NODE_VERSION" "$@"
|
|
||||||
}
|
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
cd "$ROOT"
|
cd "$ROOT"
|
||||||
go fmt ./...
|
go fmt ./...
|
||||||
run_yarn run prettier --write '**/*.md' --tab-width 4 --prose-wrap always
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main "$@"
|
main "$@"
|
||||||
|
|||||||
@@ -5,25 +5,6 @@ set -eu
|
|||||||
|
|
||||||
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
||||||
|
|
||||||
# Must match the pin in script/bootstrap.
|
|
||||||
NODE_VERSION="22.17.0"
|
|
||||||
|
|
||||||
# script/bootstrap installs node and yarn under nvm and leaves neither
|
|
||||||
# on the PATH of the shell that called it, so resolve the pinned
|
|
||||||
# toolchain here the way bootstrap's own install step does. nvm is a
|
|
||||||
# bash script, hence the subshell.
|
|
||||||
run_yarn() {
|
|
||||||
if command -v yarn >/dev/null 2>&1; then
|
|
||||||
exec yarn "$@"
|
|
||||||
fi
|
|
||||||
if [ ! -s "$HOME/.nvm/nvm.sh" ]; then
|
|
||||||
echo "fmt-check: no yarn; run script/bootstrap first" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
exec bash -c '. "$HOME/.nvm/nvm.sh" && nvm use "$1" >/dev/null &&
|
|
||||||
shift && exec yarn "$@"' bash "$NODE_VERSION" "$@"
|
|
||||||
}
|
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
cd "$ROOT"
|
cd "$ROOT"
|
||||||
if [ -n "$(gofmt -l .)" ]; then
|
if [ -n "$(gofmt -l .)" ]; then
|
||||||
@@ -31,7 +12,6 @@ main() {
|
|||||||
gofmt -l .
|
gofmt -l .
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
run_yarn run prettier --check '**/*.md' --tab-width 4 --prose-wrap always
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main "$@"
|
main "$@"
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
|
||||||
# yarn lockfile v1
|
|
||||||
|
|
||||||
|
|
||||||
prettier@3.8.1:
|
|
||||||
version "3.8.1"
|
|
||||||
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.8.1.tgz#edf48977cf991558f4fcbd8a3ba6015ba2a3a173"
|
|
||||||
integrity sha512-UOnG6LftzbdaHZcKoPFtOcCKztrQ57WkHDeRD9t/PTQtmT0NHSeWWepj6pS0z/N7+08BHFDQVUrfmfMRcZwbMg==
|
|
||||||
Reference in New Issue
Block a user