Skip to content

Commit

Permalink
Updates after discussion with Ken today. Removing several functions f…
Browse files Browse the repository at this point in the history
…rom public/utils and providing a single function to get urls. Updated docs and tests
  • Loading branch information
bminnix committed May 10, 2024
1 parent 5557274 commit 8edbe98
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 235 deletions.
5 changes: 0 additions & 5 deletions docs/dev/code_reference/platform_mapper.md

This file was deleted.

4 changes: 1 addition & 3 deletions docs/user/include_jinja_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@
| mac_to_format | netutils.mac.mac_to_format |
| mac_to_int | netutils.mac.mac_to_int |
| mac_type | netutils.mac.mac_type |
| os_platform_object_builder | netutils.nist.os_platform_object_builder |
| get_nist_urls | netutils.nist.get_nist_urls |
| compare_version_loose | netutils.os_version.compare_version_loose |
| compare_version_strict | netutils.os_version.compare_version_strict |
| default_metadata | netutils.os_version.default_metadata |
| get_upgrade_path | netutils.os_version.get_upgrade_path |
| juniper_junos_metadata | netutils.os_version.juniper_junos_metadata |
| version_metadata | netutils.os_version.version_metadata |
| compare_cisco_type5 | netutils.password.compare_cisco_type5 |
| compare_cisco_type7 | netutils.password.compare_cisco_type7 |
Expand Down
21 changes: 3 additions & 18 deletions docs/user/lib_use_cases_nist.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,21 @@ For this reason, for certain Vendor/OS combinations, a custom URL needs to be bu


## Examples

The easiest way to access this utility is by using the `os_platform_object_builder`, and providing arguments for Vendor, OS/Other Platform, and Version.
Here are a few examples showing how to use this in your python code.

```python

from netutils.nist import os_platform_object_builder

# Create the platform objects to get NIST query URL(s) for.
cisco_ios = os_platform_object_builder("Cisco", "IOS", "15.5(2)S1c")
juniper_junos = os_platform_object_builder("Juniper", "JunOS", "10.2R2.11")
from netutils.nist import get_nist_urls

# Get NIST URL for the Cisco IOS object
cisco_ios.get_nist_urls()
get_nist_urls("Cisco", "IOS", "15.5(2)S1c")
# ['https://services.nvd.nist.gov/rest/json/cves/2.0?virtualMatchString=cpe:2.3:o:cisco:ios:15.5\\(2\\)s1c:*']

# Get NIST URL(s) for the Juniper JunOS object
juniper_junos.get_nist_urls()
get_nist_urls("Juniper", "JunOS", "10.2R2.11")
# ['https://services.nvd.nist.gov/rest/json/cves/2.0?virtualMatchString=cpe:2.3:o:juniper:junos:10.2r2:*:*:*:*:*:*:*', 'https://services.nvd.nist.gov/rest/json/cves/2.0?virtualMatchString=cpe:2.3:o:juniper:junos:10.2:r2:*:*:*:*:*:*']
```

The NIST URL utility can also be used as a standalone module to create defined custom NIST URLs. This would only be useful if you have defined your own custom URL builders based on a custom input dictionary and defined in `get_nist_url_funcs`. See below:
```python
from netutils.nist import get_nist_url_funcs

# The below example is using the JunOS custom builder.
juniper_junos = get_nist_url_funcs['juniper']['junos']({'isservice': False, 'ismaintenance': False, 'isfrs': True, 'isspecial': False, 'service': None, 'service_build': None, 'service_respin': None, 'main': '12', 'minor': '4', 'type': 'R', 'build': None})
# ['https://services.nvd.nist.gov/rest/json/cves/2.0?virtualMatchString=cpe:2.3:o:juniper:junos:12.4r:*:*:*:*:*:*:*']
```

Currently known OS/Other Platform types that require a custom NIST URL:

- Juniper JunOS
49 changes: 0 additions & 49 deletions docs/user/lib_use_cases_platform_mapper.md

This file was deleted.

38 changes: 28 additions & 10 deletions netutils/nist.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __getitem__(self, key: str) -> t.Any:
return getattr(self, key)


def get_nist_urls_juniper_junos(os_platform_data: t.Dict[str, t.Any]) -> t.List[str]: # pylint: disable=R0911
def _get_nist_urls_juniper_junos(os_platform_data: t.Dict[str, t.Any]) -> t.List[str]: # pylint: disable=R0911
"""Create a list of possible NIST Url strings for JuniperPlatform.
Returns:
Expand Down Expand Up @@ -156,7 +156,7 @@ def get_nist_urls_juniper_junos(os_platform_data: t.Dict[str, t.Any]) -> t.List[
raise ValueError("Failure creating Juniper JunOS Version. Format is unknown.")


def get_nist_urls_default(os_platform_data: t.Dict[str, t.Any]) -> t.List[str]:
def _get_nist_urls_default(os_platform_data: t.Dict[str, t.Any]) -> t.List[str]:
r"""Create a list of possible NIST Url strings.
Child models with NIST URL customizations need their own "get_nist_urls" method.
Expand Down Expand Up @@ -184,13 +184,7 @@ def get_nist_urls_default(os_platform_data: t.Dict[str, t.Any]) -> t.List[str]:
return nist_urls


get_nist_url_funcs: t.Dict[str, t.Any] = {
"default": get_nist_urls_default,
"juniper": {"junos": get_nist_urls_juniper_junos},
}


