Skip to content

Commit 512f74c

Browse files
committed
fix(storage): verify imported recovery isolation in the live Cilium API
1 parent afa8b89 commit 512f74c

8 files changed

Lines changed: 91 additions & 174 deletions

‎scripts/migrate-longhorn-v2/cnpg_cilium_observation.go‎

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,6 @@ func (c client) observeRecoveryCilium(ctx context.Context, input recoveryCiliumO
7979
}
8080
return b, nil
8181
}
82-
readRuntime := func() (recoveryCiliumConfig, error) {
83-
config, e := native("config", "--all", "-o", "json")
84-
if e != nil {
85-
return recoveryCiliumConfig{}, refused
86-
}
87-
version, e := native("version", "-o", "json")
88-
if e != nil {
89-
return recoveryCiliumConfig{}, refused
90-
}
91-
hive, e := native("shell", "hive")
92-
if e != nil {
93-
return recoveryCiliumConfig{}, refused
94-
}
95-
cfg, e := readRecoveryCiliumRuntime(config, version, hive)
96-
if e != nil {
97-
return recoveryCiliumConfig{}, refused
98-
}
99-
return cfg, nil
100-
}
10182
stage = "binding-before"
10283
before, policy, err := readBindings()
10384
if err != nil {
@@ -119,21 +100,27 @@ func (c client) observeRecoveryCilium(ctx context.Context, input recoveryCiliumO
119100
if phase == 1 {
120101
suffix = "after"
121102
}
122-
stage = "runtime-" + suffix
123-
cfg, e := readRuntime()
124-
if e != nil || phase == 1 && cfg != runtime {
103+
stage = "runtime-config-" + suffix
104+
config, e := native("config", "--all", "-o", "json")
105+
if e != nil {
125106
return result, refused
126107
}
127-
runtime = cfg
128-
stage = "policy-import-" + suffix
108+
stage = "runtime-version-" + suffix
109+
version, e := native("version", "-o", "json")
110+
if e != nil {
111+
return result, refused
112+
}
113+
stage = "policy-repository-" + suffix
129114
raw, e := native("policy", "get", "-o", "json")
130115
if e != nil {
131116
return result, refused
132117
}
133-
revision, e := verifyCNPGIsolationImport(raw, policy, input.ArchiveEndpoint, cfg)
134-
if e != nil || phase == 1 && revision != firstRevision {
118+
stage = "runtime-policy-binding-" + suffix
119+
cfg, revision, e := readRecoveryCiliumRuntime(config, version, raw, policy, input.ArchiveEndpoint)
120+
if e != nil || phase == 1 && (cfg != runtime || revision != firstRevision) {
135121
return result, refused
136122
}
123+
runtime = cfg
137124
stage = "endpoint-read-" + suffix
138125
raw, e = native("endpoint", "get", identifier, "-o", "json")
139126
if e != nil {

‎scripts/migrate-longhorn-v2/cnpg_cilium_observation_test.go‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ func recoveryObservationFixture(t *testing.T, change func(string, int, object))
2222
input := recoveryCiliumObservation{Agent: agent, DaemonSet: ds, Node: node, Pod: pod, Policy: policy, ArchiveEndpoint: "https://" + strings.Repeat("a", 32) + ".r2.cloudflarestorage.com"}
2323
config := []byte(`{"ClusterName":"default","EnableNonDefaultDenyPolicies":true,"EnableL7Proxy":true,"RestoreState":true,"policy-enforcement":"default"}`)
2424
version := []byte(`{"Client":{"Version":"1.20.5","Revision":"15012141"},"Daemon":{"Version":"1.20.5","Revision":"15012141"}}`)
25-
hive, err := os.ReadFile("testdata/cilium-hive-synthetic-1.20.5.txt")
26-
if err != nil {
27-
t.Fatal(err)
28-
}
2925
endpoint, err := os.ReadFile("testdata/synthetic-endpoint-get-cilium-1.20.5.json")
3026
if err != nil {
3127
t.Fatal(err)
@@ -65,8 +61,6 @@ func recoveryObservationFixture(t *testing.T, change func(string, int, object))
6561
return config, nil
6662
case "version -o json":
6763
return version, nil
68-
case "shell hive":
69-
return hive, nil
7064
case "policy get -o json":
7165
return isolationResponse(t, entries, 817), nil
7266
case "endpoint get ipv4:192.0.2.17 -o json":

‎scripts/migrate-longhorn-v2/cnpg_cilium_runtime.go‎

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,27 @@ import (
99
const recoveryCiliumSourceCommit = "150121410c147fad575ac5dcbce6de923be69042"
1010

1111
var recoveryCiliumRevision = regexp.MustCompile(`^[0-9a-f]{8,40}$`)
12-
var recoveryPolicyConfigMarker = regexp.MustCompile(`(?m)^.*⚙️ \(types\.PolicyConfig\).*$`)
13-
var recoveryPolicyConfigBlock = regexp.MustCompile(`(?m)^([\t ]*)⚙️ \(types\.PolicyConfig\) \{\r?\n([\t ]+)PolicyDefaultLocalCluster: \(bool\) (true|false)\r?\n([\t ]*)\}\r?$`)
1412

15-
// Inputs come from fixed read-only native commands in the same bound agent
16-
// process: config --all -o json, version -o json, and shell hive. ConfigMap
17-
// contents and default values cannot establish this runtime observation.
18-
// The caller must still prove agent Pod/container continuity around the reads.
19-
func readRecoveryCiliumRuntime(configJSON, versionJSON, hiveText []byte) (recoveryCiliumConfig, error) {
13+
// Bind the fixed native configuration and version reads to the same agent's
14+
// imported, identified policy at one repository revision. The caller must
15+
// prove Pod and container continuity around these reads.
16+
func readRecoveryCiliumRuntime(configJSON, versionJSON, repository []byte, policy object, archiveEndpoint string) (recoveryCiliumConfig, uint64, error) {
2017
var cfg recoveryCiliumConfig
21-
for _, raw := range [][]byte{configJSON, versionJSON, hiveText} {
18+
for _, raw := range [][]byte{configJSON, versionJSON} {
2219
if len(raw) == 0 || len(raw) > 32<<20 {
23-
return cfg, errors.New("native Cilium runtime evidence is empty or exceeds its bound")
20+
return recoveryCiliumConfig{}, 0, errors.New("native Cilium runtime evidence is empty or exceeds its bound")
2421
}
2522
}
2623
var config, version object
2724
if decodeIsolationJSON(configJSON, &config) != nil || decodeIsolationJSON(versionJSON, &version) != nil {
28-
return cfg, errors.New("native Cilium runtime JSON is malformed")
25+
return recoveryCiliumConfig{}, 0, errors.New("native Cilium runtime JSON is malformed")
2926
}
3027
cfg.Version = text(version, "Daemon", "Version")
31-
revision, clientRevision := text(version, "Daemon", "Revision"), text(version, "Client", "Revision")
28+
daemonRevision, clientRevision := text(version, "Daemon", "Revision"), text(version, "Client", "Revision")
3229
// The native version command can return success with an empty Daemon object.
3330
// A matching CLI version by itself never establishes a running daemon.
34-
if cfg.Version != "1.20.5" || text(version, "Client", "Version") != cfg.Version || revision != clientRevision || !recoveryCiliumRevision.MatchString(revision) || !strings.HasPrefix(recoveryCiliumSourceCommit, revision) {
35-
return cfg, errors.New("native Cilium daemon or client does not identify the reviewed build")
31+
if cfg.Version != "1.20.5" || text(version, "Client", "Version") != cfg.Version || daemonRevision != clientRevision || !recoveryCiliumRevision.MatchString(daemonRevision) || !strings.HasPrefix(recoveryCiliumSourceCommit, daemonRevision) {
32+
return recoveryCiliumConfig{}, 0, errors.New("native Cilium daemon or client does not identify the reviewed build")
3633
}
3734
cfg.Cluster = text(config, "ClusterName")
3835
cfg.Enforcement = text(config, "policy-enforcement")
@@ -42,17 +39,17 @@ func readRecoveryCiliumRuntime(configJSON, versionJSON, hiveText []byte) (recove
4239
// Endpoint creation's startup-cache barrier depends on the actual restore
4340
// option, exposed as RestoreState by the native running-config API.
4441
cfg.RestoreState, restoreOK = value(config, "RestoreState").(bool)
45-
// PolicyDefaultLocalCluster is a separate live Hive cell, absent from the
46-
// JSON config API. Match its complete native struct, not a similarly named
47-
// field or an arbitrary occurrence of 'true' elsewhere in the diagnostic.
48-
markers := recoveryPolicyConfigMarker.FindAll(hiveText, -1)
49-
blocks := recoveryPolicyConfigBlock.FindAllSubmatch(hiveText, -1)
50-
if len(markers) != 1 || len(blocks) != 1 || string(blocks[0][1]) != string(blocks[0][4]) || !strings.HasPrefix(string(blocks[0][2]), string(blocks[0][1])) || len(blocks[0][2]) <= len(blocks[0][1]) || string(blocks[0][3]) != "true" || !nonDefaultOK || !l7OK || !restoreOK {
51-
return cfg, errors.New("native Cilium policy configuration is missing, ambiguous or changed")
52-
}
42+
// Cilium's config API omits PolicyDefaultLocalCluster. The policy has no
43+
// explicit cluster selectors, so Cilium adds the reviewed local-cluster
44+
// constraints to its peers only when importing with local normalization.
45+
// The candidate flag becomes evidence after the complete import matches.
5346
cfg.DefaultLocalCluster = true
54-
if !supportedRecoveryCiliumConfig(cfg) {
55-
return cfg, errors.New("native Cilium runtime configuration differs from the reviewed recovery binding")
47+
if !nonDefaultOK || !l7OK || !restoreOK || !supportedRecoveryCiliumConfig(cfg) {
48+
return recoveryCiliumConfig{}, 0, errors.New("native Cilium runtime configuration differs from the reviewed recovery binding")
49+
}
50+
revision, err := verifyCNPGIsolationImport(repository, policy, archiveEndpoint, cfg)
51+
if err != nil {
52+
return recoveryCiliumConfig{}, 0, err
5653
}
57-
return cfg, nil
54+
return cfg, revision, nil
5855
}

‎scripts/migrate-longhorn-v2/cnpg_cilium_runtime_test.go‎

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,14 @@ package main
22

33
import (
44
"encoding/json"
5-
"os"
65
"strings"
76
"testing"
87
)
98

10-
func runtimeCiliumFixture(t *testing.T) (object, object, []byte) {
11-
t.Helper()
12-
h, err := os.ReadFile("testdata/cilium-hive-synthetic-1.20.5.txt")
13-
if err != nil {
14-
t.Fatal(err)
15-
}
9+
func runtimeCiliumFixture() (object, object) {
1610
c := object{"ClusterName": "default", "EnableNonDefaultDenyPolicies": true, "EnableL7Proxy": true, "RestoreState": true, "policy-enforcement": "default"}
1711
v := object{"Client": object{"Version": "1.20.5", "Revision": "15012141"}, "Daemon": object{"Version": "1.20.5", "Revision": "15012141"}}
18-
return c, v, h
12+
return c, v
1913
}
2014

2115
func encodeRuntimeFixture(t *testing.T, o object) []byte {
@@ -27,51 +21,59 @@ func encodeRuntimeFixture(t *testing.T, o object) []byte {
2721
return b
2822
}
2923

30-
func TestRecoveryReadsNativeRuntimeConfigAndHiveCell(t *testing.T) {
31-
c, v, h := runtimeCiliumFixture(t)
32-
cfg, err := readRecoveryCiliumRuntime(encodeRuntimeFixture(t, c), encodeRuntimeFixture(t, v), h)
33-
if err != nil || cfg != reviewedRecoveryCiliumConfig() {
34-
t.Fatalf("native runtime configuration: %+v %v", cfg, err)
24+
func TestRecoveryBindsNativeRuntimeToImportedPolicy(t *testing.T) {
25+
c, v := runtimeCiliumFixture()
26+
policy, entries := isolationFixture(t, "umami-db")
27+
endpoint := "https://" + strings.Repeat("a", 32) + ".r2.cloudflarestorage.com"
28+
config, version := encodeRuntimeFixture(t, c), encodeRuntimeFixture(t, v)
29+
cfg, revision, err := readRecoveryCiliumRuntime(config, version, isolationResponse(t, entries, 817), policy, endpoint)
30+
if err != nil || cfg != reviewedRecoveryCiliumConfig() || revision != 817 {
31+
t.Fatalf("native runtime configuration: %+v at revision %d: %v", cfg, revision, err)
3532
}
36-
// Other object descriptions and function signatures may mention the type.
37-
h = append([]byte("provide: func(types.PolicyConfig)\n"), h...)
38-
if _, err = readRecoveryCiliumRuntime(encodeRuntimeFixture(t, c), encodeRuntimeFixture(t, v), h); err != nil {
39-
t.Fatal(err)
33+
// Cilium's parser omits these peer constraints under all-clusters normalization.
34+
for _, entry := range entries {
35+
peers, _ := entry["L3"].([]any)
36+
for _, peer := range peers {
37+
if selector, ok := peer.(object); ok {
38+
labels, _ := selector["matchLabels"].(object)
39+
delete(labels, "k8s:io.cilium.k8s.policy.cluster")
40+
}
41+
}
42+
}
43+
for _, raw := range [][]byte{nil, isolationResponse(t, entries, 817)} {
44+
cfg, revision, err = readRecoveryCiliumRuntime(config, version, raw, policy, endpoint)
45+
if err == nil || cfg != (recoveryCiliumConfig{}) || revision != 0 {
46+
t.Fatal("missing or nonlocal policy import qualified the runtime")
47+
}
4048
}
4149
}
4250

4351
func TestRecoveryRefusesMissingOrChangedAgentRuntimeEvidence(t *testing.T) {
44-
for name, change := range map[string]func(object, object, []byte) []byte{
45-
"daemon unavailable with successful JSON": func(_, v object, h []byte) []byte { v["Daemon"] = object{}; return h },
46-
"daemon changed": func(_, v object, h []byte) []byte { v["Daemon"].(object)["Version"] = "1.20.4"; return h },
47-
"unreviewed daemon build": func(_, v object, h []byte) []byte { v["Daemon"].(object)["Revision"] = "aaaaaaaa"; return h },
48-
"different client build": func(_, v object, h []byte) []byte { v["Client"].(object)["Revision"] = "150121410"; return h },
49-
"unbound short revision": func(_, v object, h []byte) []byte {
52+
policy, entries := isolationFixture(t, "umami-db")
53+
repository := isolationResponse(t, entries, 817)
54+
endpoint := "https://" + strings.Repeat("a", 32) + ".r2.cloudflarestorage.com"
55+
for name, change := range map[string]func(object, object){
56+
"daemon unavailable with successful JSON": func(_, v object) { v["Daemon"] = object{} },
57+
"daemon changed": func(_, v object) { v["Daemon"].(object)["Version"] = "1.20.4" },
58+
"unreviewed daemon build": func(_, v object) { v["Daemon"].(object)["Revision"] = "aaaaaaaa" },
59+
"different client build": func(_, v object) { v["Client"].(object)["Revision"] = "150121410" },
60+
"unbound short revision": func(_, v object) {
5061
v["Client"].(object)["Revision"] = "150"
5162
v["Daemon"].(object)["Revision"] = "150"
52-
return h
53-
},
54-
"different cluster": func(c, _ object, h []byte) []byte { c["ClusterName"] = "other"; return h },
55-
"missing non-default deny": func(c, _ object, h []byte) []byte { delete(c, "EnableNonDefaultDenyPolicies"); return h },
56-
"L7 disabled": func(c, _ object, h []byte) []byte { c["EnableL7Proxy"] = false; return h },
57-
"quoted boolean": func(c, _ object, h []byte) []byte { c["EnableL7Proxy"] = "true"; return h },
58-
"missing restore state": func(c, _ object, h []byte) []byte { delete(c, "RestoreState"); return h },
59-
"restore state disabled": func(c, _ object, h []byte) []byte { c["RestoreState"] = false; return h },
60-
"quoted restore state": func(c, _ object, h []byte) []byte { c["RestoreState"] = "true"; return h },
61-
"no policy enforcement": func(c, _ object, h []byte) []byte { c["policy-enforcement"] = "never"; return h },
62-
"missing live Hive cell": func(_, _ object, _ []byte) []byte { return nil },
63-
"duplicate live Hive cell": func(_, _ object, h []byte) []byte { return append(h, h...) },
64-
"false local cluster cell": func(_, _ object, h []byte) []byte {
65-
return []byte(strings.ReplaceAll(string(h), "PolicyDefaultLocalCluster: (bool) true", "PolicyDefaultLocalCluster: (bool) false"))
66-
},
67-
"unknown cell field": func(_, _ object, h []byte) []byte {
68-
return []byte(strings.ReplaceAll(string(h), "PolicyDefaultLocalCluster:", "ChangedPolicyDefaultLocalCluster:"))
6963
},
64+
"different cluster": func(c, _ object) { c["ClusterName"] = "other" },
65+
"missing non-default deny": func(c, _ object) { delete(c, "EnableNonDefaultDenyPolicies") },
66+
"L7 disabled": func(c, _ object) { c["EnableL7Proxy"] = false },
67+
"quoted boolean": func(c, _ object) { c["EnableL7Proxy"] = "true" },
68+
"missing restore state": func(c, _ object) { delete(c, "RestoreState") },
69+
"restore state disabled": func(c, _ object) { c["RestoreState"] = false },
70+
"quoted restore state": func(c, _ object) { c["RestoreState"] = "true" },
71+
"no policy enforcement": func(c, _ object) { c["policy-enforcement"] = "never" },
7072
} {
7173
t.Run(name, func(t *testing.T) {
72-
c, v, h := runtimeCiliumFixture(t)
73-
h = change(c, v, h)
74-
if _, err := readRecoveryCiliumRuntime(encodeRuntimeFixture(t, c), encodeRuntimeFixture(t, v), h); err == nil {
74+
c, v := runtimeCiliumFixture()
75+
change(c, v)
76+
if _, _, err := readRecoveryCiliumRuntime(encodeRuntimeFixture(t, c), encodeRuntimeFixture(t, v), repository, policy, endpoint); err == nil {
7577
t.Fatal("unbound runtime evidence accepted")
7678
}
7779
})

‎scripts/migrate-longhorn-v2/cnpg_policy_prepare.go‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,13 @@ func (c client) observeRecoveryPolicyPreparation(ctx context.Context, input reco
8282
if e != nil {
8383
return result, e
8484
}
85-
phase = "runtime-hive"
86-
hive, e := native("shell", "hive")
87-
if e != nil {
88-
return result, e
89-
}
90-
phase = "runtime-interpretation"
91-
runtime, e := readRecoveryCiliumRuntime(configuration, version, hive)
92-
if e != nil {
93-
return result, e
94-
}
95-
phase = "policy-import"
85+
phase = "policy-repository"
9686
raw, e := native("policy", "get", "-o", "json")
9787
if e != nil {
9888
return result, e
9989
}
100-
revision, e := verifyCNPGIsolationImport(raw, input.Policy, input.ArchiveEndpoint, runtime)
90+
phase = "runtime-policy-binding"
91+
runtime, revision, e := readRecoveryCiliumRuntime(configuration, version, raw, input.Policy, input.ArchiveEndpoint)
10192
if e != nil {
10293
return result, e
10394
}

‎scripts/migrate-longhorn-v2/testdata/cilium-hive-synthetic-1.20.5.txt‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎scripts/migrate-longhorn-v2/testdata/cilium-isolation-fixtures.md‎

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,15 @@ separately. No policy labels or expected effects are inferred from live output.
6363

6464
## Runtime configuration fixture
6565

66-
`cilium-hive-synthetic-1.20.5.txt` is a separate synthetic native Hive rendering.
67-
Its generator instantiates only `types.PolicyConfig` with local-cluster policy
68-
normalization enabled. It does not connect to an agent. Reproduce it from the
69-
same verified Cilium checkout with the generator below. The generator removes
70-
trailing horizontal whitespace and extra terminal blank lines; native struct
71-
content and indentation are preserved.
72-
73-
```sh
74-
rtk proxy env GOPROXY=off GOSUMDB=off GOTOOLCHAIN=local \
75-
go run -mod=vendor /absolute/path/to/testdata/cilium-runtime-fixture-generator.go \
76-
> /tmp/cilium-hive-synthetic-1.20.5.txt
77-
```
78-
79-
The runtime payload parser consumes the native `config --all -o json`,
80-
`version -o json`, and `shell hive` results. The JSON test inputs exercise the
81-
root field names verified in Cilium's `config.go` and `version.go`; they are
82-
synthetic inputs, not captured agent responses. The PolicyConfig cell is absent
83-
from the JSON config API, so the complete native Hive struct is required.
84-
Neither payload parser proves continuity between separate reads. A reviewed
85-
workflow must bind the actual agent Pod/container process before and after them.
66+
The runtime parser reads `config --all -o json`, `version -o json` and
67+
`policy get -o json` from the same bound agent. The repository check requires the
68+
exact imported policy at its observed revision. Cilium's config API omits
69+
`PolicyDefaultLocalCluster`. For this policy, the scoped peers acquire the
70+
local-cluster selector when Cilium imports it with local normalization.
71+
Re-running the policy fixture generator with the second argument `false`
72+
omits those selectors and the resulting entries fail the import check.
73+
The runtime config and version test inputs are synthetic. The protected
74+
workflow binds the agent Pod and container identity around both observations.
8675

8776
## Endpoint serialization fixtures
8877

‎scripts/migrate-longhorn-v2/testdata/cilium-runtime-fixture-generator.go‎

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)