Skip to content

Commit

Permalink
Add strict YAML parsing to reject unknown fields
Browse files Browse the repository at this point in the history
Signed-off-by: AdeshGhadage <[email protected]>
  • Loading branch information
AdeshGhadage committed Jan 15, 2025
1 parent 37d4cfa commit 207d1a6
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pkg/mapper/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package mapper
import (
"regexp"
"time"
"errors"

"github.com/prometheus/client_golang/prometheus"

Expand Down Expand Up @@ -76,9 +77,39 @@ func (m *MetricMapping) UnmarshalYAML(unmarshal func(interface{}) error) error {
m.ObserverType = tmp.TimerType
}

// Implement strict YAML parsing
var raw map[string]interface{}
if err := unmarshal(&raw); err != nil {
return err
}

// List of valid fields in MetricMapping struct
validFields := []string{
"match", "name", "labels", "honor_labels", "observer_type", "legacy_buckets",
"legacy_quantiles", "match_type", "help", "action", "match_metric_type", "ttl",
"summary_options", "histogram_options", "scale",
}

// Check for unexpected fields
for field := range raw {
if !contains(validFields, field) {
return errors.New("unknown field: " + field)
}
}

return nil
}

// Helper function to check if a field is in the list of valid fields
func contains(slice []string, item string) bool {
for _, a := range slice {
if a == item {
return true
}
}
return false
}

type MaybeFloat64 struct {
Set bool
Val float64
Expand Down

0 comments on commit 207d1a6

Please sign in to comment.