8000 license: signed nonce on renew and deactivate by abahmed · Pull Request #578 · abahmed/kwatch · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions internal/license/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ type signedBody struct {
TS in 8000 t64 `json:"ts"`
Version string `json:"kwatch_version,omitempty"`
NodeCount int `json:"node_count,omitempty"`
// Nonce is fresh per call and signed with the body; the server
// accepts it once, so a captured request cannot be replayed.
Nonce string `json:"nonce"`
}

// newNonce returns 16 random bytes, base64url without padding.
func newNonce() (string, error) {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(b), nil
}

type wireResponse struct {
Expand Down Expand Up @@ -228,14 +240,14 @@ func (c *Client) activateOnce(
if !req.TS.IsZero() {
ts = req.TS.Unix()
}
nonce := make([]byte, 16)
if _, err := rand.Read(nonce); err != nil {
nonce, err := newNonce()
if err != nil {
return Result{}, err
}
body, err := json.Marshal(activateBody{
Key: req.Key, ClusterUUID: req.ClusterUUID, ClusterProof: req.ClusterProof,
InstancePub: req.InstancePub,
Nonce: base64.RawURLEncoding.EncodeToString(nonce),
Nonce: nonce,
Version: req.Version, NodeCount: req.NodeCount, TS: ts,
})
if err != nil {
Expand Down Expand Up @@ -265,10 +277,14 @@ func (c *Client) signed(
if len(priv) != ed25519.PrivateKeySize {
return Result{}, errors.New("license: invalid instance key")
}
nonce, err := newNonce()
if err != nil {
return Result{}, err
}
body, err := json.Marshal(signedBody{
LID: req.LID, ClusterUUID: req.ClusterUUID, ClusterProof: req.ClusterProof,
LeaseIat: req.LeaseIat, TS: req.TS.Unix(),
Version: req.Version, NodeCount: req.NodeCount,
Version: req.Version, NodeCount: req.NodeCount, Nonce: nonce,
})
if err != nil {
return Result{}, err
Expand Down
78 changes: 78 additions & 0 deletions internal/license/client_nonce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package license

import (
"context"
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
)

// signedCapture records the bodies of signed calls after checking each
// signature against pub over the raw body, as the Worker does.
func signedCapture(
t *testing.T, pub ed25519.PublicKey,
) (*httptest.Server, func() []map[string]any) {
t.Helper()
var mu sync.Mutex
var got []map[string]any
srv := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
raw, _ := io.ReadAll(r.Body)
sig, err := base64.RawURLEncoding.DecodeString(
r.Header.Get("X-Kwatch-Sig"))
if err != nil || !ed25519.Verify(pub, raw, sig) {
t.Errorf("%s: signature does not cover the body", r.URL.Path)
}
var m map[string]any
if err := json.Unmarshal(raw, &m); err != nil {
t.Errorf("body: %v", err)
}
mu.Lock()
got = append(got, m)
mu.Unlock()
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{}`))
}))
t.Cleanup(srv.Close)
return srv, func() []map[string]any {
mu.Lock()
defer mu.Unlock()
return got
}
}

func TestClientSignedCallsSendAFreshSignedNonce(t *testing.T) {
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatal(err)
}
srv, bodies := signedCapture(t, pub)
c := &Client{Endpoint: srv.URL}
req := SignedRequest{
LID: "lid", ClusterUUID: "c", ClusterProof: "p", TS: testNow,
}
ctx := context.Background()
_, _ = c.Renew(ctx, priv, req)
_, _ = c.Renew(ctx, priv, req)
_, _ = c.Deactivate(ctx, priv, req)
seen := map[string]bool{}
for i, b := range bodies() {
n, _ := b["nonce"].(string)
raw, err := base64.RawURLEncoding.DecodeString(n)
if err != nil || len(raw) != 16 {
t.Fatalf("call %d nonce = %q, want 16 bytes base64url", i, n)
}
if seen[n] {
t.Fatalf("call %d reused nonce %q", i, n)
}
seen[n] = true
}
if len(seen) != 3 {
t.Fatalf("calls = %d, want 3", len(seen))
}
}
0