Refuse an existing vault before asking for the mnemonic (closes #151)
check / check (push) Waiting to run
check / check (push) Waiting to run
`secret init` and `secret vault create` asked for the mnemonic and the unlocker passphrase, and `secret init` printed "Initialized secrets manager at", before refusing a vault that already exists. The check `vault.CreateVault` made before writing is now `vault.CheckNewVaultName`, which both commands call while holding the state directory lock, before the first prompt; `CreateVault` still calls it. `secret init` prints the "Initialized" line only once the vault is created, so `TestMnemonicNotReadNamesOnlyMnemonic` no longer expects it. Judgement call: the same check validates the name, so `secret vault create` with an invalid name also fails before the prompts. Model: opus-5-5
This commit is contained in:
@@ -17,6 +17,9 @@ import (
|
||||
// errPassphraseMismatch is returned when passphrase confirmation fails
|
||||
var errPassphraseMismatch = errors.New("passphrases do not match")
|
||||
|
||||
// defaultVaultName is the name of the vault `secret init` creates
|
||||
const defaultVaultName = "default"
|
||||
|
||||
// NewInitCmd creates the init command
|
||||
func NewInitCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
@@ -92,8 +95,10 @@ func (cli *Instance) initialize(cmd *cobra.Command) error {
|
||||
return fmt.Errorf("failed to create state directory: %w", err)
|
||||
}
|
||||
|
||||
if cmd != nil {
|
||||
cmd.Printf("Initialized secrets manager at: %s\n", stateDir)
|
||||
// Refuse an existing default vault before asking for anything
|
||||
err = vault.CheckNewVaultName(cli.fs, cli.stateDir, defaultVaultName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create default vault: %w", err)
|
||||
}
|
||||
|
||||
// Prompt for mnemonic
|
||||
@@ -133,7 +138,7 @@ func (cli *Instance) initialize(cmd *cobra.Command) error {
|
||||
// Create the default vault with its passphrase unlocker
|
||||
secret.Debug("Creating default vault")
|
||||
|
||||
vlt, err := vault.CreateVault(cli.fs, cli.stateDir, "default",
|
||||
vlt, err := vault.CreateVault(cli.fs, cli.stateDir, defaultVaultName,
|
||||
mnemonic, passphraseBuffer)
|
||||
if err != nil {
|
||||
secret.Debug("Failed to create default vault", "error", err)
|
||||
@@ -152,6 +157,7 @@ func (cli *Instance) initialize(cmd *cobra.Command) error {
|
||||
}
|
||||
|
||||
if cmd != nil {
|
||||
cmd.Printf("Initialized secrets manager at: %s\n", stateDir)
|
||||
cmd.Printf("\nDefault vault created and configured\n")
|
||||
cmd.Printf("Long-term public key: %s\n", ltIdentity.Recipient().String())
|
||||
cmd.Printf("Unlocker ID: %s\n", unlocker.GetID())
|
||||
|
||||
Reference in New Issue
Block a user