Skip to content

Commit

Permalink
fix: PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anku255 committed Feb 5, 2024
1 parent 0ba1622 commit 80d096c
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LayoutBase;
import io.supertokens.storage.mysql.Start;
import io.supertokens.storage.mysql.utils.Utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class CustomLayout extends LayoutBase<ILoggingEvent> {

Expand All @@ -37,21 +35,6 @@ class CustomLayout extends LayoutBase<ILoggingEvent> {
this.processID = processID;
}

public static String maskDBPassword(String log) {
String regex = "(\\|db_pass\\|)(.*?)(\\|db_pass\\|)";

Matcher matcher = Pattern.compile(regex).matcher(log);
StringBuffer maskedLog = new StringBuffer();

while (matcher.find()) {
String maskedPassword = "*".repeat(8);
matcher.appendReplacement(maskedLog, "|" + maskedPassword + "|");
}

matcher.appendTail(maskedLog);
return maskedLog.toString();
}

@Override
public String doLayout(ILoggingEvent event) {
StringBuilder sbuf = new StringBuilder();
Expand All @@ -75,7 +58,7 @@ public String doLayout(ILoggingEvent event) {
sbuf.append(event.getCallerData()[1]);
sbuf.append(" | ");

sbuf.append(maskDBPassword(event.getFormattedMessage()));
sbuf.append(Utils.maskDBPassword(event.getFormattedMessage()));
sbuf.append(CoreConstants.LINE_SEPARATOR);
sbuf.append(CoreConstants.LINE_SEPARATOR);

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/io/supertokens/storage/mysql/output/Logging.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public class Logging extends ResourceDistributor.SingletonResource {

private Logging(Start start, String infoLogPath, String errorLogPath) {
this.infoLogger = infoLogPath.equals("null")
? createLoggerForConsole(start, "io.supertokens.storage.mysql.Info")
? createLoggerForConsole(start, "io.supertokens.storage.mysql.Info", LOG_LEVEL.INFO)
: createLoggerForFile(start, infoLogPath, "io.supertokens.storage.mysql.Info");
this.errorLogger = errorLogPath.equals("null")
? createLoggerForConsole(start, "io.supertokens.storage.mysql.Error")
? createLoggerForConsole(start, "io.supertokens.storage.mysql.Error", LOG_LEVEL.ERROR)
: createLoggerForFile(start, errorLogPath, "io.supertokens.storage.mysql.Error");
}

Expand Down Expand Up @@ -154,12 +154,12 @@ public static void error(Start start, String message, boolean toConsoleAsWell, E

private static void systemOut(String msg) {
if (!Start.silent) {
System.out.println(msg);
System.out.println(Utils.maskDBPassword(msg));
}
}

private static void systemErr(String err) {
System.err.println(err);
System.err.println(Utils.maskDBPassword(err));
}

public static void stopLogging(Start start) {
Expand Down Expand Up @@ -198,7 +198,7 @@ private Logger createLoggerForFile(Start start, String file, String name) {
return logger;
}

private Logger createLoggerForConsole(Start start, String name) {
private Logger createLoggerForConsole(Start start, String name, LOG_LEVEL logLevel) {
Logger logger = (Logger) LoggerFactory.getLogger(name);

// We don't need to add appender if it is already added
Expand All @@ -211,6 +211,7 @@ private Logger createLoggerForConsole(Start start, String name) {
ple.setContext(lc);
ple.start();
ConsoleAppender<ILoggingEvent> logConsoleAppender = new ConsoleAppender<>();
logConsoleAppender.setTarget(logLevel == LOG_LEVEL.ERROR ? "System.err" : "System.out");
logConsoleAppender.setEncoder(ple);
logConsoleAppender.setContext(lc);
logConsoleAppender.start();
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/supertokens/storage/mysql/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
public static String exceptionStacktraceToString(Exception e) {
Expand All @@ -39,4 +41,19 @@ public static String generateCommaSeperatedQuestionMarks(int size) {
}
return builder.toString();
}

public static String maskDBPassword(String log) {
String regex = "(\\|db_pass\\|)(.*?)(\\|db_pass\\|)";

Matcher matcher = Pattern.compile(regex).matcher(log);
StringBuffer maskedLog = new StringBuffer();

while (matcher.find()) {
String maskedPassword = "*".repeat(8);
matcher.appendReplacement(maskedLog, "|" + maskedPassword + "|");
}

matcher.appendTail(maskedLog);
return maskedLog.toString();
}
}
Loading

0 comments on commit 80d096c

Please sign in to comment.