Skip to content

Commit

Permalink
SONARJNKNS-257 Fix encoding when masking passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju committed Jun 20, 2016
1 parent ea30cb5 commit deb1f3d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
36 changes: 16 additions & 20 deletions src/main/java/hudson/plugins/sonar/SonarBuildWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,21 @@
*/
package hudson.plugins.sonar;

import hudson.util.ArgumentListBuilder;

import hudson.plugins.sonar.action.SonarMarkerAction;
import hudson.plugins.sonar.utils.SonarUtils;
import hudson.model.Action;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import hudson.plugins.sonar.utils.MaskPasswordsOutputStream;
import hudson.plugins.sonar.utils.SQServerVersions;
import hudson.EnvVars;
import hudson.plugins.sonar.utils.Logger;

import javax.annotation.Nullable;

import org.kohsuke.stapler.DataBoundConstructor;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.plugins.sonar.action.SonarMarkerAction;
import hudson.plugins.sonar.utils.Logger;
import hudson.plugins.sonar.utils.MaskPasswordsOutputStream;
import hudson.plugins.sonar.utils.SQServerVersions;
import hudson.plugins.sonar.utils.SonarUtils;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;

import hudson.util.ArgumentListBuilder;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
Expand All @@ -65,6 +57,10 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;

public class SonarBuildWrapper extends BuildWrapper {
private static final String DEFAULT_SONAR = "sonar";
Expand All @@ -89,7 +85,7 @@ public OutputStream decorateLogger(AbstractBuild build, OutputStream outputStrea

Logger.LOG.info(Messages.SonarBuildWrapper_MaskingPasswords());

List<String> passwords = new ArrayList<String>();
List<String> passwords = new ArrayList<>();

if (!StringUtils.isBlank(inst.getDatabasePassword())) {
passwords.add(inst.getDatabasePassword());
Expand All @@ -101,7 +97,7 @@ public OutputStream decorateLogger(AbstractBuild build, OutputStream outputStrea
passwords.add(inst.getServerAuthenticationToken());
}

return new MaskPasswordsOutputStream(outputStream, passwords);
return new MaskPasswordsOutputStream(outputStream, build.getCharset(), passwords);
}

@Override
Expand Down Expand Up @@ -174,7 +170,7 @@ public void buildEnvVars(Map<String, String> env) {
Map<String, String> sonarEnv = createVars(installation);

// resolve variables against each other
Map<String, String> sonarEnvResolved = new HashMap<String, String>(sonarEnv);
Map<String, String> sonarEnvResolved = new HashMap<>(sonarEnv);
EnvVars.resolve(sonarEnvResolved);

for (String k : sonarEnv.keySet()) {
Expand All @@ -196,7 +192,7 @@ public boolean tearDown(AbstractBuild build, BuildListener listener) throws IOEx
}

private Map<String, String> createVars(SonarInstallation inst) {
Map<String, String> map = new HashMap<String, String>();
Map<String, String> map = new HashMap<>();

map.put("SONAR_CONFIG_NAME", inst.getName());
map.put("SONAR_HOST_URL", getOrDefault(inst.getServerUrl(), "http://localhost:9000"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
import hudson.console.LineTransformationOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
Expand All @@ -49,10 +50,12 @@ public class MaskPasswordsOutputStream extends LineTransformationOutputStream {
private static final String URL_IN_LOGS = "ANALYSIS SUCCESSFUL, you can browse ";
private final OutputStream logger;
private final Pattern passwordsAsPattern;
private final Charset charset;

public MaskPasswordsOutputStream(OutputStream logger, Collection<String> passwords) {
public MaskPasswordsOutputStream(OutputStream logger, Charset charset, Collection<String> passwords) {

this.logger = logger;
this.charset = charset;

if (passwords != null && !passwords.isEmpty()) {

Expand Down Expand Up @@ -87,11 +90,11 @@ public MaskPasswordsOutputStream(OutputStream logger, Collection<String> passwor

@Override
protected void eol(byte[] bytes, int len) throws IOException {
String line = new String(bytes, 0, len, StandardCharsets.UTF_8);
String line = charset.decode(ByteBuffer.wrap(bytes, 0, len)).toString();
if (passwordsAsPattern != null && !line.contains(URL_IN_LOGS)) {
line = passwordsAsPattern.matcher(line).replaceAll(REPLACEMENT);
}
logger.write(line.getBytes(StandardCharsets.UTF_8));
logger.write(line.getBytes(charset));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;

public class MaskPasswordsOutputStreamTest {
private ByteArrayOutputStream os;
Expand Down Expand Up @@ -77,7 +77,7 @@ public void dontMaskUrl() throws IOException {

private BufferedWriter getWriter(String... passwords) {
os = new ByteArrayOutputStream();
MaskPasswordsOutputStream filteredOs = new MaskPasswordsOutputStream(os, Arrays.asList(passwords));
MaskPasswordsOutputStream filteredOs = new MaskPasswordsOutputStream(os, StandardCharsets.UTF_8, Arrays.asList(passwords));
return new BufferedWriter(new OutputStreamWriter(filteredOs));
}

Expand Down

0 comments on commit deb1f3d

Please sign in to comment.