-
Notifications
You must be signed in to change notification settings - Fork 130
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
New Injected Test: Require a Stapler verb annotation on web method looking methods #133
Draft
daniel-beck
wants to merge
8
commits into
jenkinsci:master
Choose a base branch
from
daniel-beck:require-verb-annotation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e3ba8dc
Require a Stapler verb annotation on web method looking methods
daniel-beck 99767ef
Handle old core baselines gracefully
daniel-beck f668b86
Lower core dependency again
daniel-beck 1dd3f5a
Add TODO
daniel-beck d756a14
Cleaner code, newer core requirement
daniel-beck 1556b4c
Merge commit 'e10ad3fe19a3c8a722400217d64162a03d97ac77' into recover-…
daniel-beck bbc3726
Make this work with Jenkins 2.61+
daniel-beck cca6d0b
Merge branch 'fix-compile-error-2.61' into require-verb-annotation
daniel-beck 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,12 +26,31 @@ | |
import hudson.PluginManager.FailedPlugin; | ||
import hudson.PluginWrapper; | ||
import hudson.cli.CLICommand; | ||
import java.io.File; | ||
import java.util.Map; | ||
import io.github.classgraph.ClassGraph; | ||
import io.github.classgraph.ClassInfo; | ||
import io.github.classgraph.ScanResult; | ||
import jenkins.model.Jenkins; | ||
import junit.framework.TestCase; | ||
import junit.framework.TestSuite; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.Assert; | ||
import org.kohsuke.stapler.WebMethod; | ||
import org.kohsuke.stapler.interceptor.InterceptorAnnotation; | ||
import org.kohsuke.stapler.interceptor.RequirePOST; | ||
import org.kohsuke.stapler.verb.HttpVerbInterceptor; | ||
|
||
import java.io.File; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.logging.Logger; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.Matchers.empty; | ||
|
||
/** | ||
* Called by the code generated by maven-hpi-plugin to build tests for plugins. | ||
|
@@ -65,6 +84,7 @@ public static TestSuite build(Map<String,?> params) throws Exception { | |
String packaging = StringUtils.defaultIfBlank((String)params.get("packaging"), "hpi"); | ||
if ("hpi".equals(packaging)) { | ||
inJenkins.addTest(new OtherTests("testPluginActive", params)); | ||
inJenkins.addTest(new OtherTests("testStaplerDispatches", params)); | ||
} | ||
master.addTest(inJenkins); | ||
master.addTest(new PropertiesTestSuite(outputDirectory)); | ||
|
@@ -106,5 +126,88 @@ public void testPluginActive() { | |
} | ||
} | ||
|
||
public void testStaplerDispatches() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { | ||
List<String> methodsFound = new ArrayList<>(); | ||
|
||
Method isStaplerRoutableMethod = findIsRoutableMethod(); | ||
if (isStaplerRoutableMethod == null) { | ||
return; | ||
} | ||
|
||
PluginWrapper thisPlugin = determineCurrentPlugin(); | ||
if (thisPlugin == null) { | ||
return; // we're not in a plugin | ||
} | ||
ClassGraph classGraph = new ClassGraph(); | ||
classGraph.addClassLoader(thisPlugin.classLoader); | ||
classGraph.disableJarScanning(); | ||
classGraph.enableClassInfo(); | ||
ScanResult result = classGraph.scan(); | ||
|
||
for (ClassInfo classInfo : result.getAllClasses()) { | ||
Class clazz = classInfo.loadClass(); | ||
for (Method m : clazz.getDeclaredMethods()) { | ||
if (isStaplerDispatchable(m) && (boolean)isStaplerRoutableMethod.invoke(null, m)) { | ||
if (!hasStaplerVerbAnnotation(m)) { | ||
methodsFound.add(clazz.getName() + "#" + m.getName()); | ||
} | ||
} | ||
} | ||
} | ||
Assert.assertThat("There should be no web methods that lack HTTP verb annotations like @RequirePOST, @GET, @POST, etc. -- see https://jenkins.io/redirect/developer/csrf-protection", methodsFound, is(empty())); | ||
} | ||
|
||
private Method findIsRoutableMethod() throws NoSuchMethodException { | ||
try { | ||
Method method = Class.forName("jenkins.security.stapler.TypedFilter").getDeclaredMethod("isRoutableMethod", Method.class); | ||
method.setAccessible(true); | ||
return method; | ||
} catch (ClassNotFoundException e) { | ||
LOGGER.warning("This test requires Jenkins 2.154, Jenkins LTS 2.138.4, or newer to run, use e.g. -Djenkins.version=2.138.4"); | ||
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. Use of |
||
// TODO add a fallback implementing similar code directly here | ||
return null; | ||
} | ||
} | ||
|
||
private PluginWrapper determineCurrentPlugin() { | ||
String plugin = (String) params.get("artifactId"); | ||
if (plugin != null) { | ||
return Jenkins.getActiveInstance().pluginManager.getPlugin(plugin); | ||
} | ||
return null; | ||
} | ||
|
||
private boolean hasStaplerVerbAnnotation(Method m) { | ||
if (m.getAnnotation(RequirePOST.class) != null) { | ||
return true; | ||
} | ||
for (Annotation annotation : m.getAnnotations()) { | ||
InterceptorAnnotation interceptorAnnotation = annotation.annotationType().getAnnotation(InterceptorAnnotation.class); | ||
if (interceptorAnnotation != null && interceptorAnnotation.value() == HttpVerbInterceptor.class) { | ||
// see org.kohsuke.stapler.verb.HttpVerbInterceptor | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean isStaplerDispatchable(Method m) { | ||
if (m.isBridge()) { | ||
return false; | ||
} | ||
if (m.isSynthetic()) { | ||
return false; | ||
} | ||
if (!Modifier.isPublic(m.getModifiers())) { | ||
return false; | ||
} | ||
String name = m.getName(); | ||
if (!name.startsWith("do") && m.getAnnotation(WebMethod.class) == null) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private static Logger LOGGER = Logger.getLogger(OtherTests.class.getName()); | ||
} | ||
} |
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
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.
I would rather other things did not go into
InjectedTest
(and that it would die). The reason being is that skipping part of the tests (rather than all of them) is pretty much impossible as it uses old junit Suites rather than newer test classes, and is hard to debug.This would be much better going forward in a
generate-tests
mojo in maven-hpi-plugin which then also makes it much easier to debug in an IDE. but otherwise 👍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.
Well, you should not be skipping any of them. :-)
I am not sure how creating JUnit 4-style tests would make it easier to ignore some test cases;
target/generated-test-sources/whatever/InjectedTest.java
would still not be editable.Note that there is a reason all the tests go into one suite: so that only one Jenkins startup is needed, to minimize overhead.
At any rate,
InjectedTest
is a well-established part of Jenkins plugin development. Proposals to replace its structure should be kept separate. So long as this PR does not introduce a test which is going to fail widely and spuriously (which I have not yet reviewed), it should be fine IMO.