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" ) // 编译截止超期处置(payload.bcm)。空值等价于 deny。 const ( BuildCutoffDeny = "deny" BuildCutoffDegrade = "degrade" ) var ( ErrExpired = errors.New("license: 已过期") ErrNotYetValid = errors.New("license: 尚未生效") ErrMalformed = errors.New("license: 格式损坏") ErrUnsupported = errors.New("license: 不支持的版本或算法") ErrBuildCutoff = errors.New("license: 编译版本超出授权截止") ErrBuildTimeUnknown = 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 / 工作负载身份 BC int64 `json:"bc,omitempty"` // 编译截止(Unix 秒);0=不限制 BCM string `json:"bcm,omitempty"` // 超期处置:deny(缺省)| degrade 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) } if p.BC < 0 { return fmt.Errorf("%w: bc 不能为负数", ErrMalformed) } switch p.BCM { case "", BuildCutoffDeny, BuildCutoffDegrade: default: return fmt.Errorf("%w: 未知 bcm %q", ErrMalformed, p.BCM) } if p.BC == 0 && p.BCM != "" { return fmt.Errorf("%w: bcm 必须与 bc 同时出现", ErrMalformed) } return nil } // EffectiveBuildCutoffAction 返回生效的超期处置动作(bcm 缺省时为 deny)。 func (p *Payload) EffectiveBuildCutoffAction() string { if p.BCM == "" { return BuildCutoffDeny } return p.BCM } // BuildCutoffExceeded 判断应用编译时间是否晚于编译截止。 // 截止时刻本身允许(编译时间 ≤ bc);bc=0 表示不限制。 func (p *Payload) BuildCutoffExceeded(buildTime int64) bool { return p.BC > 0 && buildTime > p.BC } // 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 }