Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Changing util's validation to raise on exceptions #117

Merged
merged 1 commit into from
Jun 23, 2016

Conversation

insequent
Copy link
Contributor

These validation functions are used by clients such as calico-containers, and when they fail, users may want to know why such validations are failing. With this feature, pre-existing code will continue to work as is, however clients now have the option of returning the error (exception) for further diagnostics.

It feels a touch clunky with the way this is implemented, but I didn't want to break any existing code, and this seemed the simplest way. I'm definitely open to any ideas on alternatives.

@CLAassistant
Copy link

CLAassistant commented Jun 9, 2016

CLA assistant check
All committers have signed the CLA.

@insequent
Copy link
Contributor Author

insequent commented Jun 9, 2016

Added logic for version=None in validate_ip(). Seems more useful for it to be able to handle no version and just check if the IP is a valid.

Also, fixed my email to use the one I signed the CLA with.

@insequent
Copy link
Contributor Author

Last update pending review (I promise). I noticed multiple functions were missing complete docstrings for the new return_error param, and one had no param or return at all. Added those in. This was a purely cosmetic update.

@matthewdupre
Copy link
Member

Thanks for contributing @insequent! I'm sorry we got this wrong from the start. I'll make any detailed comments against the code, but first cover the general form of the validation functions.

I had a chat with some of the team, and we think the least clunky (but still back compatible) approach is probably to rename the functions to verify_, change them to return nothing and raise exceptions when needed, and then add the validate_ functions back in to point to them.

Maybe we could decorate the old validate_ functions to log a deprecation warning, call the new verify_ equivalent and return accordingly?

What do you think?


if not valid and return_error:
err = ValueError("ICMP type is invalid. Value must be between 0 and "
"255 ('{0}' given).".format(icmp_type))
Copy link
Member

Choose a reason for hiding this comment

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

The range includes 0 (but not 255).

@matthewdupre
Copy link
Member

Detail-wise, I found a couple of off-by-ones in the messages, but otherwise looks great - thanks :).

@insequent
Copy link
Contributor Author

That makes sense and sounds much cleaner. I'll get right on it!

@insequent insequent force-pushed the master branch 2 times, most recently from 590c9a6 to c95cb34 Compare June 10, 2016 23:23
@insequent
Copy link
Contributor Author

insequent commented Jun 10, 2016

This commit should be closer in line with what you're looking for. However, I did take a few liberties this time:

  1. I changed the order of the functions to be alphabetical (easier to find that way)
  2. ICMP Types are now inclusively 0 to 255. I have no qualms changing this back, but it seems some people do use 255 as ICMP type (iptables in particular).
  3. I changed everything I could to satisfy pylint/pep8 (cosmetic changes). The too long lines for MOCK_IP_ADDR and its friends in the test_util.py could not be changed without making the string look ugly, so I left those as is (you could do some trickiness with string concatenation, but the full example is much easier to read altogether).

@insequent
Copy link
Contributor Author

insequent commented Jun 10, 2016

Oh, as to deprecated decorators, I love the idea, but wasn't sure how you all would want them. As you can see in the code, I just put DEPRECATED into the docstrings. Preferably, you would make a custom decorator that logs a warning, however the logging in these modules are super limited (and uses a NullHandler?).

in_range = False
if not in_range:
break
return return_bool(validate_asn, asn)
Copy link
Member

Choose a reason for hiding this comment

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

Should call verify_, not validate_.

Copy link
Contributor Author

@insequent insequent Jun 13, 2016

Choose a reason for hiding this comment

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

Oh! I didn't even notice there's no unit tests for validate_asn. I'll fix that as well.

@insequent
Copy link
Contributor Author

insequent commented Jun 13, 2016

Added unit tests for validate_asn and verify_asn. These will catch typos like the one I made, swapping the function names on accident. Note that I had to rework the try/except block for verify_asn, because the outer try block was catching all the raises inside the try block (another bug found by the unit tests).


:param cidr: IP addr and associated routing prefix
:type cidr: str
:return: True if valid IP, False if invalid
Copy link
Member

Choose a reason for hiding this comment

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

Should be returns None.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This comment is invalid now, although Github's page doesn't seem to think so. Thought it was my browser caching it, but it shows the same way in others. shrug

Copy link
Member

Choose a reason for hiding this comment

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

I guess the comment might have moved from verify_ to validate_ as the diff changed underneath it? Either that or it was just a bogus comment in the first place.

Weird, but you're right - it's definitely fixed.

@matthewdupre
Copy link
Member

OK - I promise this is the last round of comments! Thanks for a very comprehensive PR!

@insequent insequent force-pushed the master branch 3 times, most recently from a6ed38f to b9f58a5 Compare June 16, 2016 02:18
@insequent
Copy link
Contributor Author

insequent commented Jun 16, 2016

Whew! Ok, so I took things even further, so definitely double check me.

  1. Made specific custom exceptions
  2. Changed (verify|validate)_hostname to allow IPv6 hostnames. This is kinda a big one, so definitely let me know if you want me to change that back.
  3. Changed LOG to _log. I may make a PR for the whole module at some point, unless you all just really like the current name (technically a pep8 violation).
  4. Changed return_bool to _return_bool.
  5. Changed try blocks to be as specific as possible. This is what I originally thought you were talking about @matthewdupre, not just _return_bool (oops).

@insequent insequent changed the title Adding optional error return on validation functions Changing util's validation to raise on exceptions Jun 16, 2016
raise RangeValidationError(err_mess)

# NOTE: The real limit in DNS is 255 octets (253 chars) or 254 chars
# if you include the root domain (i.e. a period on the end). RFC1035
Copy link
Member

Choose a reason for hiding this comment

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

Nice.

@matthewdupre
Copy link
Member

  1. One remaining thing - I'd like a single custom base class, so people can just catch everything, regardless of what validation we add under the covers.
  2. I'm OK with taking this, although I'm not 100% on how it would work in practice (1::2 vs 1::0:2 etc.),
  3. You're right, but I'm not sure it's worth making the change. I'll ask the team about pointing a linter at PRs though (maybe https://github.com/markstory/lint-review or something?).
  4. Sorry if I wasn't clear!

@insequent
Copy link
Contributor Author

That makes perfect sense. I'll add single base class.

To retain compatability with legacy code, a new method was created
with the name changed from 'validate_<object>' to 'verify_<object>'
@insequent
Copy link
Contributor Author

The catch-all exception class is now ValidationError.

@matthewdupre
Copy link
Member

Sorry it took me so long to get back to this: all looks great now.

Thanks for the contribution :)

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

Successfully merging this pull request may close these issues.

4 participants