-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added spring-tomcat7 smoke test application
- Loading branch information
1 parent
a7c2c02
commit 0df6bbf
Showing
6 changed files
with
198 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
plugins { | ||
id "com.github.johnrengelman.shadow" | ||
} | ||
|
||
apply from: "$rootDir/gradle/java.gradle" | ||
description = 'Spring Tomcat7 Smoke Tests.' | ||
|
||
jar { | ||
manifest { | ||
attributes('Main-Class': 'datadog.smoketest.appsec.springtomcat7.Main') | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '7.0.47' | ||
implementation group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '7.0.47' | ||
implementation group: 'org.apache.tomcat', name: 'tomcat-juli', version: '7.0.47' | ||
implementation group: 'org.springframework', name: 'spring-webmvc', version: '4.0.0.RELEASE' | ||
|
||
testImplementation project(':dd-smoke-tests:appsec') | ||
} | ||
|
||
tasks.withType(Test).configureEach { | ||
dependsOn "shadowJar" | ||
|
||
jvmArgs "-Ddatadog.smoketest.appsec.springtomcat7.shadowJar.path=${tasks.shadowJar.archiveFile.get()}" | ||
} | ||
|
||
task testRuntimeActivation(type: Test) { | ||
jvmArgs '-Dsmoke_test.appsec.enabled=inactive', | ||
"-Ddatadog.smoketest.appsec.springtomcat7.shadowJar.path=${tasks.shadowJar.archiveFile.get()}" | ||
} | ||
tasks['check'].dependsOn(testRuntimeActivation) | ||
|
||
|
||
|
||
//group 'datadog.smoketest.appsec.springtomcat7' | ||
//version '1.0-SNAPSHOT' | ||
// | ||
//apply plugin: 'java' | ||
// | ||
//// 1 - apply application and shadow plugins | ||
//apply plugin: 'application' | ||
//apply plugin: 'com.github.johnrengelman.shadow' | ||
// | ||
//sourceCompatibility = 1.8 | ||
//targetCompatibility = 1.8 | ||
//mainClassName = 'datadog.smoketest.appsec.springtomcat7.Main' | ||
// | ||
//// 2 - define the dependency to the shadow plugin | ||
//buildscript { | ||
// repositories { | ||
// jcenter() | ||
// } | ||
// dependencies { | ||
// classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1' | ||
// } | ||
//} | ||
// | ||
//// 3 - merge service descriptors | ||
//shadowJar { | ||
// mergeServiceFiles() | ||
//} | ||
// | ||
//repositories { | ||
// jcenter() | ||
//} | ||
// | ||
//dependencies { | ||
// implementation group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '7.0.109' | ||
// implementation group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '7.0.109' | ||
// implementation group: 'org.apache.tomcat', name: 'tomcat-juli', version: '7.0.109' | ||
// implementation group: 'org.springframework', name: 'spring-webmvc', version: '4.0.0.RELEASE' | ||
// | ||
// testImplementation project(':dd-smoke-tests:appsec') | ||
//} |
11 changes: 11 additions & 0 deletions
11
...ec/spring-tomcat7/src/main/java/datadog/smoketest/appsec/springtomcat7/AppConfigurer.java
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package datadog.smoketest.appsec.springtomcat7; | ||
|
||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | ||
|
||
@Configuration | ||
@EnableWebMvc | ||
@ComponentScan(basePackages = {"datadog.smoketest.appsec.springtomcat7"}) | ||
public class AppConfigurer extends WebMvcConfigurerAdapter {} |
18 changes: 18 additions & 0 deletions
18
...ppsec/spring-tomcat7/src/main/java/datadog/smoketest/appsec/springtomcat7/Controller.java
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package datadog.smoketest.appsec.springtomcat7; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class Controller { | ||
|
||
@RequestMapping("/") | ||
public String htmlString() { | ||
return "Hello world!"; | ||
} | ||
|
||
@RequestMapping("/exception") | ||
public void exceptionMethod() throws Throwable { | ||
throw new Throwable("hello"); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...ests/appsec/spring-tomcat7/src/main/java/datadog/smoketest/appsec/springtomcat7/Main.java
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package datadog.smoketest.appsec.springtomcat7; | ||
|
||
import de.thetaphi.forbiddenapis.SuppressForbidden; | ||
import java.io.File; | ||
import org.apache.catalina.Context; | ||
import org.apache.catalina.startup.Tomcat; | ||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | ||
import org.springframework.web.servlet.DispatcherServlet; | ||
|
||
public class Main { | ||
|
||
private static final String ROOT = "/"; | ||
private static final String SERVLET = "dispatcherServlet"; | ||
|
||
@SuppressForbidden | ||
public static void main(String[] args) throws Exception { | ||
int port = 8080; | ||
for (String arg : args) { | ||
if (arg.contains("=")) { | ||
String[] kv = arg.split("="); | ||
if (kv.length == 2) { | ||
if ("--server.port".equalsIgnoreCase(kv[0])) { | ||
try { | ||
port = Integer.parseInt(kv[1]); | ||
} catch (NumberFormatException e) { | ||
System.out.println( | ||
"--server.port '" | ||
+ kv[1] | ||
+ "' is not valid port. Will be used default port " | ||
+ port); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
Tomcat tomcat = new Tomcat(); | ||
tomcat.setPort(port); | ||
|
||
Context context = tomcat.addContext(ROOT, new File(".").getAbsolutePath()); | ||
|
||
Tomcat.addServlet( | ||
context, | ||
SERVLET, | ||
new DispatcherServlet( | ||
new AnnotationConfigWebApplicationContext() { | ||
{ | ||
register(AppConfigurer.class); | ||
} | ||
})); | ||
context.addServletMapping(ROOT, SERVLET); | ||
|
||
tomcat.start(); | ||
tomcat.getServer().await(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...psec/spring-tomcat7/src/test/groovy/datadog/smoketest/appsec/SpringTomcatSmokeTest.groovy
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package datadog.smoketest.appsec | ||
|
||
import okhttp3.Request | ||
|
||
class SpringTomcatSmokeTest extends AbstractAppSecServerSmokeTest { | ||
|
||
@Override | ||
ProcessBuilder createProcessBuilder() { | ||
String springBootShadowJar = System.getProperty("datadog.smoketest.appsec.springtomcat7.shadowJar.path") | ||
|
||
List<String> command = new ArrayList<>() | ||
command.add(javaPath()) | ||
command.addAll(defaultJavaProperties) | ||
command.add("-Ddd.iast.enabled=true") | ||
command.add("-Ddd.iast.stacktrace-leak.suppress=true") | ||
command.addAll((String[]) ["-jar", springBootShadowJar, "--server.port=${httpPort}"]) | ||
|
||
ProcessBuilder processBuilder = new ProcessBuilder(command) | ||
processBuilder.directory(new File(buildDirectory)) | ||
} | ||
|
||
def "suppress exception stacktrace"() { | ||
when: | ||
String url = "http://localhost:${httpPort}/exception" | ||
def request = new Request.Builder() | ||
.url(url) | ||
.build() | ||
def response = client.newCall(request).execute() | ||
def responseBodyStr = response.body().string() | ||
waitForTraceCount 1 | ||
|
||
then: | ||
responseBodyStr.contains('Sorry, you cannot access this page. Please contact the customer service team.') | ||
response.code() == 500 | ||
} | ||
} |
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