Skip to content

Commit

Permalink
Oppdatering for logging av identer
Browse files Browse the repository at this point in the history
  • Loading branch information
krharum committed Jan 10, 2025
1 parent e5518dc commit d5addc9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
public class TestnavLogbackEncoder extends LogstashEncoder {

// matches exactly 11 digits (\\d{11}) that are not immediately preceded ((?<!\\d)) or followed ((?!\\d)) by another digit.
private final Pattern identNummer = Pattern.compile("(?<!\\d)\\d{11}(?!\\d)");
private final Pattern bearer = Pattern.compile("Bearer [a-zA-Z0-9\\-_.]+");
private static final Pattern IDENT = Pattern.compile("(?<!\\d)\\d{11}(?!\\d)");
private static final Pattern BEARER = Pattern.compile("Bearer [a-zA-Z0-9\\-_.]+");
@Setter
private int maxStackTraceLength = 480;

Expand Down Expand Up @@ -113,15 +113,14 @@ private void appendStackTraceCauses(ThrowableProxy exception, StringWriter write
}

private String formatMessage(String message) {
message = identNummer.matcher(message).replaceAll(match -> {
if (match.group().charAt(2) < '4') {
return match.group().substring(0, 6) + "xxxxx";
}
return match.group();
});

message = bearer.matcher(message).replaceAll("REDACTED_BEARER");
message = IDENT.matcher(message).replaceAll(match ->

match.group().charAt(2) < '4' ?
match.group().substring(0, 6) + "xxxxx" :
match.group().substring(0, 11) + "x"
);

return message;
return BEARER.matcher(message).replaceAll("Bearer token");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
public class TestnavLogbackEncoder extends LogstashEncoder {

// matches exactly 11 digits (\\d{11}) that are not immediately preceded ((?<!\\d)) or followed ((?!\\d)) by another digit.
private final Pattern pattern = Pattern.compile("(?<!\\d)\\d{11}(?!\\d)");
private static final Pattern IDENT = Pattern.compile("(?<!\\d)\\d{11}(?!\\d)");
private static final Pattern BEARER = Pattern.compile("Bearer [a-zA-Z0-9\\-_.]+");

@Setter
private int maxStackTraceLength = 480;
Expand Down Expand Up @@ -113,24 +114,14 @@ private void appendStackTraceCauses(ThrowableProxy exception, StringWriter write
}

private String formatMessage(String message) {
var matcher = pattern.matcher(message);

if (!matcher.find()) {
return message;
}

matcher.reset();
var result = new StringBuilder();
message = IDENT.matcher(message).replaceAll(match ->

while (matcher.find()) {
var match = matcher.group();
if (match.charAt(2) == '0' || match.charAt(2) == '1') {
var replacement = match.substring(0, 6) + "xxxxx";
matcher.appendReplacement(result, replacement);
}
}
matcher.appendTail(result);
match.group().charAt(2) < '4' ?
match.group().substring(0, 6) + "xxxxx" :
match.group().substring(0, 11) + "x"
);

return result.toString();
return BEARER.matcher(message).replaceAll("Bearer token");
}
}

0 comments on commit d5addc9

Please sign in to comment.