-
Notifications
You must be signed in to change notification settings - Fork 277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JENKINS-34753] Provide safe parameters to ParametersAction #285
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c40227e
First fix all tests that should still succeed on 2.x
rsandell b1c6b52
Merge remote-tracking branch 'origin/master' into JENKINS-34753
rsandell 3116820
[JENKINS-34753] Provide safe parameters to ParametersAction
rsandell 1d084be
More selective log warning
rsandell 9f8f4f7
Remove some commented code
rsandell 305b009
One more hudson to jenkins rename
rsandell 5a6fd21
Guarding for null version
rsandell c013c5b
Missed a checkstyle warning
rsandell 6b4d78b
Inspect ParametersAction for better logging
rsandell 5590609
Check inspection error first
rsandell d8878ba
Corrected log messages and added to the javadoc
rsandell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,13 +43,19 @@ | |
import hudson.model.ParametersDefinitionProperty; | ||
import jenkins.model.Jenkins; | ||
import jenkins.model.ParameterizedJobMixIn; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.annotation.CheckForNull; | ||
import javax.annotation.Nonnull; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.concurrent.Future; | ||
|
||
import static com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl.getServerConfig; | ||
|
@@ -243,6 +249,10 @@ protected Job asJob() { | |
|
||
/** | ||
* Creates a ParameterAction and fills it with the project's default parameters + the Standard Gerrit parameters. | ||
* If running on a core version that let's us specify safeParameters for the ParameterAction | ||
* the Gerrit specific parameters will be specified in the safeParameters list in addition to anything the admin | ||
* might have set. | ||
* A warning will be printed to the log if that is not possible but SECURITY-170 appears to be in effect. | ||
* | ||
* @param event the event. | ||
* @param project the project. | ||
|
@@ -251,6 +261,42 @@ protected Job asJob() { | |
protected ParametersAction createParameters(GerritTriggeredEvent event, Job project) { | ||
List<ParameterValue> parameters = getDefaultParametersValues(project); | ||
setOrCreateParameters(event, project, parameters); | ||
try { | ||
Constructor<ParametersAction> constructor = ParametersAction.class.getConstructor(List.class, | ||
Collection.class); | ||
return constructor.newInstance(parameters, GerritTriggerParameters.getNamesSet()); | ||
} catch (NoSuchMethodException e) { | ||
ParametersActionInspection inspection = getParametersInspection(); | ||
if (inspection.isInspectionFailure()) { | ||
logger.warn("Failed to inspect ParametersAction to determine " | ||
+ "if we can behave normally around SECURITY-170.\nSee " | ||
+ "https://wiki.jenkins-ci.org/display/SECURITY/Jenkins+Security+Advisory+2016-05-11" | ||
+ " for information."); | ||
} else if (inspection.isHasSafeParameterConfig()) { | ||
StringBuilder txt = new StringBuilder( | ||
"Running on a core with SECURITY-170 fixed but no direct way for Gerrit Trigger" | ||
+ " to self-specify safe parameters."); | ||
txt.append(" You should consider upgrading to a new Jenkins core version.\n"); | ||
if (inspection.isKeepUndefinedParameters()) { | ||
txt.append(".keepUndefinedParameters is set so the trigger should behave normally."); | ||
} else if (inspection.isSafeParametersSet()) { | ||
txt.append("All Gerrit related parameters are set in .safeParameters"); | ||
txt.append(" so the trigger should behave normally."); | ||
} else { | ||
txt.append("No overriding system properties appears to be set,"); | ||
txt.append(" your builds might not work as expected.\n"); | ||
txt.append("See https://wiki.jenkins-ci.org/display/SECURITY/Jenkins+Security+Advisory+2016-05-11"); | ||
txt.append(" for information."); | ||
} | ||
logger.warn(txt.toString()); | ||
} else { | ||
logger.debug("Running on an old core before safe parameters, we should be safe.", e); | ||
} | ||
} catch (IllegalAccessException e) { | ||
logger.warn("Running on a core with safe parameters fix available, but not allowed to specify them", e); | ||
} catch (Exception e) { | ||
logger.warn("Running on a core with safe parameters fix available, but failed to provide them", e); | ||
} | ||
return new ParametersAction(parameters); | ||
} | ||
|
||
|
@@ -332,4 +378,113 @@ public int hashCode() { | |
public boolean equals(Object obj) { | ||
return obj instanceof EventListener && ((EventListener)obj).job.equals(job); | ||
} | ||
|
||
/** | ||
* Inspects {@link ParametersAction} to see what kind of capabilities it has in regards to SECURITY-170. | ||
* Assuming the safeParameters constructor could not be found. | ||
* | ||
* @return the inspection result | ||
* @see #createParameters(GerritTriggeredEvent, Job) | ||
*/ | ||
private static synchronized ParametersActionInspection getParametersInspection() { | ||
if (parametersInspectionCache == null) { | ||
parametersInspectionCache = new ParametersActionInspection(); | ||
} | ||
return parametersInspectionCache; | ||
} | ||
|
||
/** | ||
* Stored cache of the inspection. | ||
* @see #getParametersInspection() | ||
*/ | ||
private static volatile ParametersActionInspection parametersInspectionCache = null; | ||
|
||
/** | ||
* Data structure with information regarding what kind of capabilities {@link ParametersAction} has. | ||
* @see #getParametersInspection() | ||
* @see #createParameters(GerritTriggeredEvent, Job) | ||
*/ | ||
private static class ParametersActionInspection { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would even make sense to make as separate plugin because all other trigger plugins needs to do the same with same quality level of checks. |
||
private static final Class<ParametersAction> KLASS = ParametersAction.class; | ||
private boolean inspectionFailure; | ||
private boolean safeParametersSet = false; | ||
private boolean keepUndefinedParameters = false; | ||
private boolean hasSafeParameterConfig = false; | ||
|
||
/** | ||
* Constructor that performs the inspection. | ||
*/ | ||
ParametersActionInspection() { | ||
try { | ||
for (Field field : KLASS.getDeclaredFields()) { | ||
if (Modifier.isStatic(field.getModifiers()) | ||
&& ( | ||
field.getName().equals("KEEP_UNDEFINED_PARAMETERS_SYSTEM_PROPERTY_NAME") | ||
|| field.getName().equals("SAFE_PARAMETERS_SYSTEM_PROPERTY_NAME") | ||
) | ||
) { | ||
this.hasSafeParameterConfig = true; | ||
break; | ||
} | ||
} | ||
if (hasSafeParameterConfig) { | ||
if (Boolean.getBoolean(KLASS.getName() + ".keepUndefinedParameters")) { | ||
this.keepUndefinedParameters = true; | ||
} | ||
String safeParameters = System.getProperty(KLASS.getName() + ".safeParameters"); | ||
if (!StringUtils.isBlank(safeParameters)) { | ||
safeParameters = safeParameters.toUpperCase(Locale.ENGLISH); | ||
boolean declared = true; | ||
for (GerritTriggerParameters parameter : GerritTriggerParameters.values()) { | ||
if (!safeParameters.contains(parameter.name())) { | ||
declared = false; | ||
break; | ||
} | ||
} | ||
this.safeParametersSet = declared; | ||
} else { | ||
this.safeParametersSet = false; | ||
} | ||
} | ||
this.inspectionFailure = false; | ||
} catch (Exception e) { | ||
this.inspectionFailure = true; | ||
} | ||
} | ||
|
||
/** | ||
* If the system property .safeParameters is set and contains all Gerrit related parameters. | ||
* @return true if so. | ||
*/ | ||
boolean isSafeParametersSet() { | ||
return safeParametersSet; | ||
} | ||
|
||
/** | ||
* If the system property .keepUndefinedParameters is set and set to true. | ||
* | ||
* @return true if so. | ||
*/ | ||
boolean isKeepUndefinedParameters() { | ||
return keepUndefinedParameters; | ||
} | ||
|
||
/** | ||
* If any of the constant fields regarding safeParameters are declared in {@link ParametersAction}. | ||
* | ||
* @return true if so. | ||
*/ | ||
boolean isHasSafeParameterConfig() { | ||
return hasSafeParameterConfig; | ||
} | ||
|
||
/** | ||
* If there was an exception when inspecting the class. | ||
* | ||
* @return true if so. | ||
*/ | ||
public boolean isInspectionFailure() { | ||
return inspectionFailure; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,10 +30,16 @@ | |
|
||
import hudson.model.Result; | ||
|
||
import jenkins.model.Jenkins; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.powermock.modules.junit4.PowerMockRunnerDelegate; | ||
|
||
import java.util.Collection; | ||
import java.util.LinkedList; | ||
|
@@ -51,7 +57,9 @@ | |
* and {@link ParameterExpander#getVerifiedValue(hudson.model.Result, GerritTrigger)} | ||
* @author Robert Sandell <[email protected]> | ||
*/ | ||
@RunWith(Parameterized.class) | ||
@RunWith(PowerMockRunner.class) | ||
@PowerMockRunnerDelegate(Parameterized.class) | ||
@PrepareForTest(Jenkins.class) | ||
public class ParameterExpanderParameterizedTest { | ||
|
||
private TestParameters parameters; | ||
|
@@ -64,6 +72,16 @@ public ParameterExpanderParameterizedTest(TestParameters parameters) { | |
this.parameters = parameters; | ||
} | ||
|
||
/** | ||
* Mock Jenkins. | ||
*/ | ||
@Before | ||
public void setup() { | ||
PowerMockito.mockStatic(Jenkins.class); | ||
Jenkins jenkins = PowerMockito.mock(Jenkins.class); | ||
PowerMockito.when(Jenkins.getInstance()).thenReturn(jenkins); | ||
} | ||
|
||
/** | ||
* test. | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,9 +30,15 @@ | |
|
||
import hudson.model.Result; | ||
|
||
import jenkins.model.Jenkins; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.powermock.modules.junit4.PowerMockRunnerDelegate; | ||
|
||
import java.util.Collection; | ||
import java.util.LinkedList; | ||
|
@@ -51,7 +57,9 @@ | |
* | ||
* @author Robert Sandell <[email protected]> | ||
*/ | ||
@RunWith(Parameterized.class) | ||
@RunWith(PowerMockRunner.class) | ||
@PowerMockRunnerDelegate(Parameterized.class) | ||
@PrepareForTest(Jenkins.class) | ||
public class ParameterExpanderSkipVoteParameterTest { | ||
|
||
private TestParameter parameter; | ||
|
@@ -65,6 +73,16 @@ public ParameterExpanderSkipVoteParameterTest(TestParameter parameter) { | |
this.parameter = parameter; | ||
} | ||
|
||
/** | ||
* Mock Jenkins. | ||
*/ | ||
@Before | ||
public void setup() { | ||
PowerMockito.mockStatic(Jenkins.class); | ||
Jenkins jenkins = PowerMockito.mock(Jenkins.class); | ||
PowerMockito.when(Jenkins.getInstance()).thenReturn(jenkins); | ||
} | ||
|
||
/** | ||
* Tests that {@link ParameterExpander#getMinimumCodeReviewValue(BuildMemory.MemoryImprint, boolean)} | ||
* returns {@link TestParameter#expectedCodeReview}. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: you may want to update the javadoc of the method mentioning that now the
parametersAction
add the safe parameters to the default ones since it already has a description that saysCreates a ParameterAction and fills it with the project's default parameters + the Standard Gerrit parameters
.