From d794058895d572ec343af79626a78831af24b126 Mon Sep 17 00:00:00 2001 From: Matthew Kienow Date: Fri, 8 Oct 2021 13:57:14 -0400 Subject: [PATCH 1/4] Remove unnecessary null check --- recog/src/main/java/com/rapid7/recog/parser/RecogParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recog/src/main/java/com/rapid7/recog/parser/RecogParser.java b/recog/src/main/java/com/rapid7/recog/parser/RecogParser.java index 5d4f877..85dff43 100644 --- a/recog/src/main/java/com/rapid7/recog/parser/RecogParser.java +++ b/recog/src/main/java/com/rapid7/recog/parser/RecogParser.java @@ -133,7 +133,7 @@ public RecogMatchers parse(Reader reader, String name) String recogKey = root.getAttribute("matches"); - if (recogKey.isEmpty() || recogKey == null) { + if (recogKey.isEmpty()) { LOGGER.debug("Recog Matcher Key is Empty or Null. File Name: " + name); recogKey = name; } From a6c1b848acd9548de3516c8dc43cf93fe62d94d8 Mon Sep 17 00:00:00 2001 From: Matthew Kienow Date: Fri, 8 Oct 2021 14:01:48 -0400 Subject: [PATCH 2/4] Store optional XML content file path --- .../java/com/rapid7/recog/RecogMatchers.java | 10 +++++++++ .../com/rapid7/recog/parser/RecogParser.java | 21 ++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/recog/src/main/java/com/rapid7/recog/RecogMatchers.java b/recog/src/main/java/com/rapid7/recog/RecogMatchers.java index 188633d..453f96b 100644 --- a/recog/src/main/java/com/rapid7/recog/RecogMatchers.java +++ b/recog/src/main/java/com/rapid7/recog/RecogMatchers.java @@ -15,6 +15,7 @@ */ public class RecogMatchers extends ArrayList { + private String path; private String key; private String protocol; private String type; @@ -25,12 +26,21 @@ public RecogMatchers() { } public RecogMatchers(String key, String protocol, String type, float preference) { + this(null, key, protocol, type, preference); + } + + public RecogMatchers(String path, String key, String protocol, String type, float preference) { + this.path = path; this.key = requireNonNull(key); this.protocol = protocol; this.type = type; this.preference = preference; } + public String getPath() { + return path; + } + public String getKey() { return key; } diff --git a/recog/src/main/java/com/rapid7/recog/parser/RecogParser.java b/recog/src/main/java/com/rapid7/recog/parser/RecogParser.java index 85dff43..299daf4 100644 --- a/recog/src/main/java/com/rapid7/recog/parser/RecogParser.java +++ b/recog/src/main/java/com/rapid7/recog/parser/RecogParser.java @@ -92,7 +92,7 @@ public RecogParser(boolean strictMode, PatternMatcherFactory patternMatcherFacto public RecogMatchers parse(File file) throws ParseException { try (Reader reader = new FileReader(file)) { - return parse(reader, file.getName().replaceAll(".xml", "")); + return parse(reader, file.getPath(), file.getName().replaceAll(".xml", "")); } catch (Exception exception) { throw new ParseException("Failed to parse recog fingerprints from file " + file.getAbsolutePath(), exception); } @@ -102,13 +102,28 @@ public RecogMatchers parse(File file) * Parses {@link RecogMatchers} from the XML content in the specified {@link Reader}. * * @param reader The content to read from. Must not be {@code null}. - * @param name Unused + * @param name Value used for {@link RecogMatchers} key if parsed value is null or empty. * @return {@link RecogMatchers} parsed from the reader. Will not be {@code null} but may be empty * if no matchers are defined, or all matchers are invalid and strict mode is disabled. * @throws ParseException If an error is encountered and strict-mode is enabled. */ public RecogMatchers parse(Reader reader, String name) throws ParseException { + return parse(reader, null, name); + } + + /** + * Parses {@link RecogMatchers} from the XML content in the specified {@link Reader}. + * + * @param reader The content to read from. Must not be {@code null}. + * @param path Optional XML content file path. + * @param name Value used for {@link RecogMatchers} key if parsed value is null or empty. + * @return {@link RecogMatchers} parsed from the reader. Will not be {@code null} but may be empty + * if no matchers are defined, or all matchers are invalid and strict mode is disabled. + * @throws ParseException If an error is encountered and strict-mode is enabled. + */ + public RecogMatchers parse(Reader reader, String path, String name) + throws ParseException { Document document; try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); @@ -138,7 +153,7 @@ public RecogMatchers parse(Reader reader, String name) recogKey = name; } - RecogMatchers matchers = new RecogMatchers(recogKey, root.getAttribute("protocol"), root.getAttribute("database_type"), preference); + RecogMatchers matchers = new RecogMatchers(path, recogKey, root.getAttribute("protocol"), root.getAttribute("database_type"), preference); NodeList fingerprints = root.getElementsByTagName("fingerprint"); for (int index = 0; index < fingerprints.getLength(); index++) { From 0fce7d752a0466ed1e3df7391fe7dec061d1f764 Mon Sep 17 00:00:00 2001 From: Matthew Kienow Date: Fri, 8 Oct 2021 14:02:39 -0400 Subject: [PATCH 3/4] Fix detail always set true when format present --- .../src/main/java/com/rapid7/recog/verify/RecogVerifier.java | 1 - 1 file changed, 1 deletion(-) diff --git a/recog-verify/src/main/java/com/rapid7/recog/verify/RecogVerifier.java b/recog-verify/src/main/java/com/rapid7/recog/verify/RecogVerifier.java index 47d376a..3a9cdd4 100644 --- a/recog-verify/src/main/java/com/rapid7/recog/verify/RecogVerifier.java +++ b/recog-verify/src/main/java/com/rapid7/recog/verify/RecogVerifier.java @@ -157,7 +157,6 @@ private static VerifierOptions getVerifierOptions(CommandLine line) { VerifierOptions verifierOpts = new VerifierOptions(); if (line.hasOption("format")) { - verifierOpts.setDetail(true); if (line.getOptionValue("format").startsWith("d")) { verifierOpts.setDetail(true); } else if (line.getOptionValue("format").startsWith("q")) { From 654131abf8deb31dbe7421a06539437ac047d0e8 Mon Sep 17 00:00:00 2001 From: Matthew Kienow Date: Fri, 8 Oct 2021 15:49:44 -0400 Subject: [PATCH 4/4] Add optional path to VerifyReporter output --- .../rapid7/recog/verify/RecogVerifier.java | 3 ++- .../rapid7/recog/verify/VerifyReporter.java | 24 +++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/recog-verify/src/main/java/com/rapid7/recog/verify/RecogVerifier.java b/recog-verify/src/main/java/com/rapid7/recog/verify/RecogVerifier.java index 3a9cdd4..479cc0f 100644 --- a/recog-verify/src/main/java/com/rapid7/recog/verify/RecogVerifier.java +++ b/recog-verify/src/main/java/com/rapid7/recog/verify/RecogVerifier.java @@ -33,7 +33,7 @@ public static RecogVerifier create(VerifierOptions verifierOpts, RecogMatchers m requireNonNull(verifierOpts); Formatter formatter = new Formatter(verifierOpts, requireNonNull(output)); - VerifyReporter reporter = new VerifyReporter(verifierOpts, formatter); + VerifyReporter reporter = new VerifyReporter(verifierOpts, formatter, matchers.getPath()); return new RecogVerifier(requireNonNull(matchers), reporter); } @@ -51,6 +51,7 @@ public VerifyReporter getReporter() { } public void verify() { + reporter.printPath(); for (RecogMatcher matcher : fingerprints) { reporter.printName(matcher); diff --git a/recog-verify/src/main/java/com/rapid7/recog/verify/VerifyReporter.java b/recog-verify/src/main/java/com/rapid7/recog/verify/VerifyReporter.java index 096d394..4ba8572 100644 --- a/recog-verify/src/main/java/com/rapid7/recog/verify/VerifyReporter.java +++ b/recog-verify/src/main/java/com/rapid7/recog/verify/VerifyReporter.java @@ -6,13 +6,19 @@ public class VerifyReporter { private final VerifierOptions options; private final Formatter formatter; + private final String path; private int successCount; private int warningCount; private int failureCount; public VerifyReporter(VerifierOptions options, Formatter formatter) { + this(options, formatter, null); + } + + public VerifyReporter(VerifierOptions options, Formatter formatter, String path) { this.options = options; this.formatter = formatter; + this.path = path; resetCounts(); } @@ -51,12 +57,18 @@ public void warning(String text) { } warningCount++; - formatter.warningMessage(String.format("%s%s", padding(), text)); + formatter.warningMessage(String.format("%s%s%s", pathLabel(), padding(), text)); } public void failure(String text) { failureCount++; - formatter.failureMessage(String.format("%s%s", padding(), text)); + formatter.failureMessage(String.format("%s%s%s", pathLabel(), padding(), text)); + } + + public void printPath() { + if (options.isDetail() && !(path == null || path.isEmpty())) { + formatter.statusMessage(String.format("\n%s:", path)); + } } public void printName(RecogMatcher fingerprint) { @@ -87,6 +99,10 @@ private void resetCounts() { warningCount = 0; } + private String pathLabel() { + return options.isDetail() || path == null || path.isEmpty() ? "" : String.format("%s: ", path); + } + private String padding() { if (options.isDetail()) { return " "; @@ -95,8 +111,8 @@ private String padding() { } private String summaryLine() { - return String.format("SUMMARY: Test completed with %d successful, %d warnings" - + ", and %d failures", successCount, warningCount, failureCount); + return String.format("%sSUMMARY: Test completed with %d successful, %d warnings" + + ", and %d failures", pathLabel(), successCount, warningCount, failureCount); } private void colorizeSummary(String summary) {