@@ -17,6 +17,101 @@ import (
17
17
"sigs.k8s.io/hierarchical-namespaces/internal/objects"
18
18
)
19
19
20
+ func TestManagedMetaPermissions (t * testing.T ) {
21
+ f := foresttest .Create ("-" ) // a
22
+ h := & Validator {Forest : f }
23
+ l := zap .New ()
24
+
25
+ tests := []struct {
26
+ name string
27
+ labels []api.MetaKVP
28
+ annotations []api.MetaKVP
29
+ allowed bool
30
+ }{
31
+ {name : "managed label" , labels : []api.MetaKVP {{Key : "label.com/team" }}, allowed : true },
32
+ {name : "unmanaged label" , labels : []api.MetaKVP {{Key : "kubernetes.io/metadata.name" }}},
33
+ {name : "managed annotation" , annotations : []api.MetaKVP {{Key : "annot.com/log-index" }}, allowed : true },
34
+ {name : "unmanaged annotation" , annotations : []api.MetaKVP {{Key : "openshift.io/sa.scc.uid-range" }}},
35
+ }
36
+ for _ , tc := range tests {
37
+ t .Run (tc .name , func (t * testing.T ) {
38
+ // Setup
39
+ g := NewWithT (t )
40
+ // In this test we only allow labels/annotations with the defined prefixes
41
+ err := config .SetManagedMeta ([]string {"label\\ .com/.*" }, []string {"annot\\ .com/.*" })
42
+ g .Expect (err ).ToNot (HaveOccurred ())
43
+
44
+ hc := & api.HierarchyConfiguration {Spec : api.HierarchyConfigurationSpec {}}
45
+ hc .ObjectMeta .Name = api .Singleton
46
+ hc .ObjectMeta .Namespace = "a"
47
+ hc .Spec .Labels = tc .labels
48
+ hc .Spec .Annotations = tc .annotations
49
+ req := & request {hc : hc }
50
+
51
+ got := h .handle (context .Background (), l , req )
52
+
53
+ logResult (t , got .AdmissionResponse .Result )
54
+ g .Expect (got .AdmissionResponse .Allowed ).Should (Equal (tc .allowed ))
55
+ })
56
+ }
57
+ }
58
+
59
+ func TestManagedMetaSyntax (t * testing.T ) {
60
+ f := foresttest .Create ("-" ) // a
61
+ h := & Validator {Forest : f }
62
+ l := zap .New ()
63
+
64
+ tests := []struct {
65
+ name string
66
+ labels []api.MetaKVP
67
+ annotations []api.MetaKVP
68
+ allowed bool
69
+ }{
70
+ {name : "ok: prefixed label key" , labels : []api.MetaKVP {{Key : "foo.bar/team" , Value : "v" }}, allowed : true },
71
+ {name : "ok: bare label key" , labels : []api.MetaKVP {{Key : "team" , Value : "v" }}, allowed : true },
72
+ {name : "invalid: label prefix key" , labels : []api.MetaKVP {{Key : "foo;bar/team" , Value : "v" }}},
73
+ {name : "invalid: label name key" , labels : []api.MetaKVP {{Key : "foo.bar/-team" , Value : "v" }}},
74
+ {name : "invalid: empty label key" , labels : []api.MetaKVP {{Key : "" , Value : "v" }}},
75
+
76
+ {name : "ok: label value" , labels : []api.MetaKVP {{Key : "k" , Value : "foo" }}, allowed : true },
77
+ {name : "ok: empty label value" , labels : []api.MetaKVP {{Key : "k" , Value : "" }}, allowed : true },
78
+ {name : "ok: label value special char" , labels : []api.MetaKVP {{Key : "k" , Value : "f-oo" }}, allowed : true },
79
+ {name : "invalid: label value" , labels : []api.MetaKVP {{Key : "k" , Value : "-foo" }}},
80
+
81
+ {name : "ok: prefixed annotation key" , annotations : []api.MetaKVP {{Key : "foo.bar/team" , Value : "v" }}, allowed : true },
82
+ {name : "ok: bare annotation key" , annotations : []api.MetaKVP {{Key : "team" , Value : "v" }}, allowed : true },
83
+ {name : "invalid: annotation prefix key" , annotations : []api.MetaKVP {{Key : "foo;bar/team" , Value : "v" }}},
84
+ {name : "invalid: annotation name key" , annotations : []api.MetaKVP {{Key : "foo.bar/-team" , Value : "v" }}},
85
+ {name : "invalid: empty annotation key" , annotations : []api.MetaKVP {{Key : "" , Value : "v" }}},
86
+
87
+ {name : "ok: annotation value" , annotations : []api.MetaKVP {{Key : "k" , Value : "foo" }}, allowed : true },
88
+ {name : "ok: empty annotation value" , annotations : []api.MetaKVP {{Key : "k" , Value : "" }}, allowed : true },
89
+ {name : "ok: special annotation value" , annotations : []api.MetaKVP {{Key : "k" , Value : ";$+:;/*'\" " }}, allowed : true },
90
+ }
91
+ for _ , tc := range tests {
92
+ t .Run (tc .name , func (t * testing.T ) {
93
+ // Setup
94
+ g := NewWithT (t )
95
+ // For this test we accept any label or annotation not starting with 'h'
96
+ // to allow almost anything - except the hnc.x-k8s.io labels/annotations
97
+ err := config .SetManagedMeta ([]string {"[^h].*" }, []string {"[^h].*" })
98
+ g .Expect (err ).ToNot (HaveOccurred ())
99
+
100
+ hc := & api.HierarchyConfiguration {Spec : api.HierarchyConfigurationSpec {}}
101
+ hc .ObjectMeta .Name = api .Singleton
102
+ hc .ObjectMeta .Namespace = "a"
103
+ hc .Spec .Labels = tc .labels
104
+ hc .Spec .Annotations = tc .annotations
105
+ req := & request {hc : hc }
106
+
107
+ got := h .handle (context .Background (), l , req )
108
+
109
+ logResult (t , got .AdmissionResponse .Result )
110
+ g .Expect (got .AdmissionResponse .Allowed ).Should (Equal (tc .allowed ))
111
+ })
112
+ }
113
+ }
114
+
20
115
func TestStructure (t * testing.T ) {
21
116
f := foresttest .Create ("-a-" ) // a <- b; c
22
117
h := & Validator {Forest : f }
0 commit comments