Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -51,6 +51,7 @@ public VerifyReporter getReporter() {
}

public void verify() {
reporter.printPath();
for (RecogMatcher matcher : fingerprints) {
reporter.printName(matcher);

Expand Down Expand Up @@ -157,7 +158,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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 " ";
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions recog/src/main/java/com/rapid7/recog/RecogMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
public class RecogMatchers extends ArrayList<RecogMatcher> {

private String path;
private String key;
private String protocol;
private String type;
Expand All @@ -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;
}
Expand Down
23 changes: 19 additions & 4 deletions recog/src/main/java/com/rapid7/recog/parser/RecogParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
Expand All @@ -133,12 +148,12 @@ 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;
}

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++) {
Expand Down