Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 73b2d3d

Browse files
authored
Merge pull request #126 from rahulii/feature-block-changes
Protect all labels and annotations on propagated objects
2 parents cec30f5 + 2dc1a3a commit 73b2d3d

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

internal/objects/validator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ func (v *Validator) handleInherited(ctx context.Context, op k8sadm.Operation, ne
281281
"Cannot modify object propagated from namespace \""+oldSource+"\"")
282282
}
283283

284+
// Check for all the labels and annotations (including HNC and non HNC)
285+
if !reflect.DeepEqual(oldInst.GetLabels(), inst.GetLabels()) || !reflect.DeepEqual(oldInst.GetAnnotations(), inst.GetAnnotations()) {
286+
return webhooks.Deny(metav1.StatusReasonForbidden,
287+
"Cannot modify object propagated from namespace \""+oldSource+"\"")
288+
}
289+
284290
return webhooks.Allow("no illegal updates to propagated object")
285291
}
286292

internal/objects/validator_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,60 @@ func TestUserChanges(t *testing.T) {
210210
},
211211
},
212212
},
213+
}, {
214+
name: "Deny changes to HNC annotations in propagated objects",
215+
fail: true,
216+
oldInst: &unstructured.Unstructured{
217+
Object: map[string]interface{}{
218+
"apiVersion": "v1",
219+
"kind": "Pod",
220+
"metadata": map[string]interface{}{
221+
"labels": map[string]interface{}{
222+
api.LabelInheritedFrom: "foo",
223+
},
224+
},
225+
},
226+
},
227+
inst: &unstructured.Unstructured{
228+
Object: map[string]interface{}{
229+
"apiVersion": "v1",
230+
"kind": "Pod",
231+
"metadata": map[string]interface{}{
232+
"labels": map[string]interface{}{
233+
api.LabelInheritedFrom: "foo",
234+
},
235+
"annotations": map[string]interface{}{
236+
api.AnnotationPropagatePrefix + "/select": "abc",
237+
},
238+
},
239+
},
240+
},
241+
}, {
242+
name: "Deny changes to HNC labels in propagated objects",
243+
fail: true,
244+
oldInst: &unstructured.Unstructured{
245+
Object: map[string]interface{}{
246+
"apiVersion": "v1",
247+
"kind": "Pod",
248+
"metadata": map[string]interface{}{
249+
"labels": map[string]interface{}{
250+
api.LabelInheritedFrom: "foo",
251+
},
252+
},
253+
},
254+
},
255+
inst: &unstructured.Unstructured{
256+
Object: map[string]interface{}{
257+
"apiVersion": "v1",
258+
"kind": "Pod",
259+
"metadata": map[string]interface{}{
260+
"labels": map[string]interface{}{
261+
api.LabelInheritedFrom: "foo",
262+
api.MetaGroup + "/foo": "foo",
263+
},
264+
},
265+
},
266+
},
213267
}, {
214268
name: "Deny spec changes to propagated objects",
215269
fail: true,

0 commit comments

Comments
 (0)