feat(apply): support staged strategy groups

Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-10 09:24:43 +03:00
1 parent 468532dc8f
commit 2d4e4878ae
9 files changed
+764 -59

No files matched your search

+27 -7
View File
@@ -1,6 +1,6 @@
# Fresh managed-test target contract (not deployed) # Fresh managed-test target contract (not deployed)
`managed init|status|apply` is exclusively for a **new dedicated container** `managed init|status|active-ids|apply` is exclusively for a **new dedicated container**
which owns its entire config. The existing/dev/production container must refuse which owns its entire config. The existing/dev/production container must refuse
these commands. A role env or CLI-created identity is not enough. Provisioning these commands. A role env or CLI-created identity is not enough. Provisioning
must supply all of the following before any managed command can run: must supply all of the following before any managed command can run:
@@ -36,14 +36,18 @@ fragment**, not this full-config adapter. No such integration is implemented.
# Only after fresh-target provisioning establishes the entire contract above: # Only after fresh-target provisioning establishes the entire contract above:
zapret-autotune managed init zapret-autotune managed init
zapret-autotune managed status zapret-autotune managed status
zapret-autotune managed apply --frontend-candidate 1 # bootstrap before discovery
zapret-autotune managed active-ids # JSON for supervisor; no health probe
zapret-autotune managed apply --frontend-candidate 1 --googlevideo-candidate 2 zapret-autotune managed apply --frontend-candidate 1 --googlevideo-candidate 2
``` ```
Init refuses nonempty directories except an exact private disabled baseline; Init refuses nonempty directories except an exact private disabled baseline;
it never overwrites/adopts another config. It creates identity, journal and it never overwrites/adopts another config. It creates identity, journal and
backup; repeated initialization, symlinks, hardlinks, public/unowned files and backup; repeated initialization, symlinks, hardlinks, public/unowned files and
unsafe ancestors are refused. Status reports committed generation/hash/groups, unsafe ancestors are refused. Status reports generation/hash/transaction state,
not config, secrets or live health; drift and pending transactions fail closed. validated active IDs/groups and unresolved groups, not config, secrets or live
health. Pending or unverifiable state reports unknown groups and no active IDs;
drift fails closed. `active-ids` fails closed on pending or unverifiable state.
Init/status do not reload or probe, but both verify runtime and listener ownership. Init/status do not reload or probe, but both verify runtime and listener ownership.
All managed commands verify that the expected IPv4 listener is owned by a running All managed commands verify that the expected IPv4 listener is owned by a running
@@ -55,8 +59,14 @@ but is never used inside the target. The standalone `health youtube` command
retains its separate generic loopback contract; managed activation is stricter. retains its separate generic loopback contract; managed activation is stricter.
Ambient proxies/direct fallback are disabled. Player API OK is **not playback**. Ambient proxies/direct fallback are disabled. Player API OK is **not playback**.
Apply requires distinct positive qualified frontend/exact-GGC SQLite candidate Apply accepts one or both frontend/exact-GGC SQLite candidate IDs, requiring
IDs, complete endpoint × TLS evidence and inert shell-safe arguments. It opens distinct positive IDs, at most one per group, complete endpoint × TLS evidence
and inert shell-safe arguments. The arguments are the full desired active set,
not patches: omitting a group removes it. Frontend-only bootstrap supports Player
API discovery before GGC scanning; GGC-only is also permitted. No missing profile
is invented, and missing groups remain unresolved. Player API OK never asserts
GGC/video playback or full-profile health. A supervisor can read `managed active-ids`
and explicitly retain the other group while replacing a failed one. It opens
the candidate DB read-only via `ZAPRET_AUTOTUNE_DB`; candidates are not active the candidate DB read-only via `ZAPRET_AUTOTUNE_DB`; candidates are not active
state. Bindings/inputs are checked before pending transaction reconciliation. state. Bindings/inputs are checked before pending transaction reconciliation.
Production reload is exclusively the fixed argv Production reload is exclusively the fixed argv
@@ -77,7 +87,17 @@ restoring an earlier active config reinstates its firewall/daemon state.
The config replacement is atomic; the init script's stop/start sequence is The config replacement is atomic; the init script's stop/start sequence is
not a zero-interruption or kernel-atomic firewall transaction. not a zero-interruption or kernel-atomic firewall transaction.
The transaction engine is unchanged: same-directory staging/fsync/rename, Journal v2 persists complete committed and pending candidate ID lists alongside
their hashes. Commit promotes the pending list; rollback/crash reconciliation
restores the prior list and hash. Reporting validates qualification/groups and
exact rendered hash against the read-only DB. Status reports invalid refs as
unresolved; `active-ids` and activation fail closed on invalid refs.
Legacy v1 journals keep the exact deployed hash and unknown IDs: status marks
both groups unresolved, and active-ids refuses to infer a list. A guarded full-set
apply upgrades to v2 only after the exact old hash matches; rollback preserves
the unknown legacy deployment. Re-init cannot adopt an existing deployment.
The transaction engine retains same-directory staging/fsync/rename,
durable journal/backup, advisory locking, external drift rejection, rollback durable journal/backup, advisory locking, external drift rejection, rollback
and fail-closed crash reconciliation. Failed rollback reload remains pending. and fail-closed crash reconciliation. Failed rollback reload remains pending.
Directories must not be writable by untrusted processes; hostile concurrent Directories must not be writable by untrusted processes; hostile concurrent
@@ -100,7 +120,7 @@ requirement; mountinfo alone cannot establish the storage lifecycle.
Injected unit tests cover exact identity/path/config, mountpoint refusal, Injected unit tests cover exact identity/path/config, mountpoint refusal,
old-container-like env, SOCKS port/process refusal, correct fresh-target success, old-container-like env, SOCKS port/process refusal, correct fresh-target success,
non-overwrite, transaction rollback and redaction. Tests/build/network are non-overwrite, transaction rollback and redaction. Tests/build/network are
deferred to the producer; not run by this increment: deferred to the orchestrator; not run by this increment:
```sh ```sh
go test ./cmd/zapret-autotune -run TestManaged go test ./cmd/zapret-autotune -run TestManaged
+2
View File
@@ -4,6 +4,8 @@
## Готово ## Готово
- Group-wise managed activation: один или оба qualified frontend/exact-GGC candidate; full desired set (не patch), frontend-only bootstrap до Player API/GGC discovery. Journal v2 сохраняет committed/pending IDs вместе с hash; rollback/crash возвращает прежний список. `managed status` и JSON `managed active-ids` проверяют IDs/groups через DB и exact rendered hash; missing groups явно unresolved. Legacy v1 сохраняет deployment по exact hash без выдуманных IDs; unknown IDs fail closed для supervisor. Player API OK не означает GGC/video playback или full-profile health. Focused tests обновлены/добавлены; tests/build/network отложены orchestrator. Recovery callback, remote apply, текущий target и Xray не менялись.
- Добавлен internal `runManagedMonitor(ctx, deps)`: immediate/periodic probe, durable streak/reset, threshold/cooldown recovery через injected callbacks, cooldown от completion при любом результате, fatal DB/lock/finalization errors и cancellable wait. Shared runtime guards выполняются до lifetime nonblocking `flock` в target-owned directory. Одноразовая startup reconciliation под singleton использует schema-v5 PID/starttime API: exact lease снимается только при доказанном dead/PID-reused owner и запускает crash cooldown; live owner остаётся busy, legacy/unverifiable owner — fail closed. Fake-time unit-тесты scheduler core, reset/cooldown, reopen, concurrent start, cancellation и DB/finalization failures добавлены; tests/build/network не запускались в этом increment — verification отложена orchestrator. Actual recovery adapter/monitor command/entrypoint wiring и развёрнутый live target пока отсутствуют; deploy, контейнер и Xray не затронуты. - Добавлен internal `runManagedMonitor(ctx, deps)`: immediate/periodic probe, durable streak/reset, threshold/cooldown recovery через injected callbacks, cooldown от completion при любом результате, fatal DB/lock/finalization errors и cancellable wait. Shared runtime guards выполняются до lifetime nonblocking `flock` в target-owned directory. Одноразовая startup reconciliation под singleton использует schema-v5 PID/starttime API: exact lease снимается только при доказанном dead/PID-reused owner и запускает crash cooldown; live owner остаётся busy, legacy/unverifiable owner — fail closed. Fake-time unit-тесты scheduler core, reset/cooldown, reopen, concurrent start, cancellation и DB/finalization failures добавлены; tests/build/network не запускались в этом increment — verification отложена orchestrator. Actual recovery adapter/monitor command/entrypoint wiring и развёрнутый live target пока отсутствуют; deploy, контейнер и Xray не затронуты.
- Добавлены `Dockerfile.target`, ordered target entrypoint и `deploy/managed-target/compose.yaml`: pinned runtime digest, отдельная bridge-сеть, `cap_drop: ALL` и только `NET_ADMIN`, `NET_RAW`, `SETUID`, `SETGID` (nft/NFQUEUE, raw send и смена UID/GID nfqws2), новый private bind `./target-state`, отдельный gitignored `.env`, SOCKS TCP/UDP только на host loopback (default 11080), внутренний health proxy 1080; SS port не опубликован. Startup: ss-server/ss-local → listener → managed init (status при recreation) → zapret2 start; shutdown ограничен target-local процессами/firewall. Только deployment definition и process supervision, не autonomous monitor/recovery. Compose config/tests/build/pull/run/remote producer не выполнял; проверка отложена orchestrator, runtime gates открыты. - Добавлены `Dockerfile.target`, ordered target entrypoint и `deploy/managed-target/compose.yaml`: pinned runtime digest, отдельная bridge-сеть, `cap_drop: ALL` и только `NET_ADMIN`, `NET_RAW`, `SETUID`, `SETGID` (nft/NFQUEUE, raw send и смена UID/GID nfqws2), новый private bind `./target-state`, отдельный gitignored `.env`, SOCKS TCP/UDP только на host loopback (default 11080), внутренний health proxy 1080; SS port не опубликован. Startup: ss-server/ss-local → listener → managed init (status при recreation) → zapret2 start; shutdown ограничен target-local процессами/firewall. Только deployment definition и process supervision, не autonomous monitor/recovery. Compose config/tests/build/pull/run/remote producer не выполнял; проверка отложена orchestrator, runtime gates открыты.
+12 -3
View File
@@ -4,7 +4,7 @@ Go CLI for scanning and checking YouTube TLS bypass strategies with `blockcheckw
## Manual managed-test activation (not deployed) ## Manual managed-test activation (not deployed)
`managed init|status|apply` requires exact role `managed-test`, target ID `managed init|status|active-ids|apply` requires exact role `managed-test`, target ID
`zapret-autotune-managed-v1`, a provisioned runtime marker and zapret executables, `zapret-autotune-managed-v1`, a provisioned runtime marker and zapret executables,
and a private dedicated mount at `/var/lib/zapret-autotune/target`. and a private dedicated mount at `/var/lib/zapret-autotune/target`.
`ZAPRET_CONFIG` must equal `/var/lib/zapret-autotune/target/config`: the standalone `ZAPRET_CONFIG` must equal `/var/lib/zapret-autotune/target/config`: the standalone
@@ -14,8 +14,9 @@ The managed proxy must equal `socks5://127.0.0.1:$SOCKS_PORT` and that listener
must be owned by target-local `ss-local`; arbitrary loopback/host ports fail. must be owned by target-local `ss-local`; arbitrary loopback/host ports fail.
Init creates a private disabled baseline and identity/journal without overwriting Init creates a private disabled baseline and identity/journal without overwriting
existing config. Status shows generation/hash/groups, not secrets or live health. existing config. Status shows generation/hash/groups, not secrets or live health.
Apply accepts `--frontend-candidate ID --googlevideo-candidate ID` and requires Apply accepts either or both of `--frontend-candidate ID` and
a qualified pair, fixed target init executable and explicit runtime-config `--googlevideo-candidate ID` and requires one qualified candidate per selected
group, fixed target init executable and explicit runtime-config
binding. It uses full target-local firewall/daemon restart and Player API post-check, with binding. It uses full target-local firewall/daemon restart and Player API post-check, with
atomic rollback on failure. See [MANAGED_TARGET.md](MANAGED_TARGET.md) for guards atomic rollback on failure. See [MANAGED_TARGET.md](MANAGED_TARGET.md) for guards
and provisioning prerequisites. An isolated pinned deployment definition is in and provisioning prerequisites. An isolated pinned deployment definition is in
@@ -24,6 +25,14 @@ No autonomous loop, remote activation or Xray changes are included. Image
compatibility and route/rollback gates are not yet remotely proven; adapter compatibility and route/rollback gates are not yet remotely proven; adapter
tests/build are deferred to the orchestrator. tests/build are deferred to the orchestrator.
The apply flags are the entire desired group set, not a merge: a single-group
apply removes the other group. Frontend-only supports bootstrap discovery;
exact-host GGC-only is also valid. Status lists missing or unverifiable groups
as unresolved, never as healthy or a complete playback configuration. Player
API post-checks do not establish either group's health. `active-ids` fails closed
on pending, legacy or unverifiable state; journal IDs and config bytes commit
and roll back together, including after a crash.
## One-shot Player API health ## One-shot Player API health
```sh ```sh
+98 -33
View File
@@ -55,33 +55,35 @@ func managedDependencies(player playerRunner) managedDeps {
} }
func managedArguments(args []string) (string, []int64, error) { func managedArguments(args []string) (string, []int64, error) {
if len(args) == 1 && (args[0] == "init" || args[0] == "status") { invalid := fmt.Errorf("managed accepts init, status, active-ids, or apply with one or both candidate flags (full desired set); no path flags")
if len(args) == 1 && (args[0] == "init" || args[0] == "status" || args[0] == "active-ids") {
return args[0], nil, nil return args[0], nil, nil
} }
if len(args) == 5 && args[0] == "apply" { if (len(args) != 3 && len(args) != 5) || args[0] != "apply" {
ids := make([]int64, 2) return "", nil, invalid
seen := map[string]bool{} }
selected := map[string]int64{}
for i := 1; i < len(args); i += 2 { for i := 1; i < len(args); i += 2 {
key := args[i] key := args[i]
if seen[key] || (key != "--frontend-candidate" && key != "--googlevideo-candidate") || args[i+1] == "" || strings.Trim(args[i+1], "0123456789") != "" { if selected[key] != 0 || (key != "--frontend-candidate" && key != "--googlevideo-candidate") || args[i+1] == "" || strings.Trim(args[i+1], "0123456789") != "" {
break return "", nil, invalid
} }
seen[key] = true
id, err := strconv.ParseInt(args[i+1], 10, 64) id, err := strconv.ParseInt(args[i+1], 10, 64)
if err != nil || id <= 0 { if err != nil || id <= 0 {
break return "", nil, invalid
} }
index := 0 selected[key] = id
if key == "--googlevideo-candidate" {
index = 1
} }
ids[index] = id var ids []int64
for _, key := range []string{"--frontend-candidate", "--googlevideo-candidate"} {
if id := selected[key]; id != 0 {
ids = append(ids, id)
}
}
if len(ids) == 2 && ids[0] == ids[1] {
return "", nil, invalid
} }
if ids[0] > 0 && ids[1] > 0 && ids[0] != ids[1] {
return "apply", ids, nil return "apply", ids, nil
}
}
return "", nil, fmt.Errorf("managed accepts init, status, or apply --frontend-candidate ID --googlevideo-candidate ID (positive distinct IDs); no path flags")
} }
// Provisioning supplies the directory. Never create it or follow aliases. // Provisioning supplies the directory. Never create it or follow aliases.
@@ -193,27 +195,79 @@ func managedCheckIdentity(m *managedTransaction) error {
return nil return nil
} }
func managedStatus(m *managedTransaction, out io.Writer) error { // Status and supervisor reads share the same locked, DB-validated snapshot.
func managedReport(m *managedTransaction, out io.Writer, openDB func() (*sql.DB, error), activeOnly bool) error {
return m.locked(func() error { return m.locked(func() error {
j, err := m.load() j, err := m.load()
if err != nil { if err != nil {
return err return err
} }
b, err := m.read("config") b, err := m.read(m.Name)
if err != nil { if err != nil {
return err return err
} }
if j.State != "committed" { if j.State != "committed" && activeOnly {
return fmt.Errorf("pending transaction; managed apply must reconcile before activation") return fmt.Errorf("pending transaction; reconcile before reading active IDs")
} }
if managedHash(b) != j.Hash { if managedHash(b) != j.Hash && (j.State != "prepared" || managedHash(b) != j.Next) {
return errManagedDrift return errManagedDrift
} }
groups := "none (disabled)" groups := map[string]bool{}
if strings.Contains(string(b), "\nNFQWS2_ENABLE=1\n") { known := j.State == "committed" && j.Version == 2 && string(b) == managedBaseline && len(j.Candidates) == 0
groups = "frontend,googlevideo" if j.State == "committed" && j.Version == 2 && len(j.Candidates) != 0 {
db, err := openDB()
if db != nil {
defer db.Close()
} }
_, err = fmt.Fprintf(out, "managed-test generation=%d hash=%s groups=%s\nLive health: NOT CHECKED (Player API is not playback)\n", j.Generation, j.Hash, groups) if err == nil && db != nil {
rendered, err := renderManagedConfig(db, j.Candidates)
if err == nil && managedHash(rendered) == j.Hash {
known = true
for _, id := range j.Candidates {
c, err := readStrategyCandidate(db, id)
if err != nil || !c.Qualified {
known = false
break
}
groups[c.Group] = true
}
}
}
}
if !known {
groups = map[string]bool{}
// Unvalidated journal references are not active recovery candidates.
j.Candidates = nil
}
var active, missing []string
for _, group := range []string{"frontend", "googlevideo"} {
if groups[group] {
active = append(active, group)
} else {
missing = append(missing, group)
}
}
if activeOnly {
if !known {
return fmt.Errorf("active candidate IDs unresolved; deployment preserved by hash")
}
return json.NewEncoder(out).Encode(struct {
IDs []int64 `json:"active_candidate_ids"`
Missing []string `json:"unresolved_groups"`
}{append([]int64{}, j.Candidates...), missing})
}
groupText := strings.Join(active, ",")
if groupText == "" {
groupText = "none (disabled)"
if !known {
groupText = "unknown"
}
}
var reportIDs []int64
if known {
reportIDs = j.Candidates
}
_, err = fmt.Fprintf(out, "managed-test generation=%d hash=%s state=%s groups=%s active_candidate_ids=%v unresolved_groups=%s\nLive health: NOT CHECKED (Player API is not GGC/video playback)\n", j.Generation, j.Hash, j.State, groupText, reportIDs, strings.Join(missing, ","))
return err return err
}) })
} }
@@ -269,25 +323,35 @@ func managedCLI(args []string, stdout, stderr io.Writer, d managedDeps) int {
if command == "init" { if command == "init" {
err = managedInitialize(m) err = managedInitialize(m)
} else if err = managedCheckIdentity(m); err == nil { } else if err = managedCheckIdentity(m); err == nil {
if command == "status" { if command == "status" || command == "active-ids" {
err = managedStatus(m, stdout) err = managedReport(m, stdout, d.openDB, command == "active-ids")
} else { } else {
err = managedActivate(m, d, ids) expected := map[int64]string{}
for i := 1; i < len(args); i += 2 {
id, _ := strconv.ParseInt(args[i+1], 10, 64)
expected[id] = strings.TrimSuffix(strings.TrimPrefix(args[i], "--"), "-candidate")
}
err = managedActivate(m, d, ids, expected)
} }
} }
if err != nil { if err != nil {
fmt.Fprintln(stderr, "error:", err) fmt.Fprintln(stderr, "error:", err)
return 1 return 1
} }
if command != "status" { if command == "init" || command == "apply" {
if _, err := fmt.Fprintln(stdout, "managed-test "+command+": OK"); err != nil { if _, err := fmt.Fprintln(stdout, "managed-test "+command+": OK"); err != nil {
return 1 return 1
} }
if command == "apply" {
if _, err := fmt.Fprintln(stdout, "Player API only; not full-profile/GGC/playback health. Use managed status for unresolved groups."); err != nil {
return 1
}
}
} }
return 0 return 0
} }
func managedActivate(m *managedTransaction, d managedDeps, ids []int64) error { func managedActivate(m *managedTransaction, d managedDeps, ids []int64, expected map[int64]string) error {
info, err := os.Stat(d.script) info, err := os.Stat(d.script)
if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0111 == 0 { if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0111 == 0 {
return fmt.Errorf("target zapret daemon init executable unavailable") return fmt.Errorf("target zapret daemon init executable unavailable")
@@ -322,8 +386,9 @@ func managedActivate(m *managedTransaction, d managedDeps, ids []int64) error {
return fmt.Errorf("candidate database missing") return fmt.Errorf("candidate database missing")
} }
defer db.Close() defer db.Close()
for i, group := range []string{"frontend", "googlevideo"} { for _, id := range ids {
c, err := readStrategyCandidate(db, ids[i]) group := expected[id]
c, err := readStrategyCandidate(db, id)
if err != nil || c.Group != group { if err != nil || c.Group != group {
return fmt.Errorf("invalid %s candidate ID", group) return fmt.Errorf("invalid %s candidate ID", group)
} }
@@ -331,7 +396,7 @@ func managedActivate(m *managedTransaction, d managedDeps, ids []int64) error {
if _, err := renderManagedConfig(db, ids); err != nil { if _, err := renderManagedConfig(db, ids); err != nil {
return err return err
} }
if err := m.Reconcile(); err != nil { if err := m.Reconcile(db); err != nil {
return err return err
} }
return m.Apply(db, ids) return m.Apply(db, ids)
+62
View File
@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@@ -265,6 +266,7 @@ func TestManagedCLIApplyAndRollback(t *testing.T) {
if !strings.Contains(string(b), "--hostlist-domains=^rr1.googlevideo.com") { if !strings.Contains(string(b), "--hostlist-domains=^rr1.googlevideo.com") {
t.Fatalf("wrong applied scope: %s", b) t.Fatalf("wrong applied scope: %s", b)
} }
d.openDB = func() (*sql.DB, error) { return openHistoryStore(databasePath()) }
code, out, diagnostic = callManaged(t, d, "status") code, out, diagnostic = callManaged(t, d, "status")
if code != 0 || !strings.Contains(out, "generation=1") || !strings.Contains(out, "groups=frontend,googlevideo") || strings.Contains(out, "lua-desync") || strings.Contains(out, "rr1") { if code != 0 || !strings.Contains(out, "generation=1") || !strings.Contains(out, "groups=frontend,googlevideo") || strings.Contains(out, "lua-desync") || strings.Contains(out, "rr1") {
t.Fatalf("status: %d %s %s", code, out, diagnostic) t.Fatalf("status: %d %s %s", code, out, diagnostic)
@@ -391,6 +393,66 @@ func TestManagedCLIFreshTargetContract(t *testing.T) {
} }
} }
func TestManagedCLIStagedProfiles(t *testing.T) {
for _, group := range []string{"frontend", "googlevideo"} {
t.Run(group, func(t *testing.T) {
d, _, calls := managedCLIFixture(t)
initManagedCLI(t, d)
db := testStore(t)
front := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", true)
ggc := managedCandidateFixture(t, db, "googlevideo", "rr1.googlevideo.com", "--lua-desync=fake", true)
id, missing := front, "googlevideo"
if group == "googlevideo" {
id, missing = ggc, "frontend"
}
d.openDB = func() (*sql.DB, error) { return openHistoryStore(databasePath()) }
if code, out, diagnostic := callManaged(t, d, "apply", "--"+group+"-candidate", fmt.Sprint(id)); code != 0 {
t.Fatalf("single group apply: %d %s %s", code, out, diagnostic)
}
if code, out, diagnostic := callManaged(t, d, "status"); code != 0 || !strings.Contains(out, "groups="+group+" ") || !strings.Contains(out, "unresolved_groups="+missing) || !strings.Contains(out, "NOT CHECKED") {
t.Fatalf("partial status: %d %s %s", code, out, diagnostic)
}
code, out, diagnostic := callManaged(t, d, "active-ids")
var state struct {
IDs []int64 `json:"active_candidate_ids"`
Missing []string `json:"unresolved_groups"`
}
if code != 0 || json.Unmarshal([]byte(out), &state) != nil || !reflect.DeepEqual(state.IDs, []int64{id}) || !reflect.DeepEqual(state.Missing, []string{missing}) || *calls != 1 {
t.Fatalf("active IDs: %d %s %s", code, out, diagnostic)
}
if _, err := db.Exec("DELETE FROM candidate_evidence WHERE candidate_id = ?", id); err != nil {
t.Fatal(err)
}
if code, out, diagnostic := callManaged(t, d, "status"); code != 0 || !strings.Contains(out, "groups=unknown ") || !strings.Contains(out, "active_candidate_ids=[] ") || !strings.Contains(out, "unresolved_groups=frontend,googlevideo") {
t.Fatalf("invalid DB status: %d %s %s", code, out, diagnostic)
}
if code, out, diagnostic := callManaged(t, d, "active-ids"); code == 0 || out != "" || !strings.Contains(diagnostic, "active candidate IDs unresolved") || *calls != 1 {
t.Fatalf("invalid DB IDs trusted: %d %s %s", code, out, diagnostic)
}
})
}
}
func TestManagedCLILegacyIDsUnresolved(t *testing.T) {
d, _, calls := managedCLIFixture(t)
initManagedCLI(t, d)
m := &managedTransaction{Dir: d.directory, Name: "config", Reload: func() error { return nil }, Health: func() (string, error) { return "OK", nil }}
j, err := m.load()
if err != nil {
t.Fatal(err)
}
j.Version = 1
if err := m.journal(j); err != nil {
t.Fatal(err)
}
if code, out, diagnostic := callManaged(t, d, "status"); code != 0 || !strings.Contains(out, "groups=unknown") || !strings.Contains(out, "unresolved_groups=frontend,googlevideo") {
t.Fatalf("legacy status: %d %s %s", code, out, diagnostic)
}
if code, out, _ := callManaged(t, d, "active-ids"); code == 0 || out != "" || *calls != 0 {
t.Fatal("legacy IDs inferred")
}
}
func TestManagedMountpoint(t *testing.T) { func TestManagedMountpoint(t *testing.T) {
for _, path := range []string{"/", filepath.Dir(managedTargetDirectory), managedTargetDirectory + "-other"} { for _, path := range []string{"/", filepath.Dir(managedTargetDirectory), managedTargetDirectory + "-other"} {
if managedMountpoint("1 2 0:3 / "+path+" rw - tmpfs tmpfs rw\n", managedTargetDirectory) { if managedMountpoint("1 2 0:3 / "+path+" rw - tmpfs tmpfs rw\n", managedTargetDirectory) {
+9 -3
View File
@@ -15,11 +15,14 @@ const managedFirewallConfig = "INIT_APPLY_FW=1\n" +
// renderManagedConfig accepts identifiers, not caller-supplied strategies or scope. // renderManagedConfig accepts identifiers, not caller-supplied strategies or scope.
// Qualification is the existing complete endpoint × TLS evidence rule. // Qualification is the existing complete endpoint × TLS evidence rule.
func renderManagedConfig(db *sql.DB, ids []int64) ([]byte, error) { func renderManagedConfig(db *sql.DB, ids []int64) ([]byte, error) {
if db == nil {
return nil, fmt.Errorf("candidate definitions database required")
}
var profiles []string var profiles []string
groups := map[string]bool{} groups := map[string]bool{}
seen := map[int64]bool{} seen := map[int64]bool{}
for _, id := range ids { for _, id := range ids {
if seen[id] { if id <= 0 || seen[id] {
return nil, fmt.Errorf("duplicate candidate %d", id) return nil, fmt.Errorf("duplicate candidate %d", id)
} }
seen[id] = true seen[id] = true
@@ -30,6 +33,9 @@ func renderManagedConfig(db *sql.DB, ids []int64) ([]byte, error) {
if !c.Qualified { if !c.Qualified {
return nil, fmt.Errorf("candidate %d is not qualified", id) return nil, fmt.Errorf("candidate %d is not qualified", id)
} }
if groups[c.Group] {
return nil, fmt.Errorf("duplicate candidate group %q", c.Group)
}
v := candidate{Group: c.Group, Domains: append([]string(nil), c.Endpoints...), Args: c.Args} v := candidate{Group: c.Group, Domains: append([]string(nil), c.Endpoints...), Args: c.Args}
if err := v.validate(); err != nil { if err := v.validate(); err != nil {
return nil, err return nil, err
@@ -50,8 +56,8 @@ func renderManagedConfig(db *sql.DB, ids []int64) ([]byte, error) {
groups[c.Group] = true groups[c.Group] = true
profiles = append(profiles, "--filter-tcp=443 --filter-l7=tls --hostlist-domains="+strings.Join(domains, ",")+" "+c.Args) profiles = append(profiles, "--filter-tcp=443 --filter-l7=tls --hostlist-domains="+strings.Join(domains, ",")+" "+c.Args)
} }
if !groups["frontend"] || !groups["googlevideo"] { if len(profiles) == 0 {
return nil, fmt.Errorf("qualified frontend and googlevideo candidates required") return nil, fmt.Errorf("qualified candidate required")
} }
// All values are shell-inert after validateArgs; no shell evaluation/escaping // All values are shell-inert after validateArgs; no shell evaluation/escaping
// contract is extended here. QNUM is intentionally left at the target default. // contract is extended here. QNUM is intentionally left at the target default.
+156
View File
@@ -0,0 +1,156 @@
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"testing"
)
func TestManagedSingleGroupCLI(t *testing.T) {
for _, group := range []string{"frontend", "googlevideo"} {
t.Run(group, func(t *testing.T) {
d, _, calls := managedCLIFixture(t)
initManagedCLI(t, d)
db := testStore(t)
host, missing := "youtube.com", "googlevideo"
if group == "googlevideo" {
host, missing = "rr1.googlevideo.com", "frontend"
}
id := managedCandidateFixture(t, db, group, host, "--lua-desync=fake", true)
// Every CLI command owns and closes its own read-only DB handle.
d.openDB = func() (*sql.DB, error) { return openHistoryStore(databasePath()) }
if code, out, diagnostic := callManaged(t, d, "apply", "--"+group+"-candidate", fmt.Sprint(id)); code != 0 {
t.Fatalf("apply: %d %s %s", code, out, diagnostic)
}
code, out, diagnostic := callManaged(t, d, "status")
if code != 0 || !strings.Contains(out, "groups="+group+" ") || !strings.Contains(out, "unresolved_groups="+missing+"\n") || !strings.Contains(out, "Live health: NOT CHECKED") {
t.Fatalf("partial status: %d %s %s", code, out, diagnostic)
}
code, out, diagnostic = callManaged(t, d, "active-ids")
var snapshot struct {
IDs []int64 `json:"active_candidate_ids"`
Missing []string `json:"unresolved_groups"`
}
if err := json.Unmarshal([]byte(out), &snapshot); err != nil || code != 0 || !reflect.DeepEqual(snapshot.IDs, []int64{id}) || !reflect.DeepEqual(snapshot.Missing, []string{missing}) {
t.Fatalf("partial IDs: %d %s %s %v", code, out, diagnostic, err)
}
if *calls != 1 {
t.Fatalf("status/IDs restarted: %d", *calls)
}
})
}
}
func TestManagedStatusUnresolvedSnapshot(t *testing.T) {
for _, mode := range []string{"database", "qualification", "legacy", "pending"} {
t.Run(mode, func(t *testing.T) {
d, _, _ := managedCLIFixture(t)
initManagedCLI(t, d)
db := testStore(t)
front := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", true)
ggc := managedCandidateFixture(t, db, "googlevideo", "rr1.googlevideo.com", "--lua-desync=fake", true)
m := &managedTransaction{Dir: d.directory, Name: "config", Reload: func() error { return nil }, Health: func() (string, error) { return "OK", nil }}
if err := m.Apply(db, []int64{front}); err != nil {
t.Fatal(err)
}
d.openDB = func() (*sql.DB, error) { return openHistoryStore(databasePath()) }
switch mode {
case "database":
d.openDB = func() (*sql.DB, error) { return nil, errors.New("private database error") }
case "qualification":
if _, err := db.Exec("DELETE FROM candidate_evidence WHERE candidate_id = ?", front); err != nil {
t.Fatal(err)
}
case "legacy":
j, err := m.load()
if err != nil {
t.Fatal(err)
}
j.Version, j.Candidates = 1, nil
if err := m.journal(j); err != nil {
t.Fatal(err)
}
case "pending":
m.fault = func(point string) error {
if point == "replaced" {
return errors.New("death")
}
return nil
}
if err := m.Apply(db, []int64{ggc}); err == nil {
t.Fatal("crash missed")
}
}
code, out, diagnostic := callManaged(t, d, "status")
if code != 0 || !strings.Contains(out, "groups=unknown active_candidate_ids=[] unresolved_groups=frontend,googlevideo") || !strings.Contains(out, "Live health: NOT CHECKED") || strings.Contains(out+diagnostic, "private") {
t.Fatalf("unresolved status: %d %s %s", code, out, diagnostic)
}
if code, out, _ := callManaged(t, d, "active-ids"); code == 0 || out != "" {
t.Fatalf("unresolved IDs exposed: %d %s", code, out)
}
if mode == "pending" {
m.fault = nil
if err := m.Reconcile(db); err != nil {
t.Fatal(err)
}
assertActiveCandidates(t, m, db, []int64{front})
}
})
}
}
func TestManagedPartialGroupRollback(t *testing.T) {
for _, priorGroup := range []string{"frontend", "googlevideo"} {
for _, point := range []string{"staged", "backup", "prepared", "replaced", "reloaded", "healthy", "unhealthy"} {
t.Run(priorGroup+"/"+point, func(t *testing.T) {
db := testStore(t)
front := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", true)
ggc := managedCandidateFixture(t, db, "googlevideo", "rr1.googlevideo.com", "--lua-desync=fake", true)
prior, next := []int64{front}, []int64{ggc}
if priorGroup == "googlevideo" {
prior, next = next, prior
}
m, _ := managedFixture(t)
if err := m.Apply(db, prior); err != nil {
t.Fatal(err)
}
priorBytes, err := m.read(m.Name)
if err != nil {
t.Fatal(err)
}
m.fault = func(p string) error {
if p == "prepared" {
j, err := m.load()
if err != nil || !reflect.DeepEqual(j.Candidates, prior) || !reflect.DeepEqual(j.PendingCandidates, next) {
t.Fatalf("group sets: %+v %v", j, err)
}
}
if p == point {
return errors.New("death")
}
return nil
}
if point == "unhealthy" {
m.Health = func() (string, error) { return "DEGRADED", nil }
}
if err := m.Apply(db, next); err == nil {
t.Fatal("failure missed")
}
m.fault = nil
if err := m.Reconcile(db); err != nil {
t.Fatal(err)
}
assertManagedContent(t, m, string(priorBytes))
assertActiveCandidates(t, m, db, prior)
j, err := m.load()
if err != nil || len(j.PendingCandidates) != 0 {
t.Fatalf("pending IDs retained: %+v %v", j, err)
}
})
}
}
}
+150 -7
View File
@@ -11,6 +11,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"strings" "strings"
"syscall" "syscall"
) )
@@ -31,6 +32,8 @@ type managedJournal struct {
State string `json:"state"` State string `json:"state"`
Hash string `json:"committed_hash"` Hash string `json:"committed_hash"`
Next string `json:"pending_hash,omitempty"` Next string `json:"pending_hash,omitempty"`
Candidates []int64 `json:"committed_candidate_ids,omitempty"`
PendingCandidates []int64 `json:"pending_candidate_ids,omitempty"`
} }
// Hooks take no commands or arguments. The target adapter binds Reload // Hooks take no commands or arguments. The target adapter binds Reload
@@ -181,7 +184,20 @@ func (m *managedTransaction) load() (managedJournal, error) {
b, err := hex.DecodeString(s) b, err := hex.DecodeString(s)
return err == nil && len(b) == 32 && strings.ToLower(s) == s return err == nil && len(b) == 32 && strings.ToLower(s) == s
} }
if j.Version != 1 || j.ID == "" || !validHash(j.Hash) || (j.State != "committed" && j.State != "prepared") || (j.State == "prepared" && !validHash(j.Next)) || (j.State == "committed" && j.Next != "") { validIDs := func(ids []int64) bool {
if len(ids) > 2 {
return false
}
seen := map[int64]bool{}
for _, id := range ids {
if id <= 0 || seen[id] {
return false
}
seen[id] = true
}
return true
}
if (j.Version != 1 && j.Version != 2) || j.ID == "" || !validHash(j.Hash) || (j.State != "committed" && j.State != "prepared") || (j.State == "prepared" && !validHash(j.Next)) || (j.State == "committed" && (j.Next != "" || len(j.PendingCandidates) != 0)) || !validIDs(j.Candidates) || !validIDs(j.PendingCandidates) || (j.Version == 1 && (len(j.Candidates) != 0 || len(j.PendingCandidates) != 0)) {
return j, fmt.Errorf("invalid managed journal") return j, fmt.Errorf("invalid managed journal")
} }
return j, nil return j, nil
@@ -201,7 +217,7 @@ func (m *managedTransaction) Initialize() error {
if err := m.replace(managedBackupName, b); err != nil { if err := m.replace(managedBackupName, b); err != nil {
return err return err
} }
return m.journal(managedJournal{Version: 1, ID: "bootstrap", State: "committed", Hash: managedHash(b)}) return m.journal(managedJournal{Version: 2, ID: "bootstrap", State: "committed", Hash: managedHash(b)})
}) })
} }
@@ -213,14 +229,94 @@ func (m *managedTransaction) checkpoint(point string) error {
} }
func (m *managedTransaction) Apply(db *sql.DB, ids []int64) error { func (m *managedTransaction) Apply(db *sql.DB, ids []int64) error {
ids = append([]int64(nil), ids...)
b, err := renderManagedConfig(db, ids) b, err := renderManagedConfig(db, ids)
if err != nil { if err != nil {
return err return err
} }
return m.applyRendered(b) return m.applyRenderedDB(b, db, ids...)
} }
func (m *managedTransaction) applyRendered(b []byte) error { // ActiveCandidates uses the journal as active-state authority. The DB supplies
// only immutable definitions and qualification; rendering binds refs to bytes.
// Legacy v1/adopted configs have no known active refs, even when their hash matches.
// Apply's complete list replaces all groups: either single group drops the other.
func (m *managedTransaction) ActiveCandidates(db *sql.DB) ([]int64, error) {
var ids []int64
err := m.locked(func() error {
j, err := m.load()
if err != nil {
return err
}
if j.State != "committed" {
return fmt.Errorf("incomplete transaction: reconcile first")
}
b, err := m.read(m.Name)
if err != nil {
return err
}
if managedHash(b) != j.Hash {
return errManagedDrift
}
if len(j.Candidates) == 0 && (j.Version == 1 || string(b) != managedBaseline) {
return fmt.Errorf("active candidate IDs unresolved; deployment preserved by hash")
}
if len(j.Candidates) != 0 {
if db == nil {
return fmt.Errorf("candidate database required")
}
rendered, err := renderManagedConfig(db, j.Candidates)
if err != nil {
return err
}
if managedHash(rendered) != j.Hash {
return fmt.Errorf("active candidates do not match committed config")
}
}
ids = append([]int64(nil), j.Candidates...)
return nil
})
return ids, err
}
func committedManagedJournal(j managedJournal) managedJournal {
j.State, j.Hash, j.Next = "committed", j.Next, ""
j.Candidates, j.PendingCandidates = j.PendingCandidates, nil
return j
}
// A stale transaction must not overwrite another generation's journal, even
// when that generation happens to render identical config bytes.
func (m *managedTransaction) owns(j managedJournal, allowCommitted bool) error {
current, err := m.load()
if err != nil {
return err
}
if !reflect.DeepEqual(current, j) && !(allowCommitted && reflect.DeepEqual(current, committedManagedJournal(j))) {
return fmt.Errorf("stale managed transaction")
}
return nil
}
func (m *managedTransaction) applyRendered(b []byte, ids ...int64) error {
return m.applyRenderedDB(b, nil, ids...)
}
func validateManagedRefs(db *sql.DB, ids []int64, hash string) error {
if len(ids) == 0 {
return nil
}
if db == nil {
return fmt.Errorf("candidate database required")
}
b, err := renderManagedConfig(db, ids)
if err != nil || managedHash(b) != hash {
return fmt.Errorf("managed candidate state unresolved")
}
return nil
}
func (m *managedTransaction) applyRenderedDB(b []byte, db *sql.DB, ids ...int64) error {
return m.locked(func() error { return m.locked(func() error {
j, err := m.load() j, err := m.load()
if err != nil { if err != nil {
@@ -229,6 +325,12 @@ func (m *managedTransaction) applyRendered(b []byte) error {
if j.State != "committed" { if j.State != "committed" {
return fmt.Errorf("incomplete transaction: reconcile first") return fmt.Errorf("incomplete transaction: reconcile first")
} }
if err := validateManagedRefs(db, j.Candidates, j.Hash); err != nil {
return err
}
if err := validateManagedRefs(db, ids, managedHash(b)); err != nil {
return err
}
prior, err := m.read(m.Name) prior, err := m.read(m.Name)
if err != nil { if err != nil {
return err return err
@@ -251,14 +353,26 @@ func (m *managedTransaction) applyRendered(b []byte) error {
if err := m.checkpoint("staged"); err != nil { if err := m.checkpoint("staged"); err != nil {
return err return err
} }
if err := m.owns(j, false); err != nil {
return err
}
if err := m.replace(managedBackupName, prior); err != nil { if err := m.replace(managedBackupName, prior); err != nil {
return err return err
} }
if err := m.checkpoint("backup"); err != nil { if err := m.checkpoint("backup"); err != nil {
return err return err
} }
if err := m.owns(j, false); err != nil {
return err
}
// Check ownership again before replacing the journal's selected set.
if err := m.owns(j, false); err != nil {
return err
}
j.Generation++ j.Generation++
j.Version = 2
j.ID, j.State, j.Next = hex.EncodeToString(id), "prepared", managedHash(b) j.ID, j.State, j.Next = hex.EncodeToString(id), "prepared", managedHash(b)
j.PendingCandidates = append([]int64(nil), ids...)
if err := m.journal(j); err != nil { if err := m.journal(j); err != nil {
return err return err
} }
@@ -272,6 +386,9 @@ func (m *managedTransaction) applyRendered(b []byte) error {
if managedHash(current) != j.Hash { if managedHash(current) != j.Hash {
return errManagedDrift return errManagedDrift
} }
if err := m.owns(j, false); err != nil {
return err
}
err = os.Rename(stage, filepath.Join(m.Dir, m.Name)) err = os.Rename(stage, filepath.Join(m.Dir, m.Name))
if err == nil { if err == nil {
err = m.syncDir() err = m.syncDir()
@@ -302,13 +419,15 @@ func (m *managedTransaction) applyRendered(b []byte) error {
} }
} }
if err == nil { if err == nil {
committed := j committed := committedManagedJournal(j)
committed.State, committed.Hash, committed.Next = "committed", j.Next, ""
current, err = m.read(m.Name) current, err = m.read(m.Name)
if err == nil && managedHash(current) != j.Next { if err == nil && managedHash(current) != j.Next {
return errManagedDrift return errManagedDrift
} }
if err == nil { if err == nil {
if err := m.owns(j, false); err != nil {
return err
}
err = m.journal(committed) err = m.journal(committed)
} }
} }
@@ -323,6 +442,9 @@ func (m *managedTransaction) applyRendered(b []byte) error {
} }
func (m *managedTransaction) restore(j managedJournal) error { func (m *managedTransaction) restore(j managedJournal) error {
if err := m.owns(j, true); err != nil {
return err
}
current, err := m.read(m.Name) current, err := m.read(m.Name)
if err != nil { if err != nil {
return err return err
@@ -349,18 +471,39 @@ func (m *managedTransaction) restore(j managedJournal) error {
if err := m.Reload(); err != nil { if err := m.Reload(); err != nil {
return err return err
} }
if err := m.owns(j, false); err != nil {
return err
}
current, err = m.read(m.Name)
if err != nil {
return err
}
if managedHash(current) != j.Hash {
return errManagedDrift
}
j.State, j.Next = "committed", "" j.State, j.Next = "committed", ""
j.PendingCandidates = nil
return m.journal(j) return m.journal(j)
} }
// Reconcile never promotes a pending config, even if a probe had passed before // Reconcile never promotes a pending config, even if a probe had passed before
// death. A failed restore/reload retains the pending journal and blocks Apply. // death. A failed restore/reload retains the pending journal and blocks Apply.
func (m *managedTransaction) Reconcile() error { func (m *managedTransaction) Reconcile(databases ...*sql.DB) error {
return m.locked(func() error { return m.locked(func() error {
j, err := m.load() j, err := m.load()
if err != nil { if err != nil {
return err return err
} }
var db *sql.DB
if len(databases) != 0 {
db = databases[0]
}
if err := validateManagedRefs(db, j.Candidates, j.Hash); err != nil {
return err
}
if err := validateManagedRefs(db, j.PendingCandidates, j.Next); err != nil {
return err
}
if j.State == "prepared" { if j.State == "prepared" {
if err := m.restore(j); err != nil { if err := m.restore(j); err != nil {
return &managedRollbackError{Cause: errors.New("incomplete transaction"), Rollback: err} return &managedRollbackError{Cause: errors.New("incomplete transaction"), Rollback: err}
+248 -6
View File
@@ -5,6 +5,7 @@ import (
"errors" "errors"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"strings" "strings"
"testing" "testing"
) )
@@ -56,11 +57,21 @@ func TestManagedRender(t *testing.T) {
} }
unqualified := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", false) unqualified := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", false)
wildcard := managedCandidateFixture(t, db, "googlevideo", "googlevideo.com", "--lua-desync=fake", true) wildcard := managedCandidateFixture(t, db, "googlevideo", "googlevideo.com", "--lua-desync=fake", true)
for _, ids := range [][]int64{{unqualified, ggc}, {front, wildcard}, {front}, {front, ggc, ggc}, {99999, ggc}} { otherFront := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", true)
otherGGC := managedCandidateFixture(t, db, "googlevideo", "rr2.googlevideo.com", "--lua-desync=fake", true)
for _, ids := range [][]int64{{}, {unqualified, ggc}, {front, wildcard}, {front, otherFront}, {front, ggc, otherGGC}, {front, ggc, ggc}, {99999, ggc}, {0}, {-1}} {
if _, err := renderManagedConfig(db, ids); err == nil { if _, err := renderManagedConfig(db, ids); err == nil {
t.Fatalf("accepted %v", ids) t.Fatalf("accepted %v", ids)
} }
} }
bootstrap, err := renderManagedConfig(db, []int64{front})
if err != nil || strings.Contains(string(bootstrap), "googlevideo") || strings.Contains(string(bootstrap), "--new") {
t.Fatalf("frontend-only config=%q err=%v", bootstrap, err)
}
cacheOnly, err := renderManagedConfig(db, []int64{ggc})
if err != nil || !strings.Contains(string(cacheOnly), "--hostlist-domains=^rr1---sn-test.googlevideo.com") || strings.Contains(string(cacheOnly), "youtube.com") {
t.Fatalf("GGC-only config=%q err=%v", cacheOnly, err)
}
} }
func assertManagedFirewallConfig(t *testing.T, config, enabled string) { func assertManagedFirewallConfig(t *testing.T, config, enabled string) {
@@ -182,13 +193,17 @@ func TestManagedApplyCandidateIDs(t *testing.T) {
front := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", true) front := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", true)
ggc := managedCandidateFixture(t, db, "googlevideo", "rr1.googlevideo.com", "--lua-desync=multisplit", true) ggc := managedCandidateFixture(t, db, "googlevideo", "rr1.googlevideo.com", "--lua-desync=multisplit", true)
m, reloads := managedFixture(t) m, reloads := managedFixture(t)
if err := m.Apply(db, []int64{front}); err == nil { if err := m.Apply(db, []int64{ggc}); err != nil {
t.Fatal("partial qualification accepted") t.Fatal(err)
} }
assertManagedContent(t, m, "prior\n") assertActiveCandidates(t, m, db, []int64{ggc})
if *reloads != 0 { if *reloads != 1 {
t.Fatal("invalid render reloaded") t.Fatal("GGC-only not activated")
} }
if err := m.Apply(db, []int64{front}); err != nil {
t.Fatal(err)
}
assertActiveCandidates(t, m, db, []int64{front})
if err := m.Apply(db, []int64{front, ggc}); err != nil { if err := m.Apply(db, []int64{front, ggc}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -197,6 +212,30 @@ func TestManagedApplyCandidateIDs(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
assertManagedContent(t, m, string(want)) assertManagedContent(t, m, string(want))
assertActiveCandidates(t, m, db, []int64{front, ggc})
replacement := managedCandidateFixture(t, db, "frontend", "www.youtube.com", "--lua-desync=multisplit", true)
if err := m.Apply(db, []int64{replacement, ggc}); err != nil {
t.Fatal(err)
}
assertActiveCandidates(t, m, db, []int64{replacement, ggc})
if err := m.Apply(db, []int64{replacement}); err != nil {
t.Fatal(err)
}
assertActiveCandidates(t, m, db, []int64{replacement})
b, err := m.read(m.Name)
if err != nil || strings.Contains(string(b), "googlevideo") {
t.Fatalf("GGC was not removed: %q %v", b, err)
}
}
func assertActiveCandidates(t *testing.T, m *managedTransaction, db *sql.DB, want []int64) {
t.Helper()
// Re-create the engine to ensure no process-local active state is consulted.
fresh := &managedTransaction{Dir: m.Dir, Name: m.Name, Reload: m.Reload, Health: m.Health}
got, err := fresh.ActiveCandidates(db)
if err != nil || !reflect.DeepEqual(got, want) {
t.Fatalf("active=%v want=%v err=%v", got, want, err)
}
} }
func TestManagedMissingJournalNotAdopted(t *testing.T) { func TestManagedMissingJournalNotAdopted(t *testing.T) {
@@ -395,3 +434,206 @@ func TestManagedRecoveryFailsClosed(t *testing.T) {
}) })
} }
} }
func TestManagedCandidateCrashRollback(t *testing.T) {
for _, point := range []string{"staged", "backup", "prepared", "replaced", "reloaded", "healthy", "unhealthy"} {
t.Run(point, func(t *testing.T) {
db := testStore(t)
front := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", true)
ggc := managedCandidateFixture(t, db, "googlevideo", "rr1.googlevideo.com", "--lua-desync=fake", true)
next := managedCandidateFixture(t, db, "frontend", "www.youtube.com", "--lua-desync=multisplit", true)
m, _ := managedFixture(t)
if err := m.Apply(db, []int64{front, ggc}); err != nil {
t.Fatal(err)
}
prior, err := m.read(m.Name)
if err != nil {
t.Fatal(err)
}
m.fault = func(p string) error {
if p == "prepared" {
j, err := m.load()
if err != nil || !reflect.DeepEqual(j.Candidates, []int64{front, ggc}) || !reflect.DeepEqual(j.PendingCandidates, []int64{next}) {
t.Fatalf("prepared refs=%+v err=%v", j, err)
}
}
if p == point {
return errors.New("death")
}
return nil
}
if point == "unhealthy" {
m.Health = func() (string, error) { return "DEGRADED", nil }
}
if err := m.Apply(db, []int64{next}); err == nil {
t.Fatal("failure missed")
}
if point != "staged" && point != "backup" && point != "unhealthy" {
if ids, err := m.ActiveCandidates(db); err == nil || len(ids) != 0 {
t.Fatalf("incomplete transaction exposed IDs: %v %v", ids, err)
}
}
m.fault = nil
if err := m.Reconcile(db); err != nil {
t.Fatal(err)
}
assertManagedContent(t, m, string(prior))
assertActiveCandidates(t, m, db, []int64{front, ggc})
j, err := m.load()
if err != nil || j.Version != 2 || len(j.PendingCandidates) != 0 {
t.Fatalf("rollback journal=%+v err=%v", j, err)
}
})
}
}
func TestManagedActiveCandidatesFailClosed(t *testing.T) {
for _, damage := range []string{"drift", "missing", "unqualified", "wrong-refs", "duplicate", "invalid-id", "duplicate-group", "ggc-only", "pending"} {
t.Run(damage, func(t *testing.T) {
db := testStore(t)
front := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", true)
other := managedCandidateFixture(t, db, "frontend", "www.youtube.com", "--lua-desync=multisplit", true)
ggc := managedCandidateFixture(t, db, "googlevideo", "rr1.googlevideo.com", "--lua-desync=fake", true)
m, _ := managedFixture(t)
if err := m.Apply(db, []int64{front, ggc}); err != nil {
t.Fatal(err)
}
j, err := m.load()
if err != nil {
t.Fatal(err)
}
switch damage {
case "drift":
if err := os.WriteFile(filepath.Join(m.Dir, m.Name), []byte("external"), 0600); err != nil {
t.Fatal(err)
}
case "missing":
j.Candidates = []int64{99999, ggc}
case "unqualified":
if _, err := db.Exec("DELETE FROM candidate_evidence WHERE candidate_id = ?", front); err != nil {
t.Fatal(err)
}
case "wrong-refs":
j.Candidates = []int64{other, ggc}
case "duplicate":
j.Candidates = []int64{front, front}
case "invalid-id":
j.Candidates = []int64{-1}
case "duplicate-group":
j.Candidates = []int64{front, other}
case "ggc-only":
j.Candidates = []int64{ggc}
case "pending":
j.PendingCandidates = []int64{other}
}
if err := m.journal(j); err != nil {
t.Fatal(err)
}
if ids, err := m.ActiveCandidates(db); err == nil || len(ids) != 0 {
t.Fatalf("unsafe active refs=%v err=%v", ids, err)
}
})
}
}
func TestManagedLegacyJournal(t *testing.T) {
db := testStore(t)
front := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", true)
m, _ := managedFixture(t)
j, err := m.load()
if err != nil {
t.Fatal(err)
}
j.Version = 1
if err := m.journal(j); err != nil {
t.Fatal(err)
}
if _, err := m.ActiveCandidates(db); err == nil {
t.Fatal("legacy IDs inferred")
}
m.fault = func(p string) error {
if p == "replaced" {
return errors.New("death")
}
return nil
}
if err := m.Apply(db, []int64{front}); err == nil {
t.Fatal("fault missed")
}
if err := m.Reconcile(db); err != nil {
t.Fatal(err)
}
assertManagedContent(t, m, "prior\n")
if _, err := m.ActiveCandidates(db); err == nil {
t.Fatal("rollback inferred legacy IDs")
}
m.fault = nil
if err := m.Apply(db, []int64{front}); err != nil {
t.Fatal(err)
}
assertActiveCandidates(t, m, db, []int64{front})
}
func TestManagedStaleTransactionCannotCommitIDs(t *testing.T) {
for _, point := range []string{"staged", "backup", "prepared", "healthy"} {
t.Run(point, func(t *testing.T) {
db := testStore(t)
front := managedCandidateFixture(t, db, "frontend", "youtube.com", "--lua-desync=fake", true)
m, _ := managedFixture(t)
var newer managedJournal
m.fault = func(p string) error {
if p == point {
var err error
newer, err = m.load()
if err != nil {
return err
}
newer.Generation++
newer.ID = "newer-transaction"
return m.journal(newer)
}
return nil
}
if err := m.Apply(db, []int64{front}); err == nil || !strings.Contains(err.Error(), "stale") {
t.Fatalf("stale apply=%v", err)
}
got, err := m.load()
if err != nil || !reflect.DeepEqual(got, newer) || len(got.Candidates) != 0 {
t.Fatalf("stale transaction overwrote journal: %+v %v", got, err)
}
})
}
}
func TestManagedLegacyPreparedJournalRollback(t *testing.T) {
m, _ := managedFixture(t)
m.fault = func(point string) error {
if point == "replaced" {
return errors.New("death")
}
return nil
}
if err := m.applyRendered([]byte("legacy pending\n")); err == nil {
t.Fatal("fault missed")
}
j, err := m.load()
if err != nil {
t.Fatal(err)
}
j.Version = 1
if err := m.journal(j); err != nil {
t.Fatal(err)
}
m.fault = nil
if err := m.Reconcile(); err != nil {
t.Fatal(err)
}
assertManagedContent(t, m, "prior\n")
j, err = m.load()
if err != nil || j.Hash != managedHash([]byte("prior\n")) || j.State != "committed" || len(j.Candidates) != 0 {
t.Fatalf("legacy rollback: %+v %v", j, err)
}
if _, err := m.ActiveCandidates(nil); err == nil {
t.Fatal("legacy IDs inferred")
}
}