Skip to content

Commit 87554c4

Browse files
committed
Add MTU validation for networkConfig
1 parent 5f4e6cd commit 87554c4

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

pkg/types/baremetal/validation/platform.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net"
77
"net/http"
88
"net/url"
9+
"strconv"
910
"strings"
1011

1112
"github.com/apparentlymart/go-cidr/cidr"
@@ -294,6 +295,63 @@ func validateHostsCount(hosts []*baremetal.Host, installConfig *types.InstallCon
294295
return nil
295296
}
296297

298+
func validateMTUIsInteger(nmstateYAML []byte, fldPath *field.Path) field.ErrorList {
299+
var config map[string]interface{}
300+
if err := yaml.Unmarshal(nmstateYAML, &config); err != nil {
301+
return field.ErrorList{
302+
field.Invalid(fldPath, string(nmstateYAML), fmt.Sprintf("failed to unmarshal NMState config: %v", err)),
303+
}
304+
}
305+
306+
interfaces, ok := config["interfaces"].([]interface{})
307+
if !ok {
308+
return nil // no interfaces, nothing to check
309+
}
310+
311+
var allErrs field.ErrorList
312+
for idx, iface := range interfaces {
313+
ifaceMap, ok := iface.(map[string]interface{})
314+
if !ok {
315+
continue
316+
}
317+
318+
if mtu, exists := ifaceMap["mtu"]; exists {
319+
switch v := mtu.(type) {
320+
case int, int64, float64: // yaml unmarshals numbers as float64
321+
// ok
322+
case string:
323+
// check if string is actually numeric
324+
if _, err := strconv.Atoi(v); err != nil {
325+
allErrs = append(allErrs,
326+
field.Invalid(
327+
fldPath.Child("interfaces").Index(idx).Child("mtu"),
328+
v,
329+
"mtu must be an integer",
330+
),
331+
)
332+
} else {
333+
allErrs = append(allErrs,
334+
field.Invalid(
335+
fldPath.Child("interfaces").Index(idx).Child("mtu"),
336+
v,
337+
"mtu must be an integer (not quoted string)",
338+
),
339+
)
340+
}
341+
default:
342+
allErrs = append(allErrs,
343+
field.Invalid(
344+
fldPath.Child("interfaces").Index(idx).Child("mtu"),
345+
v,
346+
fmt.Sprintf("mtu must be an integer, got %T", v),
347+
),
348+
)
349+
}
350+
}
351+
}
352+
return allErrs
353+
}
354+
297355
// ensure that the NetworkConfig field contains a valid Yaml string
298356
func validateNetworkConfig(hosts []*baremetal.Host, fldPath *field.Path) (errors field.ErrorList) {
299357
for idx, host := range hosts {
@@ -303,6 +361,10 @@ func validateNetworkConfig(hosts []*baremetal.Host, fldPath *field.Path) (errors
303361
if err != nil {
304362
errors = append(errors, field.Invalid(fldPath.Index(idx).Child("networkConfig"), host.NetworkConfig, fmt.Sprintf("Not a valid yaml: %s", err.Error())))
305363
}
364+
365+
errors = append(errors,
366+
validateMTUIsInteger(host.NetworkConfig.Raw, fldPath.Index(idx).Child("networkConfig"))...,
367+
)
306368
}
307369
}
308370
return

pkg/types/baremetal/validation/platform_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,30 @@ interfaces:
341341
stp-priority: 32`)).build(),
342342
expected: "",
343343
},
344+
{
345+
name: "networkConfig_invalid_mtu_string",
346+
platform: platform().Hosts(
347+
host1().NetworkConfig(`
348+
interfaces:
349+
- name: eth0
350+
type: ethernet
351+
state: up
352+
mtu: "1500"
353+
`)).build(),
354+
expected: `baremetal.hosts\[0\].networkConfig.interfaces\[0\].mtu: Invalid value: "1500": mtu must be an integer \(not quoted string\)`,
355+
},
356+
{
357+
name: "networkConfig_valid_mtu_integer",
358+
platform: platform().Hosts(
359+
host1().NetworkConfig(`
360+
interfaces:
361+
- name: eth0
362+
type: ethernet
363+
state: up
364+
mtu: 1500
365+
`)).build(),
366+
expected: "",
367+
},
344368
}
345369

346370
for _, tc := range cases {

0 commit comments

Comments
 (0)