Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oppdatering for logging av identer #3715

Merged
merged 1 commit into from
Jan 13, 2025
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,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");
}
}
Loading