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,98 @@
package config
import (
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type Config struct {
HTTPAddr string
TrustedProxies []string
ReadHeaderTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ShutdownTimeout time.Duration
SigningPrivateKeyPEM string
}
func Load(getenv func(string) string) (Config, error) {
c := Config{HTTPAddr: "127.0.0.1:18080"}
if v := getenv("HTTP_ADDR"); v != "" {
c.HTTPAddr = v
}
_, port, err := net.SplitHostPort(c.HTTPAddr)
if err != nil {
return Config{}, fmt.Errorf("HTTP_ADDR must be host:port")
}
p, err := strconv.Atoi(port)
if err != nil || p < 1 || p > 65535 {
return Config{}, fmt.Errorf("HTTP_ADDR port must be between 1 and 65535")
}
for _, setting := range []struct {
name string
value *time.Duration
fallback time.Duration
}{
{"HTTP_READ_HEADER_TIMEOUT", &c.ReadHeaderTimeout, 5 * time.Second},
{"HTTP_READ_TIMEOUT", &c.ReadTimeout, 30 * time.Second},
{"HTTP_WRITE_TIMEOUT", &c.WriteTimeout, 30 * time.Second},
{"HTTP_IDLE_TIMEOUT", &c.IdleTimeout, 60 * time.Second},
{"HTTP_SHUTDOWN_TIMEOUT", &c.ShutdownTimeout, 10 * time.Second},
} {
*setting.value = setting.fallback
if v := getenv(setting.name); v != "" {
d, err := time.ParseDuration(v)
if err != nil || d <= 0 || d > 5*time.Minute {
return Config{}, fmt.Errorf("%s must be a duration greater than zero and at most 5m", setting.name)
}
*setting.value = d
}
}
if value := getenv("HTTP_TRUSTED_PROXY_CIDRS"); value != "" {
for _, value := range strings.Split(value, ",") {
value = strings.TrimSpace(value)
_, network, err := net.ParseCIDR(value)
if err != nil {
return Config{}, fmt.Errorf("trusted proxies must be explicit CIDRs")
}
ones, _ := network.Mask.Size()
if ones == 0 {
return Config{}, fmt.Errorf("trust-all proxies are forbidden")
}
c.TrustedProxies = append(c.TrustedProxies, value)
}
}
if value := getenv("SESSION_COOKIE_SECURE"); value != "" && value != "true" && value != "false" {
return Config{}, fmt.Errorf("SESSION_COOKIE_SECURE must be true or false")
}
c.SigningPrivateKeyPEM = getenv("LICENSE_SIGNING_PRIVATE_KEY_PEM")
if path := getenv("LICENSE_SIGNING_PRIVATE_KEY_FILE"); path != "" {
if c.SigningPrivateKeyPEM != "" {
return Config{}, fmt.Errorf("only one signing key source may be configured")
}
if !filepath.IsAbs(path) {
return Config{}, fmt.Errorf("LICENSE_SIGNING_PRIVATE_KEY_FILE must be absolute")
}
data, err := os.ReadFile(path)
if err != nil || len(data) == 0 || len(data) > 16384 {
return Config{}, fmt.Errorf("signing key file must be readable and between 1 and 16384 bytes")
}
c.SigningPrivateKeyPEM = string(data)
}
if root := getenv("PACKAGE_STORAGE_ROOT"); root != "" {
if !filepath.IsAbs(root) {
return Config{}, fmt.Errorf("PACKAGE_STORAGE_ROOT must be absolute")
}
info, err := os.Stat(root)
if err != nil || !info.IsDir() {
return Config{}, fmt.Errorf("PACKAGE_STORAGE_ROOT must be an existing directory")
}
}
return c, nil
}

View File

@@ -0,0 +1,64 @@
package config
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestLoadDefaults(t *testing.T) {
c, err := Load(func(string) string { return "" })
if err != nil || c.HTTPAddr != "127.0.0.1:18080" || c.ReadHeaderTimeout != 5*time.Second || c.ShutdownTimeout != 10*time.Second {
t.Fatalf("config=%+v err=%v", c, err)
}
}
func TestLoadOverrides(t *testing.T) {
values := map[string]string{"HTTP_ADDR": "[::1]:18081", "HTTP_WRITE_TIMEOUT": "2m"}
c, err := Load(func(key string) string { return values[key] })
if err != nil || c.HTTPAddr != values["HTTP_ADDR"] || c.WriteTimeout != 2*time.Minute {
t.Fatalf("config=%+v err=%v", c, err)
}
}
func TestLoadKeyFileAndStorage(t *testing.T) {
root := t.TempDir()
key := filepath.Join(root, "key.pem")
if err := os.WriteFile(key, []byte("test-only-content"), 0600); err != nil {
t.Fatal(err)
}
values := map[string]string{"LICENSE_SIGNING_PRIVATE_KEY_FILE": key, "PACKAGE_STORAGE_ROOT": root}
cfg, err := Load(func(k string) string { return values[k] })
if err != nil || cfg.SigningPrivateKeyPEM != "test-only-content" {
t.Fatal("file injection failed")
}
values["LICENSE_SIGNING_PRIVATE_KEY_PEM"] = "conflicting-source"
if _, err := Load(func(k string) string { return values[k] }); err == nil {
t.Fatal("ambiguous key sources accepted")
}
}
func TestLoadRejectsInvalid(t *testing.T) {
for _, tc := range []struct{ key, value string }{
{"HTTP_TRUSTED_PROXY_CIDRS", "0.0.0.0/0"}, {"HTTP_TRUSTED_PROXY_CIDRS", "::/0"},
{"HTTP_TRUSTED_PROXY_CIDRS", "127.0.0.1"}, {"HTTP_TRUSTED_PROXY_CIDRS", "bad"},
{"SESSION_COOKIE_SECURE", "yes"},
{"PACKAGE_STORAGE_ROOT", "relative/packages"}, {"LICENSE_SIGNING_PRIVATE_KEY_FILE", "relative/key.pem"},
{"HTTP_ADDR", "localhost"}, {"HTTP_ADDR", ":0"}, {"HTTP_ADDR", ":65536"}, {"HTTP_ADDR", ":abc"},
{"HTTP_READ_HEADER_TIMEOUT", "bad"}, {"HTTP_READ_TIMEOUT", "0s"},
{"HTTP_WRITE_TIMEOUT", "-1s"}, {"HTTP_IDLE_TIMEOUT", "6m"}, {"HTTP_SHUTDOWN_TIMEOUT", "0s"},
} {
t.Run(tc.key+tc.value, func(t *testing.T) {
_, err := Load(func(key string) string {
if key == tc.key {
return tc.value
}
return ""
})
if err == nil {
t.Fatal("expected configuration error")
}
})
}
}