Files
iceking2nd 81c7295893
sdk-ci / test (push) Failing after 8s
feat: 益铸 EntitleForge SDK 独立仓库首个版本
- 自主仓 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 + 零依赖断言 + 三平台交叉编译冒烟)
2026-11-09 15:42:22 +08:00

85 lines
2.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package license
import (
"encoding/json"
"fmt"
)
// BundleMaxSegmentLen 是离线授权包单段有效期上限(90 天),
// 到期必须重新导入——离线场景的授权回收窗口(docs/design/08-k8s-proxy.md §8)。
const BundleMaxSegmentLen int64 = 90 * 86400
// Bundle 是离线授权包信封载荷(docs/design/03-license-format.md §5):
// 若干覆盖未来时段的集群 lease(signing key 签名)+ CRL 快照,
// 整体由 root 签名(typ=bundle 三段式信封)。
type Bundle struct {
V int `json:"v"`
Cluster string `json:"cluster"`
Segments []string `json:"segments"` // 按时间升序的集群 lease token
ExportedAt int64 `json:"exported_at"`
Until int64 `json:"until"`
CRLVersion int64 `json:"crl_version"`
}
// SegmentsPlan 生成切段计划(nbf, exp 序列,最后一段截到 until)。
func SegmentsPlan(now, until, segLen int64) [][2]int64 {
if segLen <= 0 || segLen > BundleMaxSegmentLen {
segLen = BundleMaxSegmentLen
}
var out [][2]int64
for start := now; start < until; {
end := start + segLen
if end > until {
end = until
}
out = append(out, [2]int64{start, end})
start = end
}
return out
}
// NewBundle 组装信封(段由调用方签名生成,见 cmd/efctl/bundle.go)。
func NewBundle(clusterID string, segments []string, exportedAt, until, crlVersion int64) *Bundle {
return &Bundle{V: 1, Cluster: clusterID, Segments: segments,
ExportedAt: exportedAt, Until: until, CRLVersion: crlVersion}
}
// Payloads 解析全部段的载荷(结构解码,不验签——验签由 crypto.VerifyToken 做)。
func (b *Bundle) Payloads() ([]*Payload, error) {
out := make([]*Payload, 0, len(b.Segments))
for i, seg := range b.Segments {
_, raw, _, err := Decode(seg)
if err != nil {
return nil, fmt.Errorf("bundle 段 %d: %w", i, err)
}
var p Payload
if err := json.Unmarshal(raw, &p); err != nil {
return nil, fmt.Errorf("bundle 段 %d 载荷: %w", i, err)
}
out = append(out, &p)
}
return out, nil
}
// SegmentFor 返回覆盖 t 的段(按段 nbf/exp 精确匹配)。
// t 早于首段 nbf 时回落首段(导出前授权已存在的宽限语义);
// t ≥ until 时无段可用(包耗尽,必须重新导入)。
func (b *Bundle) SegmentFor(t int64) (string, bool) {
if len(b.Segments) == 0 {
return "", false
}
payloads, err := b.Payloads()
if err != nil {
return "", false
}
for i, p := range payloads {
if t >= p.NBF && t < p.EXP {
return b.Segments[i], true
}
}
if t < payloads[0].NBF {
return b.Segments[0], true
}
return "", false
}