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:
ZeusinaandGPT-6 Bot committed 2026-10-15 10:18:14 +03:00
1 parent 5d0be0288f
commit 8d43dd9363
10 files changed
+473 -31

No files matched your search

+37 -3
View File
@@ -195,9 +195,11 @@ The command refuses standalone/docker-exec callers: actual PID 1 environment,
entrypoint, namespace, direct-child executable/argv and held launch ACK are entrypoint, namespace, direct-child executable/argv and held launch ACK are
required, plus the existing controller singleton and no pre-existing NFQ worker. required, plus the existing controller singleton and no pre-existing NFQ worker.
First apply supports **disabled baseline → generic only**. Active legacy profiles First apply supports **disabled baseline → generic**. Explicit PID 1 startup
and different generic plan rotation are refused; no maintenance interruption is selection also permits generic → generic rotation among already verified
implemented. Every configured resource must require `both` TLS protocols and alternatives in the **same immutable authenticated registry/matrix/source**.
Active legacy profiles, changed source/catalogue/proofs and uninterrupted live
rotation remain unsupported. Every configured resource must require `both` TLS protocols and
every assigned exact host must have complete, candidate-bound passing proof for every assigned exact host must have complete, candidate-bound passing proof for
all its resources across all input labels. New apply requires completion within all its resources across all input labels. New apply requires completion within
24 hours, never in the future. Negative/unknown/unassigned hosts cannot authorize 24 hours, never in the future. Negative/unknown/unassigned hosts cannot authorize
@@ -219,6 +221,10 @@ The **existing** journal gains v4 independently tagged committed/pending refs
(`baseline`, `legacy`, `generic`). Generic refs exclude legacy IDs/scopes and bind (`baseline`, `legacy`, `generic`). Generic refs exclude legacy IDs/scopes and bind
exact authenticated registry/matrix envelope hashes, source fingerprint, exact authenticated registry/matrix envelope hashes, source fingerprint,
internally derived canonical assignment digest and complete config hash. internally derived canonical assignment digest and complete config hash.
Optional scalar `strategy_ids` retains the canonical selection policy on each
generic ref; missing/empty means the original default greedy policy. No policy
is permitted on baseline/legacy refs. This remains v4: older strict-decoding
engines reject the new field, so downgrade is blocked, not silently reinterpreted.
Unknown versions/tags, mixed refs and unresolved historical reconstruction block Unknown versions/tags, mixed refs and unresolved historical reconstruction block
without adopting or erasing foreign state; v1–v3 legacy behavior remains. without adopting or erasing foreign state; v1–v3 legacy behavior remains.
Staging/backup/prepared/replace/fixed restart/health/commit reuse the same engine. Staging/backup/prepared/replace/fixed restart/health/commit reuse the same engine.
@@ -239,6 +245,34 @@ checks before rollback; PID 1 bounds the owned session and retains prepared stat
on unsafe/unfinished cleanup. `ActiveCandidates` refuses generic deployments; on unsafe/unfinished cleanup. `ActiveCandidates` refuses generic deployments;
managed/resource status validates generic refs without inventing legacy groups. managed/resource status validates generic refs without inventing legacy groups.
### Explicit startup selection (local implementation, remote acceptance pending)
Set `ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS=s1,s2` with
`ZAPRET_AUTOTUNE_RESOURCE_CONFIG` and `ZAPRET_AUTOTUNE_RESOURCE_APPLY=1` on a
fresh container PID 1. Empty retains default greedy selection. The bounded list
accepts at most 32 unique registry IDs, canonicalized in lexical order; malformed,
duplicate, unknown or incomplete-coverage requests cannot authorize a transaction.
Eligibility is filtered **before** equal-args/selectors coverage merging; excluded
candidates cannot lend proof to a selected profile. Compatibility evidence and
all alternatives remain visible; the policy is not proof or a new qualification.
No registry/matrix files are written by rotation. Exact hosts, both TLS versions,
all required cells/M=3, native/source integrity and the 24-hour freshness gate
remain required for every new selection. Identical committed selection replays
without reload even after expiry. Distinct redundant allowlists are distinct
policies and may create a transaction even if rendered bytes coincide.
PID 1 forwards the setting in environment only; the apply child authenticates it
against PID 1's original environment, not CLI arguments. Syntax is validated
even with APPLY=0; nonempty policy requires generic resource config. With APPLY=0
selection is ignored for reconciliation/monitoring (registry membership is not
resolved): committed and pending refs reconstruct independently from their own
stored policies and historical completion horizon, never the requested setting.
Prepared recovery restores the old generic bytes **and policy**, never promotes
the pending side; failed rollback health retains prepared state for repair.
Startup still allows just one attempt per PID 1; external restart policies reset
that authorization. There is no monitor autotuning, new qualification, proof
import, snapshot overwrite or uninterrupted maintenance in this slice.
Local root fixture tests cover the production snapshot factory and shared engine; Local root fixture tests cover the production snapshot factory and shared engine;
independent local review and deterministic 180-second retry-window fixtures passed. independent local review and deterministic 180-second retry-window fixtures passed.
The bounded remote gates below now cover guarded apply/replay, strict rollback, The bounded remote gates below now cover guarded apply/replay, strict rollback,
+5
View File
@@ -65,6 +65,11 @@ func runWithPlayer(args []string, stdout, stderr io.Writer, player playerRunner)
} }
if len(args) == 1 && args[0] == "target-config-check" { if len(args) == 1 && args[0] == "target-config-check" {
path, _ := os.LookupEnv("ZAPRET_AUTOTUNE_RESOURCE_CONFIG") 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 == "" { if path == "" {
path, _ = os.LookupEnv("ZAPRET_AUTOTUNE_QUALIFY_CONFIG") path, _ = os.LookupEnv("ZAPRET_AUTOTUNE_QUALIFY_CONFIG")
} }
+5 -4
View File
@@ -381,7 +381,7 @@ func (m *managedTransaction) applyPolicy(b []byte, db *sql.DB, requested *Google
return fmt.Errorf("generic journal cannot authorize legacy apply") return fmt.Errorf("generic journal cannot authorize legacy apply")
} }
if generic != nil { 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 return errResourcePlan
} }
if j.Version == 4 && j.Ref.Kind == "generic" { 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 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") return fmt.Errorf("generic apply requires disabled baseline")
} }
if m.resource.fresh() != nil { if m.resource.fresh() != nil {
@@ -485,8 +484,10 @@ func (m *managedTransaction) applyPolicy(b []byte, db *sql.DB, requested *Google
} }
j.Generation++ j.Generation++
if generic != nil { if generic != nil {
j.Version = 4 if j.Version != 4 {
j.Ref = &managedConfigRef{Kind: "baseline"} j.Ref = &managedConfigRef{Kind: "baseline"}
}
j.Version = 4
j.Scope, j.PendingScope = "", "" j.Scope, j.PendingScope = "", ""
j.PendingRef = generic j.PendingRef = generic
} else if requested != nil || j.Version == 3 { } else if requested != nil || j.Version == 3 {
+88 -16
View File
@@ -9,6 +9,7 @@ import (
"net" "net"
"os" "os"
"os/signal" "os/signal"
"sort"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
@@ -23,6 +24,32 @@ type managedConfigRef struct {
Source string `json:"source_fingerprint,omitempty"` Source string `json:"source_fingerprint,omitempty"`
Plan string `json:"plan_digest,omitempty"` Plan string `json:"plan_digest,omitempty"`
Config string `json:"config_hash,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 { 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 { switch r.Kind {
case "generic": 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": 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 return false
} }
@@ -68,8 +96,27 @@ type resourceApplySnapshot struct {
// Historical reconstruction uses the proof's original completion horizon. Only // Historical reconstruction uses the proof's original completion horizon. Only
// new apply checks wall-clock freshness; expiry cannot erase committed identity. // 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) { func resourceApplyPlan(source resourceMonitorSource, key []byte, registry strategyRegistry, matrix qualificationMatrix, now time.Time, policies ...string) (resourcePlan, []byte, error) {
p := deriveResourcePlan(source, key, registry, matrix, now) 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 { if p.Evidence != "authenticated" || len(p.Unassigned) != 0 || len(p.Assignments) == 0 {
return p, nil, errResourcePlan return p, nil, errResourcePlan
} }
@@ -101,23 +148,45 @@ func resourceApplyPlan(source resourceMonitorSource, key []byte, registry strate
} }
func (s *resourceApplySnapshot) fresh() error { 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 { if e != nil || planDigest(p.Assignments) != s.ref.Plan || managedHash(b) != s.ref.Config {
return errResourcePlan return errResourcePlan
} }
return s.guard() return s.guard()
} }
// ponytail: one immutable qualification batch, baseline-only activation and // Each journal side reconstructs independently inside the same immutable boundary.
// identical-request resume. Rotation/active maintenance need versioned snapshots
// and job-bound rollback, not overwriting the held registry or matrix.
func (s *resourceApplySnapshot) validate(ref *managedConfigRef, hash string) error { 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 errResourcePlan
} }
return s.guard() 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 // No imported proposal or version probe. Keep private proof/source/native files
// held across staging, health and commit and bind every selected candidate. // held across staging, health and commit and bind every selected candidate.
func openResourceApplySnapshot(config string, d managedDeps) (*resourceApplySnapshot, error) { func openResourceApplySnapshot(config string, d managedDeps) (*resourceApplySnapshot, error) {
@@ -193,13 +262,7 @@ func openResourceApplySnapshotAt(config string, d managedDeps, root string) (*re
} }
lua[path] = f lua[path] = f
} }
var horizon int64 p, rendered, e := s.historicalPlan("")
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))
if e != nil { if e != nil {
return fail() 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"] != "" { 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 return errResourcePlan
} }
requested, _ := d.lookup(resourceStrategyIDsEnv)
if _, e := parseResourceStrategyIDs(requested); e != nil || requested != values[resourceStrategyIDsEnv] {
return errResourcePlan
}
for _, ns := range []string{"pid", "net"} { for _, ns := range []string{"pid", "net"} {
a, e := os.Readlink("/proc/1/ns/" + ns) a, e := os.Readlink("/proc/1/ns/" + ns)
b, f := os.Readlink("/proc/self/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 { if resourceSingletonGuard(lock, d.directory) != nil || m.Reconcile() != nil {
return blocked() return blocked()
} }
requested, _ := d.lookup(resourceStrategyIDsEnv)
policy, e := parseResourceStrategyIDs(requested)
if e != nil || s.selectPolicy(policy) != nil {
return blocked()
}
originalGuard := s.guard originalGuard := s.guard
s.guard = func() error { s.guard = func() error {
if resourceApplyMode(config, d) != nil || resourceSingletonGuard(lock, d.directory) != nil { if resourceApplyMode(config, d) != nil || resourceSingletonGuard(lock, d.directory) != nil {
+39 -6
View File
@@ -69,6 +69,14 @@ func TestResourceApplyRender(t *testing.T) {
// Uses the production snapshot factory, private HMAC envelopes and shared // Uses the production snapshot factory, private HMAC envelopes and shared
// transaction engine; native/HTTP fixtures perform no network operations. // transaction engine; native/HTTP fixtures perform no network operations.
func nativeApplyFixture(t *testing.T) (*managedTransaction, *resourceApplySnapshot, string, string) { 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() t.Helper()
root, _, _ := qualificationBinaryFixture(t) root, _, _ := qualificationBinaryFixture(t)
dir := t.TempDir() dir := t.TempDir()
@@ -91,6 +99,15 @@ func nativeApplyFixture(t *testing.T) (*managedTransaction, *resourceApplySnapsh
} }
_, _, registry, _, _ := planFixture(t) _, _, registry, _, _ := planFixture(t)
registry.Strategies = registry.Strategies[:1] 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) b, _ := json.Marshal(registry)
if e := store.importDefinitions(b); e != nil { if e := store.importDefinitions(b); e != nil {
t.Fatal(e) t.Fatal(e)
@@ -115,6 +132,13 @@ func nativeApplyFixture(t *testing.T) (*managedTransaction, *resourceApplySnapsh
} }
} }
matrix := qualificationMatrix{Version: 1, Registry: planDigest(registry), Runs: []qualificationRun{run}} 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 { if e := store.saveCompleted(completedQualification{matrix: matrix, seal: planDigest(matrix)}); e != nil {
t.Fatal(e) t.Fatal(e)
} }
@@ -456,11 +480,20 @@ func TestResourceApplyRefusesActiveLegacy(t *testing.T) {
db := testStore(t) db := testStore(t)
id := managedCandidateFixture(t, db, "frontend", "www.youtube.com", "--lua-desync=multisplit", true) id := managedCandidateFixture(t, db, "frontend", "www.youtube.com", "--lua-desync=multisplit", true)
m.resource = nil m.resource = nil
if e := m.Apply(db, []int64{id}, GooglevideoScopeExact); e != nil { t.Fatal(e) } if e := m.Apply(db, []int64{id}, GooglevideoScopeExact); e != nil {
before, _ := m.read("config"); journal, _ := m.read(managedJournalName) t.Fatal(e)
}
before, _ := m.read("config")
journal, _ := m.read(managedJournalName)
m.resource = s m.resource = s
reloads := 0; m.Reload = func() error { reloads++; return nil } reloads := 0
if e := m.applyPolicy(s.rendered, db, nil, &s.ref); e == nil || reloads != 0 { t.Fatal("active legacy maintenance authorized", e) } m.Reload = func() error { reloads++; return nil }
after, _ := m.read("config"); nextJournal, _ := m.read(managedJournalName) if e := m.applyPolicy(s.rendered, db, nil, &s.ref); e == nil || reloads != 0 {
if !bytes.Equal(before, after) || !bytes.Equal(journal, nextJournal) { t.Fatal("legacy state changed") } 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")
}
} }
+8 -1
View File
@@ -210,14 +210,18 @@ type runtimeAssignment struct {
} }
type resourcePlan struct { type resourcePlan struct {
ProposalOnly bool `json:"proposal_only"` ProposalOnly bool `json:"proposal_only"`
Policy string `json:"strategy_ids,omitempty"`
Evidence string `json:"evidence"` Evidence string `json:"evidence"`
Domains []domainCompatibility `json:"domains"` Domains []domainCompatibility `json:"domains"`
Assignments []runtimeAssignment `json:"assignments"` Assignments []runtimeAssignment `json:"assignments"`
Unassigned []string `json:"unassigned"` 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{}} p := resourcePlan{ProposalOnly: true, Evidence: "unknown", Domains: []domainCompatibility{}, Assignments: []runtimeAssignment{}, Unassigned: []string{}}
if len(policy) != 0 {
p.Policy = policy[0]
}
hosts := map[string][]qualificationCheck{} hosts := map[string][]qualificationCheck{}
for _, s := range source.config.Services { for _, s := range source.config.Services {
for _, r := range s.Resources { for _, r := range s.Resources {
@@ -279,6 +283,9 @@ func deriveResourcePlan(source resourceMonitorSource, key []byte, registry strat
profiles := map[string]runtimeAssignment{} profiles := map[string]runtimeAssignment{}
coverage := map[string]map[string]bool{} coverage := map[string]map[string]bool{}
for _, d := range registry.Strategies { for _, d := range registry.Strategies {
if len(policy) != 0 && policy[0] != "" && !strings.Contains(","+policy[0]+",", ","+d.ID+",") {
continue
}
profile := planDigest(struct { profile := planDigest(struct {
Args string Args string
Selectors strategySelectors 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)
}
})
}
}
+9 -1
View File
@@ -34,7 +34,8 @@ with tempfile.TemporaryDirectory(prefix='target-config-') as tmp:
SOCKS_PORT='1080', SOCKS_PORT='1080',
ZAPRET_AUTOTUNE_SOCKS_PROXY='socks5h://127.0.0.1:1080') ZAPRET_AUTOTUNE_SOCKS_PROXY='socks5h://127.0.0.1:1080')
for name in ('ZAPRET_AUTOTUNE_RESOURCE_APPLY', 'ZAPRET_AUTOTUNE_RESOURCE_CONFIG', for name in ('ZAPRET_AUTOTUNE_RESOURCE_APPLY', 'ZAPRET_AUTOTUNE_RESOURCE_CONFIG',
'ZAPRET_AUTOTUNE_QUALIFY_CONFIG', 'ZAPRET_AUTOTUNE_STRATEGY_CONFIG'): 'ZAPRET_AUTOTUNE_QUALIFY_CONFIG', 'ZAPRET_AUTOTUNE_STRATEGY_CONFIG',
'ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS'):
base.pop(name, None) base.pop(name, None)
for dns, valid in (('', True), ('8.8.8.8', True), ('2001:4860:4860::8888', True), for dns, valid in (('', True), ('8.8.8.8', True), ('2001:4860:4860::8888', True),
('example.org', False), ('8.8.8.8\n', False), ('-d 8.8.8.8', False)): ('example.org', False), ('8.8.8.8\n', False), ('-d 8.8.8.8', False)):
@@ -70,6 +71,11 @@ with tempfile.TemporaryDirectory(prefix='target-config-') as tmp:
if not valid: if not valid:
assert not list(private.iterdir()) assert not list(private.iterdir())
for overrides, valid in ( for overrides, valid in (
({'ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS': 's1'}, False),
({'ZAPRET_AUTOTUNE_RESOURCE_CONFIG': '/private/resources.json',
'ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS': 's2,s1'}, True),
({'ZAPRET_AUTOTUNE_RESOURCE_CONFIG': '/private/resources.json',
'ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS': 's1,s1'}, False),
({'ZAPRET_AUTOTUNE_RESOURCE_APPLY': '1'}, False), ({'ZAPRET_AUTOTUNE_RESOURCE_APPLY': '1'}, False),
({'ZAPRET_AUTOTUNE_RESOURCE_APPLY': 'yes'}, False), ({'ZAPRET_AUTOTUNE_RESOURCE_APPLY': 'yes'}, False),
({'ZAPRET_AUTOTUNE_RESOURCE_APPLY': '1', 'ZAPRET_AUTOTUNE_RESOURCE_CONFIG': '/private/resources.json'}, True), ({'ZAPRET_AUTOTUNE_RESOURCE_APPLY': '1', 'ZAPRET_AUTOTUNE_RESOURCE_CONFIG': '/private/resources.json'}, True),
@@ -128,11 +134,13 @@ with tempfile.TemporaryDirectory(prefix='target-config-') as tmp:
result = subprocess.run(['docker', 'compose', '--env-file', '/dev/null', '-f', result = subprocess.run(['docker', 'compose', '--env-file', '/dev/null', '-f',
str(ROOT / 'deploy/managed-target/compose.yaml'), 'config', '--format', 'json'], str(ROOT / 'deploy/managed-target/compose.yaml'), 'config', '--format', 'json'],
env=dict(compose_env, ZAPRET_AUTOTUNE_RESOURCE_APPLY='1', env=dict(compose_env, ZAPRET_AUTOTUNE_RESOURCE_APPLY='1',
ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS='s2,s1',
ZAPRET_AUTOTUNE_RESOURCE_CONFIG='/private/resources.json', ZAPRET_AUTOTUNE_RESOURCE_CONFIG='/private/resources.json',
ZAPRET_AUTOTUNE_STOP_GRACE_PERIOD='90s'), capture_output=True, timeout=10) ZAPRET_AUTOTUNE_STOP_GRACE_PERIOD='90s'), capture_output=True, timeout=10)
assert result.returncode == 0, 'Generic Compose config failed (output suppressed)' assert result.returncode == 0, 'Generic Compose config failed (output suppressed)'
target = json.loads(result.stdout)['services']['target'] target = json.loads(result.stdout)['services']['target']
assert target['environment']['ZAPRET_AUTOTUNE_RESOURCE_APPLY'] == '1' assert target['environment']['ZAPRET_AUTOTUNE_RESOURCE_APPLY'] == '1'
assert target['environment']['ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS'] == 's2,s1'
assert target['environment']['ZAPRET_AUTOTUNE_RESOURCE_CONFIG'] == '/private/resources.json' assert target['environment']['ZAPRET_AUTOTUNE_RESOURCE_CONFIG'] == '/private/resources.json'
assert target['stop_grace_period'] == '1m30s' assert target['stop_grace_period'] == '1m30s'
# Compose must not inject Google bootstrap into a custom-provider request. # Compose must not inject Google bootstrap into a custom-provider request.
@@ -352,6 +352,7 @@ elif command == 'monitor':
elif command == 'resource-apply': elif command == 'resource-apply':
assert sys.argv[3:] == ['--resource-config', '/operator/resources.json'] assert sys.argv[3:] == ['--resource-config', '/operator/resources.json']
assert os.environ['ZAPRET_AUTOTUNE_RESOURCE_APPLY'] == '1' assert os.environ['ZAPRET_AUTOTUNE_RESOURCE_APPLY'] == '1'
assert os.environ['ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS'] == 's2,s1'
assert os.getsid(0) == os.getpid(), 'apply lacks owned session' assert os.getsid(0) == os.getpid(), 'apply lacks owned session'
assert (root / 'private' / 'monitor-ready').read_text() == str(os.getpid()) + '\n' assert (root / 'private' / 'monitor-ready').read_text() == str(os.getpid()) + '\n'
events = (root / 'events').read_text().splitlines() events = (root / 'events').read_text().splitlines()
@@ -458,8 +459,10 @@ def run(scenario, expected, dns='', mode='native'):
env.pop('ZAPRET_AUTOTUNE_QUALIFY_CONFIG', None) env.pop('ZAPRET_AUTOTUNE_QUALIFY_CONFIG', None)
env.pop('ZAPRET_AUTOTUNE_STRATEGY_CONFIG', None) env.pop('ZAPRET_AUTOTUNE_STRATEGY_CONFIG', None)
env.pop('ZAPRET_AUTOTUNE_RESOURCE_APPLY', None) env.pop('ZAPRET_AUTOTUNE_RESOURCE_APPLY', None)
env.pop('ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS', None)
if scenario.startswith('generic-apply-') and scenario != 'generic-apply-disabled': if scenario.startswith('generic-apply-') and scenario != 'generic-apply-disabled':
env['ZAPRET_AUTOTUNE_RESOURCE_APPLY'] = '1' env['ZAPRET_AUTOTUNE_RESOURCE_APPLY'] = '1'
env['ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS'] = 's2,s1'
if scenario.startswith('qualify-'): if scenario.startswith('qualify-'):
env['ZAPRET_AUTOTUNE_QUALIFY_CONFIG'] = '/operator/resources.json' env['ZAPRET_AUTOTUNE_QUALIFY_CONFIG'] = '/operator/resources.json'
if scenario != 'qualify-missing': if scenario != 'qualify-missing':
+1
View File
@@ -20,6 +20,7 @@ services:
ZAPRET_AUTOTUNE_YOUTUBE_SERVICE: ${ZAPRET_AUTOTUNE_YOUTUBE_SERVICE-0} ZAPRET_AUTOTUNE_YOUTUBE_SERVICE: ${ZAPRET_AUTOTUNE_YOUTUBE_SERVICE-0}
ZAPRET_AUTOTUNE_RESOURCE_CONFIG: ${ZAPRET_AUTOTUNE_RESOURCE_CONFIG-} ZAPRET_AUTOTUNE_RESOURCE_CONFIG: ${ZAPRET_AUTOTUNE_RESOURCE_CONFIG-}
ZAPRET_AUTOTUNE_RESOURCE_APPLY: ${ZAPRET_AUTOTUNE_RESOURCE_APPLY-0} ZAPRET_AUTOTUNE_RESOURCE_APPLY: ${ZAPRET_AUTOTUNE_RESOURCE_APPLY-0}
ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS: ${ZAPRET_AUTOTUNE_RESOURCE_STRATEGY_IDS-}
# INTRUSIVE isolated startup batch. Never route working traffic here. # INTRUSIVE isolated startup batch. Never route working traffic here.
ZAPRET_AUTOTUNE_QUALIFY_CONFIG: ${ZAPRET_AUTOTUNE_QUALIFY_CONFIG-} ZAPRET_AUTOTUNE_QUALIFY_CONFIG: ${ZAPRET_AUTOTUNE_QUALIFY_CONFIG-}
ZAPRET_AUTOTUNE_STRATEGY_CONFIG: ${ZAPRET_AUTOTUNE_STRATEGY_CONFIG-} ZAPRET_AUTOTUNE_STRATEGY_CONFIG: ${ZAPRET_AUTOTUNE_STRATEGY_CONFIG-}