Skip to content

Commit 137fa9d

Browse files
committed
fix(storage): accept native Kubernetes default-deny in archive recovery
1 parent ac00b69 commit 137fa9d

4 files changed

Lines changed: 134 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ func verifyRecoveryPolicyAllowances(entries, expected []object, namespace string
2626
body[key] = value
2727
}
2828
}
29+
// Kubernetes NetworkPolicy has nil peers and ports where a Cilium
30+
// NetworkPolicy default-deny has empty lists. Both grant no traffic.
31+
// Retain the explicit field presence and match all other fields.
32+
peers, hasPeers := body["L3"]
33+
ports, hasPorts := body["L4"]
34+
if body["DefaultDeny"] == true && hasPeers && peers == nil && hasPorts && ports == nil {
35+
body["L3"], body["L4"] = []any{}, []any{}
36+
}
2937
raw, err := json.Marshal(body)
3038
if err != nil {
3139
return ""

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,41 @@ func TestRecoveryIsolationAccountsForTheReviewedGeneratedBaseline(t *testing.T)
2424
}
2525
}
2626

27+
// Cilium's native Kubernetes NetworkPolicy parser emits null peers and ports for default-deny.
28+
func TestRecoveryIsolationAcceptsKubernetesDefaultDeny(t *testing.T) {
29+
policy, entries := isolationFixture(t, "umami-db")
30+
raw, err := os.ReadFile("testdata/cnpg-baseline-knp-entries-cilium-1.20.5.json")
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
var generated []object
35+
if json.Unmarshal(raw, &generated) != nil || len(generated) != 2 {
36+
t.Fatal("invalid native Kubernetes default-deny fixture")
37+
}
38+
endpoint := "https://" + strings.Repeat("a", 32) + ".r2.cloudflarestorage.com"
39+
cfg := reviewedRecoveryCiliumConfig()
40+
if _, err = verifyCNPGIsolationImport(isolationResponse(t, append(entries, generated...), 817), policy, endpoint, cfg); err != nil {
41+
t.Fatalf("native Kubernetes default-deny entries refused: %v", err)
42+
}
43+
for name, widen := range map[string]func(object){
44+
"unrestricted peers": func(entry object) { entry["L3"] = []any{object{}} },
45+
"unspecified peers": func(entry object) { delete(entry, "L3") },
46+
"different tier": func(entry object) { entry["Tier"] = 100 },
47+
"deny disabled": func(entry object) { entry["DefaultDeny"] = false },
48+
} {
49+
t.Run(name, func(t *testing.T) {
50+
entry, err := cloneRecoverySpec(generated[0])
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
widen(entry)
55+
if _, err = verifyCNPGIsolationImport(isolationResponse(t, append(entries, entry), 817), policy, endpoint, cfg); err == nil {
56+
t.Fatal("unexpected translation allowed")
57+
}
58+
})
59+
}
60+
}
61+
2762
// TestRecoveryIsolationCannotWidenTheGeneratedResolverAllowance guards the baseline's destination and port bounds.
2863
func TestRecoveryIsolationCannotWidenTheGeneratedResolverAllowance(t *testing.T) {
2964
for name, mutate := range map[string]func(object){

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ resolver-only UDP/TCP port-53 allowance. The latter is an existing platform
5252
allowance, including its absence of DNS-layer restrictions. The owned recovery
5353
policy retains its DNS proxy rule for archive FQDN resolution.
5454

55+
The same admission policy generates a Kubernetes `NetworkPolicy` named
56+
`default-deny` with an empty Pod selector and both policy directions.
57+
`cnpg-baseline-knp-entries-cilium-1.20.5.json` is the v1.20.5
58+
`pkg/k8s.ParseNetworkPolicy` output for that policy with synthetic namespace
59+
`longhorn-db-proof-umami-db-1234` and UID
60+
`44444444-4444-4444-8444-444444444444`. Its two entries have explicit null
61+
peers and ports, which grant no traffic just like the empty lists on the
62+
Cilium default-deny entries. Only these paired, empty, enforced directions
63+
normalize in the allowance comparison. A missing field or a wildcard peer
64+
still fails.
65+
5566
The repository-wide check examines all potentially applicable allowances,
5667
including entries without the owned CNP's labels. Apart from identity labels
5768
and the separately validated subject selector, their complete effect must
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[
2+
{
3+
"Tier": 200,
4+
"Priority": 0,
5+
"Authentication": null,
6+
"Log": {},
7+
"Subject": {
8+
"matchLabels": {
9+
"k8s:io.kubernetes.pod.namespace": "longhorn-db-proof-umami-db-1234"
10+
}
11+
},
12+
"L3": null,
13+
"L4": null,
14+
"Labels": [
15+
{
16+
"key": "io.cilium.k8s.policy.derived-from",
17+
"value": "NetworkPolicy",
18+
"source": "k8s"
19+
},
20+
{
21+
"key": "io.cilium.k8s.policy.name",
22+
"value": "default-deny",
23+
"source": "k8s"
24+
},
25+
{
26+
"key": "io.cilium.k8s.policy.namespace",
27+
"value": "longhorn-db-proof-umami-db-1234",
28+
"source": "k8s"
29+
},
30+
{
31+
"key": "io.cilium.k8s.policy.uid",
32+
"value": "44444444-4444-4444-8444-444444444444",
33+
"source": "k8s"
34+
}
35+
],
36+
"DefaultDeny": true,
37+
"Verdict": 0,
38+
"Ingress": true,
39+
"Node": false
40+
},
41+
{
42+
"Tier": 200,
43+
"Priority": 0,
44+
"Authentication": null,
45+
"Log": {},
46+
"Subject": {
47+
"matchLabels": {
48+
"k8s:io.kubernetes.pod.namespace": "longhorn-db-proof-umami-db-1234"
49+
}
50+
},
51+
"L3": null,
52+
"L4": null,
53+
"Labels": [
54+
{
55+
"key": "io.cilium.k8s.policy.derived-from",
56+
"value": "NetworkPolicy",
57+
"source": "k8s"
58+
},
59+
{
60+
"key": "io.cilium.k8s.policy.name",
61+
"value": "default-deny",
62+
"source": "k8s"
63+
},
64+
{
65+
"key": "io.cilium.k8s.policy.namespace",
66+
"value": "longhorn-db-proof-umami-db-1234",
67+
"source": "k8s"
68+
},
69+
{
70+
"key": "io.cilium.k8s.policy.uid",
71+
"value": "44444444-4444-4444-8444-444444444444",
72+
"source": "k8s"
73+
}
74+
],
75+
"DefaultDeny": true,
76+
"Verdict": 0,
77+
"Ingress": false,
78+
"Node": false
79+
}
80+
]

0 commit comments

Comments
 (0)