- 自主仓 git.wxccs.org/EntitleForge/EntitleForge 拆分(协议实现单一归属: license/crypto/ulid 随 SDK 迁出,服务端反向依赖本仓) - sdk:Client 三模式(文件/远程/代理)、GRACE 状态机、查询 API(6.6ns/0 alloc) - license:三段式编解码与严格解析;crypto:Ed25519 信任链 + Proxy 受约束证书 - 零第三方依赖(CI 有依赖闭包断言红线);go.mod 零 require - CI:Gitea Actions(test + 零依赖断言 + 三平台交叉编译冒烟)
This commit is contained in:
32 files changed
+3940
No files matched your search
@@ -0,0 +1,175 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.wxccs.org/EntitleForge/EntitleForge-SDK-go/license"
|
||||
)
|
||||
|
||||
// proxyFixture 构造 root + signing + proxy 三层密钥与受约束证书。
|
||||
type proxyFixture struct {
|
||||
root, signing, proxy *KeyPair
|
||||
chain *TrustChain // SDK 侧信任链(root + signing + proxy 约束)
|
||||
cert string // root 签名的 Proxy 受约束证书
|
||||
}
|
||||
|
||||
func newProxyFixture(t *testing.T) *proxyFixture {
|
||||
t.Helper()
|
||||
root, _ := GenerateRootKey()
|
||||
signing, _ := GenerateSigningKey()
|
||||
proxy, _ := GenerateSigningKey() // Proxy Key 也是 Ed25519 对
|
||||
|
||||
// 受约束证书:声明 proxy key 的授权上限(集群 ent 快照)
|
||||
maxEnt := map[string]license.Entitlement{
|
||||
"seats": license.Quantity(100),
|
||||
"tier": license.TierValue("enterprise"),
|
||||
}
|
||||
certPayload, _ := json.Marshal(map[string]any{
|
||||
"v": 1, "kid": proxy.Kid, "scope": "proxy", "cluster": "cluster-demo",
|
||||
"max_ent": maxEnt, "iat": testNow - 60, "exp": testNow + 86400,
|
||||
})
|
||||
// 证书由 root 签名
|
||||
certToken, err := SignToken(root.Priv, "root", license.TypProxyCert, certPayload)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
chain := NewTrustChain(root.Pub)
|
||||
if err := chain.AddSigningKey(signing.Kid, signing.Pub, KeyStatusActive, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// SDK 装载 proxy 受约束信任(校验证书 root 签名)
|
||||
if err := chain.AddProxyKey(certToken, proxy.Pub, testNow); err != nil {
|
||||
t.Fatalf("AddProxyKey: %v", err)
|
||||
}
|
||||
return &proxyFixture{root: root, signing: signing, proxy: proxy, chain: chain, cert: certToken}
|
||||
}
|
||||
|
||||
// proxyLease 用 Proxy Key 签 per-workload lease。
|
||||
func (f *proxyFixture) proxyLease(t *testing.T, mut func(*license.Payload)) string {
|
||||
t.Helper()
|
||||
p := validPayload()
|
||||
p.LT = license.TypeLease
|
||||
p.ST = license.StateRenewed
|
||||
p.ACT = "system:serviceaccount:payments:api"
|
||||
p.EXP = testNow + 600
|
||||
p.Pol = &license.PolicySnapshot{LeaseTTL: 600, Heartbeat: 300, Grace: 259200}
|
||||
p.Proxy = &license.ProxyScope{Cluster: "cluster-demo", MaxEnt: map[string]license.Entitlement{
|
||||
"seats": license.Quantity(100), "tier": license.TierValue("enterprise"),
|
||||
}}
|
||||
if mut != nil {
|
||||
mut(&p)
|
||||
}
|
||||
b, _ := json.Marshal(p)
|
||||
token, err := SignToken(f.proxy.Priv, f.proxy.Kid, license.TypLease, b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
func TestProxyLease_SubsetVerifies(t *testing.T) {
|
||||
f := newProxyFixture(t)
|
||||
// 裁剪后的子集授权(seats 20 < 100)
|
||||
token := f.proxyLease(t, func(p *license.Payload) {
|
||||
p.Ent = map[string]license.Entitlement{"seats": license.Quantity(20)}
|
||||
})
|
||||
got, err := VerifyToken(token, f.chain, testNow)
|
||||
if err != nil {
|
||||
t.Fatalf("子集授权应验证通过: %v", err)
|
||||
}
|
||||
if got.Ent["seats"].AsInt() != 20 {
|
||||
t.Errorf("seats: %d", got.Ent["seats"].AsInt())
|
||||
}
|
||||
if got.Proxy == nil || got.Proxy.Cluster != "cluster-demo" {
|
||||
t.Errorf("proxy scope 缺失: %+v", got.Proxy)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyLease_EscalationRejected(t *testing.T) {
|
||||
f := newProxyFixture(t)
|
||||
// 攻破 Proxy 后尝试扩权:seats 500 > max 100、新增未授权 feature
|
||||
cases := []struct {
|
||||
name string
|
||||
mut func(*license.Payload)
|
||||
}{
|
||||
{"数量越权", func(p *license.Payload) {
|
||||
p.Ent = map[string]license.Entitlement{"seats": license.Quantity(500)}
|
||||
}},
|
||||
{"新增未授权项", func(p *license.Payload) {
|
||||
p.Ent = map[string]license.Entitlement{"module.secret": license.Bool(true)}
|
||||
}},
|
||||
{"档位越权", func(p *license.Payload) {
|
||||
p.Ent = map[string]license.Entitlement{"tier": license.TierValue("ultimate")}
|
||||
}},
|
||||
{"隐匿 scope(绕过约束校验)", func(p *license.Payload) {
|
||||
p.Proxy = nil
|
||||
}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
token := f.proxyLease(t, func(p *license.Payload) {
|
||||
tc.mut(p)
|
||||
// 越权时同步抬高 scope 上限尝试绕过(scope 本身来自证书,此处伪造)
|
||||
if p.Proxy != nil {
|
||||
p.Proxy.MaxEnt = map[string]license.Entitlement{
|
||||
"seats": license.Quantity(5000), "tier": license.TierValue("ultimate"),
|
||||
"module.secret": license.Bool(true),
|
||||
}
|
||||
}
|
||||
})
|
||||
if _, err := VerifyToken(token, f.chain, testNow); err == nil {
|
||||
t.Error("越权 lease 应被拒绝")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyLease_UnknownKeyRejected(t *testing.T) {
|
||||
// 未在信任链登记的 proxy key(无证书)签的 lease 拒绝
|
||||
f := newProxyFixture(t)
|
||||
rogue, _ := GenerateSigningKey()
|
||||
p := validPayload()
|
||||
p.LT = license.TypeLease
|
||||
p.ST = license.StateRenewed
|
||||
p.EXP = testNow + 600
|
||||
p.Proxy = &license.ProxyScope{Cluster: "x", MaxEnt: p.Ent}
|
||||
b, _ := json.Marshal(p)
|
||||
token, _ := SignToken(rogue.Priv, rogue.Kid, license.TypLease, b)
|
||||
if _, err := VerifyToken(token, f.chain, testNow); err == nil {
|
||||
t.Error("未登记 proxy key 应拒绝")
|
||||
} else if !strings.Contains(err.Error(), "kid") {
|
||||
t.Errorf("错误语义: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyCert_ForgedRejected(t *testing.T) {
|
||||
// 伪造证书(非 root 签名)装载失败
|
||||
root, _ := GenerateRootKey()
|
||||
attacker, _ := GenerateSigningKey()
|
||||
chain := NewTrustChain(root.Pub)
|
||||
certPayload, _ := json.Marshal(map[string]any{
|
||||
"v": 1, "kid": attacker.Kid, "scope": "proxy", "max_ent": map[string]any{},
|
||||
})
|
||||
forged, _ := SignToken(attacker.Priv, "root", license.TypProxyCert, certPayload)
|
||||
if err := chain.AddProxyKey(forged, attacker.Pub, testNow); err == nil {
|
||||
t.Error("伪造证书应装载失败")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyCert_Expired(t *testing.T) {
|
||||
f := newProxyFixture(t)
|
||||
// 证书过期:用旧时间验证
|
||||
token := f.proxyLease(t, func(p *license.Payload) {
|
||||
p.Ent = map[string]license.Entitlement{"seats": license.Quantity(20)}
|
||||
p.IAT, p.NBF, p.EXP = testNow-7200, testNow-7200, testNow-3600 // lease 自身过期也会拒
|
||||
})
|
||||
if _, err := VerifyToken(token, f.chain, testNow); err == nil {
|
||||
t.Error("过期 lease 应拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
var _ = time.Now
|
||||
Reference in new issue
Block a user