[th/zone-lookup] some cleanup of the get_zones() functions etc.#1176
Open
thom311 wants to merge 14 commits intofirewalld:mainfrom
Open
[th/zone-lookup] some cleanup of the get_zones() functions etc.#1176thom311 wants to merge 14 commits intofirewalld:mainfrom
thom311 wants to merge 14 commits intofirewalld:mainfrom
Conversation
0526dad to
3ea8eba
Compare
erig0
requested changes
Jul 28, 2023
Collaborator
There was a problem hiding this comment.
In general, I feel like a lot of these changes are unnecessary. I'm not sure they improve code quality or the code efficiency. Some of the changes to use dictionary lookups vs lists are fine. I do not intend to merge this series without major changes.
5696b9b to
a7f2f34
Compare
erig0
requested changes
Sep 6, 2023
a7f2f34 to
1834c34
Compare
1834c34 to
0a61b66
Compare
0a61b66 to
eee9bf5
Compare
eee9bf5 to
192355a
Compare
192355a to
b1388ee
Compare
Collaborator
Author
|
Hi @erig0
where where where making the entire operation needlessly O(n). This branch fixes this, among other things. What is the problem with this? |
b1388ee to
dbdf6be
Compare
Firewall.check_zone() used to call "if _zone not in self.zone.get_zones()", where get_zones() literally needs to create a list of all zone names, needlessly sort them, and then we search the string by iterating the list. All this, when self.zone._zones is already a dictionary with the zone name as key. Do a dictionary lookup instead. Note that FirewallZone.get_zone() already was calling Firewall.check_zone() to resolve the default name (thereby, iterating the list of all zones too). So even a function like get_zone(), which seemingly does a dictionary lookup, first iterates the entire list. Rework this. Firewall.check_zone() now just calls FirewallZone.get_zone() (using a dictionary lookup). FirewallZone.get_zone() can resolve the default name directly, without calling Firewall.check_zone(). It can raise the appropriate exception, if the zone does not exist.
…k for zone has_zone() just does a dictionary lookup. get_zones() creates a sorted list of all zones first and then iterates the list. Use has_zone().
- don't call self.policy_from_chain() twice. - return early from function and unindent the block.
Don't iterate ove the keys of the _policies dictionary, to then lookup the object. Also, sorted() creates a new list. We already had a temporary list policies, which we could have sorted inplace with policies.sort(). But replace this with a generator expression, which avoids the intermediate list.
…iterate policies O(n^2) This was not necessary. This change also renders get_policies() without callers. Delete that.
Also, for checking whether a policy is active, avoid initializing the same sets over and over.
No need to iterate the whole list of active policies. Just check whether the policy in question is active.
Code does not get simpler, by having aliases that do mostly the same and call each other. Calling self._fw.icmp.check_icmptype() is perfectly fine. Use that.
dbdf6be to
a681f7f
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The problem I want to solve, is that the key for policies/zones is
p_obj.name, a plain stringNote that for user-created zones we generate 4 policies like
zone_foo_ANYandzone_HOST_foo.The API does not prevent users from creating a policy with name
zone_foo_ANYor a zone with nameANY. Bad things happen, if they do.The eventual solution will be, that the primary key is not a plain string, but a type+name. In particular, there is a difference between user-created policy
zone_foo_ANYand a policyzone_foo_ANYfor zonefoo. And there is a difference between user-created zoneANYand the special, internalANYzone.Anyway. Replacing the "name" with an identifier will touch a lot of code. In preparation for that, this branch cleans up some minor things related to zone/policy lookup (
check_zone(),get_zone(),get_policies()).