-
Notifications
You must be signed in to change notification settings - Fork 116
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
OCPBUGS-39403 - Fix parseIPList
to Continue Processing After Invalid IPs and Return Valid IPs
#621
Open
mJace
wants to merge
1
commit into
openshift:master
Choose a base branch
from
mJace:fix_whitelist_ip_parser
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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 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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -981,33 +981,40 @@ func TestGenerateHAProxyWhiteListFile(t *testing.T) { | |||||||||||||
|
||||||||||||||
func TestParseIPList(t *testing.T) { | ||||||||||||||
testCases := []struct { | ||||||||||||||
name string | ||||||||||||||
input string | ||||||||||||||
expectedEmpty bool | ||||||||||||||
name string | ||||||||||||||
input string | ||||||||||||||
expectedEmpty bool | ||||||||||||||
expectedReturn string | ||||||||||||||
}{ | ||||||||||||||
{ | ||||||||||||||
name: "All mixed", | ||||||||||||||
input: "192.168.1.0 2001:0db8:85a3:0000:0000:8a2e:0370:7334 172.16.14.10/24 2001:0db8:85a3::8a2e:370:10/64 64:ff9b::192.168.0.1 2600:14a0::/40", | ||||||||||||||
name: "All mixed", | ||||||||||||||
input: "192.168.1.0 2001:0db8:85a3:0000:0000:8a2e:0370:7334 172.16.14.10/24 2001:0db8:85a3::8a2e:370:10/64 64:ff9b::192.168.0.1 2600:14a0::/40", | ||||||||||||||
expectedReturn: "192.168.1.0 2001:0db8:85a3:0000:0000:8a2e:0370:7334 172.16.14.10/24 2001:0db8:85a3::8a2e:370:10/64 64:ff9b::192.168.0.1 2600:14a0::/40", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
name: "IPs only", | ||||||||||||||
input: "192.168.1.0 2001:0db8:85a3:0000:0000:8a2e:0370:7334 64:ff9b::192.168.0.1 172.16.14.10", | ||||||||||||||
name: "IPs only", | ||||||||||||||
input: "192.168.1.0 2001:0db8:85a3:0000:0000:8a2e:0370:7334 64:ff9b::192.168.0.1 172.16.14.10", | ||||||||||||||
expectedReturn: "192.168.1.0 2001:0db8:85a3:0000:0000:8a2e:0370:7334 64:ff9b::192.168.0.1 172.16.14.10", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
name: "CIDRs only", | ||||||||||||||
input: "192.168.1.0/16 2001:0db8:85a3:0000:0000:8a2e:0370:7334/48 172.16.14.10/24 2001:0db8:85a3::8a2e:0370:10/64 2600:14a0::/40", | ||||||||||||||
name: "CIDRs only", | ||||||||||||||
input: "192.168.1.0/16 2001:0db8:85a3:0000:0000:8a2e:0370:7334/48 172.16.14.10/24 2001:0db8:85a3::8a2e:0370:10/64 2600:14a0::/40", | ||||||||||||||
expectedReturn: "192.168.1.0/16 2001:0db8:85a3:0000:0000:8a2e:0370:7334/48 172.16.14.10/24 2001:0db8:85a3::8a2e:0370:10/64 2600:14a0::/40", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
name: "IPv6 only", | ||||||||||||||
input: "2001:0db8:85a3:0000:0000:8a2e:0370:7334 2001:0db8:85a3::8a2e:370:10/64 2001:db8::2:1 ::ffff:192.168.0.1 2600:14a0::/40", | ||||||||||||||
name: "IPv6 only", | ||||||||||||||
input: "2001:0db8:85a3:0000:0000:8a2e:0370:7334 2001:0db8:85a3::8a2e:370:10/64 2001:db8::2:1 ::ffff:192.168.0.1 2600:14a0::/40", | ||||||||||||||
expectedReturn: "2001:0db8:85a3:0000:0000:8a2e:0370:7334 2001:0db8:85a3::8a2e:370:10/64 2001:db8::2:1 ::ffff:192.168.0.1 2600:14a0::/40", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
name: "IPv4 only", | ||||||||||||||
input: "192.168.10.10 10.168.12.10/8 8.8.8.8 172.16.0.0/24", | ||||||||||||||
name: "IPv4 only", | ||||||||||||||
input: "192.168.10.10 10.168.12.10/8 8.8.8.8 172.16.0.0/24", | ||||||||||||||
expectedReturn: "192.168.10.10 10.168.12.10/8 8.8.8.8 172.16.0.0/24", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
name: "Single IP", | ||||||||||||||
input: "192.168.15.15", | ||||||||||||||
name: "Single IP", | ||||||||||||||
input: "192.168.15.15", | ||||||||||||||
expectedReturn: "192.168.15.15", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
// as behavior as the previous (regexp) approach | ||||||||||||||
|
@@ -1046,9 +1053,39 @@ func TestParseIPList(t *testing.T) { | |||||||||||||
expectedEmpty: true, | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
name: "Wrong IP in a list", | ||||||||||||||
input: "192.168.1.0 2001:0db8:85a3:0000:0000:8a2e:0370:7334 172.16.14.10/24 2001:0db8:85a3::8a2e:370:10/64 64:ff9b::192.168.0.1 10.", | ||||||||||||||
expectedEmpty: true, | ||||||||||||||
name: "Wrong IPv4 in an IPs only list", | ||||||||||||||
input: "192.168.1.0 2001:0db8:85a3:0000:0000:8a2e:0370:7334 172.16.14.10/24 2001:0db8:85a3::8a2e:370:10/64 64:ff9b::192.168.0.1 10.", | ||||||||||||||
expectedReturn: "192.168.1.0 2001:0db8:85a3:0000:0000:8a2e:0370:7334 172.16.14.10/24 2001:0db8:85a3::8a2e:370:10/64 64:ff9b::192.168.0.1", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
name: "Wrong IPv6 in an IPs only list", | ||||||||||||||
input: "192.168.1.0 2001:0db8:85a3:0000:0000:8a2e:0370 172.16.14.10/24 2001:0db8:85a3::8a2e:370:10/64 64:ff9b::192.168.0.1 10.", | ||||||||||||||
expectedReturn: "192.168.1.0 172.16.14.10/24 2001:0db8:85a3::8a2e:370:10/64 64:ff9b::192.168.0.1", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
name: "Wrong IPv4 in an IPv4 list", | ||||||||||||||
input: "192.168.1.0 10.10.0.1 192.168. 10.", | ||||||||||||||
expectedReturn: "192.168.1.0 10.10.0.1", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
name: "Wrong IPv6 in an IPv6 list", | ||||||||||||||
input: "2001:0db8:85a3:0000:8a2e:0370:7334 2001:0db8:85a3::8a2e:370:10/64 2001:db8::2:1 ::ffff:192.168.0.1 :/40", | ||||||||||||||
expectedReturn: "2001:0db8:85a3::8a2e:370:10/64 2001:db8::2:1 ::ffff:192.168.0.1", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
name: "All mixed type with invalid IPv4", | ||||||||||||||
input: "192.168.1 2001:0db8:85a3:0000:0000:8a2e:0370:7334 172.16.14.10/24 2001:0db8:85a3::8a2e:370:10/64 64:ff9b::192.168.0.1 2600:14a0::/40", | ||||||||||||||
expectedReturn: "2001:0db8:85a3:0000:0000:8a2e:0370:7334 172.16.14.10/24 2001:0db8:85a3::8a2e:370:10/64 64:ff9b::192.168.0.1 2600:14a0::/40", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
name: "Wrong IPv4 CIDR in a CIDRs only list", | ||||||||||||||
input: "192.168.1./16 2001:0db8:85a3:0000:0000:8a2e:0370:7334/48 172.16.14.10/24 2001:0db8:85a3::8a2e:0370:10/64 2600:14a0::/40", | ||||||||||||||
expectedReturn: "2001:0db8:85a3:0000:0000:8a2e:0370:7334/48 172.16.14.10/24 2001:0db8:85a3::8a2e:0370:10/64 2600:14a0::/40", | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
name: "Wrong IPv6 CIDR in a CIDRs only list", | ||||||||||||||
input: "192.168.1.0/16 2001:0db8:85a3:0000:0000:8a2e:0370/48 172.16.14.10/24 2001:0db8:85a3::8a2e:0370:10/64 2600:14a0::/40", | ||||||||||||||
expectedReturn: "192.168.1.0/16 172.16.14.10/24 2001:0db8:85a3::8a2e:0370:10/64 2600:14a0::/40", | ||||||||||||||
}, | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
@@ -1061,9 +1098,24 @@ func TestParseIPList(t *testing.T) { | |||||||||||||
} | ||||||||||||||
return | ||||||||||||||
} | ||||||||||||||
if got != tc.input { | ||||||||||||||
if tc.expectedEmpty && got != "" || got != tc.expectedReturn { | ||||||||||||||
t.Errorf("Failure: expected %q, got %q", tc.input, got) | ||||||||||||||
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.
Suggested change
|
||||||||||||||
} | ||||||||||||||
}) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
for _, tc := range testCases { | ||||||||||||||
t.Run(tc.name, func(t *testing.T) { | ||||||||||||||
got := parseIPList(tc.input) | ||||||||||||||
if tc.expectedEmpty { | ||||||||||||||
if got != "" { | ||||||||||||||
t.Errorf("Expected empty, but got %q", got) | ||||||||||||||
} | ||||||||||||||
return | ||||||||||||||
} | ||||||||||||||
if got != tc.expectedReturn { | ||||||||||||||
t.Errorf("Failure: expected %q, got %q", tc.expectedReturn, got) | ||||||||||||||
} | ||||||||||||||
}) | ||||||||||||||
} | ||||||||||||||
} |
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.
Since this changes behavior, we need to make sure this error is noticed, and indicate that the invalid IP is now ignored. Something like this:
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.
I added a customErr for IP/CIDR since only
net.parseCIDR
returnserr
.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.
I'm wondering whether this one should be an error, taking into account that now we don't stop the parsing and there can be some valid IPs still. I think that the error would make more sense down the code where we find that the list is empty. However in the similar conditions in the same function (when we return
""
) we uselog.V(7).Info()
. So it seems to be inconsistent.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.
@alebedev87 That’s why I use log.V(0).Info at the beginning.
I reviewed all scenarios that call log.Error() in
template_helper.go
, and they all return "" immediately.Since, in this case, we will continue parsing the rest of the IP, is it okay to use log.Info but at a higher log level?
If yes, what level should I use here?
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 default log level is 2 so if we want this message to be visible it has to be
<= 2
. From what I can see intemplate_helper.go
the log level0
is what's used in similar cases (example) - when the user needs to be informed but the flow may continue. Similarly, looking at the other functions - failing the parsing with an error if the list of valid IPs/CIDRs is empty seems to be the way to go.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.
Btw the condition about the trimmer list being compared to the given list doesn't seem to use the right logging level:
I would prefer it to be a level
0
too as we want the user to see this.