Skip to content
Snippets Groups Projects

Iss #113 - Update obfuscation logic to use new somewhat readable format

Merged Iss #113 - Update obfuscation logic to use new somewhat readable format
Merged Martin Lowe requested to merge (removed):malowe/main/113 into main
2 files
+ 116
24
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -12,10 +12,9 @@
package org.eclipsefoundation.git.eca.config;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.eclipsefoundation.git.eca.dto.CommitValidationMessage;
import org.eclipsefoundation.git.eca.dto.CommitValidationStatus;
@@ -23,8 +22,6 @@ import io.quarkus.qute.TemplateExtension;
@TemplateExtension
public class EclipseQuteTemplateExtensions {
public static final Pattern EMAIL_OBFUSCATION_PATTERN = Pattern.compile("^(.).*?(@.*)$");
public static final String EMAIL_OBFUSCATION_REPLACEMENT = "$1*****$2";
/**
* Made to count nested errors in a list of commit status objects.
@@ -37,8 +34,7 @@ public class EclipseQuteTemplateExtensions {
}
/**
* Formats and flattens a list of statuses to a list of errors encountered while
* validation a set of commits.
* Formats and flattens a list of statuses to a list of errors encountered while validation a set of commits.
*
* @param statuses a list of commit validation statuses to retrieve errors from
* @return a list of flattened errors, or an empty list if (none are found.
@@ -49,29 +45,54 @@ public class EclipseQuteTemplateExtensions {
/**
* <p>
* Obfuscates an email for public consumption, showing the first letter of the
* email address, and the domain of the address for identification, and
* stripping out the rest of the address.
* Obfuscates an email for public consumption, showing the first letter of the email address, and the domain of the
* address for identification, and stripping out the rest of the address.
* </p>
*
* <p>
* <strong>Example:</strong> <br />
* Source: sample.address+123@eclipse.org <br />
* Output: s*****@eclipse.org
* Output: sample.address+123@ecl*pse DOT org
* </p>
*
* @param email the address to obfuscate
* @return the obfuscated address, or empty string if (the string could not be
* parsed as an email address.
* @return the obfuscated address, or empty string if (the string could not be parsed as an email address.
*/
static String obfuscateEmail(String email) {
Matcher m = EMAIL_OBFUSCATION_PATTERN.matcher(email);
return m.matches() ? m.replaceAll(EMAIL_OBFUSCATION_REPLACEMENT) : "";
if (StringUtils.isBlank(email)) {
return "";
}
// split on the @ symbol
String[] emailParts = email.split("\\@");
if (emailParts.length != 2) {
// valid emails will have 2 sections when split this way
return "";
}
String[] domainParts = emailParts[1].split("\\.");
if (domainParts.length < 2) {
// emails should have at least 2 parts here
return "";
}
// the index in the middle of the first domain part of the email address
int middleIndexDomain = Math.floorDiv(domainParts[0].length(), 2);
// build the obfuscated email address in the pattern defined in the block comment
StringBuilder sb = new StringBuilder();
sb.append(emailParts[0]);
sb.append("@");
sb.append(domainParts[0].substring(0, middleIndexDomain));
sb.append('*');
sb.append(domainParts[0].substring(middleIndexDomain + 1));
for (int i = 1; i < domainParts.length; i++) {
sb.append(" DOT ");
sb.append(domainParts[i]);
}
return sb.toString();
}
/**
* Standardized conversion of error codes into messages to be consumed
* downstream.
* Standardized conversion of error codes into messages to be consumed downstream.
*
* @param message the validation error to convert into a meaningful message
* @return the message string for the given validation error.
@@ -79,17 +100,21 @@ public class EclipseQuteTemplateExtensions {
static String getMessageForError(CommitValidationMessage message) {
String out = "";
if (message.getStatusCode() == -406) {
out = String.format("Committer does not have permission to push on behalf of another user (Legacy). (%s)",
EclipseQuteTemplateExtensions.obfuscateEmail(message.getCommitterEmail()));
out = String
.format("Committer does not have permission to push on behalf of another user (Legacy). (%s)",
EclipseQuteTemplateExtensions.obfuscateEmail(message.getCommitterEmail()));
} else if (message.getStatusCode() == -405) {
out = String.format("Committer did not have a signed ECA on file. (%s)",
EclipseQuteTemplateExtensions.obfuscateEmail(message.getCommitterEmail()));
out = String
.format("Committer did not have a signed ECA on file. (%s)",
EclipseQuteTemplateExtensions.obfuscateEmail(message.getCommitterEmail()));
} else if (message.getStatusCode() == -404) {
out = String.format("Author did not have a signed ECA on file. (%s)",
EclipseQuteTemplateExtensions.obfuscateEmail(message.getAuthorEmail()));
out = String
.format("Author did not have a signed ECA on file. (%s)",
EclipseQuteTemplateExtensions.obfuscateEmail(message.getAuthorEmail()));
} else if (message.getStatusCode() == -403) {
out = String.format("Committer does not have permission to commit on specification projects. (%s)",
EclipseQuteTemplateExtensions.obfuscateEmail(message.getCommitterEmail()));
out = String
.format("Committer does not have permission to commit on specification projects. (%s)",
EclipseQuteTemplateExtensions.obfuscateEmail(message.getCommitterEmail()));
} else if (message.getStatusCode() == -402) {
out = "Sign-off not detected in the commit message (Legacy).";
} else if (message.getStatusCode() == -401) {
Loading