@@ -2,20 +2,14 @@ package main
22
33import (
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
2115func 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
4351func 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 })
0 commit comments