Skip to content

Commit

Permalink
Add validation for indices array in static filters
Browse files Browse the repository at this point in the history
Signed-off-by: Sébastien Coavoux <[email protected]>
  • Loading branch information
sebastien-coavoux committed Sep 19, 2022
1 parent d4644eb commit 34d10d1
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion generator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ package main

import (
"fmt"

"github.com/prometheus/snmp_exporter/config"
"strconv"
)

// The generator config.
Expand Down Expand Up @@ -53,6 +53,26 @@ type ModuleConfig struct {
Filters config.Filters `yaml:"filters,omitempty"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *ModuleConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain ModuleConfig
if err := unmarshal((*plain)(c)); err != nil {
return err
}

// Ensure indices in static filters are integer for input validation.
for _, filter := range c.Filters.Static {
for _, index := range filter.Indices {
_, err := strconv.Atoi(index)
if err != nil {
return fmt.Errorf("invalid index '%s'. Index must be integer", index)
}
}
}

return nil
}

type Lookup struct {
SourceIndexes []string `yaml:"source_indexes"`
Lookup string `yaml:"lookup"`
Expand Down

0 comments on commit 34d10d1

Please sign in to comment.