Skip to content

Commit

Permalink
[FIXED JENKINS-11399]
Browse files Browse the repository at this point in the history
  • Loading branch information
rseguy committed Oct 20, 2011
1 parent 9019979 commit f58d49c
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 45 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2010-2011, Manufacture Française des Pneumatiques Michelin, Romain Seguy
Copyright (c) 2010-2011, Manufacture Francaise des Pneumatiques Michelin, Romain Seguy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
Mask Passwords plugin
=====================
Copyright © 2010-2011, Manufacture Française des Pneumatiques Michelin, Romain Seguy. Licensed under [MIT License][0].
Copyright © 2010-2011, Manufacture Francaise des Pneumatiques Michelin, Romain Seguy. Licensed under [MIT License][0].

About this plugin
-----------------
Expand Down
33 changes: 3 additions & 30 deletions pom.xml
Expand Up @@ -2,19 +2,18 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.375</version>
<version>1.409</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>mask-passwords</artifactId>
<packaging>hpi</packaging>
<name>Mask Passwords Plugin</name>
<description>Masks passwords that may appear in the console</description>
<url>http://wiki.jenkins-ci.org/display/JENKINS/Mask+Passwords+Plugin</url>
<version>2.6.2-SNAPSHOT</version>
<version>2.7-SNAPSHOT</version>

<developers>
<developer>
Expand All @@ -24,30 +23,4 @@
</developer>
</developers>

<!-- see http://groups.google.com/group/jenkinsci-dev/browse_thread/thread/83e34c639eec470a for the rationale behind this -->
<build>
<plugins>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<goals>deploy</goals>
</configuration>
</plugin>
</plugins>
</build>

<!-- see http://groups.google.com/group/jenkinsci-dev/browse_thread/thread/83e34c639eec470a for the rationale behind this -->
<scm>
<connection>scm:svn:https://guest@svn.jenkins-ci.org/trunk/hudson/plugins/mask-passwords/</connection>
<developerConnection>scm:svn:https://svn.jenkins-ci.org/trunk/hudson/plugins/mask-passwords/</developerConnection>
<url>https://hudson.dev.java.net/source/browse/hudson/trunk/hudson/plugins/mask-passwords</url>
</scm>

