Pep8
This commit is contained in:
parent
06a5a758a3
commit
58fa7d1ee6
19 changed files with 567 additions and 377 deletions
|
|
@ -25,42 +25,35 @@ class FirewallZone(BaseModel):
|
|||
"""Represents a firewalld zone configuration"""
|
||||
|
||||
name: Annotated[str, StringConstraints(min_length=1)] = Field(
|
||||
..., description="Name of the zone"
|
||||
)
|
||||
target: Optional[FirewallTarget] = Field(
|
||||
default=FirewallTarget.DEFAULT, description="Zone target type"
|
||||
)
|
||||
services: List[str] = Field(
|
||||
default_factory=list, description="List of enabled services"
|
||||
)
|
||||
ports: List[str] = Field(default_factory=list, description="List of enabled ports")
|
||||
sources: List[str] = Field(
|
||||
default_factory=list, description="List of source addresses"
|
||||
)
|
||||
interfaces: List[str] = Field(
|
||||
default_factory=list, description="List of network interfaces"
|
||||
)
|
||||
..., description="Name of the zone")
|
||||
target: Optional[FirewallTarget] = Field(default=FirewallTarget.DEFAULT,
|
||||
description="Zone target type")
|
||||
services: List[str] = Field(default_factory=list,
|
||||
description="List of enabled services")
|
||||
ports: List[str] = Field(default_factory=list,
|
||||
description="List of enabled ports")
|
||||
sources: List[str] = Field(default_factory=list,
|
||||
description="List of source addresses")
|
||||
interfaces: List[str] = Field(default_factory=list,
|
||||
description="List of network interfaces")
|
||||
|
||||
|
||||
class FirewallService(BaseModel):
|
||||
"""Represents a firewalld service configuration"""
|
||||
|
||||
name: Annotated[str, StringConstraints(min_length=1)] = Field(
|
||||
..., description="Name of the service"
|
||||
)
|
||||
ports: List[str] = Field(
|
||||
default_factory=list, description="List of ports used by the service"
|
||||
)
|
||||
..., description="Name of the service")
|
||||
ports: List[str] = Field(default_factory=list,
|
||||
description="List of ports used by the service")
|
||||
protocols: List[str] = Field(
|
||||
default_factory=list, description="List of protocols used by the service"
|
||||
)
|
||||
default_factory=list,
|
||||
description="List of protocols used by the service")
|
||||
modules: List[str] = Field(
|
||||
default_factory=list,
|
||||
description="List of kernel modules required by the service",
|
||||
)
|
||||
timeout: Optional[int] = Field(
|
||||
default=0, ge=0, description="Timeout in seconds (0 for permanent)"
|
||||
)
|
||||
default=0, ge=0, description="Timeout in seconds (0 for permanent)")
|
||||
|
||||
|
||||
class FirewallPort(BaseModel):
|
||||
|
|
@ -69,8 +62,7 @@ class FirewallPort(BaseModel):
|
|||
port: int = Field(..., ge=1, le=65535, description="Port number (1-65535)")
|
||||
protocol: Protocol = Field(..., description="Network protocol (tcp/udp)")
|
||||
timeout: Optional[int] = Field(
|
||||
default=0, ge=0, description="Timeout in seconds (0 for permanent)"
|
||||
)
|
||||
default=0, ge=0, description="Timeout in seconds (0 for permanent)")
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.port}/{self.protocol.value}"
|
||||
|
|
@ -81,9 +73,11 @@ class FirewallSource(BaseModel):
|
|||
|
||||
address: Annotated[
|
||||
str,
|
||||
StringConstraints(pattern=r"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}(?:/[0-9]{1,2})?$"),
|
||||
StringConstraints(
|
||||
pattern=r"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}(?:/[0-9]{1,2})?$"),
|
||||
] = Field(..., description="IP address or network in CIDR notation")
|
||||
zone: Optional[str] = Field(default=None, description="Zone name for the source")
|
||||
zone: Optional[str] = Field(default=None,
|
||||
description="Zone name for the source")
|
||||
|
||||
@field_validator("address")
|
||||
def validate_ip_address(cls, v):
|
||||
|
|
@ -106,10 +100,10 @@ class FirewallRule(BaseModel):
|
|||
|
||||
zone: str = Field(..., description="Zone name")
|
||||
source: Optional[FirewallSource] = Field(
|
||||
default=None, description="Source configuration"
|
||||
)
|
||||
port: Optional[FirewallPort] = Field(default=None, description="Port configuration")
|
||||
default=None, description="Source configuration")
|
||||
port: Optional[FirewallPort] = Field(default=None,
|
||||
description="Port configuration")
|
||||
service: Optional[str] = Field(default=None, description="Service name")
|
||||
timeout: Optional[int] = Field(
|
||||
default=0, ge=0, description="Rule timeout in seconds"
|
||||
)
|
||||
timeout: Optional[int] = Field(default=0,
|
||||
ge=0,
|
||||
description="Rule timeout in seconds")
|
||||
|
|
|
|||
|
|
@ -74,7 +74,8 @@ class UFWRule(BaseModel):
|
|||
"""
|
||||
if isinstance(v, int):
|
||||
if not (1 <= v <= 65535):
|
||||
raise ValueError(f"Порт должен быть в диапазоне 1-65535, получено: {v}")
|
||||
raise ValueError(
|
||||
f"Порт должен быть в диапазоне 1-65535, получено: {v}")
|
||||
return v
|
||||
elif isinstance(v, str):
|
||||
try:
|
||||
|
|
@ -112,8 +113,7 @@ class UFWRule(BaseModel):
|
|||
to_ip = ipaddress.ip_network(v, strict=False)
|
||||
|
||||
if isinstance(from_ip, ipaddress.IPv6Network) != isinstance(
|
||||
to_ip, ipaddress.IPv6Network
|
||||
):
|
||||
to_ip, ipaddress.IPv6Network):
|
||||
raise ValueError(
|
||||
"Несовместимые версии IP: нельзя смешивать IPv4 и IPv6 в одном правиле"
|
||||
)
|
||||
|
|
@ -151,7 +151,8 @@ class UFWRule(BaseModel):
|
|||
cmd_parts.extend(["to", self.to_ip])
|
||||
|
||||
# Добавляем порт и протокол
|
||||
port_str = str(self.port) # Работает как с числами, так и с диапазонами
|
||||
port_str = str(
|
||||
self.port) # Работает как с числами, так и с диапазонами
|
||||
cmd_parts.extend(["port", port_str])
|
||||
|
||||
# Добавляем протокол
|
||||
|
|
@ -195,25 +196,20 @@ class UFWRule(BaseModel):
|
|||
|
||||
match = re.match(pattern, line, re.VERBOSE | re.IGNORECASE)
|
||||
if not match:
|
||||
raise ValueError(f"Невозможно разобрать строку правила UFW: {line}")
|
||||
raise ValueError(
|
||||
f"Невозможно разобрать строку правила UFW: {line}")
|
||||
|
||||
groups = match.groupdict()
|
||||
|
||||
# Преобразуем протокол в enum
|
||||
proto_str = groups["protocol"].lower()
|
||||
protocol = (
|
||||
Protocol.BOTH
|
||||
if proto_str == "tcp/udp"
|
||||
else Protocol.UDP if proto_str == "udp" else Protocol.TCP
|
||||
)
|
||||
protocol = (Protocol.BOTH if proto_str == "tcp/udp" else
|
||||
Protocol.UDP if proto_str == "udp" else Protocol.TCP)
|
||||
|
||||
# Преобразуем действие в enum (игнорируем регистр)
|
||||
action_str = groups["action"].lower()
|
||||
action = (
|
||||
Action.ALLOW
|
||||
if action_str == "allow"
|
||||
else Action.DENY if action_str == "deny" else Action.REJECT
|
||||
)
|
||||
action = (Action.ALLOW if action_str == "allow" else
|
||||
Action.DENY if action_str == "deny" else Action.REJECT)
|
||||
|
||||
# Преобразуем направление в enum
|
||||
direction_str = groups["direction"].lower()
|
||||
|
|
@ -237,7 +233,8 @@ class UFWRule(BaseModel):
|
|||
port = int(port)
|
||||
|
||||
# Обрабатываем комментарий
|
||||
comment = groups.get("comment", "").strip() if groups.get("comment") else None
|
||||
comment = groups.get("comment",
|
||||
"").strip() if groups.get("comment") else None
|
||||
|
||||
return cls(
|
||||
action=action,
|
||||
|
|
@ -261,7 +258,8 @@ class UFWRulesResponse(BaseModel):
|
|||
"""
|
||||
|
||||
enabled: bool = Field(..., description="Включен ли UFW")
|
||||
rules: List[UFWRule] = Field(default_factory=list, description="Список правил UFW")
|
||||
rules: List[UFWRule] = Field(default_factory=list,
|
||||
description="Список правил UFW")
|
||||
|
||||
@property
|
||||
def total_rules(self) -> int:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue