Skip to content

Commit 6f10616

Browse files
committed
Rewrite the mac address regexing
- there's no need for the beginning/end match (that's implied as far as I can tell) - drop some unnecessary groups - use non-capturing groups since we're just looking for a match - as a consequence this can be constructed with String.join - add a few more tests
1 parent 5dda98d commit 6f10616

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CefParser.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.util.regex.MatchResult;
3434
import java.util.regex.Matcher;
3535
import java.util.regex.Pattern;
36-
import java.util.stream.Collectors;
3736
import java.util.stream.Stream;
3837

3938
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
@@ -69,16 +68,17 @@ final class CefParser {
6968

7069
// Comprehensive regex pattern to match various MAC address formats
7170
private static final Pattern MAC_ADDRESS_PATTERN = Pattern.compile(
72-
Stream.of(
71+
String.join(
72+
"|",
7373
// Combined colon and hyphen separated 6-group patterns
74-
"(([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2})",
74+
"(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}",
7575
// Dot-separated 6-group pattern
76-
"([0-9A-Fa-f]{4}\\.){2}[0-9A-Fa-f]{4}",
76+
"(?:[0-9A-Fa-f]{4}\\.){2}[0-9A-Fa-f]{4}",
7777
// Combined colon and hyphen separated 8-group patterns
78-
"([0-9A-Fa-f]{2}[:-]){7}[0-9A-Fa-f]{2}",
78+
"(?:[0-9A-Fa-f]{2}[:-]){7}[0-9A-Fa-f]{2}",
7979
// Dot-separated EUI-64
80-
"([0-9A-Fa-f]{4}\\.){3}[0-9A-Fa-f]{4}"
81-
).collect(Collectors.joining("|", "^(", ")$"))
80+
"(?:[0-9A-Fa-f]{4}\\.){3}[0-9A-Fa-f]{4}"
81+
)
8282
);
8383
private static final int EUI48_HEX_LENGTH = 48 / 4;
8484
private static final int EUI64_HEX_LENGTH = 64 / 4;

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CefProcessorTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -970,10 +970,12 @@ public void testToMacAddressWithSeparators() {
970970
});
971971
}
972972

973-
public void testInvalidMacAddressSeparator() {
973+
public void testInvalidMacAddresses() {
974974
CefParser parser = new CefParser(ZoneId.of("UTC"), false);
975-
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.toMACAddress("00|0D|60|AF|1B|61"));
976-
assertThat(e.getMessage(), equalTo("Invalid MAC address format"));
975+
for (String invalid : List.of("00|0D|60|AF|1B|61", "00:0D:60:AF:1B:61 foo", "0000:0D:60:AF:1B:61")) {
976+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.toMACAddress(invalid));
977+
assertThat(e.getMessage(), equalTo("Invalid MAC address format"));
978+
}
977979
}
978980

979981
public void testEUI48ToMacAddressWithOutSeparators() {

0 commit comments

Comments
 (0)