Skip to content

Commit 94364b7

Browse files
authored
restrict host field in probe (#2309)
Signed-off-by: 守辰 <shouchen.zz@alibaba-inc.com>
1 parent 0e6f0d2 commit 94364b7

2 files changed

Lines changed: 50 additions & 8 deletions

File tree

pkg/webhook/podprobemarker/validating/probe_create_update_handler.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,14 @@ func validateExecAction(exec *corev1.ExecAction, fldPath *field.Path) field.Erro
241241
}
242242

243243
func validateTCPSocketAction(tcp *corev1.TCPSocketAction, fldPath *field.Path) field.ErrorList {
244-
return ValidatePortNumOrName(tcp.Port, fldPath.Child("port"))
244+
allErrors := field.ErrorList{}
245+
if len(tcp.Host) > 0 {
246+
allErrors = append(allErrors, field.Invalid(fldPath.Child("host"), tcp.Host, "host field in probes is not allowed"))
247+
}
248+
249+
allErrors = append(allErrors, ValidatePortNumOrName(tcp.Port, fldPath.Child("port"))...)
250+
251+
return allErrors
245252
}
246253

247254
var supportedHTTPSchemes = sets.New(corev1.URISchemeHTTP, corev1.URISchemeHTTPS)
@@ -251,6 +258,10 @@ func validateHTTPGetAction(http *corev1.HTTPGetAction, fldPath *field.Path) fiel
251258
if len(http.Path) == 0 {
252259
allErrors = append(allErrors, field.Required(fldPath.Child("path"), ""))
253260
}
261+
262+
if len(http.Host) > 0 {
263+
allErrors = append(allErrors, field.Invalid(fldPath.Child("host"), http.Host, "host field in probes is not allowed"))
264+
}
254265
if _, err := url.Parse(http.Path); err != nil {
255266
allErrors = append(allErrors, field.Invalid(fldPath.Child("path"), http.Path, "must be a valid URL path"))
256267
}

pkg/webhook/podprobemarker/validating/probe_validating_test.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,24 @@ func TestValidatingPodProbeMarker(t *testing.T) {
225225
},
226226
expectErrList: 1,
227227
},
228+
{
229+
name: "test10, invalid ppm",
230+
getPpm: func() *appsv1alpha1.PodProbeMarker {
231+
ppm := ppmDemo.DeepCopy()
232+
ppm.Spec.Probes[0].Name = ""
233+
return ppm
234+
},
235+
expectErrList: 1,
236+
},
237+
{
238+
name: "test11, invalid ppm",
239+
getPpm: func() *appsv1alpha1.PodProbeMarker {
240+
ppm := ppmDemo.DeepCopy()
241+
ppm.Spec.Probes[0].ContainerName = ""
242+
return ppm
243+
},
244+
expectErrList: 1,
245+
},
228246
}
229247

230248
decoder := admission.NewDecoder(scheme)
@@ -246,11 +264,9 @@ func TestValidateHandler(t *testing.T) {
246264
{Exec: &corev1.ExecAction{Command: []string{"echo"}}},
247265
{TCPSocket: &corev1.TCPSocketAction{
248266
Port: intstr.IntOrString{Type: intstr.Int, IntVal: int32(8000)},
249-
Host: "3.3.3.3",
250267
}},
251268
{TCPSocket: &corev1.TCPSocketAction{
252269
Port: intstr.IntOrString{Type: intstr.String, StrVal: "container-port"},
253-
Host: "3.3.3.3",
254270
}},
255271
}
256272
for _, h := range successCases {
@@ -264,11 +280,9 @@ func TestValidateHandler(t *testing.T) {
264280
{Exec: &corev1.ExecAction{Command: []string{}}},
265281
{TCPSocket: &corev1.TCPSocketAction{
266282
Port: intstr.IntOrString{Type: intstr.String, StrVal: "container-port-v2"},
267-
Host: "3.3.3.3",
268283
}},
269284
{TCPSocket: &corev1.TCPSocketAction{
270285
Port: intstr.IntOrString{Type: intstr.Int, IntVal: -1},
271-
Host: "3.3.3.3",
272286
}},
273287
{HTTPGet: &corev1.HTTPGetAction{Path: "", Port: intstr.FromInt(0), Host: ""}},
274288
{HTTPGet: &corev1.HTTPGetAction{Path: "/foo", Port: intstr.FromInt(65536), Host: "host"}},
@@ -277,7 +291,6 @@ func TestValidateHandler(t *testing.T) {
277291
Exec: &corev1.ExecAction{Command: []string{}},
278292
TCPSocket: &corev1.TCPSocketAction{
279293
Port: intstr.IntOrString{Type: intstr.String, StrVal: "container-port-v2"},
280-
Host: "3.3.3.3",
281294
},
282295
},
283296
{
@@ -287,15 +300,13 @@ func TestValidateHandler(t *testing.T) {
287300
{
288301
TCPSocket: &corev1.TCPSocketAction{
289302
Port: intstr.IntOrString{Type: intstr.String, StrVal: "container-port-v2"},
290-
Host: "3.3.3.3",
291303
},
292304
HTTPGet: &corev1.HTTPGetAction{Path: "", Port: intstr.FromString(""), Host: ""},
293305
},
294306
{
295307
Exec: &corev1.ExecAction{Command: []string{}},
296308
TCPSocket: &corev1.TCPSocketAction{
297309
Port: intstr.IntOrString{Type: intstr.String, StrVal: "container-port-v2"},
298-
Host: "3.3.3.3",
299310
},
300311
HTTPGet: &corev1.HTTPGetAction{Path: "", Port: intstr.FromString(""), Host: ""},
301312
},
@@ -447,6 +458,16 @@ func TestValidateTCPSocketAction(t *testing.T) {
447458
},
448459
fldPath: field.NewPath("field"),
449460
},
461+
{
462+
tcp: &corev1.TCPSocketAction{
463+
Port: intstr.IntOrString{
464+
Type: intstr.Int,
465+
IntVal: 80,
466+
},
467+
Host: "127.0.0.1",
468+
},
469+
fldPath: field.NewPath("field"),
470+
},
450471
}
451472
for _, cs := range errorCases {
452473
if getErrs := validateTCPSocketAction(cs.tcp, cs.fldPath); len(getErrs) == 0 {
@@ -552,6 +573,16 @@ func TestValidateHTTPGetAction(t *testing.T) {
552573
},
553574
expectErrs: 1,
554575
},
576+
{
577+
name: "invalid host field",
578+
httpGet: &corev1.HTTPGetAction{
579+
Host: "127.0.0.1",
580+
Path: "/healthz",
581+
Port: intstr.FromInt(8080),
582+
Scheme: corev1.URISchemeHTTP,
583+
},
584+
expectErrs: 1,
585+
},
555586
}
556587

557588
for _, tc := range testCases {

0 commit comments

Comments
 (0)