1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-28 02:37:11 +00:00

Add validation for feature label values

Prevents NFD errors e.g. in the case custom hooks happen to output
invalid label values.
This commit is contained in:
Markus Lehtonen 2018-12-04 18:07:13 +02:00
parent ab5b286bd7
commit 7009fa2b5b

17
main.go
View file

@ -50,6 +50,7 @@ const (
var (
version = "" // Must not be const, set using ldflags at build time
validFeatureNameRe = regexp.MustCompile(`^([-.\w]*)?[A-Za-z0-9]$`)
validFeatureValRe = regexp.MustCompile(`^(([A-Za-z0-9][-.\w]*)?[A-Za-z0-9])?$`)
)
// package loggers
@ -359,20 +360,28 @@ func getFeatureLabels(source source.FeatureSource) (labels Labels, err error) {
if err != nil {
return nil, err
}
for k := range features {
// Validate label
for k, v := range features {
// Validate label name
if !validFeatureNameRe.MatchString(k) {
stderrLogger.Printf("Invalid feature name '%s', ignoring...", k)
continue
}
prefix := source.Name() + "-"
switch source.(type) {
case local.Source:
// Do not prefix labels from the hooks
prefix = ""
}
labels[fmt.Sprintf("%s%s", prefix, k)] = fmt.Sprintf("%v", features[k])
label := prefix + k
// Validate label value
value := fmt.Sprintf("%v", v)
if !validFeatureValRe.MatchString(value) {
stderrLogger.Printf("Invalid value '%s' for label '%s', ignoring...", value, label)
continue
}
labels[label] = value
}
return labels, nil
}