Skip to content

Commit

Permalink
Use SecretPatterns from MaskingConsoleLogFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Jun 23, 2021
1 parent 72c88cf commit 789e6d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 55 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.277.x</artifactId>
<version>807.v6d348e44c987</version>
<version>876.vc43b4c6423b6</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down Expand Up @@ -114,6 +114,11 @@
<artifactId>workflow-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials-binding</artifactId>
<version>1.26-rc457.6dc28f0f8735</version> <!-- TODO https://github.com/jenkinsci/credentials-binding-plugin/pull/139 -->
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package org.jenkinsci.plugins.azurekeyvaultplugin;

import hudson.console.ConsoleLogFilter;
import hudson.console.LineTransformationOutputStream;
import hudson.model.Run;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.plugins.credentialsbinding.masking.SecretPatterns;

/*The logic in this class is borrowed from https://github.com/jenkinsci/credentials-binding-plugin/*/
public class MaskingConsoleLogFilter extends ConsoleLogFilter
implements Serializable {
private static final long serialVersionUID = 1L;
Expand All @@ -34,52 +27,7 @@ public OutputStream decorateLogger(
Run run,
final OutputStream logger
) {
return new LineTransformationOutputStream() {
Pattern p;

@Override
protected void eol(byte[] b, int len) throws IOException {
p = Pattern.compile(getPatternStringForSecrets(valuesToMask));
if (StringUtils.isBlank(p.pattern())) {
logger.write(b, 0, len);
return;
}
Matcher m = p.matcher(new String(b, 0, len, charsetName));
if (m.find()) {
logger.write(m.replaceAll("****").getBytes(charsetName));
} else {
// Avoid byte → char → byte conversion unless we are actually doing something.
logger.write(b, 0, len);
}
}
};
return new SecretPatterns.MaskingOutputStream(logger, () -> SecretPatterns.getAggregateSecretPattern(valuesToMask), charsetName);
}

/**
* Utility method for turning a collection of secret strings into a single {@link String} for pattern compilation.
*
* @param secrets A collection of secret strings
* @return A {@link String} generated from that collection.
*/
public static String getPatternStringForSecrets(Collection<String> secrets) {
if (secrets == null) {
return "";
}
StringBuilder b = new StringBuilder();
List<String> sortedByLength = new ArrayList<>(secrets.size());
for (String secret : secrets) {
if (secret != null) {
sortedByLength.add(secret);
}
}
sortedByLength.sort((o1, o2) -> o2.length() - o1.length());

for (String secret : sortedByLength) {
if (b.length() > 0) {
b.append('|');
}
b.append(Pattern.quote(secret));
}
return b.toString();
}
}

0 comments on commit 789e6d2

Please sign in to comment.