Skip to content

Commit

Permalink
Merge pull request #27 from pyieh/master
Browse files Browse the repository at this point in the history
Unique Names for Regex Patterns
  • Loading branch information
pyieh committed May 16, 2022
2 parents 474ca80 + 644e7cc commit e382f25
Show file tree
Hide file tree
Showing 14 changed files with 546 additions and 150 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ work

# Intellij project files
.idea
*.iml

**/.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package com.michelin.cio.hudson.plugins.maskpasswords;

import com.michelin.cio.hudson.plugins.maskpasswords.MaskPasswordsConfig.VarMaskRegexEntry;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
Expand All @@ -33,8 +34,6 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.console.ConsoleLogFilter;
import hudson.model.AbstractBuild;
import hudson.model.AbstractDescribableImpl;
Expand All @@ -46,7 +45,20 @@
import hudson.model.TaskListener;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.util.Secret;
import jenkins.tasks.SimpleBuildWrapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.structs.describable.CustomDescribableModel;
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
import org.jvnet.localizer.Localizable;
import org.jvnet.localizer.ResourceBundleHolder;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
Expand All @@ -59,20 +71,6 @@
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import jenkins.tasks.SimpleBuildWrapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.structs.describable.CustomDescribableModel;
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
import org.jvnet.localizer.Localizable;
import org.jvnet.localizer.ResourceBundleHolder;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

