Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Pashkov committed May 27, 2024
1 parent b4fd8db commit 90b4db0
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 82 deletions.
87 changes: 87 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 @@ -226,6 +227,92 @@ type HTTPProbe struct {
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"`
Expand Down
86 changes: 4 additions & 82 deletions prober/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,10 @@ func Handler(w http.ResponseWriter, r *http.Request, c *config.Config, logger lo
return
}

if module.HTTP.EnableRegexpsFromParams {
var err error
paramRegexps, err := extractHTTPParamRegexps(params)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to extract regular expressions from params: %s", err), http.StatusBadRequest)
return
}
if paramRegexps.FailIfBodyMatchesRegexp != nil {
module.HTTP.FailIfBodyMatchesRegexp = append(module.HTTP.FailIfBodyMatchesRegexp, *paramRegexps.FailIfBodyMatchesRegexp)
}
if paramRegexps.FailIfBodyNotMatchesRegexp != nil {
module.HTTP.FailIfBodyNotMatchesRegexp = append(module.HTTP.FailIfBodyNotMatchesRegexp, *paramRegexps.FailIfBodyNotMatchesRegexp)
}
if paramRegexps.FailIfHeaderMatchesRegexp != nil {
module.HTTP.FailIfHeaderMatchesRegexp = append(module.HTTP.FailIfHeaderMatchesRegexp, *paramRegexps.FailIfHeaderMatchesRegexp)
}
if paramRegexps.FailIfHeaderNotMatchesRegexp != nil {
module.HTTP.FailIfHeaderNotMatchesRegexp = append(module.HTTP.FailIfHeaderNotMatchesRegexp, *paramRegexps.FailIfHeaderNotMatchesRegexp)
}
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)
Expand Down Expand Up @@ -160,69 +145,6 @@ func Handler(w http.ResponseWriter, r *http.Request, c *config.Config, logger lo
h.ServeHTTP(w, r)
}

func extractHTTPParamRegexps(params url.Values) (*config.HTTPRegexps, error) {
var (
dynamicHTTPRegexps config.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) (*config.HeaderMatch, error) {
var dynamicHeaderMatch config.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) (*config.Regexp, error) {
re, err := url.QueryUnescape(a)
if err != nil {
return nil, fmt.Errorf("failed to unescape regexp: %s", err)
}
regexp, err := config.NewRegexp(re)
if err != nil {
return nil, fmt.Errorf("failed to compile regexp: %s", err)
}
return &regexp, nil
}

func setHTTPHost(hostname string, module *config.Module) error {
// By creating a new hashmap and copying values there we
// ensure that the initial configuration remain intact.
Expand Down

0 comments on commit 90b4db0

Please sign in to comment.