feat: rotate generic profiles across verified alternatives
Select bounded canonical strategy allowlists at explicit PID1 startup within the same immutable authenticated registry, matrix and source. Reconstruct stored policies independently for historical replay, reconciliation and rollback; preserve freshness for new policies even when rendered bytes match. Retain legacy refusal and one-shot startup guards. Local fixtures and independent local review passed; live native rotation acceptance remains pending. No uninterrupted rotation, new qualification or proof-store overwrite. Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
1 parent
5d0be0288f
commit
8d43dd9363
10 files changed
+473
-31
No files matched your search
@@ -65,6 +65,11 @@ func runWithPlayer(args []string, stdout, stderr io.Writer, player playerRunner)
|
||||
}
|
||||
if len(args) == 1 && args[0] == "target-config-check" {
|
||||
path, _ := os.LookupEnv("ZAPRET_AUTOTUNE_RESOURCE_CONFIG")
|
||||
policy, _ := os.LookupEnv(resourceStrategyIDsEnv)
|
||||
if _, err := parseResourceStrategyIDs(policy); err != nil || policy != "" && path == "" {
|
||||
fmt.Fprintln(stderr, "configerror: invalid generic strategy selection")
|
||||
return 64
|
||||
}
|
||||
if path == "" {
|
||||
path, _ = os.LookupEnv("ZAPRET_AUTOTUNE_QUALIFY_CONFIG")
|
||||
}
|
||||
|
||||
@@ -381,7 +381,7 @@ func (m *managedTransaction) applyPolicy(b []byte, db *sql.DB, requested *Google
|
||||
return fmt.Errorf("generic journal cannot authorize legacy apply")
|
||||
}
|
||||
if generic != nil {
|
||||
if m.resource == nil || m.resource.guard() != nil || m.resource.validate(generic, managedHash(b)) != nil {
|
||||
if m.resource == nil || *generic != m.resource.ref || m.resource.guard() != nil || m.resource.validate(generic, managedHash(b)) != nil {
|
||||
return errResourcePlan
|
||||
}
|
||||
if j.Version == 4 && j.Ref.Kind == "generic" {
|
||||
@@ -392,9 +392,8 @@ func (m *managedTransaction) applyPolicy(b []byte, db *sql.DB, requested *Google
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("generic profile rotation unsupported")
|
||||
}
|
||||
if len(j.Candidates) != 0 || j.Hash != managedHash([]byte(managedBaseline)) {
|
||||
if !(j.Version == 4 && j.Ref.Kind == "generic") && (len(j.Candidates) != 0 || j.Hash != managedHash([]byte(managedBaseline))) {
|
||||
return fmt.Errorf("generic apply requires disabled baseline")
|
||||
}
|
||||
if m.resource.fresh() != nil {
|
||||
@@ -485,8 +484,10 @@ func (m *managedTransaction) applyPolicy(b []byte, db *sql.DB, requested *Google
|
||||
}
|
||||
j.Generation++
|
||||
if generic != nil {
|
||||
if j.Version != 4 {
|
||||
j.Ref = &managedConfigRef{Kind: "baseline"}
|
||||
}
|
||||
j.Version = 4
|
||||
j.Ref = &managedConfigRef{Kind: "baseline"}
|
||||
j.Scope, j.PendingScope = "", ""
|
||||
j.PendingRef = generic
|
||||
} else if requested != nil || j.Version == 3 {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
@@ -23,6 +24,32 @@ type managedConfigRef struct {
|
||||
Source string `json:"source_fingerprint,omitempty"`
|
||||
Plan string `json:"plan_digest,omitempty"`
|
||||
Config string `json:"config_hash,omitempty"`
|
||||
Policy string `json:"strategy_ids,omitempty"`
|
||||
}
|
||||
|
||||
const resourceStrategyIDsEnv = "ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS"
|
||||
|
||||
// Policy is a canonical bounded allowlist, never qualification evidence.
|
||||
func parseResourceStrategyIDs(value string) (string, error) {
|
||||
if value == "" {
|
||||
return "", nil
|
||||
}
|
||||
if len(value) > 4096 {
|
||||
return "", errResourcePlan
|
||||
}
|
||||
ids := strings.Split(value, ",")
|
||||
if len(ids) > 32 {
|
||||
return "", errResourcePlan
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, id := range ids {
|
||||
if !resourceID.MatchString(id) || seen[id] {
|
||||
return "", errResourcePlan
|
||||
}
|
||||
seen[id] = true
|
||||
}
|
||||
sort.Strings(ids)
|
||||
return strings.Join(ids, ","), nil
|
||||
}
|
||||
|
||||
func validManagedConfigRef(r *managedConfigRef, ids []int64, scope GooglevideoScope, hash string) bool {
|
||||
@@ -31,9 +58,10 @@ func validManagedConfigRef(r *managedConfigRef, ids []int64, scope GooglevideoSc
|
||||
}
|
||||
switch r.Kind {
|
||||
case "generic":
|
||||
return len(ids) == 0 && scope == "" && hashOK(r.Registry) && hashOK(r.Matrix) && hashOK(r.Source) && hashOK(r.Plan) && r.Config == hash
|
||||
policy, e := parseResourceStrategyIDs(r.Policy)
|
||||
return e == nil && policy == r.Policy && len(ids) == 0 && scope == "" && hashOK(r.Registry) && hashOK(r.Matrix) && hashOK(r.Source) && hashOK(r.Plan) && r.Config == hash
|
||||
case "baseline", "legacy":
|
||||
return r.Registry == "" && r.Matrix == "" && r.Source == "" && r.Plan == "" && r.Config == "" && (r.Kind == "legacy" && len(ids) > 0 || r.Kind == "baseline" && len(ids) == 0 && scope == "" && hash == managedHash([]byte(managedBaseline)))
|
||||
return r.Policy == "" && r.Registry == "" && r.Matrix == "" && r.Source == "" && r.Plan == "" && r.Config == "" && (r.Kind == "legacy" && len(ids) > 0 || r.Kind == "baseline" && len(ids) == 0 && scope == "" && hash == managedHash([]byte(managedBaseline)))
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -68,8 +96,27 @@ type resourceApplySnapshot struct {
|
||||
|
||||
// Historical reconstruction uses the proof's original completion horizon. Only
|
||||
// new apply checks wall-clock freshness; expiry cannot erase committed identity.
|
||||
func resourceApplyPlan(source resourceMonitorSource, key []byte, registry strategyRegistry, matrix qualificationMatrix, now time.Time) (resourcePlan, []byte, error) {
|
||||
p := deriveResourcePlan(source, key, registry, matrix, now)
|
||||
func resourceApplyPlan(source resourceMonitorSource, key []byte, registry strategyRegistry, matrix qualificationMatrix, now time.Time, policies ...string) (resourcePlan, []byte, error) {
|
||||
policy := ""
|
||||
if len(policies) != 0 {
|
||||
policy = policies[0]
|
||||
}
|
||||
canonical, e := parseResourceStrategyIDs(policy)
|
||||
if e != nil || canonical != policy {
|
||||
return resourcePlan{}, nil, errResourcePlan
|
||||
}
|
||||
if policy != "" {
|
||||
for _, id := range strings.Split(policy, ",") {
|
||||
found := false
|
||||
for _, d := range registry.Strategies {
|
||||
found = found || d.ID == id
|
||||
}
|
||||
if !found {
|
||||
return resourcePlan{}, nil, errResourcePlan
|
||||
}
|
||||
}
|
||||
}
|
||||
p := deriveResourcePlan(source, key, registry, matrix, now, policy)
|
||||
if p.Evidence != "authenticated" || len(p.Unassigned) != 0 || len(p.Assignments) == 0 {
|
||||
return p, nil, errResourcePlan
|
||||
}
|
||||
@@ -101,23 +148,45 @@ func resourceApplyPlan(source resourceMonitorSource, key []byte, registry strate
|
||||
}
|
||||
|
||||
func (s *resourceApplySnapshot) fresh() error {
|
||||
p, b, e := resourceApplyPlan(s.source, s.store.files.key, s.registry, s.matrix, s.now())
|
||||
p, b, e := resourceApplyPlan(s.source, s.store.files.key, s.registry, s.matrix, s.now(), s.ref.Policy)
|
||||
if e != nil || planDigest(p.Assignments) != s.ref.Plan || managedHash(b) != s.ref.Config {
|
||||
return errResourcePlan
|
||||
}
|
||||
return s.guard()
|
||||
}
|
||||
|
||||
// ponytail: one immutable qualification batch, baseline-only activation and
|
||||
// identical-request resume. Rotation/active maintenance need versioned snapshots
|
||||
// and job-bound rollback, not overwriting the held registry or matrix.
|
||||
// Each journal side reconstructs independently inside the same immutable boundary.
|
||||
func (s *resourceApplySnapshot) validate(ref *managedConfigRef, hash string) error {
|
||||
if ref == nil || *ref != s.ref || hash != s.ref.Config {
|
||||
if ref == nil || ref.Kind != "generic" || ref.Registry != s.ref.Registry || ref.Matrix != s.ref.Matrix || ref.Source != s.ref.Source || hash != ref.Config {
|
||||
return errResourcePlan
|
||||
}
|
||||
p, b, e := s.historicalPlan(ref.Policy)
|
||||
if e != nil || planDigest(p.Assignments) != ref.Plan || managedHash(b) != ref.Config {
|
||||
return errResourcePlan
|
||||
}
|
||||
return s.guard()
|
||||
}
|
||||
|
||||
func (s *resourceApplySnapshot) historicalPlan(policy string) (resourcePlan, []byte, error) {
|
||||
var horizon int64
|
||||
for _, run := range s.matrix.Runs {
|
||||
if run.Completed > horizon {
|
||||
horizon = run.Completed
|
||||
}
|
||||
}
|
||||
return resourceApplyPlan(s.source, s.store.files.key, s.registry, s.matrix, time.UnixMilli(horizon), policy)
|
||||
}
|
||||
|
||||
func (s *resourceApplySnapshot) selectPolicy(policy string) error {
|
||||
p, b, e := s.historicalPlan(policy)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
s.ref.Policy, s.ref.Plan, s.ref.Config = policy, planDigest(p.Assignments), managedHash(b)
|
||||
s.rendered = b
|
||||
return nil
|
||||
}
|
||||
|
||||
// No imported proposal or version probe. Keep private proof/source/native files
|
||||
// held across staging, health and commit and bind every selected candidate.
|
||||
func openResourceApplySnapshot(config string, d managedDeps) (*resourceApplySnapshot, error) {
|
||||
@@ -193,13 +262,7 @@ func openResourceApplySnapshotAt(config string, d managedDeps, root string) (*re
|
||||
}
|
||||
lua[path] = f
|
||||
}
|
||||
var horizon int64
|
||||
for _, run := range s.matrix.Runs {
|
||||
if run.Completed > horizon {
|
||||
horizon = run.Completed
|
||||
}
|
||||
}
|
||||
p, rendered, e := resourceApplyPlan(s.source, store.files.key, s.registry, s.matrix, time.UnixMilli(horizon))
|
||||
p, rendered, e := s.historicalPlan("")
|
||||
if e != nil {
|
||||
return fail()
|
||||
}
|
||||
@@ -289,6 +352,10 @@ func resourceApplyMode(config string, d managedDeps) error {
|
||||
if values["ZAPRET_AUTOTUNE_RESOURCE_APPLY"] != "1" || values["ZAPRET_AUTOTUNE_RESOURCE_CONFIG"] != config || values["ZAPRET_AUTOTUNE_QUALIFY_CONFIG"] != "" || values["ZAPRET_AUTOTUNE_STRATEGY_CONFIG"] != "" {
|
||||
return errResourcePlan
|
||||
}
|
||||
requested, _ := d.lookup(resourceStrategyIDsEnv)
|
||||
if _, e := parseResourceStrategyIDs(requested); e != nil || requested != values[resourceStrategyIDsEnv] {
|
||||
return errResourcePlan
|
||||
}
|
||||
for _, ns := range []string{"pid", "net"} {
|
||||
a, e := os.Readlink("/proc/1/ns/" + ns)
|
||||
b, f := os.Readlink("/proc/self/ns/" + ns)
|
||||
@@ -353,6 +420,11 @@ func resourceApplyCLI(config string, stdout, stderr io.Writer, d managedDeps) in
|
||||
if resourceSingletonGuard(lock, d.directory) != nil || m.Reconcile() != nil {
|
||||
return blocked()
|
||||
}
|
||||
requested, _ := d.lookup(resourceStrategyIDsEnv)
|
||||
policy, e := parseResourceStrategyIDs(requested)
|
||||
if e != nil || s.selectPolicy(policy) != nil {
|
||||
return blocked()
|
||||
}
|
||||
originalGuard := s.guard
|
||||
s.guard = func() error {
|
||||
if resourceApplyMode(config, d) != nil || resourceSingletonGuard(lock, d.directory) != nil {
|
||||
|
||||
@@ -69,6 +69,14 @@ func TestResourceApplyRender(t *testing.T) {
|
||||
// Uses the production snapshot factory, private HMAC envelopes and shared
|
||||
// transaction engine; native/HTTP fixtures perform no network operations.
|
||||
func nativeApplyFixture(t *testing.T) (*managedTransaction, *resourceApplySnapshot, string, string) {
|
||||
return nativeApplyAlternativesFixture(t, false)
|
||||
}
|
||||
|
||||
func nativeApplyAlternativesFixture(t *testing.T, alternatives bool) (*managedTransaction, *resourceApplySnapshot, string, string) {
|
||||
return nativeApplyProfilesFixture(t, alternatives, false)
|
||||
}
|
||||
|
||||
func nativeApplyProfilesFixture(t *testing.T, alternatives, sameArgs bool) (*managedTransaction, *resourceApplySnapshot, string, string) {
|
||||
t.Helper()
|
||||
root, _, _ := qualificationBinaryFixture(t)
|
||||
dir := t.TempDir()
|
||||
@@ -91,6 +99,15 @@ func nativeApplyFixture(t *testing.T) (*managedTransaction, *resourceApplySnapsh
|
||||
}
|
||||
_, _, registry, _, _ := planFixture(t)
|
||||
registry.Strategies = registry.Strategies[:1]
|
||||
if alternatives {
|
||||
other := registry.Strategies[0]
|
||||
other.ID = "zz-alternative"
|
||||
other.Args = "--lua-desync=split:pos=2"
|
||||
if sameArgs {
|
||||
other.Args = registry.Strategies[0].Args
|
||||
}
|
||||
registry.Strategies = append(registry.Strategies, other)
|
||||
}
|
||||
b, _ := json.Marshal(registry)
|
||||
if e := store.importDefinitions(b); e != nil {
|
||||
t.Fatal(e)
|
||||
@@ -115,6 +132,13 @@ func nativeApplyFixture(t *testing.T) (*managedTransaction, *resourceApplySnapsh
|
||||
}
|
||||
}
|
||||
matrix := qualificationMatrix{Version: 1, Registry: planDigest(registry), Runs: []qualificationRun{run}}
|
||||
if alternatives {
|
||||
other := run
|
||||
other.ID, other.Strategy = "run-alternative", registry.Strategies[1].ID
|
||||
other.Definition = planDigest(registry.Strategies[1])
|
||||
other.Attestation.Definition = other.Definition
|
||||
matrix.Runs = append(matrix.Runs, other)
|
||||
}
|
||||
if e := store.saveCompleted(completedQualification{matrix: matrix, seal: planDigest(matrix)}); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
@@ -456,11 +480,20 @@ func TestResourceApplyRefusesActiveLegacy(t *testing.T) {
|
||||
db := testStore(t)
|
||||
id := managedCandidateFixture(t, db, "frontend", "www.youtube.com", "--lua-desync=multisplit", true)
|
||||
m.resource = nil
|
||||
if e := m.Apply(db, []int64{id}, GooglevideoScopeExact); e != nil { t.Fatal(e) }
|
||||
before, _ := m.read("config"); journal, _ := m.read(managedJournalName)
|
||||
if e := m.Apply(db, []int64{id}, GooglevideoScopeExact); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
before, _ := m.read("config")
|
||||
journal, _ := m.read(managedJournalName)
|
||||
m.resource = s
|
||||
reloads := 0; m.Reload = func() error { reloads++; return nil }
|
||||
if e := m.applyPolicy(s.rendered, db, nil, &s.ref); e == nil || reloads != 0 { t.Fatal("active legacy maintenance authorized", e) }
|
||||
after, _ := m.read("config"); nextJournal, _ := m.read(managedJournalName)
|
||||
if !bytes.Equal(before, after) || !bytes.Equal(journal, nextJournal) { t.Fatal("legacy state changed") }
|
||||
reloads := 0
|
||||
m.Reload = func() error { reloads++; return nil }
|
||||
if e := m.applyPolicy(s.rendered, db, nil, &s.ref); e == nil || reloads != 0 {
|
||||
t.Fatal("active legacy maintenance authorized", e)
|
||||
}
|
||||
after, _ := m.read("config")
|
||||
nextJournal, _ := m.read(managedJournalName)
|
||||
if !bytes.Equal(before, after) || !bytes.Equal(journal, nextJournal) {
|
||||
t.Fatal("legacy state changed")
|
||||
}
|
||||
}
|
||||
@@ -210,14 +210,18 @@ type runtimeAssignment struct {
|
||||
}
|
||||
type resourcePlan struct {
|
||||
ProposalOnly bool `json:"proposal_only"`
|
||||
Policy string `json:"strategy_ids,omitempty"`
|
||||
Evidence string `json:"evidence"`
|
||||
Domains []domainCompatibility `json:"domains"`
|
||||
Assignments []runtimeAssignment `json:"assignments"`
|
||||
Unassigned []string `json:"unassigned"`
|
||||
}
|
||||
|
||||
func deriveResourcePlan(source resourceMonitorSource, key []byte, registry strategyRegistry, matrix qualificationMatrix, now time.Time) resourcePlan {
|
||||
func deriveResourcePlan(source resourceMonitorSource, key []byte, registry strategyRegistry, matrix qualificationMatrix, now time.Time, policy ...string) resourcePlan {
|
||||
p := resourcePlan{ProposalOnly: true, Evidence: "unknown", Domains: []domainCompatibility{}, Assignments: []runtimeAssignment{}, Unassigned: []string{}}
|
||||
if len(policy) != 0 {
|
||||
p.Policy = policy[0]
|
||||
}
|
||||
hosts := map[string][]qualificationCheck{}
|
||||
for _, s := range source.config.Services {
|
||||
for _, r := range s.Resources {
|
||||
@@ -279,6 +283,9 @@ func deriveResourcePlan(source resourceMonitorSource, key []byte, registry strat
|
||||
profiles := map[string]runtimeAssignment{}
|
||||
coverage := map[string]map[string]bool{}
|
||||
for _, d := range registry.Strategies {
|
||||
if len(policy) != 0 && policy[0] != "" && !strings.Contains(","+policy[0]+",", ","+d.ID+",") {
|
||||
continue
|
||||
}
|
||||
profile := planDigest(struct {
|
||||
Args string
|
||||
Selectors strategySelectors
|
||||
|
||||
@@ -0,0 +1,278 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestResourceStrategyIDs(t *testing.T) {
|
||||
for _, value := range []string{"a,a", "a,", " a", "../a", "a\n", strings.Repeat("a,", 32) + "b", strings.Repeat("a", 4097)} {
|
||||
if _, e := parseResourceStrategyIDs(value); e == nil {
|
||||
t.Fatalf("accepted %q", value)
|
||||
}
|
||||
}
|
||||
if got, e := parseResourceStrategyIDs("b,a"); e != nil || got != "a,b" {
|
||||
t.Fatal(got, e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceSelectionCoverage(t *testing.T) {
|
||||
s, key, r, m, now := completeApplyFixture(t)
|
||||
// Equal profiles must not inherit coverage from an excluded candidate.
|
||||
r.Strategies[1].Args = r.Strategies[0].Args
|
||||
m.Registry = planDigest(r)
|
||||
m.Runs[1].Definition = planDigest(r.Strategies[1])
|
||||
m.Runs[1].Attestation.Definition = m.Runs[1].Definition
|
||||
defaultPlan, _, e := resourceApplyPlan(s, key, r, m, now)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
p, _, e := resourceApplyPlan(s, key, r, m, now, "s1,s2,s3")
|
||||
if e != nil || p.Policy != "s1,s2,s3" || len(p.Assignments) != 2 {
|
||||
t.Fatal(p, e)
|
||||
}
|
||||
if !reflect.DeepEqual(p.Domains, defaultPlan.Domains) {
|
||||
t.Fatal("hidden alternatives")
|
||||
}
|
||||
for _, policy := range []string{"s1", "s2,s3", "s1,s3", "unknown", "s2,s1,s3"} {
|
||||
if _, _, e := resourceApplyPlan(s, key, r, m, now, policy); e == nil {
|
||||
t.Fatal("invalid coverage/policy", policy)
|
||||
}
|
||||
}
|
||||
for _, kind := range []string{"m3", "fingerprint", "unknown", "tls"} {
|
||||
bad := cloneMatrix(m)
|
||||
switch kind {
|
||||
case "m3":
|
||||
bad.Runs[0].Checks[0].Attempts = 2
|
||||
case "fingerprint":
|
||||
bad.Runs[0].Checks[0].Fingerprint = strings.Repeat("a", 64)
|
||||
case "unknown":
|
||||
bad.Runs[0].Attestation.Joined = false
|
||||
case "tls":
|
||||
r.Strategies[0].Selectors.TLS = "tls12"
|
||||
bad.Registry = planDigest(r)
|
||||
bad.Runs[0].Definition = planDigest(r.Strategies[0])
|
||||
bad.Runs[0].Attestation.Definition = bad.Runs[0].Definition
|
||||
}
|
||||
if _, _, e := resourceApplyPlan(s, key, r, bad, now, "s1,s2,s3"); e == nil {
|
||||
t.Fatal("invalid selected evidence", kind)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceGenericRotation(t *testing.T) {
|
||||
m, s, config, root := nativeApplyAlternativesFixture(t, true)
|
||||
proofs := map[string][]byte{}
|
||||
for _, name := range []string{planRegistryName, planMatrixName} {
|
||||
b, e := os.ReadFile(filepath.Join(m.Dir, name))
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
proofs[name] = b
|
||||
}
|
||||
reloads := 0
|
||||
m.Reload = func() error { reloads++; return nil }
|
||||
apply := func(policy string) {
|
||||
t.Helper()
|
||||
if e := s.selectPolicy(policy); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e := m.applyPolicy(s.rendered, nil, nil, &s.ref); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
apply("")
|
||||
a, _ := m.load()
|
||||
old, _ := m.read("config")
|
||||
apply("zz-alternative")
|
||||
b, _ := m.load()
|
||||
if *a.Ref == *b.Ref || a.Hash == b.Hash || b.Ref.Policy != "zz-alternative" {
|
||||
t.Fatal(a, b)
|
||||
}
|
||||
// Reopen with a different startup request: identity is the stored policy.
|
||||
d := managedDeps{directory: m.Dir, lookup: func(k string) (string, bool) {
|
||||
if k == resourceStrategyIDsEnv {
|
||||
return "unknown", true
|
||||
}
|
||||
return resourceCheckLookup(k)
|
||||
}}
|
||||
reopened, e := openResourceApplySnapshotAt(config, d, root)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer reopened.close()
|
||||
m.resource = reopened
|
||||
if e := m.Reconcile(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
m.resource = s
|
||||
s.now = func() time.Time { return time.Now().Add(25 * time.Hour) }
|
||||
apply("zz-alternative") // historical replay, no reload
|
||||
if reloads != 2 {
|
||||
t.Fatal(reloads)
|
||||
}
|
||||
if e := s.selectPolicy(""); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e := m.applyPolicy(s.rendered, nil, nil, &s.ref); e == nil {
|
||||
t.Fatal("stale rotation")
|
||||
}
|
||||
if reloads != 2 {
|
||||
t.Fatal(reloads)
|
||||
}
|
||||
s.now = time.Now
|
||||
apply("")
|
||||
got, _ := m.read("config")
|
||||
if !bytes.Equal(old, got) || reloads != 3 {
|
||||
t.Fatal("A not restored", reloads)
|
||||
}
|
||||
for _, policy := range []string{"unknown", "zz-alternative,zz-alternative", "zz-alternative,"} {
|
||||
if s.selectPolicy(policy) == nil {
|
||||
t.Fatal("invalid policy", policy)
|
||||
}
|
||||
}
|
||||
bad := *a.Ref
|
||||
bad.Policy = "zz-alternative"
|
||||
if s.validate(&bad, bad.Config) == nil {
|
||||
t.Fatal("policy forged render")
|
||||
}
|
||||
bad = managedConfigRef{Kind: "baseline", Policy: "zz-alternative"}
|
||||
if validManagedConfigRef(&bad, nil, "", managedHash([]byte(managedBaseline))) {
|
||||
t.Fatal("baseline policy")
|
||||
}
|
||||
for name, before := range proofs {
|
||||
after, e := os.ReadFile(filepath.Join(m.Dir, name))
|
||||
if e != nil || !bytes.Equal(before, after) {
|
||||
t.Fatal("immutable proof changed", name, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceSameBytesPolicyRequiresFreshProof(t *testing.T) {
|
||||
m, s, _, _ := nativeApplyProfilesFixture(t, true, true)
|
||||
policyA := s.registry.Strategies[0].ID
|
||||
if e := s.selectPolicy(policyA); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e := m.applyPolicy(s.rendered, nil, nil, &s.ref); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
oldRef := s.ref
|
||||
oldBytes := append([]byte(nil), s.rendered...)
|
||||
before := map[string][]byte{}
|
||||
for _, name := range []string{m.Name, managedJournalName, managedBackupName, planRegistryName, planMatrixName} {
|
||||
b, e := os.ReadFile(filepath.Join(m.Dir, name))
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
before[name] = b
|
||||
}
|
||||
reloads, checkpoints := 0, 0
|
||||
m.Reload = func() error { reloads++; return nil }
|
||||
m.fault = func(string) error { checkpoints++; return nil }
|
||||
assertUnchanged := func() {
|
||||
t.Helper()
|
||||
if reloads != 0 || checkpoints != 0 {
|
||||
t.Fatal("request reached staging/reload", checkpoints, reloads)
|
||||
}
|
||||
for name, want := range before {
|
||||
got, e := os.ReadFile(filepath.Join(m.Dir, name))
|
||||
if e != nil || !bytes.Equal(got, want) {
|
||||
t.Fatal("request mutated deployment/proof", name, e)
|
||||
}
|
||||
}
|
||||
j, e := m.load()
|
||||
if e != nil || j.State != "committed" || j.Ref == nil || *j.Ref != oldRef || j.PendingRef != nil {
|
||||
t.Fatal("committed policy changed", j, e)
|
||||
}
|
||||
}
|
||||
aged := time.Now().Add(25 * time.Hour)
|
||||
s.now = func() time.Time { return aged }
|
||||
if e := s.selectPolicy("zz-alternative"); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if s.ref.Policy == oldRef.Policy || s.ref.Config != oldRef.Config || !bytes.Equal(s.rendered, oldBytes) {
|
||||
t.Fatal("fixture must change policy but preserve rendered bytes", oldRef, s.ref)
|
||||
}
|
||||
if e := s.validate(&s.ref, s.ref.Config); e != nil {
|
||||
t.Fatal("alternative lacks valid historical proof", e)
|
||||
}
|
||||
if e := m.applyPolicy(s.rendered, nil, nil, &s.ref); !errors.Is(e, errResourcePlan) {
|
||||
t.Fatal("same-byte rotation bypassed freshness", e)
|
||||
}
|
||||
assertUnchanged()
|
||||
unknown := s.ref
|
||||
unknown.Policy = "unknown"
|
||||
if e := m.validateConfigRef(nil, &unknown, nil, unknown.Config, ""); e == nil {
|
||||
t.Fatal("unknown policy ref authorized")
|
||||
}
|
||||
assertUnchanged()
|
||||
if e := s.selectPolicy(policyA); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if s.fresh() == nil {
|
||||
t.Fatal("fixture proof must be expired")
|
||||
}
|
||||
if e := m.applyPolicy(s.rendered, nil, nil, &s.ref); e != nil {
|
||||
t.Fatal("identical committed policy replay rejected after expiry", e)
|
||||
}
|
||||
assertUnchanged()
|
||||
}
|
||||
|
||||
func TestResourceRotationRecovery(t *testing.T) {
|
||||
for _, point := range []string{"staged", "backup", "prepared", "replaced", "reloaded", "healthy", "health-failure", "rollback-health"} {
|
||||
t.Run(point, func(t *testing.T) {
|
||||
m, s, _, _ := nativeApplyAlternativesFixture(t, true)
|
||||
if e := s.selectPolicy("zz-alternative"); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e := m.applyPolicy(s.rendered, nil, nil, &s.ref); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
old, _ := m.load()
|
||||
oldBytes, _ := m.read("config")
|
||||
if e := s.selectPolicy(""); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
m.fault = func(p string) error {
|
||||
if p == point {
|
||||
return errors.New("death")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if point == "health-failure" || point == "rollback-health" {
|
||||
m.Health = func() (string, error) { return "DEGRADED", nil }
|
||||
}
|
||||
if point == "rollback-health" {
|
||||
m.RollbackHealth = func() (string, error) { return "DEGRADED", nil }
|
||||
}
|
||||
if e := m.applyPolicy(s.rendered, nil, nil, &s.ref); e == nil {
|
||||
t.Fatal("fault ignored")
|
||||
}
|
||||
j, _ := m.load()
|
||||
if *j.Ref != *old.Ref {
|
||||
t.Fatal("old policy lost", j)
|
||||
}
|
||||
if point == "rollback-health" && j.State != "prepared" {
|
||||
t.Fatal(j)
|
||||
}
|
||||
m.fault = nil
|
||||
m.RollbackHealth = func() (string, error) { return "OK", nil }
|
||||
s.now = func() time.Time { return time.Now().Add(25 * time.Hour) }
|
||||
if e := m.Reconcile(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
j, _ = m.load()
|
||||
got, _ := m.read("config")
|
||||
if j.State != "committed" || *j.Ref != *old.Ref || !bytes.Equal(got, oldBytes) {
|
||||
t.Fatal("pending promoted", j)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user