-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Issue #, if available:** ## Description of changes: By adding the ability to list rules per region, we make it possible to render documentation per region. This is useful when you have a firewall per region. **Checklist** <!--- Leave unchecked if your change doesn't seem to apply --> * [x] Update tests * [ ] Update docs * [x] PR title follows [conventional commit semantics](https://www.conventionalcommits.org/en/v1.0.0-beta.2/#commit-message-for-a-fix-using-an-optional-issue-number) By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
- Loading branch information
Showing
14 changed files
with
201 additions
and
140 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,54 @@ | ||
from __future__ import annotations | ||
from typing import List, Optional | ||
|
||
import itertools | ||
from typing import List, Union | ||
from landingzone_organization import Account as LandingZoneAccount | ||
from aws_network_firewall.cidr_ranges import CidrRanges | ||
from aws_network_firewall.destination import Destination | ||
from aws_network_firewall.rule import Rule | ||
from aws_network_firewall.rule_set import RuleSet | ||
from aws_network_firewall.source import Source | ||
|
||
|
||
class Account(LandingZoneAccount): | ||
__rules: List[Rule] | ||
__rules: RuleSet | ||
__cidr_ranges: CidrRanges | ||
|
||
def __init__( | ||
self, name: str, account_id: str, cidr_ranges: CidrRanges, rules: List[Rule] | ||
) -> None: | ||
super().__init__(name, account_id) | ||
self.__cidr_ranges = cidr_ranges | ||
self.__rules = list(map(self.__enrich_rule, rules)) | ||
self.__rules = RuleSet(rules=list(map(self.__enrich_rule, rules))) | ||
|
||
def __enrich_rule(self, rule: Rule) -> Rule: | ||
list( | ||
map( | ||
lambda source: source.resolve_region_cidr_ranges(self.__cidr_ranges), | ||
rule.sources, | ||
) | ||
) | ||
list( | ||
map( | ||
lambda destination: destination.resolve_region_cidr_ranges( | ||
self.__cidr_ranges | ||
), | ||
rule.destinations, | ||
) | ||
) | ||
cidr_range = self.__cidr_ranges.by_region(rule.region) | ||
|
||
def update_cidr_if_not_set(entry: Source) -> None: | ||
if cidr_range and not entry.cidr: | ||
entry.cidr = cidr_range.value | ||
|
||
list(map(update_cidr_if_not_set, rule.sources)) | ||
|
||
return rule | ||
|
||
@property | ||
def rules(self) -> List[Rule]: | ||
def regions(self) -> List[str]: | ||
return list(set(filter(None, map(lambda rule: rule.region, self.rules.all)))) | ||
|
||
def rules_by_region(self, region: str) -> RuleSet: | ||
return RuleSet( | ||
rules=list(filter(lambda rule: region == rule.region, self.rules.all)) | ||
) | ||
|
||
@property | ||
def rules(self) -> RuleSet: | ||
return self.__rules | ||
|
||
@property | ||
def inspection_rules(self) -> List[Rule]: | ||
return list(filter(lambda rule: rule.is_inspection_rule, self.rules)) | ||
return list(filter(lambda rule: rule.is_inspection_rule, self.rules.all)) | ||
|
||
@property | ||
def egress_rules(self) -> List[Rule]: | ||
return list(filter(lambda rule: rule.is_egress_rule, self.rules)) | ||
return list(filter(lambda rule: rule.is_egress_rule, self.rules.all)) |
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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
|
||
from aws_network_firewall.rule import Rule | ||
|
||
|
||
class RuleSet: | ||
__rules: List[Rule] | ||
|
||
def __init__(self, rules: List[Rule]) -> None: | ||
self.__rules = rules | ||
|
||
def __len__(self) -> int: | ||
return len(self.all) | ||
|
||
def __iter__(self): | ||
for value in self.all: | ||
yield value | ||
|
||
@property | ||
def all(self) -> List[Rule]: | ||
return self.__rules | ||
|
||
@property | ||
def inspection_rules(self) -> List[Rule]: | ||
return list(filter(lambda rule: rule.is_inspection_rule, self.all)) | ||
|
||
@property | ||
def egress_rules(self) -> List[Rule]: | ||
return list(filter(lambda rule: rule.is_egress_rule, self.all)) |
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
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
Oops, something went wrong.