1
0
Fork 1
mirror of https://github.com/thatmattlove/hyperglass.git synced 2026-01-17 00:38:06 +00:00

fix BGP route validation

This commit is contained in:
thatmattlove 2024-06-30 23:33:06 -04:00
parent 872c3ec654
commit f340e65082
2 changed files with 11 additions and 13 deletions

View file

@ -158,13 +158,6 @@ BGP_ROUTES = [
},
]
STRUCTURED = BGPRouteTable(
vrf="default",
count=len(BGP_ROUTES),
routes=BGP_ROUTES,
winning_weight="high",
)
PING = r"""PING 1.1.1.1 (1.1.1.1): 56 data bytes
64 bytes from 1.1.1.1: icmp_seq=0 ttl=59 time=4.696 ms
64 bytes from 1.1.1.1: icmp_seq=1 ttl=59 time=4.699 ms
@ -196,6 +189,11 @@ async def fake_output(query_type: str, structured: bool) -> t.Union[str, BGPRout
return TRACEROUTE
if "bgp" in query_type:
if structured:
return STRUCTURED
return BGPRouteTable(
vrf="default",
count=len(BGP_ROUTES),
routes=BGP_ROUTES,
winning_weight="high",
)
return BGP_PLAIN
return BGP_PLAIN

View file

@ -6,7 +6,7 @@ import typing as t
from ipaddress import ip_network
# Third Party
from pydantic import field_validator
from pydantic import field_validator, ValidationInfo
# Project
from hyperglass.state import use_state
@ -70,7 +70,7 @@ class BGPRoute(HyperglassModel):
return [c for c in value if func(c)]
@field_validator("rpki_state")
def validate_rpki_state(cls, value, values):
def validate_rpki_state(cls, value, info: ValidationInfo):
"""If external RPKI validation is enabled, get validation state."""
(structured := use_state("params").structured)
@ -82,7 +82,7 @@ class BGPRoute(HyperglassModel):
if structured.rpki.mode == "external":
# If external validation is enabled, validate the prefix
# & asn with Cloudflare's RPKI API.
as_path = values["as_path"]
as_path = info.data.get("as_path", [])
if len(as_path) == 0:
# If the AS_PATH length is 0, i.e. for an internal route,
@ -92,13 +92,13 @@ class BGPRoute(HyperglassModel):
asn = as_path[-1]
try:
net = ip_network(values["prefix"])
net = ip_network(info.data["prefix"])
except ValueError:
return 3
# Only do external RPKI lookups for global prefixes.
if net.is_global:
return rpki_state(prefix=values["prefix"], asn=asn)
return rpki_state(prefix=info.data["prefix"], asn=asn)
return value