59 lines
2.0 KiB
Go
59 lines
2.0 KiB
Go
//go:build integration
|
|
|
|
package link
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ed25519"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
"github.com/novox/mesh-controller/internal/broker"
|
|
"github.com/novox/mesh-controller/internal/testbus"
|
|
)
|
|
|
|
func TestAMoveIsAskedOfTheMachineSignedUnderItsOwnContext(t *testing.T) {
|
|
conn, err := nats.Connect(testbus.URL(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(conn.Close)
|
|
signer, public := testSigner(t)
|
|
var heard HandOverAsk
|
|
var row MoveSecretAsk
|
|
engine, err := conn.Subscribe(broker.AskMoveSecretSubject("laptop"), func(msg *nats.Msg) {
|
|
var signed SignedHandOver
|
|
_ = json.Unmarshal(msg.Data, &signed)
|
|
if ed25519.Verify(public, append([]byte(HandOverContext), signed.Ask...), signed.Signature) ||
|
|
!ed25519.Verify(public, append([]byte(MoveSecretContext), signed.Ask...), signed.Signature) {
|
|
_ = msg.Respond([]byte(`{"refused":"not signed as a move"}`))
|
|
return
|
|
}
|
|
_ = json.Unmarshal(signed.Ask, &heard)
|
|
_ = json.Unmarshal(signed.Ask, &row)
|
|
body, _ := json.Marshal(MoveSecretAnswer{Said: "moved", Secret: "own/laptop/" + row.Module + "/" + row.Name,
|
|
Generation: 1})
|
|
_ = msg.Respond(body)
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = engine.Unsubscribe() })
|
|
a, err := AskMoveSecret(context.Background(), conn, signer, "laptop", "the move",
|
|
MoveSecretAsk{Module: "postgres", Name: "superuser-password", Origin: "made", Sealed: "c2Vh"}, 5*time.Second)
|
|
if err != nil || a.Secret != "own/laptop/postgres/superuser-password" || a.Generation != 1 {
|
|
t.Fatalf("answered %+v, %v", a, err)
|
|
}
|
|
if heard.Node != "laptop" || heard.Path != "" || heard.Nonce == "" || heard.Expires.IsZero() || heard.By != "the move" ||
|
|
row.Sealed != "c2Vh" || row.Origin != "made" {
|
|
t.Fatalf("the engine heard %+v %+v", heard, row)
|
|
}
|
|
if _, err := AskMoveSecret(context.Background(), conn, signer, "anchor", "the move", MoveSecretAsk{}, 2*time.Second); err == nil ||
|
|
!strings.Contains(err.Error(), "nothing was moved") {
|
|
t.Fatalf("a machine with no engine listening: %v", err)
|
|
}
|
|
}
|