Ask for the mnemonic and passphrase before taking the state directory lock (closes #152)
check / check (push) Waiting to run

init, vault create, unlocker add, and add, import and generate secret
with --force asked for the mnemonic or passphrase, or unlocked the
vault, while holding the state directory lock, so every other command
that changes anything waited at the prompt. They now ask and unlock
first and lock after. What they checked before asking is checked again
under the lock: unlocker add fails if the vault was removed meanwhile
and refuses a GPG key added meanwhile, and a forced add fails if another
command created the secret meanwhile, rather than ask under the lock.
secret.CreatePGPUnlocker now takes the vault, already unlocked.

Not done: secret move between vaults still unlocks both under the lock;
the issue does not name it.

Model: opus-5-5
This commit is contained in:
2026-10-15 05:32:58 +00:00
parent 9c4968c768
commit eadaad2040
12 changed files with 492 additions and 138 deletions
+59 -18
View File
@@ -47,6 +47,10 @@ var (
errCrossVaultSourceUnqualified = errors.New(
"source must specify vault (e.g., vault:secret) for cross-vault move")
errMoveOntoItself = errors.New("cannot be moved onto itself")
// errAddedMeanwhile carries only the message tail; storeSecret composes
// "secret <name> was added by another command ...".
errAddedMeanwhile = errors.New("was added by another command in the " +
"meantime, so nothing was changed; run the command again to replace it")
)
// ParseVaultSecretRef parses a "vault:secret" or just "secret" reference
@@ -422,26 +426,70 @@ func (cli *Instance) AddSecret(secretName string, force bool) error {
// Locked only now that stdin has been read: in `secret encrypt key |
// secret add name`, holding the lock while reading would leave each
// command waiting for the other.
return cli.storeSecret(vlt, secretName, valueBuffer, force)
}
// storeSecret adds the secret name with value to vlt, as vault.AddSecret
// does, holding the state directory lock while it does. With force, adding
// an existing secret replaces its current version, which needs the vault's
// long-term key. Getting the key can ask for the passphrase, so storeSecret
// gets it before it takes the lock, and no other command waits while the
// user answers. If, once the lock is held, the add replaces a version after
// all, because another command added the secret in the meantime, it fails
// rather than ask with the lock held.
func (cli *Instance) storeSecret(
vlt *vault.Vault, name string, value *memguard.LockedBuffer, force bool,
) error {
err := vault.ValidateSecretName(name)
if err != nil {
return err
}
replacing, err := replacesVersion(vlt, name, force)
if err != nil {
return err
}
if replacing {
_, err = vlt.GetOrDeriveLongTermKey()
if err != nil {
return fmt.Errorf("failed to get long-term key: %w", err)
}
}
release, err := vault.LockStateDir(cli.fs, cli.stateDir)
if err != nil {
return err
}
defer release()
// Add the secret to the vault
secret.Debug("Calling vault.AddSecret", "secret_name", secretName,
"value_length", valueBuffer.Size(), "force", force)
if !replacing {
replacing, err = replacesVersion(vlt, name, force)
if err != nil {
return err
}
err = vlt.AddSecret(secretName, valueBuffer, force)
if err != nil {
secret.Debug("vault.AddSecret failed", "error", err)
return err
if replacing {
return fmt.Errorf("secret %s %w", name, errAddedMeanwhile)
}
}
secret.Debug("vault.AddSecret completed successfully")
return vlt.AddSecret(name, value, force)
}
return nil
// replacesVersion reports whether adding the secret name to vlt replaces
// the secret's current version: whether force is set and the secret has one.
func replacesVersion(vlt *vault.Vault, name string, force bool) (bool, error) {
if !force {
return false, nil
}
exists, err := secret.NewSecret(vlt, name).Exists()
if err != nil {
return false, fmt.Errorf("failed to check if secret exists: %w", err)
}
return exists, nil
}
// GetSecret retrieves and prints the current version of a secret
@@ -707,14 +755,7 @@ func (cli *Instance) ImportSecret(
// Locked only now that the file has been read, as in AddSecret: the
// file may be a pipe written by another secret command.
release, err := vault.LockStateDir(cli.fs, cli.stateDir)
if err != nil {
return err
}
defer release()
// Store the secret in the vault
err = vlt.AddSecret(secretName, valueBuffer, force)
err = cli.storeSecret(vlt, secretName, valueBuffer, force)
if err != nil {
return err
}