Skip to content

Commit

Permalink
Make logging finer grained and configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
DolphFlynn committed Nov 8, 2023
1 parent f7889c4 commit 2a8ceed
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/main/java/Extension.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import bcheck.BCheckManager;
import burp.api.montoya.BurpExtension;
import burp.api.montoya.MontoyaApi;
import burp.api.montoya.logging.Logging;
import burp.api.montoya.persistence.Persistence;
import client.GitHubClient;
import fetcher.BCheckFetcher;
import file.finder.BCheckFileFinder;
import file.system.FileSystem;
import file.temp.TempFileCreator;
import file.zip.ZipExtractor;
import logging.Logger;
import network.RequestSender;
import settings.controller.SettingsController;
import ui.clipboard.ClipboardManager;
Expand All @@ -22,10 +22,10 @@ public class Extension implements BurpExtension {

@Override
public void initialize(MontoyaApi api) {
Logging logger = api.logging();
Persistence persistence = api.persistence();
SettingsController settingsController = new SettingsController(persistence);

Logger logger = new Logger(api.logging(), settingsController.debugSettings());
RequestSender requestSender = new RequestSender(api.http(), logger);
BCheckFactory bCheckFactory = new BCheckFactory(logger);
GitHubClient gitHubClient = new GitHubClient(requestSender);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/bcheck/BCheckFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package bcheck;

import burp.api.montoya.logging.Logging;
import logging.Logger;

import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -19,9 +19,9 @@ public class BCheckFactory {
private static final Pattern BCHECK_DESCRIPTION_EXTRACTING_REGEX_PATTERN = compile("description:\\s\"(.+)\"");
private static final Pattern BCHECK_TAG_EXTRACTING_REGEX_PATTERN = compile("tags:\\s(.+)");

private final Logging logger;
private final Logger logger;

public BCheckFactory(Logging logger) {
public BCheckFactory(Logger logger) {
this.logger = logger;
}

Expand All @@ -35,7 +35,7 @@ public BCheck fromFile(Path bCheckFilePath) {

return parseFileContents(bCheckFilePath, fileContents);
} catch (IOException e) {
logger.logToError("Couldn't read BCheck file " + bCheckFilePath + ": " + e);
logger.logError("Couldn't read BCheck file " + bCheckFilePath + ": " + e);
throw new IllegalStateException(e);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/file/system/FileSystem.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package file.system;

import burp.api.montoya.logging.Logging;
import logging.Logger;

import java.io.IOException;
import java.nio.file.Path;

import static java.nio.file.Files.writeString;

public class FileSystem {
private final Logging logger;
private final Logger logger;

public FileSystem(Logging logger) {
public FileSystem(Logger logger) {
this.logger = logger;
}

public void saveFile(String fileContents, Path path) {
try {
writeString(path, fileContents);
} catch (IOException e) {
logger.logToError("Failed to save BCheck: " + e);
logger.logError("Failed to save BCheck: " + e);
throw new IllegalStateException(e);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/file/temp/TempFileCreator.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package file.temp;

import burp.api.montoya.logging.Logging;
import logging.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class TempFileCreator {
private final Logging logger;
private final Logger logger;

public TempFileCreator(Logging logger) {
public TempFileCreator(Logger logger) {
this.logger = logger;
}

Expand All @@ -21,11 +21,11 @@ public Path createTempDirectory(String prefix) {

tempDirectoryAsFile.deleteOnExit();

logger.logToOutput("Created temp directory: " + tempDirectoryAsFile.getAbsolutePath());
logger.logDebug("Created temp directory: " + tempDirectoryAsFile.getAbsolutePath());

return tempDirectory;
} catch (IOException e) {
logger.logToError("Failed to create temp directory. Exception: " + e);
logger.logError("Failed to create temp directory. Exception: " + e);
throw new IllegalStateException(e);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/file/zip/ZipExtractor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package file.zip;

import burp.api.montoya.logging.Logging;
import logging.Logger;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -12,9 +12,9 @@
import static java.nio.file.Files.createDirectory;

public class ZipExtractor {
private final Logging logger;
private final Logger logger;

public ZipExtractor(Logging logger) {
public ZipExtractor(Logger logger) {
this.logger = logger;
}

Expand All @@ -27,18 +27,18 @@ public void extractZip(byte[] zip, Path extractLocation) {
Path bCheckCopyPath = extractLocation.resolve(entry.getName());

if (entry.isDirectory()) {
logger.logToOutput("Creating directory: " + bCheckCopyPath);
logger.logDebug("Creating directory: " + bCheckCopyPath);
createDirectory(bCheckCopyPath);
} else {
logger.logToOutput("Copying file: " + bCheckCopyPath);
logger.logDebug("Copying file: " + bCheckCopyPath);
copy(zipStream, bCheckCopyPath);
}
}
} catch (IOException e) {
logger.logToError("Failed to deserialise ZIP response. Exception: " + e);
logger.logError("Failed to deserialise ZIP response. Exception: " + e);
throw new IllegalStateException(e);
}

logger.logToOutput("Successfully extracted BChecks");
logger.logInfo("Successfully extracted BChecks");
}
}
28 changes: 28 additions & 0 deletions src/main/java/logging/Logger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package logging;

import burp.api.montoya.logging.Logging;
import settings.debug.DebugSettings;

public class Logger {
private final Logging logging;
private final DebugSettings debugSettings;

public Logger(Logging logging, DebugSettings debugSettings) {
this.logging = logging;
this.debugSettings = debugSettings;
}

public void logError(String error) {
logging.logToError(error);
}

public void logInfo(String info) {
logging.logToOutput(info);
}

public void logDebug(String debug) {
if (debugSettings.logging()) {
logging.logToOutput(debug);
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/network/RequestSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import burp.api.montoya.http.Http;
import burp.api.montoya.http.message.requests.HttpRequest;
import burp.api.montoya.http.message.responses.HttpResponse;
import burp.api.montoya.logging.Logging;
import logging.Logger;

import java.util.Map;

Expand All @@ -12,9 +12,9 @@

public class RequestSender {
private final Http http;
private final Logging logger;
private final Logger logger;

public RequestSender(Http http, Logging logger) {
public RequestSender(Http http, Logger logger) {
this.http = http;
this.logger = logger;
}
Expand All @@ -26,20 +26,20 @@ public HttpResponse sendRequest(String url, Map<String, String> headers) {
request = request.withAddedHeader(header.getKey(), header.getValue());
}

logger.logToOutput("Requesting " + url);
logger.logDebug("Requesting " + url);
HttpResponse response = http.sendRequest(request).response();

if (response.isStatusCodeClass(CLASS_4XX_CLIENT_ERRORS) || response.isStatusCodeClass(CLASS_5XX_SERVER_ERRORS)) {
String responseBody = response.bodyToString();
String exceptionMessage = "Failed to make request to " + url + ". Error code: " + response.statusCode() + ". Error message: " + responseBody;

logger.logToError(exceptionMessage);
logger.logError(exceptionMessage);
throw new IllegalStateException(exceptionMessage);
} else if (response.isStatusCodeClass(CLASS_3XX_REDIRECTION)) {
String redirectLocation = response.headerValue("Location");
return sendRequest(redirectLocation, headers);
} else {
logger.logToOutput("Request to " + url + " successful");
logger.logDebug("Request to " + url + " successful");
return response;
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/settings/controller/SettingsController.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package settings.controller;

import burp.api.montoya.persistence.Persistence;
import settings.debug.DebugSettings;
import settings.defaultsavelocation.DefaultSaveLocationSettings;
import settings.github.GitHubSettings;

public class SettingsController {
private final DefaultSaveLocationSettings defaultSaveLocationSettings;
private final GitHubSettings gitHubSettings;
private final DebugSettings debugSettings;

public SettingsController(Persistence persistence) {
this.defaultSaveLocationSettings = new DefaultSaveLocationSettings(persistence);
this.gitHubSettings = new GitHubSettings(persistence);
this.debugSettings = new DebugSettings(persistence);
}

public DefaultSaveLocationSettings defaultSaveLocationSettings() {
Expand All @@ -20,4 +23,8 @@ public DefaultSaveLocationSettings defaultSaveLocationSettings() {
public GitHubSettings gitHubSettings() {
return gitHubSettings;
}

public DebugSettings debugSettings() {
return debugSettings;
}
}
22 changes: 22 additions & 0 deletions src/main/java/settings/debug/DebugSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package settings.debug;

import burp.api.montoya.persistence.Persistence;

public class DebugSettings {
static final String LOGGING_KEY = "logging";

private final Persistence persistence;

public DebugSettings(Persistence persistence) {
this.persistence = persistence;
}

public boolean logging() {
Boolean logging = persistence.preferences().getBoolean(LOGGING_KEY);
return logging != null && logging;
}

public void setLogging(boolean logging) {
persistence.preferences().setBoolean(LOGGING_KEY, logging);
}
}
47 changes: 47 additions & 0 deletions src/main/java/ui/view/pane/settings/DebugSettingsPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ui.view.pane.settings;

import settings.debug.DebugSettings;
import ui.view.component.HeaderLabel;

import javax.swing.*;
import java.awt.*;

import static java.awt.GridBagConstraints.FIRST_LINE_START;
import static java.awt.GridBagConstraints.HORIZONTAL;

class DebugSettingsPanel extends JPanel {
DebugSettingsPanel(DebugSettings debugSettings) {
GridBagLayout layout = new GridBagLayout();
layout.columnWidths = new int[]{0, 20};
layout.rowHeights = new int[]{0, 10, 0, 5, 0};
setLayout(layout);

JCheckBox loggingCheckbox = new JCheckBox("Enabled");
loggingCheckbox.setSelected(debugSettings.logging());
loggingCheckbox.addActionListener(e -> debugSettings.setLogging(loggingCheckbox.isSelected()));

GridBagConstraints constraints = new GridBagConstraints();

constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = FIRST_LINE_START;
constraints.gridwidth = 3;
JLabel headerLabel = new HeaderLabel("Logging");
add(headerLabel, constraints);

constraints.gridx = 0;
constraints.gridy = 2;
constraints.anchor = FIRST_LINE_START;
constraints.gridwidth = 3;
constraints.weightx = 1.0;
constraints.fill = HORIZONTAL;
JLabel descriptionLabel = new JLabel("Use this setting to enable or disable logging");
add(descriptionLabel, constraints);

constraints.gridx = 0;
constraints.gridy = 4;
constraints.anchor = FIRST_LINE_START;
constraints.gridwidth = 3;
add(loggingCheckbox, constraints);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import static java.awt.GridBagConstraints.HORIZONTAL;
import static ui.view.component.filechooser.ChooseMode.DIRECTORIES_ONLY;

public class DefaultSaveLocationSettingsPanel extends JPanel {
class DefaultSaveLocationSettingsPanel extends JPanel {
private final JLabel headerLabel = new HeaderLabel("Default save location for BChecks");
private final JLabel descriptionLabel = new JLabel("Use this setting to define where BChecks should be saved to by default. If you don't use this setting, you'll be asked where to save the BCheck to every time that you save one");
private final JButton chooseFileButton = new JButton("Choose directory");
Expand All @@ -22,9 +22,7 @@ public class DefaultSaveLocationSettingsPanel extends JPanel {
private final DefaultSaveLocationSettings defaultSaveLocationSettings;
private final JTextField pathField = new JTextField();

public DefaultSaveLocationSettingsPanel(DefaultSaveLocationSettings defaultSaveLocationSettings) {
super();

DefaultSaveLocationSettingsPanel(DefaultSaveLocationSettings defaultSaveLocationSettings) {
this.defaultSaveLocationSettings = defaultSaveLocationSettings;

initialiseUi();
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/ui/view/pane/settings/GitHubSettingsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import static java.awt.GridBagConstraints.FIRST_LINE_START;

public class GitHubSettingsPanel extends JPanel {
class GitHubSettingsPanel extends JPanel {
private final JLabel headerLabel = new HeaderLabel("GitHub configuration");
private final JLabel descriptionLabelFirstLine = new JLabel("Use these settings to define which GitHub repo the extension looks at to find BChecks.");
private final JLabel descriptionLabelSecondLine = new JLabel("If the repo isn't public, you'll need to specify an API key too. You can look at GitHub's documentation to find out how to create one.");
Expand All @@ -22,9 +22,7 @@ public class GitHubSettingsPanel extends JPanel {

private final GitHubSettings gitHubSettings;

public GitHubSettingsPanel(GitHubSettings gitHubSettings) {
super();

GitHubSettingsPanel(GitHubSettings gitHubSettings) {
this.gitHubSettings = gitHubSettings;

initialiseUi();
Expand Down
Loading

0 comments on commit 2a8ceed

Please sign in to comment.