Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle HTTP regexps from params #1247

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"math"
"net/textproto"
"net/url"
"os"
"regexp"
"runtime"
Expand Down Expand Up @@ -223,6 +224,100 @@ type HTTPProbe struct {
HTTPClientConfig config.HTTPClientConfig `yaml:"http_client_config,inline"`
Compression string `yaml:"compression,omitempty"`
BodySizeLimit units.Base2Bytes `yaml:"body_size_limit,omitempty"`
EnableRegexpsFromParams bool `yaml:"enable_regexps_from_params,omitempty"`
}

func (s *HTTPProbe) AddRegexpsFromParams(params url.Values) error {
if !s.EnableRegexpsFromParams {
return nil
}
paramRegexps, err := extractParamRegexps(params)
if err != nil {
return err
}
if paramRegexps.FailIfBodyMatchesRegexp != nil {
s.FailIfBodyMatchesRegexp = append(s.FailIfBodyMatchesRegexp, *paramRegexps.FailIfBodyMatchesRegexp)
}
if paramRegexps.FailIfBodyNotMatchesRegexp != nil {
s.FailIfBodyNotMatchesRegexp = append(s.FailIfBodyNotMatchesRegexp, *paramRegexps.FailIfBodyNotMatchesRegexp)
}
if paramRegexps.FailIfHeaderMatchesRegexp != nil {
s.FailIfHeaderMatchesRegexp = append(s.FailIfHeaderMatchesRegexp, *paramRegexps.FailIfHeaderMatchesRegexp)
}
if paramRegexps.FailIfHeaderNotMatchesRegexp != nil {
s.FailIfHeaderNotMatchesRegexp = append(s.FailIfHeaderNotMatchesRegexp, *paramRegexps.FailIfHeaderNotMatchesRegexp)
}
return nil
}

func extractParamRegexps(params url.Values) (*HTTPRegexps, error) {
var (
dynamicHTTPRegexps HTTPRegexps
err error
)

if re := params.Get("fail_if_body_matches_regexp"); re != "" {
dynamicHTTPRegexps.FailIfBodyMatchesRegexp, err = regexpFromURLEncodedString(re)
if err != nil {
return nil, fmt.Errorf("failed to parse fail_if_body_matches_regexp: %s", err)
}
}
if re := params.Get("fail_if_body_not_matches_regexp"); re != "" {
dynamicHTTPRegexps.FailIfBodyNotMatchesRegexp, err = regexpFromURLEncodedString(re)
if err != nil {
return nil, fmt.Errorf("failed to parse fail_if_body_not_matches_regexp: %s", err)
}
}
if re := params.Get("fail_if_header_matches_regexp"); re != "" {
dynamicHTTPRegexps.FailIfHeaderMatchesRegexp, err = extractHeaderMatch(
params.Get("fail_if_header_matches_regexp_header"), re)
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %s", "fail_if_header_matches_regexp", err)
}
}
if re := params.Get("fail_if_header_not_matches_regexp"); re != "" {
dynamicHTTPRegexps.FailIfHeaderNotMatchesRegexp, err = extractHeaderMatch(re, "fail_if_header_not_matches_regexp")
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %s", "fail_if_header_matches_regexp", err)
}
}
return &dynamicHTTPRegexps, nil
}

func extractHeaderMatch(headerParam, headerRegexpParam string) (*HeaderMatch, error) {
var dynamicHeaderMatch HeaderMatch
var err error

if headerParam == "" || headerRegexpParam == "" {
return nil, fmt.Errorf("both fail_if_header_matches_regexp and fail_if_header_matches_regexp_header must be specified")
}

dynamicHeaderMatch.Header = headerParam
regexp, err := regexpFromURLEncodedString(headerRegexpParam)
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %s", headerRegexpParam, err)
}
dynamicHeaderMatch.Regexp = *regexp
return &dynamicHeaderMatch, err
}

func regexpFromURLEncodedString(a string) (*Regexp, error) {
re, err := url.QueryUnescape(a)
if err != nil {
return nil, fmt.Errorf("failed to unescape regexp: %s", err)
}
regexp, err := NewRegexp(re)
if err != nil {
return nil, fmt.Errorf("failed to compile regexp: %s", err)
}
return &regexp, nil
}

type HTTPRegexps struct {
FailIfBodyMatchesRegexp *Regexp `yaml:"fail_if_body_matches_regexp,omitempty"`
FailIfBodyNotMatchesRegexp *Regexp `yaml:"fail_if_body_not_matches_regexp,omitempty"`
FailIfHeaderMatchesRegexp *HeaderMatch `yaml:"fail_if_header_matches,omitempty"`
FailIfHeaderNotMatchesRegexp *HeaderMatch `yaml:"fail_if_header_not_matches,omitempty"`
}

type GRPCProbe struct {
Expand Down
6 changes: 6 additions & 0 deletions prober/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ func Handler(w http.ResponseWriter, r *http.Request, c *config.Config, logger lo
return
}

err := module.HTTP.AddRegexpsFromParams(params)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to parse regexps from URL parameters: %s", err), http.StatusBadRequest)
return
}

timeoutSeconds, err := getTimeout(r, module, timeoutOffset)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to parse timeout from Prometheus header: %s", err), http.StatusInternalServerError)
Expand Down