Skip to content

Commit 391e868

Browse files
committed
Fix #719: convert macaddr in all sorts of forms to std format
Signed-off-by: Dinesh Dutt <[email protected]> (cherry picked from commit 948f824)
1 parent 4852a18 commit 391e868

File tree

6 files changed

+19
-26
lines changed

6 files changed

+19
-26
lines changed

suzieq/engines/pandas/address.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ def get(self, **kwargs) -> pd.DataFrame:
104104
for i, a in enumerate(addr):
105105
if addr_types[i] == 0:
106106
# convert the macaddr format to internal format
107-
if '.' in a:
108-
a = convert_macaddr_format_to_colon(a)
109-
macaddr.append(a.lower())
107+
a = convert_macaddr_format_to_colon(a)
108+
macaddr.append(a)
110109
elif addr_types[i] == 4:
111110
if '/' not in a:
112111
a += '/'

suzieq/engines/pandas/network.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ def _find_address(self, addr: str, **kwargs) -> pd.DataFrame:
7878

7979
# Convert Cisco-style MAC address to standard MAC addr format,
8080
# and lowercase all letters of the alphabet
81-
if '.' in addr:
82-
addr = convert_macaddr_format_to_colon(addr)
83-
addr = addr.lower()
81+
addr = convert_macaddr_format_to_colon(addr)
8482

8583
addr_df = self._find_addr_arp(addr, **kwargs)
8684
if addr_df.empty:

suzieq/shared/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,8 @@ def validate_macaddr(macaddr: str) -> bool:
518518
519519
"""
520520
if isinstance(macaddr, str):
521-
if re.fullmatch(r'([0-9a-fA-F]{4}.){2}[0-9a-fA-F]{4}', macaddr) or \
522-
re.fullmatch(r'([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}', macaddr):
521+
macaddr = convert_macaddr_format_to_colon(macaddr)
522+
if re.fullmatch(r'([0-9a-f]{2}:){5}[0-9a-f]{2}', macaddr):
523523
return True
524524

525525
return False

suzieq/sqobjects/network.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pandas as pd
44

55
from suzieq.sqobjects.basicobj import SqObject
6-
from suzieq.shared.utils import (humanize_timestamp,
6+
from suzieq.shared.utils import (humanize_timestamp, validate_macaddr,
77
convert_macaddr_format_to_colon)
88

99

@@ -31,9 +31,7 @@ def find(self, **kwargs) -> pd.DataFrame():
3131
ip_address(addr)
3232
except ValueError:
3333
addr = convert_macaddr_format_to_colon(addr)
34-
if not re.match(
35-
"[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$",
36-
addr):
34+
if not validate_macaddr(addr):
3735
return pd.DataFrame(
3836
{'error': [f'Not valid IP or MAC address: {addr}']})
3937
try:

tests/integration/test_parsing_arpnd.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
import pandas as pd
7+
from suzieq.shared.utils import validate_macaddr
78
from tests.conftest import validate_host_shape, DATADIR, _get_table_data
89

910

@@ -15,9 +16,7 @@ def validate_arpnd_tbl(df: pd.DataFrame):
1516
assert (df.state.isin(["permanent", 'reachable', 'router', 'noarp',
1617
'failed'])).all()
1718
pass_df = df.query('state != "failed"')
18-
assert pass_df.macaddr.apply(
19-
lambda x: re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$",
20-
x) is not None).all()
19+
assert pass_df.macaddr.apply(validate_macaddr).all()
2120
assert not (pass_df.oif.isin(["", "None"])).all() # noqa
2221
assert (pass_df.remote.isin([True, False])).all()
2322

tests/integration/test_parsing_macs.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
import numpy as np
55
import pandas as pd
6+
from suzieq.shared.utils import validate_macaddr
67
from tests.conftest import DATADIR, validate_host_shape, _get_table_data
78

89

@@ -13,9 +14,7 @@ def validate_macs(df: pd.DataFrame):
1314
assert (df.oif != '').all()
1415
assert (df.mackey != '').all()
1516
# Validate that the only MAC addresses there are fit the macaddr format
16-
assert df.macaddr.apply(
17-
lambda x: re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$",
18-
x) is not None).all()
17+
assert df.macaddr.apply(validate_macaddr).all()
1918
# Ignore Linux HER entries and interface MAC entries, and some NXOS entries
2019
assert (df.query(
2120
'macaddr != "00:00:00:00:00:00" and flags != "permanent" and '
@@ -46,14 +45,14 @@ def validate_interfaces(df: pd.DataFrame, datadir: str):
4645
# as routed interfaces that have a VLAN of 0
4746
only_oifs = df.query('~oif.isin(["bridge", "sup-eth1", '
4847
'"vPC Peer-Link", "nve1", "Router"])') \
49-
.query('~oif.str.startswith("vtep.")') \
50-
.query('vlan != 0') \
51-
.groupby(by=['namespace', 'hostname', 'vlan'])['oif'] \
52-
.unique() \
53-
.reset_index() \
54-
.explode('oif') \
55-
.rename(columns={'oif': 'ifname'}) \
56-
.reset_index(drop=True)
48+
.query('~oif.str.startswith("vtep.")') \
49+
.query('vlan != 0') \
50+
.groupby(by=['namespace', 'hostname', 'vlan'])['oif'] \
51+
.unique() \
52+
.reset_index() \
53+
.explode('oif') \
54+
.rename(columns={'oif': 'ifname'}) \
55+
.reset_index(drop=True) \
5756

5857
# Fetch the address table
5958
if_df = _get_table_data('interface', datadir) \

0 commit comments

Comments
 (0)