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 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=0 ttl=59 time=4.696 ms
64 bytes from 1.1.1.1: icmp_seq=1 ttl=59 time=4.699 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 return TRACEROUTE
if "bgp" in query_type: if "bgp" in query_type:
if structured: if structured:
return STRUCTURED return BGPRouteTable(
vrf="default",
count=len(BGP_ROUTES),
routes=BGP_ROUTES,
winning_weight="high",
)
return BGP_PLAIN return BGP_PLAIN
return BGP_PLAIN return BGP_PLAIN

View file

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