File: models.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (168 lines) | stat: -rw-r--r-- 6,221 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""NetworkFirewallBackend class with methods for supported APIs."""

from typing import Any, Optional

from moto.core.base_backend import BackendDict, BaseBackend
from moto.core.common_models import BaseModel
from moto.es.exceptions import ResourceNotFound
from moto.utilities.paginator import paginate
from moto.utilities.tagging_service import TaggingService

PAGINATION_MODEL = {
    "list_firewalls": {
        "input_token": "next_token",
        "limit_key": "max_results",
        "limit_default": 100,
        "unique_attribute": "arn",
    },
}


class NetworkFirewallModel(BaseModel):
    def __init__(
        self,
        account_id: str,
        region_name: str,
        firewall_name: str,
        firewall_policy_arn: str,
        vpc_id: str,
        subnet_mappings: list[str],
        delete_protection: bool,
        subnet_change_protection: bool,
        firewall_policy_change_protection: bool,
        description: str,
        tags: list[dict[str, str]],
        encryption_configuration: dict[str, str],
        enabled_analysis_types: list[str],
    ):
        self.firewall_name = firewall_name
        self.firewall_policy_arn = firewall_policy_arn
        self.vpc_id = vpc_id
        self.subnet_mappings = subnet_mappings
        self.delete_protection: bool = (
            delete_protection if delete_protection is not None else True
        )
        self.subnet_change_protection: bool = (
            subnet_change_protection if subnet_change_protection is not None else True
        )
        self.firewall_policy_change_protection: bool = (
            firewall_policy_change_protection
            if firewall_policy_change_protection is not None
            else True
        )
        self.description = description
        self.tags = tags
        self.encryption_configuration = encryption_configuration
        self.enabled_analysis_types = enabled_analysis_types

        self.arn = f"arn:aws:network-firewall:{region_name}:{account_id}:firewall/{self.firewall_name}"

        self.update_token = "1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d"
        self.firewall_status = {
            "Status": "READY",
            "ConfigurationSyncStateSummary": "IN_SYNC",
        }
        self.logging_configs: dict[str, list[dict[str, Any]]] = {}

    def to_dict(self) -> dict[str, Any]:
        return {
            "FirewallName": self.firewall_name,
            "FirewallArn": self.arn,
            "FirewallPolicyArn": self.firewall_policy_arn,
            "VpcId": self.vpc_id,
            "SubnetMappings": self.subnet_mappings,
            "DeleteProtection": self.delete_protection,
            "SubnetChangeProtection": self.subnet_change_protection,
            "FirewallPolicyChangeProtection": self.firewall_policy_change_protection,
            "Description": self.description,
            "Tags": self.tags,
            "EncryptionConfiguration": self.encryption_configuration,
            "EnabledAnalysisTypes": self.enabled_analysis_types,
        }


class NetworkFirewallBackend(BaseBackend):
    """Implementation of NetworkFirewall APIs."""

    def __init__(self, region_name: str, account_id: str) -> None:
        super().__init__(region_name, account_id)
        self.firewalls: dict[str, NetworkFirewallModel] = {}
        self.tagger = TaggingService()

    def create_firewall(
        self,
        firewall_name: str,
        firewall_policy_arn: str,
        vpc_id: str,
        subnet_mappings: list[str],
        delete_protection: bool,
        subnet_change_protection: bool,
        firewall_policy_change_protection: bool,
        description: str,
        tags: list[dict[str, str]],
        encryption_configuration: dict[str, str],
        enabled_analysis_types: list[str],
    ) -> NetworkFirewallModel:
        firewall = NetworkFirewallModel(
            self.account_id,
            self.region_name,
            firewall_name=firewall_name,
            firewall_policy_arn=firewall_policy_arn,
            vpc_id=vpc_id,
            subnet_mappings=subnet_mappings,
            delete_protection=delete_protection,
            subnet_change_protection=subnet_change_protection,
            firewall_policy_change_protection=firewall_policy_change_protection,
            description=description,
            tags=tags,
            encryption_configuration=encryption_configuration,
            enabled_analysis_types=enabled_analysis_types,
        )
        self.firewalls[firewall.arn] = firewall

        if tags:
            self.tagger.tag_resource(firewall.arn, tags)

        return firewall

    def _get_firewall(
        self, firewall_arn: Optional[str], firewall_name: Optional[str]
    ) -> NetworkFirewallModel:
        if firewall_arn:
            if firewall_arn in self.firewalls:
                return self.firewalls[firewall_arn]
        for firewall in self.firewalls.values():
            if firewall.firewall_name == firewall_name:
                return firewall
        raise ResourceNotFound("NetworkFirewall", str(firewall_arn or firewall_name))

    def describe_logging_configuration(
        self, firewall_arn: str, firewall_name: str
    ) -> NetworkFirewallModel:
        firewall: NetworkFirewallModel = self._get_firewall(firewall_arn, firewall_name)
        return firewall

    def update_logging_configuration(
        self,
        firewall_arn: str,
        firewall_name: str,
        logging_configuration: dict[str, list[dict[str, Any]]],
    ) -> NetworkFirewallModel:
        firewall: NetworkFirewallModel = self._get_firewall(firewall_arn, firewall_name)
        firewall.logging_configs = logging_configuration
        return firewall

    @paginate(pagination_model=PAGINATION_MODEL)
    def list_firewalls(self, vpc_ids: list[str]) -> list[NetworkFirewallModel]:
        firewalls = list(self.firewalls.values())
        if vpc_ids:
            firewalls = [fw for fw in firewalls if fw.vpc_id in vpc_ids]
        return firewalls

    def describe_firewall(
        self, firewall_name: str, firewall_arn: str
    ) -> NetworkFirewallModel:
        return self._get_firewall(firewall_arn, firewall_name)


networkfirewall_backends = BackendDict(NetworkFirewallBackend, "network-firewall")