-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Implement subnet config validation (fixes #4552) #5364
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5691b82
Implement subnet config validation (fixes #4552)
DrewRomanyk 68c636d
Fix subnet config test for windows
DrewRomanyk 6c8184d
Add format to other v3 configs & remove unix dependency
DrewRomanyk badd4d7
Refactor subnet cidr validator & add new test
DrewRomanyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2846,6 +2846,94 @@ def check_config(self, cfg): | |
| ) | ||
|
|
||
|
|
||
| class SubnetTest(unittest.TestCase): | ||
| INVALID_SUBNET_TYPES = [ | ||
| None, | ||
| False, | ||
| 10, | ||
| ] | ||
|
|
||
| INVALID_SUBNET_MAPPINGS = [ | ||
| "", | ||
| "192.168.0.1/sdfsdfs", | ||
| "192.168.0.1/", | ||
| "192.168.0.1/33", | ||
| "192.168.0.1/01", | ||
| "192.168.0.1", | ||
| "fe80:0000:0000:0000:0204:61ff:fe9d:f156/sdfsdfs", | ||
| "fe80:0000:0000:0000:0204:61ff:fe9d:f156/", | ||
| "fe80:0000:0000:0000:0204:61ff:fe9d:f156/129", | ||
| "fe80:0000:0000:0000:0204:61ff:fe9d:f156/01", | ||
| "fe80:0000:0000:0000:0204:61ff:fe9d:f156", | ||
| "ge80:0000:0000:0000:0204:61ff:fe9d:f156/128", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a test for more than 1 slash as well!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do, good idea! |
||
| "192.168.0.1/31/31", | ||
| ] | ||
|
|
||
| VALID_SUBNET_MAPPINGS = [ | ||
| "192.168.0.1/0", | ||
| "192.168.0.1/32", | ||
| "fe80:0000:0000:0000:0204:61ff:fe9d:f156/0", | ||
| "fe80:0000:0000:0000:0204:61ff:fe9d:f156/128", | ||
| "1:2:3:4:5:6:7:8/0", | ||
| "1::/0", | ||
| "1:2:3:4:5:6:7::/0", | ||
| "1::8/0", | ||
| "1:2:3:4:5:6::8/0", | ||
| "::/0", | ||
| "::8/0", | ||
| "::2:3:4:5:6:7:8/0", | ||
| "fe80::7:8%eth0/0", | ||
| "fe80::7:8%1/0", | ||
| "::255.255.255.255/0", | ||
| "::ffff:255.255.255.255/0", | ||
| "::ffff:0:255.255.255.255/0", | ||
| "2001:db8:3:4::192.0.2.33/0", | ||
| "64:ff9b::192.0.2.33/0", | ||
| ] | ||
|
|
||
| def test_config_invalid_subnet_type_validation(self): | ||
| for invalid_subnet in self.INVALID_SUBNET_TYPES: | ||
| with pytest.raises(ConfigurationError) as exc: | ||
| self.check_config(invalid_subnet) | ||
|
|
||
| assert "contains an invalid type" in exc.value.msg | ||
|
|
||
| def test_config_invalid_subnet_format_validation(self): | ||
| for invalid_subnet in self.INVALID_SUBNET_MAPPINGS: | ||
| with pytest.raises(ConfigurationError) as exc: | ||
| self.check_config(invalid_subnet) | ||
|
|
||
| assert "should use the CIDR format" in exc.value.msg | ||
|
|
||
| def test_config_valid_subnet_format_validation(self): | ||
| for valid_subnet in self.VALID_SUBNET_MAPPINGS: | ||
| self.check_config(valid_subnet) | ||
|
|
||
| def check_config(self, subnet): | ||
| config.load( | ||
| build_config_details({ | ||
| 'version': '3.5', | ||
| 'services': { | ||
| 'web': { | ||
| 'image': 'busybox' | ||
| } | ||
| }, | ||
| 'networks': { | ||
| 'default': { | ||
| 'ipam': { | ||
| 'config': [ | ||
| { | ||
| 'subnet': subnet | ||
| } | ||
| ], | ||
| 'driver': 'default' | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| ) | ||
|
|
||
|
|
||
| class InterpolationTest(unittest.TestCase): | ||
|
|
||
| @mock.patch.dict(os.environ) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unlikely this will ever be fed something that isn't a string, but if it does, this will break when you try to
split()a few lines later.I'd remove the type check altogether.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it seems that this check is common for the format checker methods in order for it to go to the type checker. Otherwise, in the old code, it will fail at the '/' check or in the new code where I just use regex, at the re.match code.
So would you prefer me to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other 2
format_*methods can take integers as well, which is why they check the type. Since we enforcetype: stringon subnet entries, it will exit before ever hitting this method if the type is invalid:Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Joffrey!
If you are using the new format code with those commands then maybe our environments are different, otherwise, it appears that at least on my end it is running the format checker before running the type checker. When I remove the isinstance check and run those two same commands with the same docker compose file:
And when I modify the 3.5 config json schema file, to where subnet requires a number type, the type checker still doesn't complain when I give it a boolean. And when I run the type:number/format:subnet with a valid subnet, it fails at the type checker, but goes through the format checker (I tested by a simple print statement). Could our Json schema libraries be out of sync then?
So then, in this case, could it be:
If I go inside the jsonschema._format.py code, here is a few examples of what I've seen, which they all seem to have that isinstance check even for string only types such as emails
Thank you again for all the assistance/feedback Joffrey, I really appreciate it!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, I'll check out your branch and take a closer look. I'll let you know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, you were right, my bad. I expected the type constraint to be enforced before format validation.