init --no-phrase: an identity without a recovery phrase
For bootstrap nodes and other unattended setups: nothing is shown or logged, and the data directory is marked so later messages do not point to 'allmende recover', which cannot work. The identity lasts the certificate lifetime (a year) and then has to be replaced; the node says so. The Kubernetes StatefulSet now initialises itself on first start with it, so no manual exec step is left. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
+20
-6
@@ -21,6 +21,8 @@ import (
|
||||
func cmdInit(args []string) error {
|
||||
fs := flag.NewFlagSet("init", flag.ExitOnError)
|
||||
data := dataFlag(fs)
|
||||
noPhrase := fs.Bool("no-phrase", false,
|
||||
"create the identity without a recovery phrase (nothing is shown, nothing can be recovered); for bootstrap nodes and other unattended setups")
|
||||
fs.Parse(args)
|
||||
|
||||
if _, err := os.Stat(filepath.Join(*data, "device.key")); err == nil {
|
||||
@@ -31,13 +33,15 @@ func cmdInit(args []string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
words := strings.Fields(phrase)
|
||||
showPhrase(words)
|
||||
if !*noPhrase {
|
||||
words := strings.Fields(phrase)
|
||||
showPhrase(words)
|
||||
|
||||
// Only ask for confirmation when a person is at the keyboard.
|
||||
if term.IsTerminal(int(os.Stdin.Fd())) {
|
||||
if err := confirmPhrase(bufio.NewReader(os.Stdin), words); err != nil {
|
||||
return err
|
||||
// Only ask for confirmation when a person is at the keyboard.
|
||||
if term.IsTerminal(int(os.Stdin.Fd())) {
|
||||
if err := confirmPhrase(bufio.NewReader(os.Stdin), words); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,9 +60,19 @@ func cmdInit(args []string) error {
|
||||
if err := writeToken(*data, token); err != nil {
|
||||
return err
|
||||
}
|
||||
if *noPhrase {
|
||||
// Marks the data directory, so later messages do not send the owner to
|
||||
// 'allmende recover', which cannot work without a phrase.
|
||||
if err := os.WriteFile(filepath.Join(*data, noRecoveryFile), []byte("created with init --no-phrase\n"), 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("\nDone. This machine is set up as a device of identity %s…\n", dev.Cert.Author[:12])
|
||||
fmt.Printf("Your device certificate is valid until %s.\n\n", time.Unix(dev.Cert.ExpiresAt, 0).Format("2006-01-02"))
|
||||
if *noPhrase {
|
||||
fmt.Print("No recovery phrase was made. This identity cannot be recovered or renewed: when\nthe certificate expires, start again with a new 'allmende init' (a new identity).\nKeep a backup of the data directory instead.\n\n")
|
||||
}
|
||||
fmt.Printf("Next: allmende run --data %s\nThen open the sign-in link it prints and choose your region (or add --region <region> to start with one).\n", *data)
|
||||
fmt.Printf("The admin token for publishing is in %s\n", filepath.Join(*data, tokenFile))
|
||||
return nil
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
||||
)
|
||||
|
||||
// captureStdout runs f and returns what it printed.
|
||||
func captureStdout(t *testing.T, f func()) string {
|
||||
t.Helper()
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := os.Stdout
|
||||
os.Stdout = w
|
||||
f()
|
||||
os.Stdout = old
|
||||
w.Close()
|
||||
b, _ := io.ReadAll(r)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func TestInitWithoutPhraseShowsNoneAndMarksTheDirectory(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
var err error
|
||||
out := captureStdout(t, func() { err = cmdInit([]string{"--data", dir, "--no-phrase"}) })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(out, "recovery phrase\n---") || strings.Contains(out, " 1. ") {
|
||||
t.Fatalf("no phrase may be shown:\n%s", out)
|
||||
}
|
||||
if !strings.Contains(out, "cannot be recovered") {
|
||||
t.Errorf("the owner must be told it cannot be recovered:\n%s", out)
|
||||
}
|
||||
for _, f := range []string{"device.key", tokenFile, noRecoveryFile} {
|
||||
if _, err := os.Stat(filepath.Join(dir, f)); err != nil {
|
||||
t.Errorf("%s was not written: %v", f, err)
|
||||
}
|
||||
}
|
||||
if _, err := identity.LoadDevice(dir); err != nil {
|
||||
t.Errorf("the node could not load its identity: %v", err)
|
||||
}
|
||||
// A second init must not replace the identity.
|
||||
if err := cmdInit([]string{"--data", dir, "--no-phrase"}); err == nil {
|
||||
t.Error("init over an existing identity must fail")
|
||||
}
|
||||
}
|
||||
@@ -22,12 +22,14 @@ const (
|
||||
dbFile = "allmende.db"
|
||||
tokenFile = "admin.token"
|
||||
peersFile = "peers.json" // peers met on earlier runs
|
||||
// noRecoveryFile exists when the identity was made with 'init --no-phrase'.
|
||||
noRecoveryFile = "no-recovery"
|
||||
)
|
||||
|
||||
const usage = `Allmende: a free, distributed marketplace for homes.
|
||||
|
||||
Usage:
|
||||
allmende init [--data DIR] create your identity (first run)
|
||||
allmende init [--data DIR] [--no-phrase] create your identity (first run)
|
||||
allmende run [--data DIR] --region u1x run the node
|
||||
allmende recover [--data DIR] restore access using your recovery phrase
|
||||
|
||||
|
||||
+6
-2
@@ -97,12 +97,16 @@ func cmdRun(args []string) error {
|
||||
return err
|
||||
}
|
||||
until := time.Until(time.Unix(dev.Cert.ExpiresAt, 0))
|
||||
renew := "run 'allmende recover' to renew it"
|
||||
if _, err := os.Stat(filepath.Join(*data, noRecoveryFile)); err == nil {
|
||||
renew = "this identity has no recovery phrase, so it cannot be renewed: start over with 'allmende init --no-phrase' on an empty data directory (a new identity, so update any DNS records that name this node)"
|
||||
}
|
||||
if until <= 0 {
|
||||
return errors.New("this device's certificate has expired; run 'allmende recover' to renew it")
|
||||
return errors.New("this device's certificate has expired; " + renew)
|
||||
}
|
||||
log := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
if until < certWarnBefore {
|
||||
log.Warn("device certificate expires soon; run 'allmende recover' to renew it",
|
||||
log.Warn("device certificate expires soon; "+renew,
|
||||
"expires", time.Unix(dev.Cert.ExpiresAt, 0).Format("2006-01-02"))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user