Skip to content
Snippets Groups Projects
Commit daf602de authored by Maryam Mokhtarifar's avatar Maryam Mokhtarifar
Browse files

Implement whois method in msr.py and update the response and change the schema of `/asn-peers`

parent fedb7e54
No related branches found
No related tags found
1 merge request!49Implement whois method in msr.py and update the response and change the schema of `/asn-peers`
This commit is part of merge request !49. Comments created here will be created in the context of that merge request.
...@@ -104,6 +104,7 @@ import json ...@@ -104,6 +104,7 @@ import json
import ipaddress import ipaddress
import logging import logging
import re import re
import subprocess
import threading import threading
from collections import defaultdict from collections import defaultdict
from typing import Dict from typing import Dict
...@@ -160,7 +161,6 @@ PEERING_LIST_SCHEMA = { ...@@ -160,7 +161,6 @@ PEERING_LIST_SCHEMA = {
'items': {'$ref': '#/definitions/peering-instance'} 'items': {'$ref': '#/definitions/peering-instance'}
} }
IP_ADDRESS_LIST_SCHEMA = { IP_ADDRESS_LIST_SCHEMA = {
'$schema': 'https://json-schema.org/draft-07/schema#', '$schema': 'https://json-schema.org/draft-07/schema#',
'definitions': { 'definitions': {
...@@ -441,7 +441,8 @@ ASN_PEER_LIST_SCHEMA = { ...@@ -441,7 +441,8 @@ ASN_PEER_LIST_SCHEMA = {
'remote-asn': {'type': 'integer'}, 'remote-asn': {'type': 'integer'},
'local-asn': {'type': 'integer'}, 'local-asn': {'type': 'integer'},
'instance': {'type': 'string'}, 'instance': {'type': 'string'},
'nren': {'type': 'string'} 'nren': {'type': 'string'},
'whois-info': {'type': 'string'}
}, },
# only vrr peerings have remote-asn # only vrr peerings have remote-asn
# only group peerings have local-asn or instance # only group peerings have local-asn or instance
...@@ -451,7 +452,8 @@ ASN_PEER_LIST_SCHEMA = { ...@@ -451,7 +452,8 @@ ASN_PEER_LIST_SCHEMA = {
'address', 'address',
'group', 'group',
'hostname', 'hostname',
'nren'], 'nren',
'whois-info'],
'additionalProperties': False 'additionalProperties': False
} }
}, },
...@@ -1397,6 +1399,25 @@ def _asn_peers(asn, group, instance): ...@@ -1397,6 +1399,25 @@ def _asn_peers(asn, group, instance):
""" """
r = common.get_current_redis() r = common.get_current_redis()
def _whois_str(asn: int) -> str:
args = ["whois", "-h", "whois.cymru.com", f"AS{asn}"]
env = {"PATH": "/bin:/usr/bin"}
r = None
try:
r = subprocess.check_output(args, env=env)
except (subprocess.CalledProcessError, OSError):
logger.exception(f'error calling {" ".join(args)}')
return "?"
if not r:
logger.error("no whois result returned")
return ""
result = r.splitlines()
if not result:
logger.error("error executing whois")
return ""
return result[-1].decode("utf-8")
def _get_filtered_peers_for_asn(asn, nren, group, instance): def _get_filtered_peers_for_asn(asn, nren, group, instance):
peers = json.loads(r.get(f'router-peerings:peer-asn:{asn}')) peers = json.loads(r.get(f'router-peerings:peer-asn:{asn}'))
...@@ -1407,10 +1428,12 @@ def _asn_peers(asn, group, instance): ...@@ -1407,10 +1428,12 @@ def _asn_peers(asn, group, instance):
return False # no value exists, cannot meet condition return False # no value exists, cannot meet condition
return peer[name] == value return peer[name] == value
whois_info = _whois_str(asn)
for peer in peers: for peer in peers:
if _attribute_filter(peer, "group", group) and \ if _attribute_filter(peer, "group", group) and \
_attribute_filter(peer, "instance", instance): _attribute_filter(peer, "instance", instance):
peer['nren'] = nren peer['nren'] = nren
peer['whois-info'] = whois_info
yield peer yield peer
def _get_filtered_peers(asn_nren_map, group, instance): def _get_filtered_peers(asn_nren_map, group, instance):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment