Skip to content

Commit

Permalink
pkg/prometheus/promcfg*.go: Validate EnforcedBodySizeLimit
Browse files Browse the repository at this point in the history
Related to #4282

Signed-off-by: Jayapriya Pai <slashpai9@gmail.com>
  • Loading branch information
slashpai committed Sep 27, 2021
1 parent c6b5cc2 commit 410842b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/prometheus/promcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1804,10 +1804,22 @@ func (l *limitEnforcer) addBodySizeLimitsToYAML(cfg yaml.MapSlice, enforcedLimit
return cfg
}

if !validInput("^(0[K,M,G,T,P,E]*B$|[1-9]\\d+[K,M,G,T,P,E]*B$)", enforcedLimit) {
level.Warn(l.logger).Log("msg", "body_size_limit provided is invalid it should be a format of size",
"enforcedBodySizeLimit", enforcedLimit)
return cfg
}

if l.prometheusVersion.LT(semver.MustParse("2.28.0")) {
level.Warn(l.logger).Log("msg", "body_size_limit is only available starting from prometheus 2.28.0",
"version", l.prometheusVersion)
return cfg
}

return append(cfg, yaml.MapItem{Key: "body_size_limit", Value: enforcedLimit})
}

func validInput(regex, input string) bool {
matched, _ := regexp.MatchString(regex, input)
return matched
}
25 changes: 25 additions & 0 deletions pkg/prometheus/promcfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5802,6 +5802,31 @@ alerting:
enforcedBodySizeLimit: "",
expected: expectNoLimit,
},
{
version: "v2.28.0",
enforcedBodySizeLimit: "0B",
expected: fmt.Sprintf(expectLimit, "0B"),
},
{
version: "v2.28.0",
enforcedBodySizeLimit: "100",
expected: expectNoLimit,
},
{
version: "v2.28.0",
enforcedBodySizeLimit: "200kb",
expected: expectNoLimit,
},
{
version: "v2.28.0",
enforcedBodySizeLimit: "300 MB",
expected: expectNoLimit,
},
{
version: "v2.28.0",
enforcedBodySizeLimit: "300M",
expected: expectNoLimit,
},
} {
t.Run(fmt.Sprintf("%s enforcedBodySizeLimit(%s)", tc.version, tc.enforcedBodySizeLimit), func(t *testing.T) {
cg := NewConfigGenerator(log.NewLogfmtLogger(os.Stdout))
Expand Down

0 comments on commit 410842b

Please sign in to comment.