513aa9e6f5
build / build (push) Has been cancelled
为什么:Astra 复核证实五组实现缺陷——正式装配缺 scope/快照且来源与 before-image 混用;激活撤销不在 CAS 事务内、lease 用事务起始时间;激活只接受任意字符串且撤销可 被 SQL 重置/删除;准入忽略绑定冲突与 prepared 矛盾、收尾只看当前 plan;prepared 已 提交但 mark-intent 未落盘时被误判 legacy。 改了什么: - 绑定来自原锚点(scope/来源快照/backup/实例/目标指纹/索引 UUID/恢复投递),每次 重建都实时重读目标身份并与冻结值比较; - activation 行锁与 CAS 同事务,lease 用 clock_timestamp(),同事务核对 job/operation/ attempt/维护窗口/恢复 queue 与 operation,同 effect 换 plan 摘要拒绝; - 激活消费带摘要的证据文件(writer 退出+排空、目标/治理库身份、有效期),撤销终局, down 在 activation 有行时拒绝; - 前置/收尾共用权威窗口全集,绑定冲突与 prepared 矛盾零写 held,合法 dispatching+ 精确 marker 仍可确认; - prepared 无 mark-intent 仅在绑定一致且无矛盾时补同内容意图,旧无台账意图仍 held。 验证:真 PG 锁等待覆盖撤销先胜/CAS 先胜/等锁跨租约/错锚点/plan 别名/恢复投递; COMMIT 已到服务端但回包丢失与未决事务分别读回;真 ES+PG+对象存储的 DS01 用例全部 通过;go test ./... 全绿。未迁移正式库、未部署、未激活。
130 lines
5.6 KiB
Go
130 lines
5.6 KiB
Go
package libraryrelease
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// 恢复派发激活证据(DS01-F3)。
|
|
//
|
|
// 配置开关与"三个非空字符串"都不是独占证明。激活必须消费一份**可核验的证据文件**:
|
|
// - 文件字节的 sha256 由调用方给出并在读取后重算(不是文件里自报的布尔值);
|
|
// - 证据只对某一目标身份/目标指纹/治理库生效;
|
|
// - 受控 writer 必须逐个登记"已退出 + 队列已排空"以及各自的退出观察;
|
|
// - 证据有明确的签署人与时间窗口,过期不接受。
|
|
//
|
|
// 边界(必须如实写进报告):代码能核验文件完整性、结构完整性、目标/治理库身份与
|
|
// 新鲜度;"这些 writer 真的已经停止"仍是部署流程的受控 attestation(附退出时间和
|
|
// 排空证据摘要),不是本进程运行时能独立观察到的外部事实。没有这份证据就不建立
|
|
// active 行——这一点由代码强制。
|
|
|
|
const RestoreDispatchActivationMaxAge = 2 * time.Hour
|
|
|
|
type RestoreDispatchWriterDrain struct {
|
|
Name string `json:"name"`
|
|
Kind string `json:"kind"`
|
|
Stopped bool `json:"stopped"`
|
|
ExitedAt *time.Time `json:"exited_at"`
|
|
QueueDrained bool `json:"queue_drained"`
|
|
DrainEvidence string `json:"drain_evidence"`
|
|
}
|
|
|
|
type RestoreDispatchActivationEvidence struct {
|
|
ProtocolVersion int `json:"protocol_version"`
|
|
TargetIdentity string `json:"target_identity"`
|
|
TargetFingerprint string `json:"target_fingerprint"`
|
|
TargetInstance string `json:"target_instance"`
|
|
GovernanceDatabase string `json:"governance_database"`
|
|
ProtocolBuild string `json:"protocol_build"`
|
|
AttestedBy string `json:"attested_by"`
|
|
AttestedAt time.Time `json:"attested_at"`
|
|
Writers []RestoreDispatchWriterDrain `json:"writers"`
|
|
}
|
|
|
|
func (e RestoreDispatchActivationEvidence) Validate(now time.Time, maxAge time.Duration) error {
|
|
if e.ProtocolVersion != 1 {
|
|
return fmt.Errorf("restore dispatch activation protocol version %d is not supported", e.ProtocolVersion)
|
|
}
|
|
for name, value := range map[string]string{
|
|
"target identity": e.TargetIdentity, "target fingerprint": e.TargetFingerprint,
|
|
"target instance": e.TargetInstance, "governance database": e.GovernanceDatabase,
|
|
"protocol build": e.ProtocolBuild, "attested by": e.AttestedBy,
|
|
} {
|
|
if strings.TrimSpace(value) == "" {
|
|
return fmt.Errorf("restore dispatch activation evidence lacks %s", name)
|
|
}
|
|
}
|
|
if len(e.Writers) == 0 {
|
|
return errors.New("restore dispatch activation evidence lists no controlled writers")
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, writer := range e.Writers {
|
|
if strings.TrimSpace(writer.Name) == "" || strings.TrimSpace(writer.Kind) == "" {
|
|
return errors.New("restore dispatch activation writer lacks name or kind")
|
|
}
|
|
if seen[writer.Name] {
|
|
return fmt.Errorf("restore dispatch activation writer %s is listed twice", writer.Name)
|
|
}
|
|
seen[writer.Name] = true
|
|
if !writer.Stopped || !writer.QueueDrained {
|
|
return fmt.Errorf("restore dispatch activation writer %s has not exited and drained", writer.Name)
|
|
}
|
|
if writer.ExitedAt == nil || writer.ExitedAt.IsZero() {
|
|
return fmt.Errorf("restore dispatch activation writer %s has no exit observation", writer.Name)
|
|
}
|
|
if strings.TrimSpace(writer.DrainEvidence) == "" {
|
|
return fmt.Errorf("restore dispatch activation writer %s has no drain evidence", writer.Name)
|
|
}
|
|
if writer.ExitedAt.After(e.AttestedAt) {
|
|
return fmt.Errorf("restore dispatch activation writer %s exited after the attestation", writer.Name)
|
|
}
|
|
}
|
|
if e.AttestedAt.IsZero() || e.AttestedAt.After(now.Add(5*time.Minute)) {
|
|
return errors.New("restore dispatch activation attestation time is not credible")
|
|
}
|
|
if maxAge <= 0 {
|
|
maxAge = RestoreDispatchActivationMaxAge
|
|
}
|
|
if now.Sub(e.AttestedAt) > maxAge {
|
|
return fmt.Errorf("restore dispatch activation evidence is older than %s", maxAge)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// LoadRestoreDispatchActivationEvidence 读取并核验证据文件字节摘要。expectedDigest 必须
|
|
// 与文件实际 sha256 相同;不接受"文件里写的摘要"。
|
|
func LoadRestoreDispatchActivationEvidence(path, expectedDigest string, now time.Time, maxAge time.Duration) (RestoreDispatchActivationEvidence, error) {
|
|
var evidence RestoreDispatchActivationEvidence
|
|
if strings.TrimSpace(path) == "" || strings.TrimSpace(expectedDigest) == "" {
|
|
return evidence, errors.New("restore dispatch activation requires an evidence file and its digest")
|
|
}
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return evidence, fmt.Errorf("read restore dispatch activation evidence: %w", err)
|
|
}
|
|
sum := sha256.Sum256(raw)
|
|
if got := hex.EncodeToString(sum[:]); got != strings.ToLower(strings.TrimSpace(expectedDigest)) {
|
|
return evidence, errors.New("restore dispatch activation evidence digest does not match the file")
|
|
}
|
|
decoder := json.NewDecoder(strings.NewReader(string(raw)))
|
|
decoder.DisallowUnknownFields()
|
|
if err := decoder.Decode(&evidence); err != nil {
|
|
return RestoreDispatchActivationEvidence{}, fmt.Errorf("parse restore dispatch activation evidence: %w", err)
|
|
}
|
|
if err := evidence.Validate(now, maxAge); err != nil {
|
|
return RestoreDispatchActivationEvidence{}, err
|
|
}
|
|
return evidence, nil
|
|
}
|
|
|
|
// CanonicalBytes 是写入台账的规范字节(同一份证据只有一种序列化)。
|
|
func (e RestoreDispatchActivationEvidence) CanonicalBytes() ([]byte, error) {
|
|
return json.Marshal(e)
|
|
}
|