Skip to content

Commit

Permalink
Added spring-tomcat7 smoke test application
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinZakharov committed Nov 1, 2023
1 parent a7c2c02 commit 0df6bbf
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 0 deletions.
76 changes: 76 additions & 0 deletions dd-smoke-tests/appsec/spring-tomcat7/build.gradle
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')
//}
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 {}
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");
}
}
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();
}
}
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
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ include ':dd-smoke-tests:vertx-3.9-resteasy'
include ':dd-smoke-tests:vertx-4.2'
include ':dd-smoke-tests:wildfly'
include ':dd-smoke-tests:appsec'
include ':dd-smoke-tests:appsec:spring-tomcat7'
include ':dd-smoke-tests:appsec:springboot'
include ':dd-smoke-tests:appsec:springboot-grpc'
include ':dd-smoke-tests:appsec:springboot-security'
Expand Down

0 comments on commit 0df6bbf

Please sign in to comment.