feat: implement license platform business flows and delivery tooling
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:
119
backend/internal/httpapi/integration_kit_test.go
Normal file
119
backend/internal/httpapi/integration_kit_test.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"crypto/ecdsa"
|
||||
"crypto/sha256"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestIntegrationAuthentication(t *testing.T) {
|
||||
r := gin.New()
|
||||
registerBusinessRoutes(r, &BusinessAPI{})
|
||||
for _, path := range []string{"/api/v1/integration-kit", "/api/v1/integration-kit/download", "/api/v1/integration-kit/archive"} {
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
|
||||
if w.Code != 401 {
|
||||
t.Fatalf("%s: %d", path, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestIntegrationInputs(t *testing.T) {
|
||||
for _, query := range []string{"?language=rust", "?mode=browser", "?language=../../secret"} {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/"+query, nil)
|
||||
(&BusinessAPI{}).integrationKit(c)
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("%s: %d", query, w.Code)
|
||||
}
|
||||
}
|
||||
for language := range integrationLanguages {
|
||||
kit := buildIntegrationKit(language, "service")
|
||||
if !strings.Contains(kit.Prompt, languageRequirements(language)) || !strings.Contains(kit.Prompt, defaultIntegrationBaseURL) || !strings.Contains(kit.Protocol, defaultIntegrationBaseURL) || kit.PlatformBaseURL != defaultIntegrationBaseURL || !strings.Contains(kit.Protocol, "DER") {
|
||||
t.Fatal("incomplete kit")
|
||||
}
|
||||
}
|
||||
content, err := integrationAssets.ReadFile("integration_assets/openapi.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var spec map[string]any
|
||||
if err = json.Unmarshal(content, &spec); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if spec["openapi"] != "3.0.3" {
|
||||
t.Fatal("invalid OpenAPI")
|
||||
}
|
||||
}
|
||||
func TestIntegrationArchiveAndSignatures(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("GET", "/?language=java&mode=container", nil)
|
||||
(&BusinessAPI{}).downloadIntegrationArchive(c)
|
||||
if w.Code != 200 || w.Header().Get("Content-Type") != "application/zip" {
|
||||
t.Fatalf("%d %s", w.Code, w.Body)
|
||||
}
|
||||
archive, err := zip.NewReader(bytes.NewReader(w.Body.Bytes()), int64(w.Body.Len()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(archive.File) != 5 {
|
||||
t.Fatal("missing files")
|
||||
}
|
||||
for _, file := range archive.File {
|
||||
reader, err := file.Open()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
content, err := io.ReadAll(reader)
|
||||
reader.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if file.Name == "openapi.json" && !strings.Contains(string(content), defaultIntegrationBaseURL) {
|
||||
t.Fatal("OpenAPI does not use the default platform address")
|
||||
}
|
||||
if file.Name != "test-vectors.json" {
|
||||
continue
|
||||
}
|
||||
var vectors struct {
|
||||
PinnedKeys []struct {
|
||||
PublicKey string `json:"public_key_pem"`
|
||||
} `json:"pinned_keys"`
|
||||
License, Lease struct{ Signature, Canonical string }
|
||||
}
|
||||
if err := json.Unmarshal(content, &vectors); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
block, _ := pem.Decode([]byte(vectors.PinnedKeys[0].PublicKey))
|
||||
key, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, vector := range []struct{ Signature, Canonical string }{vectors.License, vectors.Lease} {
|
||||
sig, err := base64.RawStdEncoding.DecodeString(vector.Signature)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
digest := sha256.Sum256([]byte(vector.Canonical))
|
||||
if !ecdsa.VerifyASN1(key.(*ecdsa.PublicKey), digest[:], sig) {
|
||||
t.Fatal("bad signature")
|
||||
}
|
||||
digest = sha256.Sum256([]byte(vector.Canonical + "tampered"))
|
||||
if ecdsa.VerifyASN1(key.(*ecdsa.PublicKey), digest[:], sig) {
|
||||
t.Fatal("accepted tampering")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user