Add runnable node, HTTP API, all-devices revocation and reconnect sync
- cmd/node: init (recovery phrase onboarding), run, recover - api: public reads of canonical signed records, token-protected writes - identity: RevokeAll voids every device certified before a cutoff, so recovery works with only the phrase; store purges by certificate age - p2p: redial bootstrap peers, sync on every new connection (in-flight guard only), validate bootstrap addresses up front - docs: API, recovery semantics, peer-ID change on recovery Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"golang.org/x/term"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/store"
|
||||
)
|
||||
|
||||
// cmdRecover restores access from the recovery phrase. It voids every device
|
||||
// certified so far (including a stolen one), enrolls this machine as a fresh
|
||||
// device, and re-signs this node's own listings so they carry on. It also
|
||||
// serves as the yearly certificate renewal.
|
||||
func cmdRecover(args []string) error {
|
||||
fs := flag.NewFlagSet("recover", flag.ExitOnError)
|
||||
data := dataFlag(fs)
|
||||
difficulty := fs.Int("difficulty", listing.DefaultDifficulty, "proof-of-work bits; must match the network")
|
||||
fs.Parse(args)
|
||||
|
||||
phrase, err := readPhrase()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
root, err := identity.FromPhrase(phrase)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
author := root.PublicKeyHex()
|
||||
ctx := context.Background()
|
||||
|
||||
st, err := store.Open(filepath.Join(*data, dbFile), store.Options{Difficulty: *difficulty})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer st.Close()
|
||||
|
||||
// Remember our listings: the revocation below purges them from the store.
|
||||
own, err := st.ByAuthor(ctx, author)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
rev, err := identity.RevokeAll(root, now, *difficulty)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Enrolled at the same instant: a certificate issued at the cutoff stays valid.
|
||||
dev, err := identity.Enroll(root, now, certLifetime)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := retireOldDevice(*data, now); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := dev.Save(*data); err != nil {
|
||||
return err
|
||||
}
|
||||
token, err := newToken()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writeToken(*data, token); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := st.PutRevocation(ctx, rev); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
republished := 0
|
||||
for _, old := range own {
|
||||
l := *old
|
||||
l.Seq = old.Seq + 1
|
||||
l.CreatedAt = now.Unix()
|
||||
if max := now.Add(listing.MaxLifetime).Unix(); l.ExpiresAt > max {
|
||||
l.ExpiresAt = max
|
||||
}
|
||||
if err := l.Seal(dev, *difficulty); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := st.Put(ctx, &l); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "warning: could not re-sign listing %s: %v\n", old.ID, err)
|
||||
continue
|
||||
}
|
||||
republished++
|
||||
}
|
||||
|
||||
fmt.Printf("Recovered identity %s…\n", author[:12])
|
||||
fmt.Println(" - All earlier devices of this identity have been revoked.")
|
||||
fmt.Printf(" - This machine is now a new device (certificate valid until %s).\n",
|
||||
time.Unix(dev.Cert.ExpiresAt, 0).Format("2006-01-02"))
|
||||
fmt.Printf(" - %d of your listings were re-signed from this device.\n", republished)
|
||||
fmt.Println(" - A new admin token was written; the old one no longer works.")
|
||||
fmt.Println("\nStart the node with 'node run' so the revocation spreads through the network.")
|
||||
return nil
|
||||
}
|
||||
|
||||
// retireOldDevice moves a previous device's files aside so the revoked key is
|
||||
// never used again but is not silently destroyed either.
|
||||
func retireOldDevice(dir string, now time.Time) error {
|
||||
for _, name := range []string{"device.key", "device.cert"} {
|
||||
src := filepath.Join(dir, name)
|
||||
if _, err := os.Stat(src); errors.Is(err, os.ErrNotExist) {
|
||||
continue
|
||||
}
|
||||
dst := fmt.Sprintf("%s.revoked-%d", src, now.Unix())
|
||||
if err := os.Rename(src, dst); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// readPhrase reads the recovery phrase without echoing it when possible.
|
||||
func readPhrase() (string, error) {
|
||||
fd := int(os.Stdin.Fd())
|
||||
if term.IsTerminal(fd) {
|
||||
fmt.Fprint(os.Stderr, "Recovery phrase (12 words; what you type is hidden): ")
|
||||
b, err := term.ReadPassword(fd)
|
||||
fmt.Fprintln(os.Stderr)
|
||||
return string(b), err
|
||||
}
|
||||
b, err := io.ReadAll(io.LimitReader(os.Stdin, 4096))
|
||||
return string(b), err
|
||||
}
|
||||
Reference in New Issue
Block a user