From 08e504084201975df104895d830e4f5aef081140 Mon Sep 17 00:00:00 2001 From: checktheroads Date: Thu, 23 Jul 2020 09:09:40 -0700 Subject: [PATCH] Move default values from constants to individual modules --- hyperglass/api/tasks.py | 65 ++++++++++++++++++++ hyperglass/configuration/__init__.py | 10 ++-- hyperglass/configuration/defaults.py | 83 ++++++++++++++++++++++++++ hyperglass/constants.py | 89 +++------------------------- 4 files changed, 161 insertions(+), 86 deletions(-) create mode 100644 hyperglass/api/tasks.py create mode 100644 hyperglass/configuration/defaults.py diff --git a/hyperglass/api/tasks.py b/hyperglass/api/tasks.py new file mode 100644 index 0000000..0e2c1ee --- /dev/null +++ b/hyperglass/api/tasks.py @@ -0,0 +1,65 @@ +"""Tasks to be executed from web API.""" + +# Standard Library +import re +from typing import Dict, Union +from pathlib import Path + +# Third Party +from httpx import Headers + + +def import_public_key( + app_path: Union[Path, str], device_name: str, keystring: str +) -> bool: + """Import a public key for hyperglass-agent. + + Arguments: + app_path {Path|str} -- hyperglass app path + device_name {str} -- Device name + keystring {str} -- Public key + + Raises: + RuntimeError: Raised if unable to create certs directory + RuntimeError: Raised if written key does not match input + + Returns: + {bool} -- True if file was written + """ + if not isinstance(app_path, Path): + app_path = Path(app_path) + + cert_dir = app_path / "certs" + + if not cert_dir.exists(): + cert_dir.mkdir() + + if not cert_dir.exists(): + raise RuntimeError(f"Failed to create certs directory at {str(cert_dir)}") + + filename = re.sub(r"[^A-Za-z0-9]", "_", device_name) + ".pem" + cert_file = cert_dir / filename + + with cert_file.open("w+") as file: + file.write(str(keystring)) + + with cert_file.open("r") as file: + read_file = file.read().strip() + if not keystring == read_file: + raise RuntimeError("Wrote key, but written file did not match input key") + + return True + + +async def process_headers(headers: Headers) -> Dict: + """Filter out unwanted headers and return as a dictionary.""" + headers = dict(headers) + header_keys = ( + "user-agent", + "referer", + "accept-encoding", + "accept-language", + "x-real-ip", + "x-forwarded-for", + ) + return {k: headers.get(k) for k in header_keys} diff --git a/hyperglass/configuration/__init__.py b/hyperglass/configuration/__init__.py index 8dfc72a..eea268f 100644 --- a/hyperglass/configuration/__init__.py +++ b/hyperglass/configuration/__init__.py @@ -21,11 +21,7 @@ from hyperglass.log import ( from hyperglass.util import check_path, set_app_path, set_cache_env, current_log_level from hyperglass.models import HyperglassModel from hyperglass.constants import ( - CREDIT, - DEFAULT_HELP, - DEFAULT_TERMS, TRANSPORT_REST, - DEFAULT_DETAILS, SUPPORTED_QUERY_TYPES, PARSED_RESPONSE_FIELDS, SUPPORTED_STRUCTURED_OUTPUT, @@ -35,6 +31,12 @@ from hyperglass.exceptions import ConfigError, ConfigInvalid, ConfigMissing from hyperglass.configuration.models import params as _params from hyperglass.configuration.models import routers as _routers from hyperglass.configuration.models import commands as _commands +from hyperglass.configuration.defaults import ( + CREDIT, + DEFAULT_HELP, + DEFAULT_TERMS, + DEFAULT_DETAILS, +) from hyperglass.configuration.markdown import get_markdown set_app_path(required=True) diff --git a/hyperglass/configuration/defaults.py b/hyperglass/configuration/defaults.py new file mode 100644 index 0000000..3d68742 --- /dev/null +++ b/hyperglass/configuration/defaults.py @@ -0,0 +1,83 @@ +"""Constant store for large default values.""" + +CREDIT = """ +Powered by [**hyperglass**](https://hyperglass.io) version {version}. \ +Source code licensed [_BSD 3-Clause Clear_](https://hyperglass.io/docs/license/). +""" + +DEFAULT_TERMS = """ +By using {site_title}, you agree to be bound by the following terms of use: + +All queries executed on this page are logged for analysis and troubleshooting. \ +Users are prohibited from automating queries, or attempting to process queries in \ +bulk. This service is provided on a best effort basis, and {org_name} \ +makes no availability or performance warranties or guarantees whatsoever. +""" + +DEFAULT_DETAILS = { + "bgp_aspath": """ +{site_title} accepts the following `AS_PATH` regular expression patterns: + +| Expression | Match | +| :------------------- | :-------------------------------------------- | +| `_65000$` | Originated by 65000 | +| `^65000_` | Received from 65000 | +| `_65000_` | Via 65000 | +| `_65000_65001_` | Via 65000 and 65001 | +| `_65000(_.+_)65001$` | Anything from 65001 that passed through 65000 | +""", + "bgp_community": """ +{site_title} makes use of the following BGP communities: + +| Community | Description | +| :-------- | :---------- | +| `65000:1` | Example 1 | +| `65000:2` | Example 2 | +| `65000:3` | Example 3 | +""", + "bgp_route": """ +Performs BGP table lookup based on IPv4/IPv6 prefix. +""", + "ping": """ +Sends 5 ICMP echo requests to the target. +""", + "traceroute": """ +Performs UDP Based traceroute to the target. \ +For information about how to interpret traceroute results, [click here]\ +(https://hyperglass.io/traceroute_nanog.pdf). +""", +} + +DEFAULT_HELP = """ +##### BGP Route + +Performs BGP table lookup based on IPv4/IPv6 prefix. + +--- + +##### BGP Community + +Performs BGP table lookup based on [Extended](https://tools.ietf.org/html/rfc4360) \ +or [Large](https://tools.ietf.org/html/rfc8195) community value. + +--- + +##### BGP AS Path + +Performs BGP table lookup based on `AS_PATH` regular expression. + +--- + +##### Ping + +Sends 5 ICMP echo requests to the target. + +--- + +##### Traceroute + +Performs UDP Based traceroute to the target. + +For information about how to interpret traceroute results, [click here]\ +(https://hyperglass.io/traceroute_nanog.pdf). +""" diff --git a/hyperglass/constants.py b/hyperglass/constants.py index b5c0614..8135e9b 100644 --- a/hyperglass/constants.py +++ b/hyperglass/constants.py @@ -42,88 +42,6 @@ PARSED_RESPONSE_FIELDS = ( ("Age", "age", "right"), ) -CREDIT = """ -Powered by [**hyperglass**](https://hyperglass.io) version {version}. \ -Source code licensed [_BSD 3-Clause Clear_](https://hyperglass.io/docs/license/). -""" - -DEFAULT_TERMS = """ -By using {site_title}, you agree to be bound by the following terms of use: - -All queries executed on this page are logged for analysis and troubleshooting. \ -Users are prohibited from automating queries, or attempting to process queries in \ -bulk. This service is provided on a best effort basis, and {org_name} \ -makes no availability or performance warranties or guarantees whatsoever. -""" - -DEFAULT_DETAILS = { - "bgp_aspath": """ -{site_title} accepts the following `AS_PATH` regular expression patterns: - -| Expression | Match | -| :------------------- | :-------------------------------------------- | -| `_65000$` | Originated by 65000 | -| `^65000_` | Received from 65000 | -| `_65000_` | Via 65000 | -| `_65000_65001_` | Via 65000 and 65001 | -| `_65000(_.+_)65001$` | Anything from 65001 that passed through 65000 | -""", - "bgp_community": """ -{site_title} makes use of the following BGP communities: - -| Community | Description | -| :-------- | :---------- | -| `65000:1` | Example 1 | -| `65000:2` | Example 2 | -| `65000:3` | Example 3 | -""", - "bgp_route": """ -Performs BGP table lookup based on IPv4/IPv6 prefix. -""", - "ping": """ -Sends 5 ICMP echo requests to the target. -""", - "traceroute": """ -Performs UDP Based traceroute to the target. \ -For information about how to interpret traceroute results, [click here]\ -(https://hyperglass.io/traceroute_nanog.pdf). -""", -} - -DEFAULT_HELP = """ -##### BGP Route - -Performs BGP table lookup based on IPv4/IPv6 prefix. - ---- - -##### BGP Community - -Performs BGP table lookup based on [Extended](https://tools.ietf.org/html/rfc4360) \ -or [Large](https://tools.ietf.org/html/rfc8195) community value. - ---- - -##### BGP AS Path - -Performs BGP table lookup based on `AS_PATH` regular expression. - ---- - -##### Ping - -Sends 5 ICMP echo requests to the target. - ---- - -##### Traceroute - -Performs UDP Based traceroute to the target. - -For information about how to interpret traceroute results, [click here]\ -(https://hyperglass.io/traceroute_nanog.pdf). -""" - SUPPORTED_QUERY_FIELDS = ("query_location", "query_type", "query_target", "query_vrf") SUPPORTED_QUERY_TYPES = ( "bgp_route", @@ -148,3 +66,10 @@ SCRAPE_HELPERS = { "junos": "juniper", "ios": "cisco_ios", } + +DRIVER_MAP = { + "cisco_ios": "netmiko", + "juniper": "netmiko", + "frr": "hyperglass_agent", + "bird": "hyperglass_agent", +}