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
143 lines
4.3 KiB
Go
143 lines
4.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"crypto/rand"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"math/big"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"golang.org/x/term"
|
|
|
|
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
|
)
|
|
|
|
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 {
|
|
return fmt.Errorf("%s is already set up. To get access back after losing this machine, use 'allmende recover'", *data)
|
|
}
|
|
|
|
root, phrase, err := identity.NewRoot()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
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
|
|
}
|
|
}
|
|
}
|
|
|
|
dev, err := identity.Enroll(root, time.Now(), certLifetime)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// The identity key is not saved anywhere: only the phrase can recreate it.
|
|
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 *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
|
|
}
|
|
|
|
func showPhrase(words []string) {
|
|
fmt.Print(`
|
|
Your recovery phrase
|
|
--------------------
|
|
These 12 words ARE your identity. They are shown only once and are not stored
|
|
on this computer. Write them on paper and keep them somewhere safe.
|
|
|
|
With them you can recover your account if this computer is lost, broken or
|
|
hacked. Anyone who has them can act as you, so never type them into a website
|
|
or share them.
|
|
`)
|
|
for i := 0; i < len(words); i += 3 {
|
|
fmt.Printf(" %2d. %-12s %2d. %-12s %2d. %-12s\n",
|
|
i+1, words[i], i+2, words[i+1], i+3, words[i+2])
|
|
}
|
|
// The table pastes badly (numbers, columns). One plain line can be selected
|
|
// with a triple click and pasted into a password manager or 'allmende recover'.
|
|
fmt.Printf("\nTo copy: select the line below (triple-click) and paste it where you keep it.\n\n%s\n\n", strings.Join(words, " "))
|
|
}
|
|
|
|
// confirmPhrase asks for two random words so the user has really written
|
|
// the phrase down.
|
|
func confirmPhrase(in *bufio.Reader, words []string) error {
|
|
picks, err := twoDistinct(len(words))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println("To make sure you wrote it down, please enter two of the words.")
|
|
for _, i := range picks {
|
|
for {
|
|
fmt.Printf("Word number %d: ", i+1)
|
|
line, err := in.ReadString('\n')
|
|
if err != nil && (!errors.Is(err, io.EOF) || line == "") {
|
|
return errors.New("no input; run 'allmende init' again in a terminal")
|
|
}
|
|
if strings.EqualFold(strings.TrimSpace(line), words[i]) {
|
|
break
|
|
}
|
|
fmt.Println("That is not the right word. Check your notes and try again.")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func twoDistinct(n int) ([2]int, error) {
|
|
a, err := rand.Int(rand.Reader, big.NewInt(int64(n)))
|
|
if err != nil {
|
|
return [2]int{}, err
|
|
}
|
|
b, err := rand.Int(rand.Reader, big.NewInt(int64(n-1)))
|
|
if err != nil {
|
|
return [2]int{}, err
|
|
}
|
|
x, y := int(a.Int64()), int(b.Int64())
|
|
if y >= x {
|
|
y++
|
|
}
|
|
if x > y {
|
|
x, y = y, x
|
|
}
|
|
return [2]int{x, y}, nil
|
|
}
|