<!-- see http://groups.google.com/group/jenkinsci-dev/browse_thread/thread/6ea03cf2b527376c for the rationale behind this -->
<distributionManagement>
<repository>
<id>java.net-m2-repository</id>
<url>http://maven.jenkins-ci.org/content/repositories/releases/</url>
</repository>
</distributionManagement>
</project>
Expand Up @@ -73,6 +73,12 @@ public final class MaskPasswordsBuildWrapper extends BuildWrapper {
public MaskPasswordsBuildWrapper(List<VarPasswordPair> varPasswordPairs) {
this.varPasswordPairs = varPasswordPairs;

// global passwords
List<VarPasswordPair> globalVarPasswordPairs = MaskPasswordsConfig.getInstance().getGlobalVarPasswordPairs();
for(VarPasswordPair globalVarPasswordPair: globalVarPasswordPairs) {
allPasswords.add(globalVarPasswordPair.getPassword());
}

if(varPasswordPairs != null) {
for(VarPasswordPair varPasswordPair: varPasswordPairs) {
String password = varPasswordPair.getPassword();
Expand Down Expand Up @@ -113,8 +119,17 @@ public OutputStream decorateLogger(AbstractBuild build, OutputStream logger) {
*/
@Override
public void makeBuildVariables(AbstractBuild build, Map<String, String> variables) {
// global var/password pairs
MaskPasswordsConfig config = MaskPasswordsConfig.getInstance();
List<VarPasswordPair> globalVarPasswordPairs = config.getGlobalVarPasswordPairs();
// we can't use variables.putAll() since passwords are ciphered when in varPasswordPairs
for(VarPasswordPair globalVarPasswordPair: globalVarPasswordPairs) {
variables.put(globalVarPasswordPair.getVar(), globalVarPasswordPair.getPassword());
}

// job's var/password pairs
if(varPasswordPairs != null) {
// cf. comment above
for(VarPasswordPair varPasswordPair: varPasswordPairs) {
if(StringUtils.isNotBlank(varPasswordPair.getVar())) {
variables.put(varPasswordPair.getVar(), varPasswordPair.getPassword());
Expand All @@ -136,8 +151,10 @@ public List<VarPasswordPair> getVarPasswordPairs() {

/**
* Represents name/password entries defined by users in their jobs.
* <p>Equality and hashcode are based on {@code var} only, not
* {@code password}.</p>
*/
public static class VarPasswordPair {
public static class VarPasswordPair implements Cloneable {

private final String var;
private final Secret password;
Expand All @@ -148,22 +165,50 @@ public VarPasswordPair(String var, String password) {
this.password = Secret.fromString(password);
}

@Override
public Object clone() {
return new VarPasswordPair(getVar(), getPassword());
}

@Override
public boolean equals(Object obj) {
if(obj == null) {
return false;
}
if(getClass() != obj.getClass()) {
return false;
}
final VarPasswordPair other = (VarPasswordPair) obj;
if((this.var == null) ? (other.var != null) : !this.var.equals(other.var)) {
return false;
}
return true;
}

public String getVar() {
return var;
}

public String getPassword() {
return Secret.toString(password);
}

public Secret getPasswordAsSecret() {
return password;
}


@Override
public int hashCode() {
int hash = 3;
hash = 67 * hash + (this.var != null ? this.var.hashCode() : 0);
return hash;
}

}

@Extension
public static final class DescriptorImpl extends BuildWrapperDescriptor {

public DescriptorImpl() {
super(MaskPasswordsBuildWrapper.class);
}
Expand All @@ -178,6 +223,7 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti

LOGGER.fine("Processing the maskedParamDefs and selectedMaskedParamDefs JSON objects");

// parameter definitions to be automatically masked
JSONArray paramDefinitions = req.getSubmittedForm().getJSONArray("maskedParamDefs");
JSONArray selectedParamDefinitions = req.getSubmittedForm().getJSONArray("selectedMaskedParamDefs");
for(int i = 0; i < selectedParamDefinitions.size(); i++) {
Expand All @@ -186,6 +232,14 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
}
}

// global var/password pairs
JSONArray jSONArray = req.getSubmittedForm().getJSONArray("globalVarPasswordPairs");
for(int i = 0; i < jSONArray.size(); i++) {
getConfig().addGlobalVarPasswordPair(new VarPasswordPair(
jSONArray.getJSONObject(i).getString("var"),
jSONArray.getJSONObject(i).getString("password")));
}

MaskPasswordsConfig.save(getConfig());

return true;
Expand Down
@@ -1,8 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2011, Manufacture Francaise des Pneumatiques Michelin,
* Romain Seguy
* Copyright (c) 2011, Manufacture Francaise des Pneumatiques Michelin, Romain Seguy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,6 +24,7 @@

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

import com.michelin.cio.hudson.plugins.maskpasswords.MaskPasswordsBuildWrapper.VarPasswordPair;
import hudson.ExtensionList;
import hudson.XmlFile;
import hudson.model.Hudson;
Expand All @@ -44,6 +44,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;

/**
Expand All @@ -68,13 +69,37 @@ public class MaskPasswordsConfig {
* builds' console.
*/
private transient Set<String> maskPasswordsParamValueClasses;
/**
* Users can define name/password pairs at the global level to share common
* passwords with several jobs.
* @since 2.7
*/
private List<VarPasswordPair> globalVarPasswordPairs;

public MaskPasswordsConfig() {
maskPasswordsParamDefClasses = new LinkedHashSet<String>();

// default values for the first time the config is created
addMaskedPasswordParameterDefinition(hudson.model.PasswordParameterDefinition.class.getName());
addMaskedPasswordParameterDefinition(com.michelin.cio.hudson.plugins.passwordparam.PasswordParameterDefinition.class.getName());

globalVarPasswordPairs = new ArrayList<VarPasswordPair>(); // order matters for display purposes
}

/**
* Adds a name/password pair at the global level.
*
* <p>If either name or password is blank (as defined per the Commons Lang
* library), then the pair is not added.</p>
*
* @since 2.7
*/
public void addGlobalVarPasswordPair(VarPasswordPair varPasswordPair) {
// blank values are forbidden
if(StringUtils.isBlank(varPasswordPair.getVar()) || StringUtils.isBlank(varPasswordPair.getPassword())) {
return;
}
globalVarPasswordPairs.add(varPasswordPair);
}

/**
Expand All @@ -88,6 +113,7 @@ public void addMaskedPasswordParameterDefinition(String className) {

public void clear() {
maskPasswordsParamDefClasses.clear();
globalVarPasswordPairs.clear();
}

public static MaskPasswordsConfig getInstance() {
Expand All @@ -101,6 +127,26 @@ private static XmlFile getConfigFile() {
return new XmlFile(new File(Hudson.getInstance().getRootDir(), CONFIG_FILE));
}

/**
* Returns the list of name/password pairs defined at the global level.
*
* <p>Modifications broughts to the returned list has no impact on this
* configuration (the returned value is a copy). Also, the list can be
* empty but never {@code null}.</p>
*
* @since 2.7
*/
public List<VarPasswordPair> getGlobalVarPasswordPairs() {
List<VarPasswordPair> r = new ArrayList<VarPasswordPair>(globalVarPasswordPairs.size());

// deep copy
for(VarPasswordPair varPasswordPair: globalVarPasswordPairs) {
r.add((VarPasswordPair) varPasswordPair.clone());
}

return r;
}

/**
* Returns a map of all {@link ParameterDefinition}s that can be used in
* jobs.
Expand Down
Expand Up @@ -24,12 +24,33 @@
-->

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:section title="${%Mask Passwords Configuration}">
<!-- parameter definitions to be automatically masked -->
<f:section title="${%ParameterDefinitions}">
<j:forEach var="paramDef" items="${descriptor.config.parameterDefinitions}">
<f:entry title="${paramDef.value}">
<input type="hidden" name="maskedParamDefs" value="${paramDef.key}"/>
<f:checkbox name="selectedMaskedParamDefs" checked="${descriptor.config.isSelected(paramDef.key)}"/>
</f:entry>
</j:forEach>
</f:section>
<!-- global var/password pairs -->
<f:section title="${%GlobalVarPasswordPairs}">
<f:entry field="globalVarPasswordPairs">
<f:repeatable name="globalVarPasswordPairs" items="${descriptor.config.globalVarPasswordPairs}" var="globalVarPasswordPair">
<table width="100%">
<tr>
<td width="10%" align="right">${%Name}</td>
<td width="30%">
<f:textbox name="globalVarPasswordPair.var" value="${!empty globalVarPasswordPair.var?globalVarPasswordPair.var:''}"/>
</td>
<td width="10%" align="right">${%Password}</td>
<td width="30%">
<f:password name="globalVarPasswordPair.password" value="${!empty globalVarPasswordPair.password?globalVarPasswordPair.password:''}"/>
</td>
<td width="20%" align="right"><f:repeatableDeleteButton/></td>
</tr>
</table>
</f:repeatable>
</f:entry>
</f:section>
</j:jelly>
@@ -0,0 +1,24 @@
# The MIT License
#
# Copyright (c) 2011, Manufacture Francaise des Pneumatiques Michelin, Romain Seguy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

GlobalVarPasswordPairs=Mask Passwords &mdash; Global name/password pairs
ParameterDefinitions=Mask Passwords &mdash; Parameters to automatically mask
@@ -0,0 +1,31 @@
<!--
- The MIT License
-
- Copyright (c) 2011, Manufacture Francaise des Pneumatiques Michelin, Romain Seguy
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
-->

<div>
<p>Define a list of <b>Name</b>/<b>Password</b> pairs to be used from jobs
as build variables. If the <b>Mask passwords</b> option is enabled on these
jobs, then the defined passwords will be automatically masked from the
console.</p>
<p>Blank values are not accepted (for both name and password).</p>
</div>

0 comments on commit f58d49c

Please sign in to comment.