def os_platform_object_builder(vendor: str, platform: str, version: str) -> object:
def _os_platform_object_builder(vendor: str, platform: str, version: str) -> object:
"""Creates a platform object relative to its need and definition.
Args:
Expand All @@ -202,7 +196,7 @@ def os_platform_object_builder(vendor: str, platform: str, version: str) -> obje
object: Platform object
Examples:
>>> jp = os_platform_object_builder("juniper", "junos", "12.1R3-S4.1")
>>> jp = _os_platform_object_builder("juniper", "junos", "12.1R3-S4.1")
>>> jp.get_nist_urls()
['https://services.nvd.nist.gov/rest/json/cves/2.0?virtualMatchString=cpe:2.3:o:juniper:junos:12.1r3:s4.1:*:*:*:*:*:*', 'https://services.nvd.nist.gov/rest/json/cves/2.0?virtualMatchString=cpe:2.3:o:juniper:junos:12.1r3-s4.1:*:*:*:*:*:*:*']
"""
Expand Down Expand Up @@ -232,3 +226,27 @@ def os_platform_object_builder(vendor: str, platform: str, version: str) -> obje
)

return platform_cls(**field_values)


get_nist_url_funcs: t.Dict[str, t.Any] = {
"default": _get_nist_urls_default,
"juniper": {"junos": _get_nist_urls_juniper_junos},
}


def get_nist_urls(vendor: str, platform: str, version: str) -> t.List[str]:
"""Generate list of possible NIST URLs for the Vendor, OS Platform, and Version.
Args:
vendor (str): OS Software Platform Vendor/Manufacturer
platform (str): OS Software Platform Name
version (str): OS Software Platform Version
Returns:
t.List[str]: NIST URLs to search for possible CVE matches
"""
platform_data = _os_platform_object_builder(vendor, platform, version).__dict__

if vendor.lower() == "juniper" and platform.lower() == "junos":
return _get_nist_urls_juniper_junos(platform_data)
return _get_nist_urls_default(platform_data)
16 changes: 8 additions & 8 deletions netutils/os_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def compare_version_strict(current_version: str, comparison: str, target_version
return _compare_version(current_version, comparison, target_version, "strict")


def juniper_junos_metadata(version: str) -> t.Dict[str, t.Any]:
def _juniper_junos_version_metadata(version: str) -> t.Dict[str, t.Any]:
"""Parses JunOS Version into usable bits matching JunOS Standards.
Args:
Expand All @@ -141,7 +141,7 @@ def juniper_junos_metadata(version: str) -> t.Dict[str, t.Any]:
A dictionary containing parsed version information
Examples:
>>> juniper_junos_metadata("12.3R4")
>>> _juniper_junos_version_metadata("12.3R4")
{'isservice': False, 'ismaintenance': True, 'isfrs': False, 'isspecial': False, 'service': None, 'service_build': None, 'service_respin': None, 'main': '12', 'minor': '3', 'type': 'R', 'build': '4', 'major': '12', 'patch': '4'}
"""
# Use regex to group the main, minor, type and build into useable pieces
Expand Down Expand Up @@ -217,7 +217,7 @@ def juniper_junos_metadata(version: str) -> t.Dict[str, t.Any]:
return parsed_version


def default_metadata(version: str) -> t.Dict[str, t.Any]:
def _default_version_metadata(version: str) -> t.Dict[str, t.Any]:
"""Parses version value using SemVer 2.0.0 standards. https://semver.org/spec/v2.0.0.html.
Args:
Expand All @@ -227,13 +227,13 @@ def default_metadata(version: str) -> t.Dict[str, t.Any]:
A dictionary containing parsed version information
Examples:
>>> default_metadata("10.20.30")
>>> _default_version_metadata("10.20.30")
{'major': '10', 'minor': '20', 'patch': '30', 'prerelease': None, 'buildmetadata': None}
>>> default_metadata("1.0.0-alpha.beta.1")
>>> _default_version_metadata("1.0.0-alpha.beta.1")
{'major': '1', 'minor': '0', 'patch': '0', 'prerelease': 'alpha.beta.1', 'buildmetadata': None}
>>> default_metadata("1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay")
>>> _default_version_metadata("1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay")
{'major': '1', 'minor': '0', 'patch': '0', 'prerelease': 'alpha-a.b-c-somethinglong', 'buildmetadata': 'build.1-aef.1-its-okay'}
"""
Expand Down Expand Up @@ -280,9 +280,9 @@ def default_metadata(version: str) -> t.Dict[str, t.Any]:


version_metadata_parsers = {
"default": default_metadata,
"default": _default_version_metadata,
"juniper": {
"junos": juniper_junos_metadata,
"junos": _juniper_junos_version_metadata,
},
}

Expand Down
4 changes: 1 addition & 3 deletions netutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@
"get_upgrade_path": "os_version.get_upgrade_path",
"hash_data": "hash.hash_data",
"get_ips_sorted": "ip.get_ips_sorted",
"os_platform_object_builder": "nist.os_platform_object_builder",
"juniper_junos_metadata": "os_version.juniper_junos_metadata",
"version_metadata": "os_version.version_metadata",
"default_metadata": "os_version.default_metadata",
"get_nist_urls": "nist.get_nist_urls",
}


Expand Down
Loading

0 comments on commit 8edbe98

Please sign in to comment.