Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #16 from armfergom/JENKINS-35095
[JENKINS-35095] Findbugs issues. Migrate to 2.9 parent pom.
  • Loading branch information
jglick committed Jun 9, 2016
2 parents a2bf336 + 4424c20 commit dfe5288
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
7 changes: 4 additions & 3 deletions pom.xml
Expand Up @@ -5,10 +5,9 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.596.1</version>
<version>2.9</version>
</parent>

<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials-binding</artifactId>
<version>1.8-SNAPSHOT</version>
<packaging>hpi</packaging>
Expand All @@ -19,7 +18,9 @@
<url>http://wiki.jenkins-ci.org/display/JENKINS/Credentials+Binding+Plugin</url>
<properties>
<workflow.version>1.7</workflow.version>
</properties>
<jenkins.version>1.596.1</jenkins.version>
<java.level>6</java.level>
</properties>
<licenses>
<license>
<name>MIT</name>
Expand Down
Expand Up @@ -25,12 +25,15 @@
package org.jenkinsci.plugins.credentialsbinding;

import com.cloudbees.plugins.credentials.common.StandardCredentials;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import java.io.IOException;

import javax.annotation.Nonnull;

import hudson.model.Run;
Expand Down Expand Up @@ -110,9 +113,13 @@ public Environment bind(@Nonnull final AbstractBuild build, final Launcher launc
throw new AbstractMethodError("you must override bindSingle");
}
}

private static class UnbinderWrapper implements Unbinder {
private static final long serialVersionUID = 1; // only really serialize if what it wraps is, too

@SuppressFBWarnings(value="SE_BAD_FIELD", justification="Environment is deprecated and will generally be not serializable")
private final Environment e;

UnbinderWrapper(Environment e) {
this.e = e;
}
Expand Down
Expand Up @@ -126,7 +126,8 @@ protected static final class NullUnbinder implements Unbinder {
if (type().isInstance(cred))
return type().cast(cred);

Descriptor expected = Jenkins.getInstance().getDescriptor(type());

Descriptor expected = Jenkins.getActiveInstance().getDescriptor(type());
throw new CredentialNotFoundException("Credentials '"+credentialsId+"' is of type '"+
cred.getDescriptor().getDisplayName()+"' where '"+
(expected!=null ? expected.getDisplayName() : type().getName())+
Expand Down
Expand Up @@ -36,6 +36,7 @@
import hudson.model.TaskListener;
import hudson.util.Secret;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
Expand All @@ -45,6 +46,8 @@
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.codec.Charsets;
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
Expand Down Expand Up @@ -92,7 +95,7 @@ public static final class Execution extends AbstractStepExecutionImpl {
}
getContext().newBodyInvoker().
withContext(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class), new Overrider(overrides))).
withContext(BodyInvoker.mergeConsoleLogFilters(getContext().get(ConsoleLogFilter.class), new Filter(overrides.values()))).
withContext(BodyInvoker.mergeConsoleLogFilters(getContext().get(ConsoleLogFilter.class), new Filter(overrides.values(), run.getCharset().name()))).
withCallback(new Callback(unbinders)).
start();
return false;
Expand Down Expand Up @@ -130,8 +133,9 @@ private static final class Filter extends ConsoleLogFilter implements Serializab
private static final long serialVersionUID = 1;

private final Secret pattern;

Filter(Collection<String> secrets) {
private String charsetName;

Filter(Collection<String> secrets, String charsetName) {
StringBuilder b = new StringBuilder();
for (String secret : secrets) {
if (b.length() > 0) {
Expand All @@ -140,15 +144,24 @@ private static final class Filter extends ConsoleLogFilter implements Serializab
b.append(Pattern.quote(secret));
}
pattern = Secret.fromString(b.toString());
this.charsetName = charsetName;
}

// To avoid de-serialization issues with newly added field (charsetName)
private Object readResolve() throws ObjectStreamException {
if (this.charsetName == null) {
this.charsetName = Charsets.UTF_8.name();
}
return this;
}

@Override public OutputStream decorateLogger(AbstractBuild _ignore, final OutputStream logger) throws IOException, InterruptedException {
final Pattern p = Pattern.compile(pattern.getPlainText());
return new LineTransformationOutputStream() {
@Override protected void eol(byte[] b, int len) throws IOException {
Matcher m = p.matcher(new String(b, 0, len));
Matcher m = p.matcher(new String(b, 0, len, charsetName));
if (m.find()) {
logger.write(m.replaceAll("****").getBytes());
logger.write(m.replaceAll("****").getBytes(charsetName));
} else {
// Avoid byte → char → byte conversion unless we are actually doing something.
logger.write(b, 0, len);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/index.jelly
@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
<div>
Allows credentials to be bound to environment variables for use from miscellaneous build steps.
</div>

0 comments on commit dfe5288

Please sign in to comment.