Skip to content

Commit

Permalink
Add validation for feature label values
Browse files Browse the repository at this point in the history
Prevents NFD errors e.g. in the case custom hooks happen to output
invalid label values.
  • Loading branch information
marquiz committed Dec 5, 2018
1 parent 1ccd69e commit 3bb9b05
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (

"github.com/docopt/docopt-go"
"github.com/ghodss/yaml"
api "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sclient "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"sigs.k8s.io/node-feature-discovery/source"
"sigs.k8s.io/node-feature-discovery/source/cpuid"
"sigs.k8s.io/node-feature-discovery/source/fake"
Expand All @@ -26,10 +30,6 @@ import (
"sigs.k8s.io/node-feature-discovery/source/rdt"
"sigs.k8s.io/node-feature-discovery/source/selinux"
"sigs.k8s.io/node-feature-discovery/source/storage"
api "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sclient "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
)

const (
Expand All @@ -49,6 +49,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
Expand Down Expand Up @@ -357,20 +358,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
}
Expand Down

0 comments on commit 3bb9b05

Please sign in to comment.