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

Solve #717 Validating config should try to compile regexes #729

Merged
merged 6 commits into from
Jan 6, 2021

Conversation

sshota0809
Copy link
Contributor

Hello, This is my first contribution.

I solved issue #717.
I modified Validating config so that it includes trying to compile regexes and then output error if there are invalid regexes. Also I added test related to them.

Please check if my commit is correct. Thank you for your consideration.!

@brian-brazil
Copy link
Contributor

Thanks for your PR, could we use the same approach as in https://github.com/prometheus/prometheus/blob/master/pkg/relabel/relabel.go ?

@sshota0809
Copy link
Contributor Author

@brian-brazil
Thank you for comment!
I have modified them based on your request. Please check if it seems what you intend.

prober/tcp.go Outdated
level.Error(logger).Log("msg", "Could not compile into regular expression", "regexp", qr.Expect, "err", err)
return false
}
if qr.Expect.Regexp != nil && qr.Expect.Regexp.String() != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't Regexp always non-nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't Regexp always non-nil?

There is the case that config.Module instance is created in prober/tcp_test.go.

module := config.Module{
TCP: config.TCPProbe{
IPProtocolFallback: true,
QueryResponse: []config.QueryResponse{
{Expect: config.MustNewRegexp("^220.*ESMTP.*$")},
{Send: "EHLO tls.prober"},
{Expect: config.MustNewRegexp("^250-STARTTLS")},
{Send: "STARTTLS"},
{Expect: config.MustNewRegexp("^220")},
{StartTLS: true},
{Send: "EHLO tls.prober"},
{Expect: config.MustNewRegexp("^250-AUTH")},
{Send: "QUIT"},
},
TLSConfig: pconfig.TLSConfig{
CAFile: tmpCaFile.Name(),
InsecureSkipVerify: false,
},
},
}

And when Expect isn't explicitly specified as following line, it's possible that qr.Expect.Rege is nil.

{Send: "EHLO tls.prober"},

But that occors only in the test senario. In this case, should I modify prober/tcp_test.go as following.

   {Expect: config.MustNewRegexp(""), Send: "EHLO tls.prober"}, 

Could you give me an advice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just need the first predicate here, only check for nil. Expecting an empty response is valid after all.

Copy link
Contributor Author

@sshota0809 sshota0809 Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just need the first predicate here, only check for nil.

Thank you. I deleted the latter predicate.

config/config.go Outdated
@@ -303,7 +355,7 @@ func (s *HeaderMatch) UnmarshalYAML(unmarshal func(interface{}) error) error {
return errors.New("header name must be set for HTTP header matchers")
}

if s.Regexp == "" {
if s.Regexp.Regexp == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this never nil, and you should be checking for the empty string?

I think you're being inconsistent in how you're treating this.

Copy link
Contributor Author

@sshota0809 sshota0809 Jan 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible that s.Regexp.Regexp is nil when following case. (http.fail_if_header_not_matches.regexp isn't explicitly specified.)

modules:
  http_headers:
    prober: http
    timeout: 5s
    http:
      fail_if_header_not_matches:
        - header: Access-Control-Allow-Origin
          allow_missing: false

And as you said I have to check for the empty string, so I added its condition.

config/config.go Outdated
@@ -289,6 +337,10 @@ func (s *QueryResponse) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal((*plain)(s)); err != nil {
return err
}
if s.Expect.Regexp == nil {
s.Expect = MustNewRegexp("")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not being set and being the empty string are different, if there's handling here it should be in the probe rather than here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted this IF statement to accept nil.

prober/tcp.go Outdated
level.Error(logger).Log("msg", "Could not compile into regular expression", "regexp", qr.Expect, "err", err)
return false
}
if qr.Expect.Regexp != nil && qr.Expect.Regexp.String() != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just need the first predicate here, only check for nil. Expecting an empty response is valid after all.

@sshota0809
Copy link
Contributor Author

@brian-brazil
I appreciate that you've spent much time to review code I wrote.

I'm done with modifying what you mentioned.
Would you check it again?

@brian-brazil brian-brazil merged commit ffd84c6 into prometheus:master Jan 6, 2021
@brian-brazil
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants