diff --git a/core/src/main/java/hudson/model/AbstractBuild.java b/core/src/main/java/hudson/model/AbstractBuild.java index 319a3af134..a50b8c1fde 100644 --- a/core/src/main/java/hudson/model/AbstractBuild.java +++ b/core/src/main/java/hudson/model/AbstractBuild.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -714,6 +714,36 @@ public abstract class AbstractBuild

,R extends Abs return getTimestamp(); } + /** + * Builds up a set of variable names that contain sensitive values that + * should not be exposed. The expection is that this set is populated with + * keys returned by {@link #getBuildVariables()} that should have their + * values masked for display purposes. + * + * @since 1.378 + */ + public Set getSensitiveBuildVariables() { + Set s = new HashSet(); + + ParametersAction parameters = getAction(ParametersAction.class); + if (parameters != null) { + for (ParameterValue p : parameters) { + if (p.isSensitive()) { + s.add(p.getName()); + } + } + } + + // Allow BuildWrappers to determine if any of their data is sensitive + if (project instanceof BuildableItemWithBuildWrappers) { + for (BuildWrapper bw : ((BuildableItemWithBuildWrappers) project).getBuildWrappersList()) { + bw.makeSensitiveBuildVariables(this, s); + } + } + + return s; + } + /** * Provides additional variables and their values to {@link Builder}s. * diff --git a/core/src/main/java/hudson/model/ParameterValue.java b/core/src/main/java/hudson/model/ParameterValue.java index 52fc95bd50..924c5a1c3c 100644 --- a/core/src/main/java/hudson/model/ParameterValue.java +++ b/core/src/main/java/hudson/model/ParameterValue.java @@ -1,7 +1,8 @@ /* * The MIT License * - * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts, + * Yahoo! Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -236,4 +237,18 @@ public abstract class ParameterValue implements Serializable { public String getShortDescription() { return toString(); } + + /** + * Returns whether the information contained in this ParameterValue is + * sensitive or security related. Used to determine whether the value + * provided by this object should be masked in output. + * + *

+ * Subclasses can override this to control the returne value. + * + * @since 1.378 + */ + public boolean isSensitive() { + return false; +} } diff --git a/core/src/main/java/hudson/model/PasswordParameterValue.java b/core/src/main/java/hudson/model/PasswordParameterValue.java index 79cb4c1aa6..a93d427ebb 100644 --- a/core/src/main/java/hudson/model/PasswordParameterValue.java +++ b/core/src/main/java/hudson/model/PasswordParameterValue.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Romain Seguy + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Romain Seguy, Yahoo! Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -63,4 +63,9 @@ public class PasswordParameterValue extends ParameterValue { } }; } + + @Override + public boolean isSensitive() { + return true; +} } diff --git a/core/src/main/java/hudson/tasks/BuildWrapper.java b/core/src/main/java/hudson/tasks/BuildWrapper.java index a7f3199525..9a791d95c0 100644 --- a/core/src/main/java/hudson/tasks/BuildWrapper.java +++ b/core/src/main/java/hudson/tasks/BuildWrapper.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -35,6 +35,7 @@ import java.io.OutputStream; import java.util.Collection; import java.util.Collections; import java.util.Map; +import java.util.Set; /** * Pluggability point for performing pre/post actions for the build process. @@ -247,6 +248,22 @@ public abstract class BuildWrapper extends AbstractDescribableImpl // noop } + /** + * Called to define sensitive build variables. This provides an opportunity + * for a BuildWrapper to denote the names of variables that are sensitive in + * nature and should not be exposed in output. + * + * @param build + * The build in progress for which this {@link BuildWrapper} is called. Never null. + * @param sensitiveVariables + * Contains names of sensitive build variables. Names of sensitive variables + * that were added with {@link #makeBuildVariables(hudson.model.AbstractBuild, java.util.Map)} + * @since 1.378 + */ + public void makeSensitiveBuildVariables(AbstractBuild build, Set sensitiveVariables) { + // noop + } + /** * Returns all the registered {@link BuildWrapper} descriptors. */ diff --git a/test/src/test/java/hudson/model/ParametersTest.java b/test/src/test/java/hudson/model/ParametersTest.java index 00d19d14af..5b0b331387 100755 --- a/test/src/test/java/hudson/model/ParametersTest.java +++ b/test/src/test/java/hudson/model/ParametersTest.java @@ -8,6 +8,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlElement; import com.gargoylesoftware.htmlunit.html.HtmlTextInput; import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput; import com.gargoylesoftware.htmlunit.html.HtmlOption; +import java.util.Set; /** * @author huybrechts @@ -102,4 +103,57 @@ public class ParametersTest extends HudsonTestCase { assertNotNull(builder.getEnvVars()); assertEquals("Choice <2>", builder.getEnvVars().get("CHOICE")); } + + public void testSensitiveParameters() throws Exception { + FreeStyleProject project = createFreeStyleProject(); + ParametersDefinitionProperty pdb = new ParametersDefinitionProperty( + new PasswordParameterDefinition("password", "12345", "password description")); + project.addProperty(pdb); + + CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder(); + project.getBuildersList().add(builder); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + Set sensitiveVars = build.getSensitiveBuildVariables(); + + assertNotNull(sensitiveVars); + assertTrue(sensitiveVars.contains("password")); +} + + public void testNonSensitiveParameters() throws Exception { + FreeStyleProject project = createFreeStyleProject(); + ParametersDefinitionProperty pdb = new ParametersDefinitionProperty( + new StringParameterDefinition("string", "defaultValue", "string description")); + project.addProperty(pdb); + + CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder(); + project.getBuildersList().add(builder); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + Set sensitiveVars = build.getSensitiveBuildVariables(); + + assertNotNull(sensitiveVars); + assertFalse(sensitiveVars.contains("string")); + } + + public void testMixedSensitivity() throws Exception { + FreeStyleProject project = createFreeStyleProject(); + ParametersDefinitionProperty pdb = new ParametersDefinitionProperty( + new StringParameterDefinition("string", "defaultValue", "string description"), + new PasswordParameterDefinition("password", "12345", "password description"), + new StringParameterDefinition("string2", "Value2", "string description") + ); + project.addProperty(pdb); + + CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder(); + project.getBuildersList().add(builder); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + Set sensitiveVars = build.getSensitiveBuildVariables(); + + assertNotNull(sensitiveVars); + assertFalse(sensitiveVars.contains("string")); + assertTrue(sensitiveVars.contains("password")); + assertFalse(sensitiveVars.contains("string2")); + } }