Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
858e56f
Testing oui file pull
Sep 12, 2022
d3ff7f3
Test #2
Sep 12, 2022
715c229
Test3
Sep 12, 2022
c5b25b7
Test4
Sep 12, 2022
69a2023
Flat: latest data (2022-09-12T18:38:07.891Z)
flat-data Sep 12, 2022
f1fb21b
Test5
Sep 12, 2022
7ca58c4
Flat: latest data (2022-09-12T19:24:55.944Z)
flat-data Sep 12, 2022
8fa1f77
TEst postprocess
Sep 12, 2022
6968e3b
Test7
Sep 12, 2022
eba2690
Updated
Sep 12, 2022
70ce8b9
Test9
Sep 12, 2022
fc25002
Flat: latest data (2022-09-12T19:42:33.332Z)
flat-data Sep 12, 2022
0d3c354
Test11
Sep 12, 2022
d028907
Flat: latest data (2022-09-12T19:46:33.601Z)
flat-data Sep 12, 2022
90cdd15
Updated python file path
Sep 12, 2022
f42f966
Update deno path
Sep 12, 2022
997c620
Updated
Sep 12, 2022
5eb3788
Test local
Sep 12, 2022
4df289c
Test13
Sep 12, 2022
ca3ab34
Flat: latest data (2022-09-12T20:17:34.717Z)
flat-data Sep 12, 2022
bbad67b
Add lower
Sep 12, 2022
d7ec05a
Flat: latest data (2022-09-12T20:23:39.725Z)
flat-data Sep 12, 2022
ab693f2
Updated docstring
Sep 12, 2022
2bd61cf
Flat: latest data (2022-09-12T20:38:29.667Z)
flat-data Sep 12, 2022
988d488
Add cron schedule
Sep 12, 2022
f665d4b
Added trailing comma
Sep 12, 2022
6ea8659
Flat: latest data (2022-09-12T20:55:56.830Z)
flat-data Sep 12, 2022
6e0849e
Removed oui_mappings from black
Sep 12, 2022
97dc5a4
Flat: latest data (2022-09-12T21:02:54.064Z)
flat-data Sep 12, 2022
d759ab3
Yamllint fixes
Sep 13, 2022
6822149
Flat: latest data (2022-09-13T16:53:54.938Z)
flat-data Sep 13, 2022
82797ca
Test15
Sep 13, 2022
6f94787
Flat: latest data (2022-09-13T17:03:53.398Z)
flat-data Sep 13, 2022
272a3f2
Updated to use constants file
Sep 13, 2022
d59f9da
Flat: latest data (2022-09-13T18:24:49.653Z)
flat-data Sep 13, 2022
dfc467b
Updated tests
Sep 13, 2022
c37881b
Blackened
Sep 13, 2022
f53fae8
Changed ci time
Sep 13, 2022
8ada0a1
Change schedule for flat workflow to run
Sep 13, 2022
703693b
Removed print from postprocessing script
Sep 13, 2022
c26ea60
Reverted pyproject
Sep 13, 2022
587accd
Fixed pyproject
Sep 13, 2022
5c6c5c9
Update .github/workflows/oui_filepull.yml
qduk Sep 14, 2022
3088f6c
Typed OUI_mappings dict
Sep 14, 2022
7a73e50
Testing creating dictionary
Sep 21, 2022
df2f4ae
Flat: latest data (2022-09-21T13:25:20.479Z)
flat-data Sep 21, 2022
e8da206
Updated filename in workflow
Sep 21, 2022
ea6a87a
Flat: latest data (2022-09-21T13:31:32.443Z)
flat-data Sep 21, 2022
0f250aa
linted
Sep 21, 2022
fd0ed42
Fixes
Sep 21, 2022
1589795
Flat: latest data (2022-09-21T13:52:01.337Z)
flat-data Sep 21, 2022
997dcb7
Fixed syntax in postproces file
Sep 21, 2022
7a42550
Flat: latest data (2022-09-21T13:55:19.286Z)
flat-data Sep 21, 2022
23d1d92
Linted OUI file
Sep 21, 2022
6b8f980
Flat: latest data (2022-09-21T13:57:51.812Z)
flat-data Sep 21, 2022
869f8dd
Updated workflow schedule
Sep 21, 2022
330f217
linted
Sep 21, 2022
069465c
Flat: latest data (2022-09-21T14:07:24.860Z)
flat-data Sep 21, 2022
e295cf6
postprocess updates
Sep 21, 2022
979b3d2
Flat: latest data (2022-09-21T14:09:38.997Z)
flat-data Sep 21, 2022
1e4a484
Modify flat schedule
Sep 21, 2022
ead6c29
Fix my py
Sep 21, 2022
8ee1095
linted
Sep 21, 2022
1c68134
Removed oui_mappings from bandit
Sep 21, 2022
1c1ac36
pylinted
Sep 21, 2022
76e5355
Flat: latest data (2022-09-21T17:48:18.614Z)
flat-data Sep 21, 2022
7477cb0
Add new line
Sep 21, 2022
382c399
Flat: latest data (2022-09-21T17:51:36.253Z)
flat-data Sep 21, 2022
1999bcb
Remove CI for push to my branch
Sep 21, 2022
d4a9701
Reverted tasks python version
Sep 29, 2022
6afcfd5
uncomment valid mac decorator
Oct 3, 2022
83d70c6
Removed profile decorator
Oct 4, 2022
9025e13
Update .github/workflows/oui_filepull.yml
qduk Oct 5, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bandit.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
skips: []
# No need to check for security issues in the test scripts!
exclude_dirs:
- "./tests/"
- "./docs/"
- "netutils/oui_mappings.py"
27 changes: 27 additions & 0 deletions .github/workflows/oui_filepull.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
name: "Flat"

