feat: implement license platform business flows and delivery tooling
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation.

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-09 15:33:22 +08:00
parent 3ab651d3a8
commit 54c318c615
142 changed files with 11105 additions and 178 deletions

View File

@@ -0,0 +1,140 @@
package httpapi
import (
"crypto/ecdsa"
"crypto/sha256"
"database/sql"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/gin-gonic/gin"
)
func testHeartbeatMySQL(t *testing.T, db *sql.DB, r *gin.Engine, cookie *http.Cookie, instanceID, secret string, licenseID uint64, publicKey *ecdsa.PublicKey) {
request := func(path, body, key string, auth bool) *httptest.ResponseRecorder {
req := httptest.NewRequest("POST", path, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", key)
if auth {
req.AddCookie(cookie)
}
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
path := "/api/v1/instances/" + instanceID + "/heartbeat"
bodyFor := func(secret string, at time.Time) string {
b, _ := json.Marshal(gin.H{"secret": secret, "client_time": at.UTC().Format(time.RFC3339Nano)})
return string(b)
}
now := time.Now().UTC()
body := bodyFor(secret, now)
if w := request(path, bodyFor(base64.RawURLEncoding.EncodeToString(make([]byte, 32)), now), "wrong", false); w.Code != 403 {
t.Fatalf("wrong secret status=%d", w.Code)
}
if _, err := db.Exec("CREATE TRIGGER heartbeat_audit_failure BEFORE INSERT ON audit_events FOR EACH ROW SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='test failure'"); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Exec("DROP TRIGGER IF EXISTS heartbeat_audit_failure") })
if w := request(path, body, "first", false); w.Code != 500 {
t.Fatalf("rollback status=%d", w.Code)
}
var last sql.NullTime
db.QueryRow("SELECT last_seen_at FROM license_instances WHERE instance_id=?", instanceID).Scan(&last)
if last.Valid {
t.Fatal("audit failure committed last_seen")
}
db.Exec("DROP TRIGGER heartbeat_audit_failure")
var wg sync.WaitGroup
results := make(chan *httptest.ResponseRecorder, 2)
for i := 0; i < 2; i++ {
wg.Add(1)
go func() { defer wg.Done(); results <- request(path, body, "first", false) }()
}
wg.Wait()
close(results)
var response struct {
Data struct {
Payload leasePayload `json:"payload"`
Signature string `json:"signature"`
}
}
for w := range results {
if w.Code != 200 {
t.Fatalf("concurrent status=%d", w.Code)
}
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
}
digest := sha256.Sum256([]byte(canonicalLease(response.Data.Payload)))
sig, err := base64.RawStdEncoding.DecodeString(response.Data.Signature)
if err != nil || !ecdsa.VerifyASN1(publicKey, digest[:], sig) {
t.Fatal("lease signature invalid")
}
p := response.Data.Payload
server, _ := time.Parse(time.RFC3339Nano, p.ServerTime)
end, _ := time.Parse(time.RFC3339Nano, p.LeaseExpiresAt)
entitlement, _ := time.Parse(time.RFC3339Nano, p.EntitlementExpiresAt)
if p.State != "active" || end.After(server.Add(maxOfflineLease)) || end.After(entitlement) {
t.Fatal("lease bound invalid")
}
var count int
db.QueryRow("SELECT COUNT(*) FROM heartbeat_operations").Scan(&count)
if count != 1 {
t.Fatal("duplicate heartbeat operation")
}
if w := request(path, body, "new", false); w.Code != 429 {
t.Fatalf("limit status=%d", w.Code)
}
if w := request(path, bodyFor(secret, now.Add(time.Second)), "first", false); w.Code != 409 {
t.Fatalf("conflict=%d", w.Code)
}
if w := request(path, bodyFor(secret, now.Add(-time.Hour)), "clock", false); w.Code != 409 {
t.Fatalf("clock skew=%d", w.Code)
}
freezePath := "/api/v1/instances/" + instanceID + "/freeze"
if w := request(freezePath, `{"frozen":true}`, "freeze", false); w.Code != 401 {
t.Fatal("freeze unauthorized")
}
if w := request(freezePath, `{"frozen":true}`, "freeze", true); w.Code != 200 {
t.Fatalf("freeze=%d", w.Code)
}
if w := request(path, body, "first", false); w.Code != 409 {
t.Fatal("old allow replay masked freeze")
}
w := request(path, body, "frozen", false)
if w.Code != 200 {
t.Fatalf("frozen heartbeat=%d", w.Code)
}
json.Unmarshal(w.Body.Bytes(), &response)
if response.Data.Payload.State != "frozen" || response.Data.Payload.LeaseExpiresAt != response.Data.Payload.ServerTime {
t.Fatal("frozen lease allowed use")
}
if w = request(freezePath, `{"frozen":false}`, "unfreeze", true); w.Code != 200 {
t.Fatal("unfreeze failed")
}
w = request(path, body, "resume", false)
if w.Code != 200 {
t.Fatal("resume heartbeat failed")
}
revokePath := "/api/v1/licenses/" + strconv.FormatUint(licenseID, 10) + "/revoke"
if w = request(revokePath, `{}`, "heartbeat-revoke", true); w.Code != 200 {
t.Fatalf("revoke=%d", w.Code)
}
w = request(path, body, "revoked", false)
if w.Code != 200 {
t.Fatalf("revoked heartbeat=%d", w.Code)
}
json.Unmarshal(w.Body.Bytes(), &response)
if response.Data.Payload.State != "revoked" {
t.Fatal("revocation not propagated")
}
}