Skip to content

pkg/prometheus: Add validation for proberSpec url field#4483

Merged
simonpasquier merged 1 commit intoprometheus-operator:mainfrom
slashpai:probe_url
Jan 11, 2022
Merged

pkg/prometheus: Add validation for proberSpec url field#4483
simonpasquier merged 1 commit intoprometheus-operator:mainfrom
slashpai:probe_url

Conversation

@slashpai
Copy link
Contributor

@slashpai slashpai commented Jan 6, 2022

Fixes #4476

Signed-off-by: Jayapriya Pai slashpai9@gmail.com

Description

Describe the big picture of your changes here to communicate to the maintainers why we should accept this pull request.
If it fixes a bug or resolves a feature request, be sure to link to that issue.

Add validation for proberSpec url field to avoid invalid url being passed

Type of change

What type of changes does your code introduce to the Prometheus operator? Put an x in the box that apply.

  • CHANGE (fix or feature that would cause existing functionality to not work as expected)
  • FEATURE (non-breaking change which adds functionality)
  • BUGFIX (non-breaking change which fixes an issue)
  • ENHANCEMENT (non-breaking change which improves existing functionality)
  • NONE (if none of the other choices apply. Example, tooling, build system, CI, docs, etc.)

Changelog entry

Please put a one-line changelog entry below. This will be copied to the changelog file during the release process.

Added validation for proberSpec url field in ProbeSpec

@slashpai slashpai requested a review from a team as a code owner January 6, 2022 16:26
@slashpai slashpai force-pushed the probe_url branch 3 times, most recently from c98449e to 4ddb083 Compare January 6, 2022 16:34
@slashpai slashpai force-pushed the probe_url branch 2 times, most recently from c5d8915 to 49c6238 Compare January 7, 2022 08:17
@slashpai
Copy link
Contributor Author

slashpai commented Jan 7, 2022

@fpetkovski I updated, can you review again?

Copy link
Contributor

@fpetkovski fpetkovski left a comment

Choose a reason for hiding this comment

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

Nice 👍

Copy link
Contributor

@simonpasquier simonpasquier left a comment

Choose a reason for hiding this comment

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

side-note: validateProberURL() is more strict than what Prometheus currently checks but it's fine for me.

return nil
}

func validateProberURL(url string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO it would be better if validateProberURL returns an error. This way the returned message would be clearer (e.g. "invalid host" vs. "invalid port") and it would be consistent with the other validate*() functions.

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 actually wrote with error as return but found it a bit tricky when checked with different types of inputs. Like the case url passed is http://blackbox-exporter.example.com:8080 in this case when we split the string on : it will take /blackbox-exporter.example.com as hostPort[1] and if we validate and return error as invalid port it could be more confusing

Comment on lines +2168 to +2172
if strings.Contains(url, ":") {
hostPort := strings.Split(url, ":")
return govalidator.IsHost(hostPort[0]) && govalidator.IsPort(hostPort[1])
}
return govalidator.IsHost(url)
Copy link
Contributor

Choose a reason for hiding this comment

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

Taking into account #4481, it would be more convenient if the function returns an error (only one place where the error message is constructed).
Changing the function to return an error instead of a bool could look like:

Suggested change
if strings.Contains(url, ":") {
hostPort := strings.Split(url, ":")
return govalidator.IsHost(hostPort[0]) && govalidator.IsPort(hostPort[1])
}
return govalidator.IsHost(url)
hostPort := strings.Split(url, ":")
if !govalidator.IsHost(hostPort[0]) {
return errors.Errorf("invalid host: %q", hostPort[0])
}
if len(hostPort[1]) > 1 && govalidator.IsPort(hostPort[1]) {
return errors.Errorf("invalid port: %q", hostPort[1])
}
return 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.

Thank you. I added a case for handling url scheme specified as well. Else error may be reported as invalid port for a case like

      proberSpec: monitoringv1.ProberSpec{
        URL: "https://blackbox-exporter.example.com",
      }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

discussed offline and decided we don't need to handle each case separately so removing handling the url scheme case separately

@slashpai slashpai force-pushed the probe_url branch 3 times, most recently from 1d142e4 to e52c9db Compare January 7, 2022 14:15
Fixes prometheus-operator#4476

Signed-off-by: Jayapriya Pai <slashpai9@gmail.com>
@slashpai
Copy link
Contributor Author

slashpai commented Jan 7, 2022

@simonpasquier I updated as per your suggestion, please review :)

Copy link
Contributor

@simonpasquier simonpasquier left a comment

Choose a reason for hiding this comment

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

lgtm

@simonpasquier simonpasquier merged commit 0e732af into prometheus-operator:main Jan 11, 2022
@slashpai slashpai deleted the probe_url branch October 13, 2022 16:47
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.

The operator should validate the "url" field in the Probe resource

4 participants