diff --git a/cmd/allmende/init.go b/cmd/allmende/init.go index c4d0656..ffcd763 100644 --- a/cmd/allmende/init.go +++ b/cmd/allmende/init.go @@ -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 to start with one).\n", *data) fmt.Printf("The admin token for publishing is in %s\n", filepath.Join(*data, tokenFile)) return nil diff --git a/cmd/allmende/init_test.go b/cmd/allmende/init_test.go new file mode 100644 index 0000000..c9c7204 --- /dev/null +++ b/cmd/allmende/init_test.go @@ -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") + } +} diff --git a/cmd/allmende/main.go b/cmd/allmende/main.go index f931b15..1328398 100644 --- a/cmd/allmende/main.go +++ b/cmd/allmende/main.go @@ -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 diff --git a/cmd/allmende/run.go b/cmd/allmende/run.go index defe723..90081b0 100644 --- a/cmd/allmende/run.go +++ b/cmd/allmende/run.go @@ -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")) } diff --git a/k8s/README.md b/k8s/README.md index accbd9d..93e3ac1 100644 --- a/k8s/README.md +++ b/k8s/README.md @@ -43,17 +43,11 @@ so: kubectl apply -k k8s/base # with a LoadBalancer (MetalLB, kube-vip, ...) kubectl apply -k k8s/overlays/nodeport # or: NodePort 30401, no load balancer ``` - The pod starts and waits: it has no identity yet. -4. **Create the identity, once.** This shows the 12-word recovery phrase; it is - done by hand so the phrase never lands in a log. - ``` - kubectl -n allmende exec -it allmende-0 -- allmende init - ``` - Write the words down. The node starts by itself a few seconds later. Keys - and the database are on the volume (`data-allmende-0`): keep it, and back - it up if you can; the phrase can recover the identity (`allmende recover`) - but not the listings of other people. -5. **Publish the DNS records.** The node prints them at start: + On its first start the pod creates its identity by itself + (`allmende init --no-phrase`): no recovery phrase is made or logged. The keys + and the database live on the volume (`data-allmende-0`). **Keep that volume** + (and back it up if you can): the peer ID in your DNS records is stored there. +4. **Publish the DNS records.** The node prints them at start: ``` kubectl -n allmende logs allmende-0 | grep dnsaddr= ``` @@ -62,7 +56,7 @@ so: ``` dig +short TXT _dnsaddr.allmende.stinnesbeck.com ``` -6. **Try it from outside your network** (a phone on mobile data, or a VPS): +5. **Try it from outside your network** (a phone on mobile data, or a VPS): ``` allmende init && allmende run # no --bootstrap needed ``` @@ -70,6 +64,15 @@ so: ## Good to know +- **The identity lasts a year, and cannot be renewed.** A certificate is valid + for at most 366 days, and without a recovery phrase nothing can sign a new + one. The node warns in its log 30 days before and refuses to start after. + Then: delete the volume (or its contents), let the pod start again (a new + identity, so a new peer ID), and replace the `dnsaddr=` records with the new + ones from the log. Do it a few days early and keep the old records until the + new node is up. If you would rather have a renewable identity, create it once + by hand with a phrase (`allmende init`, without `--no-phrase`) on the volume + before the first start. - **One replica only.** A second pod would be a second node with its own identity, not a copy. - **No sign-in link in the log.** `allmende run` normally prints the owner diff --git a/k8s/base/statefulset.yaml b/k8s/base/statefulset.yaml index cd43bd8..980d35a 100644 --- a/k8s/base/statefulset.yaml +++ b/k8s/base/statefulset.yaml @@ -1,9 +1,8 @@ # One node, with its keys and database on a volume. Keep it at one replica: a # second pod would be a second node with its own identity, not a copy. # -# The identity is created by hand, once, because it shows a recovery phrase -# that must never end up in a log. Until you do, the pod waits: -# kubectl -n allmende exec -it allmende-0 -- allmende init +# The identity is created on the first start, without a recovery phrase (so +# nothing secret is logged). It lasts a year; see k8s/README.md. apiVersion: apps/v1 kind: StatefulSet metadata: @@ -31,16 +30,15 @@ spec: - name: allmende image: allmende imagePullPolicy: IfNotPresent - # Wait for 'allmende init' (it writes admin.token last), then run as a - # bootstrap node: --public serves the DHT and relays for nodes behind - # a NAT, --announce names the address the router forwards. + # First start: create the identity without a recovery phrase (a + # bootstrap node has nothing to recover; its identity is its peer ID, + # which lives on the volume). Then run as a bootstrap node: --public + # serves the DHT and relays for nodes behind a NAT, --announce names + # the address the router forwards. command: ["/bin/sh", "-c"] args: - | - until [ -f /data/admin.token ]; do - echo "not set up yet: run kubectl -n allmende exec -it allmende-0 -- allmende init" - sleep 10 - done + [ -f /data/admin.token ] || allmende init --no-phrase exec allmende run \ --public \ --no-default-bootstrap \ @@ -67,21 +65,17 @@ spec: - name: http containerPort: 8080 protocol: TCP - # Ready once the node answers; before 'init' it never does, which keeps - # a half-set-up node out of the Services without restarting it. readinessProbe: httpGet: path: /api/v1/status port: http periodSeconds: 10 - # Only judge it dead after the first start succeeded (the wait for - # 'init' may take as long as you need). startupProbe: httpGet: path: /api/v1/status port: http - periodSeconds: 10 - failureThreshold: 360 + periodSeconds: 5 + failureThreshold: 60 livenessProbe: httpGet: path: /api/v1/status