Skip to content
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
29 changes: 16 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@
<artifactId>workflow-step-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pipeline-stage-step</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
Expand All @@ -140,19 +153,19 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.13.0</version>
<version>2.25.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0-beta.5</version>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0-beta.5</version>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -188,11 +201,6 @@
<artifactId>workflow-cps</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
Expand All @@ -203,11 +211,6 @@
<artifactId>workflow-scm-step</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ public class LogstashConsoleLogFilter extends ConsoleLogFilter implements Serial

private static final Logger LOGGER = Logger.getLogger(LogstashConsoleLogFilter.class.getName());

private transient Run<?, ?> run;
public LogstashConsoleLogFilter() {}

public LogstashConsoleLogFilter(Run<?, ?> run)
{
this.run = run;
}
private static final long serialVersionUID = 1L;

@Override
Expand All @@ -49,15 +44,7 @@ public OutputStream decorateLogger(Run build, OutputStream logger) throws IOExce
return logger;
}
}
if (run != null)
{
LogstashWriter logstash = getLogStashWriter(run, logger);
return new LogstashOutputStream(logger, logstash);
}
else
{
return logger;
}
return logger;
}

LogstashWriter getLogStashWriter(Run<?, ?> build, OutputStream errorStream)
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/jenkins/plugins/logstash/LogstashWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Date;
Expand All @@ -51,22 +54,31 @@
* @author Liam Newman
* @since 1.0.5
*/
public class LogstashWriter {
@SuppressFBWarnings(value="SE_NO_SERIALVERSIONID")
public class LogstashWriter implements Serializable {

private final OutputStream errorStream;
private final Run<?, ?> build;
private final transient Run<?, ?> build;
private final TaskListener listener;
private final BuildData buildData;
private final String jenkinsUrl;
private final LogstashIndexerDao dao;
private boolean connectionBroken;
private final Charset charset;
private final String charset;
private final String stageName;
private final String agentName;

public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset) {
this(run, error, listener, charset, null, null);
}

public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener, Charset charset, String stageName, String agentName) {
this.errorStream = error != null ? error : System.err;
this.stageName = stageName;
this.agentName = agentName;
this.build = run;
this.listener = listener;
this.charset = charset;
this.charset = charset.toString();
this.dao = this.getDaoOrNull();
if (this.dao == null) {
this.jenkinsUrl = "";
Expand All @@ -82,7 +94,7 @@ public LogstashWriter(Run<?, ?> run, OutputStream error, TaskListener listener,
*
* @return the charset
*/
public Charset getCharset()
public String getCharset()
{
return charset;
}
Expand Down Expand Up @@ -154,12 +166,12 @@ BuildData getBuildData() {
if (build instanceof AbstractBuild) {
return new BuildData((AbstractBuild<?, ?>) build, new Date(), listener);
} else {
return new BuildData(build, new Date(), listener);
return new BuildData(build, new Date(), listener, stageName, agentName);
}
}

String getJenkinsUrl() {
return Jenkins.getInstance().getRootUrl();
return Jenkins.get().getRootUrl();
}

/**
Expand Down Expand Up @@ -207,8 +219,10 @@ private LogstashIndexerDao getDaoOrNull() {
private void logErrorMessage(String msg) {
try {
connectionBroken = true;
errorStream.write(msg.getBytes(charset));
errorStream.flush();
if (errorStream != null) {
errorStream.write(msg.getBytes(charset));
errorStream.flush();
}
} catch (IOException ex) {
// This should never happen, but if it does we just have to let it go.
ex.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;
import javax.annotation.Nonnull;

import java.security.cert.CertificateException;

import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -132,7 +134,7 @@ public boolean equals(Object obj)
if (getClass() != obj.getClass())
return false;
ElasticSearch other = (ElasticSearch) obj;
if (!Secret.toString(password).equals(other.getPassword().getPlainText()))
if (!Secret.toString(password).equals(Secret.toString(other.getPassword())))
{
return false;
}
Expand Down Expand Up @@ -188,35 +190,26 @@ public ElasticSearchDao createIndexerInstance()
ElasticSearchDao esDao = new ElasticSearchDao(getUri(), username, Secret.toString(password));

esDao.setMimeType(getMimeType());
try {
esDao.setCustomKeyStore(getCustomKeyStore());
} catch (KeyStoreException | CertificateException |
NoSuchAlgorithmException | KeyManagementException | IOException e) {
LOGGER.log(Level.WARNING, e.getMessage(), e);
}
return esDao;
}

private KeyStore getCustomKeyStore() {
KeyStore customKeyStore = null;

// Fetch custom alias+certificate as a keystore (if present)
if (!StringUtils.isBlank(customServerCertificateId)) {
StandardCertificateCredentials certificateCredentials = getCredentials(customServerCertificateId);
if (certificateCredentials != null) {
// Fetch keystore containing custom certificate
customKeyStore = certificateCredentials.getKeyStore();
try {
StandardCertificateCredentials certificateCredentials = getCredentials(customServerCertificateId);
if (certificateCredentials != null) {
esDao.setCustomKeyStore(certificateCredentials.getKeyStore(),
Secret.toString(certificateCredentials.getPassword()));
}
} catch (KeyStoreException | CertificateException |
NoSuchAlgorithmException | IOException e) {
LOGGER.log(Level.WARNING, e.getMessage(), e);
}
}

return customKeyStore;
return esDao;
}

private StandardCertificateCredentials getCredentials(String credentials)
{
return (StandardCertificateCredentials) CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(StandardCredentials.class,
Jenkins.getInstance(), ACL.SYSTEM, Collections.emptyList()),
CredentialsProvider.lookupCredentials(StandardCertificateCredentials.class,
Jenkins.get(), ACL.SYSTEM, Collections.emptyList()),
CredentialsMatchers.withId(credentials)
);
}
Expand Down Expand Up @@ -247,7 +240,7 @@ public ListBoxModel doFillCustomServerCertificateIdItems(
CredentialsMatchers.instanceOf(StandardCertificateCredentials.class)
),
CredentialsProvider.lookupCredentials(StandardCredentials.class,
Jenkins.getInstance(),
Jenkins.get(),
ACL.SYSTEM,
Collections.emptyList()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public boolean equals(Object obj)
if (getClass() != obj.getClass())
return false;
RabbitMq other = (RabbitMq) obj;
if (!Secret.toString(password).equals(other.getPassword().getPlainText()))
if (!Secret.toString(password).equals(Secret.toString(other.getPassword())))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public boolean equals(Object obj)
if (getClass() != obj.getClass())
return false;
Redis other = (Redis) obj;
if (!Secret.toString(password).equals(other.getPassword().getPlainText()))
if (!Secret.toString(password).equals(Secret.toString(other.getPassword())))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package jenkins.plugins.logstash.persistence;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;

Expand All @@ -36,7 +37,7 @@
* @author Rusty Gerard
* @since 1.0.0
*/
public abstract class AbstractLogstashIndexerDao implements LogstashIndexerDao {
public abstract class AbstractLogstashIndexerDao implements LogstashIndexerDao, Serializable {

@Override
public JSONObject buildPayload(BuildData buildData, String jenkinsUrl, List<String> logLines) {
Expand Down
50 changes: 38 additions & 12 deletions src/main/java/jenkins/plugins/logstash/persistence/BuildData.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

import static java.util.logging.Level.WARNING;
import java.io.IOException;
import java.io.Serializable;
import java.lang.invoke.MethodHandles;
import net.sf.json.JSONObject;

Expand All @@ -56,21 +57,25 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
* POJO for mapping build info to JSON.
*
* @author Rusty Gerard
* @since 1.0.0
*/
public class BuildData {
@SuppressFBWarnings(value="SE_NO_SERIALVERSIONID")
public class BuildData implements Serializable {

// ISO 8601 date format
private final static Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getCanonicalName());
public static class TestData {
public static class TestData implements Serializable {
private final int totalCount, skipCount, failCount, passCount;
private final List<FailedTest> failedTestsWithErrorDetail;
private final List<String> failedTests;

public static class FailedTest {
public static class FailedTest implements Serializable {
private final String fullName, errorDetails;
public FailedTest(String fullName, String errorDetails) {
super();
Expand Down Expand Up @@ -160,6 +165,8 @@ public List<String> getFailedTests()
private String url;
private String buildHost;
private String buildLabel;
private String stageName;
private String agentName;
private int buildNum;
private long buildDuration;
private transient String timestamp; // This belongs in the root object
Expand Down Expand Up @@ -212,9 +219,11 @@ public BuildData(AbstractBuild<?, ?> build, Date currentTime, TaskListener liste
}

// Pipeline project build
public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener) {
public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener, String stageName, String agentName) {
initData(build, currentTime);

this.agentName = agentName;
this.stageName = stageName;
rootProjectName = projectName;
rootFullProjectName = fullProjectName;
rootProjectDisplayName = displayName;
Expand Down Expand Up @@ -262,14 +271,15 @@ private void initData(Run<?, ?> build, Date currentTime) {

public void updateResult()
{
if (result == null && build.getResult() != null)
{
Result result = build.getResult();
this.result = result == null ? null : result.toString();
}
Action testResultAction = build.getAction(AbstractTestResultAction.class);
if (testResults == null && testResultAction != null) {
testResults = new TestData(testResultAction);
if (build != null) {
if (result == null && build.getResult() != null) {
Result result = build.getResult();
this.result = result == null ? null : result.toString();
}
Action testResultAction = build.getAction(AbstractTestResultAction.class);
if (testResults == null && testResultAction != null) {
testResults = new TestData(testResultAction);
}
}
}

Expand Down Expand Up @@ -443,4 +453,20 @@ public TestData getTestResults() {
public void setTestResults(TestData testResults) {
this.testResults = testResults;
}

public String getStageName() {
return stageName;
}

public void setStageName(String stageName) {
this.stageName = stageName;
}

public String getAgentName() {
return agentName;
}

public void setAgentName(String agentName) {
this.agentName = agentName;
}
}
Loading