/**
* Build wrapper that alters the console so that passwords don't get displayed.
Expand All @@ -82,17 +80,17 @@
public final class MaskPasswordsBuildWrapper extends SimpleBuildWrapper {

private final List<VarPasswordPair> varPasswordPairs;
private final List<VarMaskRegex> varMaskRegexes;
private final List<VarMaskRegexEntry> varMaskRegexes;

@DataBoundConstructor
public MaskPasswordsBuildWrapper(List<VarPasswordPair> varPasswordPairs, List<VarMaskRegex> varMaskRegexes) {
public MaskPasswordsBuildWrapper(List<VarPasswordPair> varPasswordPairs, List<VarMaskRegexEntry> varMaskRegexes) {
this.varPasswordPairs = varPasswordPairs;
this.varMaskRegexes = varMaskRegexes;
}

public MaskPasswordsBuildWrapper(List<VarPasswordPair> varPasswordPairs) {
this.varPasswordPairs = varPasswordPairs;
this.varMaskRegexes = new ArrayList<VarMaskRegex>();
this.varMaskRegexes = new ArrayList<>();
}

@Override
Expand All @@ -108,9 +106,9 @@ public ConsoleLogFilter createLoggerDecorator(Run<?, ?> build) {
}

// global regexes
List<VarMaskRegex> globalVarMaskRegexes = config.getGlobalVarMaskRegexes();
for(VarMaskRegex globalVarMaskRegex: globalVarMaskRegexes) {
allRegexes.add(globalVarMaskRegex.getRegex());
List<MaskPasswordsConfig.VarMaskRegexEntry> globalVarMaskRegexes = config.getGlobalVarMaskRegexesU();
for(MaskPasswordsConfig.VarMaskRegexEntry globalVarMaskRegex: globalVarMaskRegexes) {
allRegexes.add(globalVarMaskRegex.getValue().getRegex());
}

// job's passwords
Expand All @@ -125,8 +123,8 @@ public ConsoleLogFilter createLoggerDecorator(Run<?, ?> build) {

// job's regexes
if(varMaskRegexes != null) {
for(VarMaskRegex varMaskRegex: varMaskRegexes) {
String regex = varMaskRegex.getRegex();
for(VarMaskRegexEntry entry: varMaskRegexes) {
String regex = entry.getRegexString();
if(StringUtils.isNotBlank(regex)) {
allRegexes.add(regex);
}
Expand Down Expand Up @@ -174,9 +172,8 @@ private static final class FilterImpl extends ConsoleLogFilter implements Serial
}
}

@SuppressWarnings("rawtypes")
@Override
public OutputStream decorateLogger(AbstractBuild _ignore, OutputStream logger) throws IOException, InterruptedException {
public OutputStream decorateLogger(Run run, OutputStream logger) {
List<String> passwords = new ArrayList<String>();
List<String> regexes = new ArrayList<String>();
for (Secret password : allPasswords) {
Expand All @@ -185,7 +182,8 @@ public OutputStream decorateLogger(AbstractBuild _ignore, OutputStream logger) t
for (String regex : allRegexes) {
regexes.add(regex);
}
return new MaskPasswordsOutputStream(logger, passwords, regexes);
String runName = run != null ? run.getFullDisplayName() : "";
return new MaskPasswordsOutputStream(logger, passwords, regexes, runName);
}

}
Expand Down Expand Up @@ -231,7 +229,7 @@ public List<VarPasswordPair> getVarPasswordPairs() {
return varPasswordPairs;
}

public List<VarMaskRegex> getVarMaskRegexes() {
public List<VarMaskRegexEntry> getVarMaskRegexes() {
return varMaskRegexes;
}

Expand Down Expand Up @@ -369,6 +367,10 @@ public int hashCode() {
return hash;
}

public String toString() {
return regex;
}

@Extension
public static class DescriptorImpl extends Descriptor<VarMaskRegex> {}

Expand All @@ -391,7 +393,7 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
getConfig().clear();

LOGGER.fine("Processing the maskedParamDefs and selectedMaskedParamDefs JSON objects");
JSONObject submittedForm = req.getSubmittedForm();
JSONObject submittedForm = req.getSubmittedForm();

// parameter definitions to be automatically masked
JSONArray paramDefinitions = submittedForm.getJSONArray("maskedParamDefs");
Expand Down Expand Up @@ -423,20 +425,22 @@ else if(o instanceof JSONObject) {
}

// global regexes
if(submittedForm.has("globalVarMaskRegexes")) {
Object o = submittedForm.get("globalVarMaskRegexes");
if(submittedForm.has("globalVarMaskRegexesU")) {
Object o = submittedForm.get("globalVarMaskRegexesU");

if(o instanceof JSONArray) {
JSONArray jsonArray = submittedForm.getJSONArray("globalVarMaskRegexes");
JSONArray jsonArray = submittedForm.getJSONArray("globalVarMaskRegexesU");
for(int i = 0; i < jsonArray.size(); i++) {
getConfig().addGlobalVarMaskRegex(new VarMaskRegex(
jsonArray.getJSONObject(i).getString("regex")));
getConfig().addGlobalVarMaskRegex(
jsonArray.getJSONObject(i).getString("key"),
new VarMaskRegex(jsonArray.getJSONObject(i).getString("value")));
}
}
else if(o instanceof JSONObject) {
JSONObject jsonObject = submittedForm.getJSONObject("globalVarMaskRegexes");
getConfig().addGlobalVarMaskRegex(new VarMaskRegex(
jsonObject.getString("regex")));
JSONObject jsonObject = submittedForm.getJSONObject("globalVarMaskRegexesU");
getConfig().addGlobalVarMaskRegex(
jsonObject.getString("key"),
new VarMaskRegex(jsonObject.getString("value")));
}
}

Expand All @@ -462,8 +466,8 @@ public List<VarPasswordPair> getGlobalVarPasswordPairs() {
return getConfig().getGlobalVarPasswordPairs();
}

public List<VarMaskRegex> getGlobalVarMaskRegexes() {
return getConfig().getGlobalVarMaskRegexes();
public List<MaskPasswordsConfig.VarMaskRegexEntry> getGlobalVarMaskRegexesU() {
return getConfig().getGlobalVarMaskRegexesU();
}

/**
Expand Down Expand Up @@ -499,6 +503,7 @@ public static final class ConverterImpl implements Converter {
private final static String VAR_ATT = "var";
private final static String PASSWORD_ATT = "password";
private final static String REGEX_ATT = "regex";
private final static String REGEX_NAME = "name";

public boolean canConvert(Class clazz) {
return clazz.equals(MaskPasswordsBuildWrapper.class);
Expand All @@ -525,13 +530,14 @@ public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContex
// varMaskRegexes
if(maskPasswordsBuildWrapper.getVarMaskRegexes() != null) {
writer.startNode(VAR_MASK_REGEXES_NODE);
for(VarMaskRegex varMaskRegex: maskPasswordsBuildWrapper.getVarMaskRegexes()) {
for(VarMaskRegexEntry varMaskRegex: maskPasswordsBuildWrapper.getVarMaskRegexes()) {
// blank passwords are skipped
if(StringUtils.isBlank(varMaskRegex.getRegex())) {
if(StringUtils.isBlank(varMaskRegex.getRegexString())) {
continue;
}
writer.startNode(VAR_MASK_REGEX_NODE);
writer.addAttribute(REGEX_ATT, varMaskRegex.getRegex());
writer.addAttribute(REGEX_NAME, varMaskRegex.getName());
writer.addAttribute(REGEX_ATT, varMaskRegex.getRegexString());
writer.endNode();
}
writer.endNode();
Expand All @@ -540,7 +546,7 @@ public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContex

public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
List<VarPasswordPair> varPasswordPairs = new ArrayList<VarPasswordPair>();
List<VarMaskRegex> varMaskRegexes = new ArrayList<VarMaskRegex>();
List<VarMaskRegexEntry> varMaskRegexes = new ArrayList<>();

while(reader.hasMoreChildren()) {
reader.moveDown();
Expand All @@ -565,7 +571,8 @@ else if(reader.getNodeName().equals(VAR_MASK_REGEXES_NODE)) {
while(reader.hasMoreChildren()) {
reader.moveDown();
if(reader.getNodeName().equals(VAR_MASK_REGEX_NODE)) {
varMaskRegexes.add(new VarMaskRegex(
varMaskRegexes.add(new VarMaskRegexEntry(
reader.getAttribute(REGEX_NAME),
reader.getAttribute(REGEX_ATT)));
}
else {
Expand Down
Loading

0 comments on commit e382f25

Please sign in to comment.