1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00
Files
checktheroads-hyperglass/hyperglass/external/rpki.py

49 lines
1.5 KiB
Python
Raw Normal View History

"""Validate RPKI state via Cloudflare GraphQL API."""
2020-06-21 14:12:08 -07:00
# Project
from hyperglass.log import log
2021-09-15 18:25:37 -07:00
from hyperglass.state import use_state
from hyperglass.external._base import BaseExternal
RPKI_STATE_MAP = {"Invalid": 0, "Valid": 1, "NotFound": 2, "DEFAULT": 3}
RPKI_NAME_MAP = {v: k for k, v in RPKI_STATE_MAP.items()}
2020-07-13 01:55:42 -07:00
CACHE_KEY = "hyperglass.external.rpki"
def rpki_state(prefix, asn):
"""Get RPKI state and map to expected integer."""
log.debug("Validating RPKI State for {p} via AS{a}", p=prefix, a=asn)
2021-09-15 18:25:37 -07:00
(cache := use_state().redis)
state = 3
2020-07-13 01:55:42 -07:00
ro = f"{prefix}@{asn}"
2021-09-15 18:25:37 -07:00
cached = cache.hget(CACHE_KEY, ro)
2020-07-13 01:55:42 -07:00
if cached is not None:
state = cached
else:
2020-07-13 01:55:42 -07:00
ql = 'query GetValidation {{ validation(prefix: "{}", asn: {}) {{ state }} }}'
query = ql.format(prefix, asn)
try:
with BaseExternal(base_url="https://rpki.cloudflare.com") as client:
response = client._post("/api/graphql", data={"query": query})
validation_state = (
response.get("data", {}).get("validation", {}).get("state", "DEFAULT")
)
state = RPKI_STATE_MAP[validation_state]
2021-09-15 18:25:37 -07:00
cache.hset(CACHE_KEY, ro, state)
2020-07-13 01:55:42 -07:00
except Exception as err:
log.error(str(err))
state = 3
2021-09-12 15:09:24 -07:00
msg = "RPKI Validation State for {} via AS{} is {}".format(prefix, asn, RPKI_NAME_MAP[state])
2020-07-13 01:55:42 -07:00
if cached is not None:
msg += " [CACHED]"
log.debug(msg)
return state