Repository Public
1package protocol23import (4 "crypto/ed25519"5 "crypto/rand"6 "encoding/json"7 "errors"8 "testing"9 "time"10)1112type memNonces map[string]time.Time1314func (m memNonces) Remember(n string, until time.Time) (bool, error) {15 if _, seen := m[n]; seen {16 return false, nil17 }18 m[n] = until19 return true, nil20}2122func fixture(t *testing.T) (ed25519.PublicKey, ed25519.PrivateKey, Command, time.Time) {23 t.Helper()24 pub, priv, err := ed25519.GenerateKey(rand.Reader)25 if err != nil {26 t.Fatal(err)27 }28 now := time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC)29 return pub, priv, Command{V: Version, ID: "c1", ActionID: "a1", AgentID: "agent-1", NodeID: "n1", Type: "inventory.refresh",30 Params: json.RawMessage(`{}`), TimeoutS: 30, IssuedAt: now, ExpiresAt: now.Add(60 * time.Second), Nonce: NewNonce()}, now31}3233func TestVerifyAcceptsOnceThenRejectsReplay(t *testing.T) {34 pub, priv, cmd, now := fixture(t)35 sc, err := Sign(priv, "k1", cmd)36 if err != nil {37 t.Fatal(err)38 }39 keys := map[string]ed25519.PublicKey{"k1": pub}40 nonces := memNonces{}41 got, err := Verify(sc, keys, "agent-1", now.Add(time.Second), nonces)42 if err != nil || got.ID != "c1" {43 t.Fatalf("first verify: %v", err)44 }45 if _, err := Verify(sc, keys, "agent-1", now.Add(2*time.Second), nonces); !errors.Is(err, ErrReplay) {46 t.Fatalf("replay must be rejected, got %v", err)47 }48}4950func TestVerifyRejections(t *testing.T) {51 pub, priv, cmd, now := fixture(t)52 otherPub, otherPriv, _ := ed25519.GenerateKey(rand.Reader)53 keys := map[string]ed25519.PublicKey{"k1": pub}54 sc, _ := Sign(priv, "k1", cmd)5556 tampered := sc57 tampered.Payload = append([]byte{}, sc.Payload...)58 tampered.Payload[len(tampered.Payload)-2] ^= 159 forged, _ := Sign(otherPriv, "k1", cmd) // right key id, wrong key: a compromised ingest cannot forge60 _ = otherPub6162 cases := []struct {63 name string64 sc SignedCommand65 agent string66 at time.Time67 want error68 }{69 {"tampered payload", tampered, "agent-1", now, ErrBadSignature},70 {"forged by another key", forged, "agent-1", now, ErrBadSignature},71 {"unknown key id", SignedCommand{KeyID: "k9", Payload: sc.Payload, Signature: sc.Signature}, "agent-1", now, ErrUnknownKey},72 {"other recipient", sc, "agent-2", now, ErrWrongAgent},73 {"expired", sc, "agent-1", now.Add(61 * time.Second), ErrExpired},74 {"from the future", sc, "agent-1", now.Add(-MaxClockSkew - time.Second), ErrNotYetValid},75 }76 for _, c := range cases {77 if _, err := Verify(c.sc, keys, c.agent, c.at, memNonces{}); !errors.Is(err, c.want) {78 t.Errorf("%s: got %v, want %v", c.name, err, c.want)79 }80 }81}8283func TestSignRefusesLongLivedCommands(t *testing.T) {84 _, priv, cmd, now := fixture(t)85 cmd.ExpiresAt = now.Add(MaxCommandLifetime + time.Second)86 if _, err := Sign(priv, "k1", cmd); err == nil {87 t.Fatal("expected lifetime error")88 }89 // A validly signed but over-long command (malicious orchestrator build) is still refused by the agent.90 payload, _ := json.Marshal(cmd)91 sc := SignedCommand{KeyID: "k1", Payload: payload, Signature: ed25519.Sign(priv, signingInput(payload))}92 pub := priv.Public().(ed25519.PublicKey)93 if _, err := Verify(sc, map[string]ed25519.PublicKey{"k1": pub}, "agent-1", now, memNonces{}); !errors.Is(err, ErrLifetime) {94 t.Fatalf("got %v", err)95 }96}97