on: # yamllint disable-line rule:truthy
pull_request:
branches:
- "main"

jobs:
scheduled:
runs-on: "ubuntu-latest"
steps:
# This step installs Deno, which is a new Javascript runtime that improves on Node. Can be used for an optional postprocessing step
- name: "Setup deno"
uses: "denoland/setup-deno@main"
with:
deno-version: "v1.10.x"
# Check out the repository so it can read the files inside of it and do other operations
- name: "Check out repo"
uses: "actions/checkout@v2"
# The Flat Action step. We fetch the data in the http_url and save it as downloaded_filename
- name: "Fetch data"
uses: "githubocto/flat@v3"
with:
http_url: "https://standards-oui.ieee.org"
downloaded_filename: "./netutils/oui_mappings.py"
postprocess: "./flat_postprocess/oui_postprocess.ts"
23 changes: 23 additions & 0 deletions flat_postprocess/oui_postprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Python code used to postprocess Flat github action data related to OUI mappings."""
import sys
import re

HEX_RE = r"^(?P<hex>[A-Fa-f0-9]{6})\s+\(.*\)[^a-zA-Z0-9]+(?P<company>.*)$"
OUI_MAPPINGS = {}

if __name__ == "__main__":
with open(sys.argv[1], "r", encoding="utf-8") as oui_file:
for line in oui_file:
if re.search(HEX_RE, line):
group_regex_values = re.search(HEX_RE, line).groupdict()
if group_regex_values.get("hex") and group_regex_values.get("company"):
OUI_MAPPINGS.update({group_regex_values.get("hex").lower(): group_regex_values.get("company")})

with open(sys.argv[1], "w", encoding="utf-8") as oui_mappings:
oui_mappings.write('"""Dictionary object to store OUI information."""\n')
oui_mappings.write("# pylint: disable=too-many-lines\n")
oui_mappings.write("import typing\n\n")
oui_mappings.write("OUI_MAPPINGS: typing.Dict[str, str] = {\n")
for mac, company in OUI_MAPPINGS.items():
oui_mappings.write(f' "{mac}": "{company}",\n')
oui_mappings.write("}\n")
6 changes: 6 additions & 0 deletions flat_postprocess/oui_postprocess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Forwards the execution to the python script
const py_run = Deno.run({
cmd: ['python', './flat_postprocess/oui_postprocess.py'].concat(Deno.args),
});

await py_run.status();
30 changes: 29 additions & 1 deletion netutils/mac.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def mac_type(mac: str) -> t.Optional[str]:

@_valid_mac
def mac_normalize(mac: str) -> str:
"""Retuns the MAC address with only the address, and no special characters.
"""Return the MAC address with only the address, and no special characters.

Args:
mac: A MAC address in string format that matches one of the defined regex patterns.
Expand All @@ -136,3 +136,31 @@ def mac_normalize(mac: str) -> str:
if char in mac:
mac = mac.replace(char, "")
return mac


@_valid_mac
def get_oui(mac: str) -> str:
"""Returns the company name for a given mac as defined by the IEEE.

Args:
mac: A MAC address in string format that matches one of the defined regex patterns.

Returns:
The name of the company the mac is related to.

Example:
>>> from netutils.mac import get_oui
>>> from netutils.oui_mappings import OUI_MAPPINGS
>>> get_oui("cc.79.d7.dd.ee.ff")
'Cisco Systems, Inc'
>>>
"""
from netutils.oui_mappings import OUI_MAPPINGS # pylint: disable=import-outside-toplevel

normalized_mac_prefix = mac_normalize(mac)[0:6]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the pattern created already uses constants.py, that being said... perhaps we should consider running the load of the json file here and doing it intelligently?

OUI_MAPPINGS = {}
@_valid_mac
def get_oui(mac: str) -> str:
    if not OUI_MAPPINGS:
        with open("/".join([dirname(netutils_file), "oui_mappings.json"]), encoding="utf-8") as fh:
            OUI_MAPPINGS: typing.Dict[str, str] = json.loads(fh.read())
    oui_company = OUI_MAPPINGS.get(normalized_mac_prefix)

    normalized_mac_prefix = mac_normalize(mac)[0:6]
    if not oui_company:
        raise ValueError(f"There was no matching entry in OUI_MAPPINGS for {normalized_mac_prefix}")

    return oui_company

With this pattern, you only load into memory if you actually use the function.

oui_company = OUI_MAPPINGS.get(normalized_mac_prefix)

if not oui_company:
raise ValueError(f"There was no matching entry in OUI_MAPPINGS for {normalized_mac_prefix}.")

return oui_company
Loading