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

Move default values from constants to individual modules

This commit is contained in:
checktheroads
2020-07-23 09:09:40 -07:00
parent 78d4a75b46
commit 08e5040842
4 changed files with 161 additions and 86 deletions

65
hyperglass/api/tasks.py Normal file
View File

@@ -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}

View File

@@ -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)

View File

@@ -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).
"""

View File

@@ -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",
}