Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
62 changes: 41 additions & 21 deletions .github/workflows/oui_filepull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,59 @@ name: "OUI-Updates"
on: # yamllint disable-line rule:truthy
schedule:
- cron: "0 2 1 * *"
workflow_dispatch: {}

jobs:
data_gathering:
runs-on: "ubuntu-latest"
env:
BRANCH_NAME: "OUI_Updates"
steps:
# Checkout repo
- name: "Check out code"
uses: "actions/checkout@v4"
with:
ref: "develop"
# Delete old branch if it exists
fetch-depth: 0

- name: "Authenticate git remote"
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
run: |
git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"

- name: "Delete existing branch"
run: "git branch -D $BRANCH_NAME || true"
# Create branch for Flatbot
- name: "Create Flatbot branch"
run: "git checkout -b $BRANCH_NAME"
# Push new branch so Flatbot can make its commit
- name: "Push Flatbot branch"
run: "git push -f --set-upstream origin $BRANCH_NAME"
# 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"
# 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/data_files/oui_mappings.py"
postprocess: "./flat_postprocess/oui_postprocess.ts"
run: |
git branch -D "$BRANCH_NAME" || true

- name: "Create branch"
run: |
git checkout -b "$BRANCH_NAME"

- name: "Push branch"
run: |
git push -f --set-upstream origin "$BRANCH_NAME"

# Fetch data via curl as IEEE has blocked requests/urllib library requests
- name: "Download IEEE OUI data"
run: |
curl -fsSL "https://standards-oui.ieee.org/oui/oui.csv" \
-o "./netutils/data_files/oui_mappings.py"

- name: "Generate oui_mappings.py"
run: "python ./flat_postprocess/oui_postprocess.py ./netutils/data_files/oui_mappings.py"

- name: "Commit changes"
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"

git add ./netutils/data_files/oui_mappings.py
git diff --cached --quiet || git commit -m "Update OUI mappings"

- name: "Push changes"
run: |
git push origin "$BRANCH_NAME"

pr_creation:
runs-on: "ubuntu-latest"
needs: "data_gathering"
Expand Down
1 change: 1 addition & 0 deletions changes/745.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix OUI GitHub Actions workflow to correctly fetch and generate OUI mappings
36 changes: 28 additions & 8 deletions flat_postprocess/oui_postprocess.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
"""Python code used to postprocess Flat github action data related to OUI mappings."""

import re
import subprocess
import sys
from urllib.request import urlopen

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

OUI_MAPPINGS = {}
URL = "https://standards-oui.ieee.org/oui/oui.txt"


def download_csv_text(url: str = URL) -> str:
"""Download the CSV text from the given URL."""
proc = subprocess.run( # noqa: S603
["curl", "-fsSL", url], # noqa: S607
check=True,
capture_output=True,
text=True,
)
return proc.stdout


if __name__ == "__main__":
if len(sys.argv) == 3:
with urlopen("https://standards-oui.ieee.org").read().decode("utf-8") as oui_textfile: # nosec B310
with open(sys.argv[1], "w", encoding="utf-8") as oui_mappings:
oui_mappings.write(oui_textfile)
if len(sys.argv) < 2:
raise SystemExit("Usage: python oui_postprocess.py <output_file> [<download_latest>]")

output_path = sys.argv[1]
download = "--download" in sys.argv[2:]

if download:
csv_text = download_csv_text(URL)
with open(output_path, "w", encoding="utf-8", newline="") as oui_textfile:
oui_textfile.write(csv_text)

with open(sys.argv[1], "r", encoding="utf-8") as oui_file:
with open(output_path, "r", encoding="utf-8", newline="") 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:
with open(output_path, "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")
Expand Down