Renew the device certificate from the browser, with the recovery phrase
Within the last 30 days the owner page offers it. The node signs a new certificate for the same device key (peer ID and listings stay), saves it, and signs again own listings that would outlive the old certificate. The phrase is used for one request and not kept; another identity's phrase and no-phrase identities are refused. Device.Current gives the certificate to sign with while a renewal happens. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/store"
|
||||
)
|
||||
|
||||
// publisher is what the keeper needs of the node: store a listing and gossip it.
|
||||
type publisher interface {
|
||||
Publish(ctx context.Context, l *listing.Listing) error
|
||||
}
|
||||
|
||||
// certKeeper renews the running node's device certificate from the recovery
|
||||
// phrase, which the owner types into the browser. The device key stays, so the
|
||||
// peer ID and every listing stay; only the certificate is replaced.
|
||||
type certKeeper struct {
|
||||
dev *identity.Device
|
||||
dir string
|
||||
store *store.Store
|
||||
node publisher
|
||||
difficulty int
|
||||
clock func() time.Time
|
||||
mu sync.Mutex // one renewal at a time
|
||||
}
|
||||
|
||||
func (k *certKeeper) CanRenew() bool {
|
||||
_, err := os.Stat(filepath.Join(k.dir, noRecoveryFile))
|
||||
return err != nil
|
||||
}
|
||||
|
||||
func (k *certKeeper) Renew(root *identity.Identity) (*identity.Cert, error) {
|
||||
k.mu.Lock()
|
||||
defer k.mu.Unlock()
|
||||
|
||||
now := k.clock()
|
||||
cert, err := k.dev.Renew(root, now, certLifetime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := k.dev.SaveCert(k.dir); err != nil {
|
||||
// The new certificate works until the node stops; say so, so the owner
|
||||
// does not believe it will survive a restart.
|
||||
return nil, fmt.Errorf("the certificate was renewed but could not be saved, so it is lost when the node stops: %w", err)
|
||||
}
|
||||
if err := k.resign(now); err != nil {
|
||||
return nil, fmt.Errorf("the certificate was renewed, but re-signing your listings failed: %w", err)
|
||||
}
|
||||
return cert, nil
|
||||
}
|
||||
|
||||
// resign signs again, with the new certificate, every live listing of this
|
||||
// identity that would outlive the certificate it carries: a listing is refused
|
||||
// once its certificate has expired, whatever its own date says.
|
||||
func (k *certKeeper) resign(now time.Time) error {
|
||||
ctx := context.Background()
|
||||
own, err := k.store.ByAuthor(ctx, k.dev.Cert.Author)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, old := range own {
|
||||
if old.Cert == nil || old.Cert.ExpiresAt >= old.ExpiresAt {
|
||||
continue
|
||||
}
|
||||
l := *old
|
||||
l.Seq = old.Seq + 1
|
||||
l.CreatedAt = now.Unix()
|
||||
if err := l.Seal(k.dev, k.difficulty); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := k.node.Publish(ctx, &l); err != nil {
|
||||
return fmt.Errorf("listing %s: %w", old.ID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/store"
|
||||
)
|
||||
|
||||
const keeperDifficulty = 6
|
||||
|
||||
// storePublisher is a node that only stores: enough to see what gets published.
|
||||
type storePublisher struct {
|
||||
st *store.Store
|
||||
published []*listing.Listing
|
||||
}
|
||||
|
||||
func (p *storePublisher) Publish(ctx context.Context, l *listing.Listing) error {
|
||||
if _, err := p.st.Put(ctx, l); err != nil {
|
||||
return err
|
||||
}
|
||||
p.published = append(p.published, l)
|
||||
return nil
|
||||
}
|
||||
|
||||
type keeperFixture struct {
|
||||
k *certKeeper
|
||||
root *identity.Identity
|
||||
dir string
|
||||
st *store.Store
|
||||
pub *storePublisher
|
||||
}
|
||||
|
||||
// newKeeper makes a device whose certificate runs out in certLeft.
|
||||
func newKeeper(t *testing.T, certLeft time.Duration) *keeperFixture {
|
||||
t.Helper()
|
||||
root, _, err := identity.NewRoot()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Now()
|
||||
dev, err := identity.Enroll(root, now.Add(-time.Hour), certLeft+time.Hour)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dir := t.TempDir()
|
||||
if err := dev.Save(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st, err := store.Open(":memory:", store.Options{Difficulty: keeperDifficulty})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { st.Close() })
|
||||
pub := &storePublisher{st: st}
|
||||
return &keeperFixture{
|
||||
k: &certKeeper{dev: dev, dir: dir, store: st, node: pub, difficulty: keeperDifficulty, clock: time.Now},
|
||||
root: root, dir: dir, st: st, pub: pub,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *keeperFixture) add(t *testing.T, lives time.Duration) *listing.Listing {
|
||||
t.Helper()
|
||||
id, err := listing.NewID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Now()
|
||||
l := &listing.Listing{
|
||||
Version: listing.Version, ID: id, Seq: 1,
|
||||
CreatedAt: now.Add(-time.Minute).Unix(), ExpiresAt: now.Add(lives).Unix(),
|
||||
Kind: "rent", Category: "apartment", Geohash: "u1xjhx",
|
||||
Price: &listing.Price{AmountCents: 120000, Currency: "EUR", Period: "month"},
|
||||
AreaM2: 55, RoomsX2: 5, Features: []string{"balcony"}, Title: "Helle 2,5-Zimmer-Wohnung",
|
||||
}
|
||||
if err := l.Seal(f.k.dev, keeperDifficulty); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.st.Put(context.Background(), l); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func TestRenewingKeepsTheDeviceAndSavesTheNewCertificate(t *testing.T) {
|
||||
f := newKeeper(t, 10*24*time.Hour)
|
||||
oldExpiry := f.k.dev.Current().ExpiresAt
|
||||
|
||||
cert, err := f.k.Renew(f.root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cert.ExpiresAt <= oldExpiry+300*24*3600 {
|
||||
t.Fatalf("renewed certificate expires %d, only %d s after the old one", cert.ExpiresAt, cert.ExpiresAt-oldExpiry)
|
||||
}
|
||||
loaded, err := identity.LoadDevice(f.dir)
|
||||
if err != nil {
|
||||
t.Fatalf("what is on disk does not load: %v", err)
|
||||
}
|
||||
if loaded.Cert.ExpiresAt != cert.ExpiresAt {
|
||||
t.Fatalf("disk holds a certificate expiring %d, want the renewed %d: a restart would lose the renewal", loaded.Cert.ExpiresAt, cert.ExpiresAt)
|
||||
}
|
||||
if loaded.Key.PublicKeyHex() != f.k.dev.Key.PublicKeyHex() {
|
||||
t.Fatal("renewing must not change the device key (it is the peer ID)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenewingSignsAgainOnlyTheListingsThatWouldOutliveTheOldCertificate(t *testing.T) {
|
||||
f := newKeeper(t, 10*24*time.Hour)
|
||||
short := f.add(t, 5*24*time.Hour) // gone before the old certificate runs out
|
||||
long := f.add(t, 25*24*time.Hour) // would be refused after day 10
|
||||
|
||||
if _, err := f.k.Renew(f.root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(f.pub.published) != 1 {
|
||||
t.Fatalf("%d listings were signed again, want exactly the long one", len(f.pub.published))
|
||||
}
|
||||
got := f.pub.published[0]
|
||||
if got.ID != long.ID || got.Seq != long.Seq+1 {
|
||||
t.Fatalf("re-signed %s seq %d, want %s seq %d", got.ID, got.Seq, long.ID, long.Seq+1)
|
||||
}
|
||||
if got.ExpiresAt != long.ExpiresAt {
|
||||
t.Fatal("re-signing must not change when the listing expires")
|
||||
}
|
||||
if got.Cert.ExpiresAt <= got.ExpiresAt {
|
||||
t.Fatal("the re-signed listing must carry a certificate that outlives it")
|
||||
}
|
||||
if err := got.Check(time.Now(), keeperDifficulty); err != nil {
|
||||
t.Fatalf("the re-signed listing does not verify: %v", err)
|
||||
}
|
||||
stored, err := f.st.Get(context.Background(), short.Author, short.ID)
|
||||
if err != nil || stored.Seq != short.Seq {
|
||||
t.Fatalf("the short listing must be left alone: %v seq %d", err, stored.Seq)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenewingWithTheWrongIdentityChangesNothing(t *testing.T) {
|
||||
f := newKeeper(t, 10*24*time.Hour)
|
||||
f.add(t, 25*24*time.Hour)
|
||||
stranger, _, _ := identity.NewRoot()
|
||||
before := f.k.dev.Current().ExpiresAt
|
||||
|
||||
if _, err := f.k.Renew(stranger); err != identity.ErrWrongIdentity {
|
||||
t.Fatalf("err = %v, want ErrWrongIdentity", err)
|
||||
}
|
||||
if f.k.dev.Current().ExpiresAt != before {
|
||||
t.Fatal("the certificate changed")
|
||||
}
|
||||
if len(f.pub.published) != 0 {
|
||||
t.Fatal("no listing may be signed again after a refused renewal")
|
||||
}
|
||||
loaded, err := identity.LoadDevice(f.dir)
|
||||
if err != nil || loaded.Cert.ExpiresAt != before {
|
||||
t.Fatalf("the saved certificate changed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnIdentityWithoutAPhraseCannotBeRenewed(t *testing.T) {
|
||||
f := newKeeper(t, 10*24*time.Hour)
|
||||
if !f.k.CanRenew() {
|
||||
t.Fatal("an identity with a phrase must be renewable")
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(f.dir, noRecoveryFile), []byte("x"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f.k.CanRenew() {
|
||||
t.Fatal("init --no-phrase marks the directory: renewal must be off")
|
||||
}
|
||||
}
|
||||
+9
-4
@@ -112,16 +112,20 @@ func cmdRun(args []string) error {
|
||||
return err
|
||||
}
|
||||
until := time.Until(time.Unix(dev.Cert.ExpiresAt, 0))
|
||||
renew := "run 'allmende recover' to renew it"
|
||||
// An expired certificate stops the node, so the browser is out of reach
|
||||
// then and only 'recover' helps; before that the owner can renew in the browser.
|
||||
renewExpired := "run 'allmende recover' to renew it"
|
||||
renewSoon := "renew it in the browser (sign in, then \"Renew certificate\" on your listings page) or run 'allmende recover'"
|
||||
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)"
|
||||
renewExpired = "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)"
|
||||
renewSoon = renewExpired
|
||||
}
|
||||
if until <= 0 {
|
||||
return errors.New("this device's certificate has expired; " + renew)
|
||||
return errors.New("this device's certificate has expired; " + renewExpired)
|
||||
}
|
||||
log := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
if until < certWarnBefore {
|
||||
log.Warn("device certificate expires soon; "+renew,
|
||||
log.Warn("device certificate expires soon; "+renewSoon,
|
||||
"expires", time.Unix(dev.Cert.ExpiresAt, 0).Format("2006-01-02"))
|
||||
}
|
||||
|
||||
@@ -174,6 +178,7 @@ func cmdRun(args []string) error {
|
||||
MessageDifficulty: *msgDifficulty,
|
||||
Inbox: in,
|
||||
RegionKeeper: ®ionKeeper{node: node, dir: *data},
|
||||
CertKeeper: &certKeeper{dev: dev, dir: *data, store: st, node: node, difficulty: *difficulty, clock: time.Now},
|
||||
})
|
||||
|
||||
ln, err := net.Listen("tcp", *httpAddr)
|
||||
|
||||
Reference in New Issue
Block a user