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:
@@ -21,6 +21,8 @@ import (
|
|||||||
func cmdInit(args []string) error {
|
func cmdInit(args []string) error {
|
||||||
fs := flag.NewFlagSet("init", flag.ExitOnError)
|
fs := flag.NewFlagSet("init", flag.ExitOnError)
|
||||||
data := dataFlag(fs)
|
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)
|
fs.Parse(args)
|
||||||
|
|
||||||
if _, err := os.Stat(filepath.Join(*data, "device.key")); err == nil {
|
if _, err := os.Stat(filepath.Join(*data, "device.key")); err == nil {
|
||||||
@@ -31,6 +33,7 @@ func cmdInit(args []string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if !*noPhrase {
|
||||||
words := strings.Fields(phrase)
|
words := strings.Fields(phrase)
|
||||||
showPhrase(words)
|
showPhrase(words)
|
||||||
|
|
||||||
@@ -40,6 +43,7 @@ func cmdInit(args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dev, err := identity.Enroll(root, time.Now(), certLifetime)
|
dev, err := identity.Enroll(root, time.Now(), certLifetime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -56,9 +60,19 @@ func cmdInit(args []string) error {
|
|||||||
if err := writeToken(*data, token); err != nil {
|
if err := writeToken(*data, token); err != nil {
|
||||||
return err
|
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("\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"))
|
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("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))
|
fmt.Printf("The admin token for publishing is in %s\n", filepath.Join(*data, tokenFile))
|
||||||
return nil
|
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"
|
dbFile = "allmende.db"
|
||||||
tokenFile = "admin.token"
|
tokenFile = "admin.token"
|
||||||
peersFile = "peers.json" // peers met on earlier runs
|
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.
|
const usage = `Allmende: a free, distributed marketplace for homes.
|
||||||
|
|
||||||
Usage:
|
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 run [--data DIR] --region u1x run the node
|
||||||
allmende recover [--data DIR] restore access using your recovery phrase
|
allmende recover [--data DIR] restore access using your recovery phrase
|
||||||
|
|
||||||
|
|||||||
+6
-2
@@ -97,12 +97,16 @@ func cmdRun(args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
until := time.Until(time.Unix(dev.Cert.ExpiresAt, 0))
|
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 {
|
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))
|
log := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||||
if until < certWarnBefore {
|
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"))
|
"expires", time.Unix(dev.Cert.ExpiresAt, 0).Format("2006-01-02"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+15
-12
@@ -43,17 +43,11 @@ so:
|
|||||||
kubectl apply -k k8s/base # with a LoadBalancer (MetalLB, kube-vip, ...)
|
kubectl apply -k k8s/base # with a LoadBalancer (MetalLB, kube-vip, ...)
|
||||||
kubectl apply -k k8s/overlays/nodeport # or: NodePort 30401, no load balancer
|
kubectl apply -k k8s/overlays/nodeport # or: NodePort 30401, no load balancer
|
||||||
```
|
```
|
||||||
The pod starts and waits: it has no identity yet.
|
On its first start the pod creates its identity by itself
|
||||||
4. **Create the identity, once.** This shows the 12-word recovery phrase; it is
|
(`allmende init --no-phrase`): no recovery phrase is made or logged. The keys
|
||||||
done by hand so the phrase never lands in a log.
|
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.
|
||||||
kubectl -n allmende exec -it allmende-0 -- allmende init
|
4. **Publish the DNS records.** The node prints them at start:
|
||||||
```
|
|
||||||
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:
|
|
||||||
```
|
```
|
||||||
kubectl -n allmende logs allmende-0 | grep dnsaddr=
|
kubectl -n allmende logs allmende-0 | grep dnsaddr=
|
||||||
```
|
```
|
||||||
@@ -62,7 +56,7 @@ so:
|
|||||||
```
|
```
|
||||||
dig +short TXT _dnsaddr.allmende.stinnesbeck.com
|
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
|
allmende init && allmende run # no --bootstrap needed
|
||||||
```
|
```
|
||||||
@@ -70,6 +64,15 @@ so:
|
|||||||
|
|
||||||
## Good to know
|
## 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
|
- **One replica only.** A second pod would be a second node with its own
|
||||||
identity, not a copy.
|
identity, not a copy.
|
||||||
- **No sign-in link in the log.** `allmende run` normally prints the owner
|
- **No sign-in link in the log.** `allmende run` normally prints the owner
|
||||||
|
|||||||
+10
-16
@@ -1,9 +1,8 @@
|
|||||||
# One node, with its keys and database on a volume. Keep it at one replica: a
|
# 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.
|
# 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
|
# The identity is created on the first start, without a recovery phrase (so
|
||||||
# that must never end up in a log. Until you do, the pod waits:
|
# nothing secret is logged). It lasts a year; see k8s/README.md.
|
||||||
# kubectl -n allmende exec -it allmende-0 -- allmende init
|
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: StatefulSet
|
kind: StatefulSet
|
||||||
metadata:
|
metadata:
|
||||||
@@ -31,16 +30,15 @@ spec:
|
|||||||
- name: allmende
|
- name: allmende
|
||||||
image: allmende
|
image: allmende
|
||||||
imagePullPolicy: IfNotPresent
|
imagePullPolicy: IfNotPresent
|
||||||
# Wait for 'allmende init' (it writes admin.token last), then run as a
|
# First start: create the identity without a recovery phrase (a
|
||||||
# bootstrap node: --public serves the DHT and relays for nodes behind
|
# bootstrap node has nothing to recover; its identity is its peer ID,
|
||||||
# a NAT, --announce names the address the router forwards.
|
# 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"]
|
command: ["/bin/sh", "-c"]
|
||||||
args:
|
args:
|
||||||
- |
|
- |
|
||||||
until [ -f /data/admin.token ]; do
|
[ -f /data/admin.token ] || allmende init --no-phrase
|
||||||
echo "not set up yet: run kubectl -n allmende exec -it allmende-0 -- allmende init"
|
|
||||||
sleep 10
|
|
||||||
done
|
|
||||||
exec allmende run \
|
exec allmende run \
|
||||||
--public \
|
--public \
|
||||||
--no-default-bootstrap \
|
--no-default-bootstrap \
|
||||||
@@ -67,21 +65,17 @@ spec:
|
|||||||
- name: http
|
- name: http
|
||||||
containerPort: 8080
|
containerPort: 8080
|
||||||
protocol: TCP
|
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:
|
readinessProbe:
|
||||||
httpGet:
|
httpGet:
|
||||||
path: /api/v1/status
|
path: /api/v1/status
|
||||||
port: http
|
port: http
|
||||||
periodSeconds: 10
|
periodSeconds: 10
|
||||||
# Only judge it dead after the first start succeeded (the wait for
|
|
||||||
# 'init' may take as long as you need).
|
|
||||||
startupProbe:
|
startupProbe:
|
||||||
httpGet:
|
httpGet:
|
||||||
path: /api/v1/status
|
path: /api/v1/status
|
||||||
port: http
|
port: http
|
||||||
periodSeconds: 10
|
periodSeconds: 5
|
||||||
failureThreshold: 360
|
failureThreshold: 60
|
||||||
livenessProbe:
|
livenessProbe:
|
||||||
httpGet:
|
httpGet:
|
||||||
path: /api/v1/status
|
path: /api/v1/status
|
||||||
|
|||||||
Reference in New Issue
Block a user