sdk-ci / test (push) Failing after 8s
- 自主仓 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 + 零依赖断言 + 三平台交叉编译冒烟)
140 lines
4.4 KiB
Go
140 lines
4.4 KiB
Go
package license
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
)
|
||
|
||
// Header 是三段式封装的头部(docs/design/03-license-format.md §2.1)。
|
||
type Header struct {
|
||
V int `json:"v"` // 格式版本,当前 1
|
||
Alg string `json:"alg"` // 固定 Ed25519,防算法降级
|
||
Kid string `json:"kid"` // 签名密钥 ID
|
||
Typ string `json:"typ"` // license | lease | bundle | trust | crl
|
||
}
|
||
|
||
const (
|
||
FormatVersion = 1
|
||
|
||
AlgEd25519 = "Ed25519"
|
||
|
||
TypLicense = "license"
|
||
TypLease = "lease"
|
||
TypBundle = "bundle"
|
||
TypTrust = "trust"
|
||
TypCRL = "crl"
|
||
TypActoken = "actoken" // 激活凭证(心跳/停用用短期 token)
|
||
TypVerdict = "verdict" // 在线校验结论
|
||
TypProxyCert = "proxycert" // Proxy Key 受约束证书(root 签名)
|
||
)
|
||
|
||
// LicenseType(payload.lt)。
|
||
const (
|
||
TypePerpetual = "perpetual"
|
||
TypeSubscription = "subscription"
|
||
TypeTrial = "trial"
|
||
TypeLease = "lease"
|
||
)
|
||
|
||
// State(payload.st)。
|
||
const (
|
||
StateIssued = "issued"
|
||
StateRenewed = "renewed"
|
||
)
|
||
|
||
var (
|
||
ErrExpired = errors.New("license: 已过期")
|
||
ErrNotYetValid = errors.New("license: 尚未生效")
|
||
ErrMalformed = errors.New("license: 格式损坏")
|
||
ErrUnsupported = errors.New("license: 不支持的版本或算法")
|
||
)
|
||
|
||
// Bindings 是部署绑定约束(docs/design/03-license-format.md §2.2 bind 字段)。
|
||
type Bindings struct {
|
||
FP string `json:"fp,omitempty"` // 机器指纹摘要
|
||
Cluster string `json:"cluster,omitempty"` // K8s 集群 UID 摘要
|
||
Tags []string `json:"tags,omitempty"` // 自定义标签
|
||
}
|
||
|
||
// PolicySnapshot 是签发时固化的策略快照,单位均为秒。
|
||
type PolicySnapshot struct {
|
||
Heartbeat int64 `json:"heartbeat,omitempty"`
|
||
LeaseTTL int64 `json:"lease_ttl,omitempty"`
|
||
Grace int64 `json:"grace,omitempty"`
|
||
ForcedCheck int64 `json:"forced_check,omitempty"`
|
||
}
|
||
|
||
// ProxyScope 是 Proxy Key 派发 lease 的受约束声明(docs/design/08-k8s-proxy.md §3)。
|
||
// MaxEnt 为该 Proxy Key 可分配的授权上限(来自 root 签名证书);运行时 ent
|
||
// 必须是 MaxEnt 的子集,超出即拒绝(Proxy 被攻破也无法扩权)。
|
||
type ProxyScope struct {
|
||
Cluster string `json:"cluster,omitempty"`
|
||
MaxEnt map[string]Entitlement `json:"max_ent"`
|
||
}
|
||
|
||
// Payload 是 license / lease 的载荷结构。
|
||
type Payload struct {
|
||
JTI string `json:"jti"`
|
||
ISS string `json:"iss"`
|
||
TID string `json:"tid"`
|
||
TName string `json:"tname,omitempty"`
|
||
Prod string `json:"prod"`
|
||
IAT int64 `json:"iat"`
|
||
NBF int64 `json:"nbf"`
|
||
EXP int64 `json:"exp"`
|
||
LT string `json:"lt"`
|
||
Ent map[string]Entitlement `json:"ent"`
|
||
Bind *Bindings `json:"bind,omitempty"`
|
||
Pol *PolicySnapshot `json:"pol,omitempty"`
|
||
Proxy *ProxyScope `json:"proxy,omitempty"` // Proxy Key 派发的 lease 必带
|
||
ACT string `json:"act,omitempty"` // lease 关联的 activation_id / 工作负载身份
|
||
ST string `json:"st"`
|
||
}
|
||
|
||
// Validate 做结构级校验(不含时间窗,时间窗见 TimeValid)。
|
||
func (p *Payload) Validate() error {
|
||
switch {
|
||
case p.JTI == "":
|
||
return fmt.Errorf("%w: 缺少 jti", ErrMalformed)
|
||
case p.ISS == "":
|
||
return fmt.Errorf("%w: 缺少 iss", ErrMalformed)
|
||
case p.TID == "":
|
||
return fmt.Errorf("%w: 缺少 tid", ErrMalformed)
|
||
case p.Prod == "":
|
||
return fmt.Errorf("%w: 缺少 prod", ErrMalformed)
|
||
case p.IAT == 0:
|
||
return fmt.Errorf("%w: 缺少 iat", ErrMalformed)
|
||
}
|
||
switch p.LT {
|
||
case TypePerpetual, TypeSubscription, TypeTrial, TypeLease:
|
||
default:
|
||
return fmt.Errorf("%w: 未知 lt %q", ErrMalformed, p.LT)
|
||
}
|
||
switch p.ST {
|
||
case StateIssued, StateRenewed:
|
||
default:
|
||
return fmt.Errorf("%w: 未知 st %q", ErrMalformed, p.ST)
|
||
}
|
||
if p.EXP == 0 && p.LT != TypePerpetual {
|
||
return fmt.Errorf("%w: exp=0 仅 perpetual 允许", ErrMalformed)
|
||
}
|
||
if p.LT == TypeLease && (p.Pol == nil || p.Pol.LeaseTTL <= 0) {
|
||
return fmt.Errorf("%w: lease 必须携带 pol.lease_ttl", ErrMalformed)
|
||
}
|
||
if p.Ent == nil {
|
||
return fmt.Errorf("%w: ent 不允许为 null", ErrMalformed)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// TimeValid 校验时间窗(now/skew 为 Unix 秒)。exp=0 表示永久。
|
||
func (p *Payload) TimeValid(now, skew int64) error {
|
||
if p.NBF > now+skew {
|
||
return ErrNotYetValid
|
||
}
|
||
if p.EXP != 0 && p.EXP <= now-skew {
|
||
return ErrExpired
|
||
}
|
||
return nil
|
||
}
|