Skip to content

Commit

Permalink
Merge pull request #23 from delinea-sagar/maskingIssue
Browse files Browse the repository at this point in the history
Fixed the Jenkins job output masking issue
  • Loading branch information
delineaKrehl authored Apr 10, 2024
2 parents 0471a01 + 2d67512 commit b8d5fb5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .changes/1.0.7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 1.0.7 - 2024-04-10
### 🐛 Bug Fix

- Fixed the Jenkins job output masking issue.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
and is generated by [Changie](https://github.com/miniscruff/changie).
## 1.0.7 - 2024-04-10
### 🐛 Bug Fix

- Fixed the Jenkins job output masking issue.
## 1.0.6 - 2024-01-10
### 🐛 Bug Fix

Expand Down
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tasks:
bump:
desc: bump the version using changie
cmds:
- changie batch 1.0.6
- changie batch 1.0.7
- changie merge
- git add .changes/*
- git add CHANGELOG.md
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>
<groupId>io.jenkins.plugins</groupId>
<artifactId>thycotic-secret-server</artifactId>
<version>1.0.6</version>
<version>1.0.7</version>
<packaging>hpi</packaging>
<properties>
<!-- Baseline Jenkins version you use to build the plugin. Users must have this version or newer to run. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.thycotic.secrets.server.spring.Secret;
import com.thycotic.secrets.server.spring.SecretServer;
Expand All @@ -29,8 +28,6 @@
import hudson.model.TaskListener;
import hudson.tasks.BuildWrapperDescriptor;
import jenkins.tasks.SimpleBuildWrapper;
import org.jenkinsci.plugins.credentialsbinding.masking.SecretPatterns;
import java.util.stream.Collectors;

public class ServerBuildWrapper extends SimpleBuildWrapper {
private static final String USERNAME_PROPERTY = "secret_server.oauth2.username";
Expand All @@ -57,8 +54,7 @@ public void setSecrets(final List<ServerSecret> secrets) {

@Override
public ConsoleLogFilter createLoggerDecorator(final Run<?, ?> build) {
List<String> values = valuesToMask.stream().filter(Objects::nonNull).collect(Collectors.toList());
return new ServerConsoleLogFilter(build.getCharset().name(), !values.isEmpty() ? SecretPatterns.getAggregateSecretPattern(values) : null);
return new ServerConsoleLogFilter(build.getCharset().name(), valuesToMask);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,44 @@
import java.io.OutputStream;
import java.io.Serializable;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.regex.Pattern;

import org.jenkinsci.plugins.credentialsbinding.masking.SecretPatterns;
import java.util.regex.PatternSyntaxException;

// borrowed from https://github.com/jenkinsci/azure-keyvault-plugin/blob/master/src/main/java/org/jenkinsci/plugins/azurekeyvaultplugin/MaskingConsoleLogFilter.java
public class ServerConsoleLogFilter extends ConsoleLogFilter implements Serializable {
private static final long serialVersionUID = 1L;
private final String charsetName;
private final Pattern valuesToMask;
private final List<String> valuesToMask;

public ServerConsoleLogFilter(final String charsetName, final Pattern valuesToMask) {
public ServerConsoleLogFilter(final String charsetName, final List<String> valuesToMask) {
this.charsetName = charsetName;
this.valuesToMask = valuesToMask;
}

@Override
public OutputStream decorateLogger(Run run, final OutputStream logger) throws IOException, InterruptedException {
return new SecretPatterns.MaskingOutputStream(logger, () -> valuesToMask, charsetName);
return new SecretPatterns.MaskingOutputStream(logger, () -> {
List<String> values = valuesToMask.stream().filter(Objects::nonNull).collect(Collectors.toList());
if (!values.isEmpty()) {
return ServerConsoleLogFilter.getAggregateSecretPattern(values);
} else {
return null;
}
},charsetName);
}

public static Pattern getAggregateSecretPattern(List<String> patterns) {
String aggregatedPattern = String.join("|", patterns);
try {
return Pattern.compile(aggregatedPattern);
} catch (PatternSyntaxException e) {
System.err.println("Error compiling pattern: " + e.getMessage());
return null;
}
}
}

0 comments on commit b8d5fb5

Please sign in to comment.