Skip to content

Commit 4b8e1ed

Browse files
authored
[java] API Security sampling endpoints (#4209)
1 parent 773c9bd commit 4b8e1ed

10 files changed

Lines changed: 114 additions & 0 deletions

File tree

docs/weblog/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ Hello world!\n
9090

9191
This endpoint is used for API security sampling and must accept a parameter `i` as an integer.
9292

93+
The response status code must be `i`.
94+
9395
The response body may contain the following text:
9496

9597
```

utils/build/docker/java/akka-http/src/main/scala/com/datadoghq/akka_http/AppSecRoutes.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,26 @@ object AppSecRoutes {
8383
}
8484
}
8585
} ~
86+
path("api_security/sampling" / """\d{3}""".r) { (i) =>
87+
get {
88+
complete(
89+
HttpResponse(
90+
status = StatusCodes.custom(i.toInt, "some reason"),
91+
entity = HttpEntity(ContentTypes.`text/plain(UTF-8)`, "Hello!\n")
92+
)
93+
)
94+
}
95+
} ~
96+
path("api_security_sampling" / """\d{2,3}""".r) { (i) =>
97+
get {
98+
complete(
99+
HttpResponse(
100+
status = StatusCodes.OK,
101+
entity = HttpEntity(ContentTypes.`text/plain(UTF-8)`, "OK!\n")
102+
)
103+
)
104+
}
105+
} ~
86106
path("params" / Segments) { segments: Seq[String] =>
87107
get {
88108
complete(segments.toString())

utils/build/docker/java/jersey-grizzly2/src/main/java/com/datadoghq/jersey/MyResource.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ public Response tagValuePost(@PathParam("tag_value") String value, @PathParam("s
111111
return tagValue(value, code);
112112
}
113113

114+
@GET
115+
@Path("/api_security/sampling/{i}")
116+
public Response apiSecuritySamplingWithStatus(@PathParam("i") int i) {
117+
return Response.status(i)
118+
.header("content-type", "text/plain")
119+
.entity("Hello!\n").build();
120+
}
121+
122+
@GET
123+
@Path("/api_security_sampling/{i}")
124+
public Response apiSecuritySampling(@PathParam("i") int i) {
125+
return Response.status(200)
126+
.header("content-type", "text/plain")
127+
.entity("Hello!\n").build();
128+
}
129+
114130
@GET
115131
@Path("/params/{params: .*}")
116132
public String params(@PathParam("params") List<PathSegment> params) {

utils/build/docker/java/play/app/controllers/AppSecController.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ class AppSecController @Inject()(cc: MessagesControllerComponents, ws: WSClient,
9494
.as("text/plain; charset=utf-8")
9595
}
9696

97+
def apiSecuritySamplingWithStatus(code: Int) = Action { request =>
98+
Results.Status(code)("Hello!\n")
99+
.as("text/plain; charset=utf-8")
100+
}
101+
102+
def apiSecuritySampling(code: Int) = Action { request =>
103+
Results.Status(200)("OK!\n")
104+
.as("text/plain; charset=utf-8")
105+
}
106+
97107
def params(segments: Seq[String]) = Action {
98108
Results.Ok(segments.toString())
99109
}

utils/build/docker/java/play/conf/routes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ GET /healthcheck controllers.AppSecController.healthcheck
33
GET /headers controllers.AppSecController.headers
44
GET /tag_value/:tag_value/:status_code controllers.AppSecController.tagValue(tag_value: String, status_code: Int)
55
POST /tag_value/:tag_value/:status_code controllers.AppSecController.tagValuePost(tag_value: String, status_code: Int)
6+
GET /api_security/sampling/:i controllers.AppSecController.apiSecuritySamplingWithStatus(i: Int)
7+
GET /api_security_sampling/:i controllers.AppSecController.apiSecuritySampling(i: Int)
68
GET /params/*segments controllers.AppSecController.params(segments: Seq[String])
79
GET /waf controllers.AppSecController.waf
810
GET /waf/ controllers.AppSecController.waf

utils/build/docker/java/ratpack/src/main/java/com/datadoghq/ratpack/Main.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ public static void main(String[] args) throws Exception {
177177
ctx.getResponse().status(code).send("Value tagged");
178178
});
179179
})
180+
.get("api_security/sampling/:i", ctx -> {
181+
final int i = Integer.parseInt(ctx.getPathTokens().get("i"));
182+
ctx.getResponse().status(i).send("Hello!\n");
183+
})
184+
.get("api_security_sampling/:i", ctx -> {
185+
final int i = Integer.parseInt(ctx.getPathTokens().get("i"));
186+
ctx.getResponse().status(200).send("OK!\n");
187+
})
180188
.path("waf/:params?", ctx -> {
181189
HttpMethod method = ctx.getRequest().getMethod();
182190
if (method.equals(HttpMethod.GET)) {

utils/build/docker/java/resteasy-netty3/src/main/java/com/datadoghq/resteasy/MyResource.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ public Response tagValuePost(@PathParam("tag_value") String value, @PathParam("s
111111
return tagValue(value, code);
112112
}
113113

114+
@GET
115+
@Path("/api_security/sampling/{i}")
116+
public Response apiSecuritySamplingWithStatus(@PathParam("i") int i) {
117+
return Response.status(i)
118+
.header("content-type", "text/plain")
119+
.entity("Hello!\n").build();
120+
}
121+
122+
@GET
123+
@Path("/api_security_sampling/{i}")
124+
public Response apiSecuritySampling(@PathParam("i") int i) {
125+
return Response.status(200)
126+
.header("content-type", "text/plain")
127+
.entity("Hello!\n").build();
128+
}
129+
114130
@GET
115131
@Path("/params/{params: .*}")
116132
public String params(@PathParam("params") List<PathSegment> params) {

utils/build/docker/java/spring-boot/src/main/java/com/datadoghq/system_tests/springboot/App.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ ResponseEntity<String> tagValueWithUrlencodedBody(@PathVariable final String tag
192192
return tagValue(tag_value, status_code);
193193
}
194194

195+
@GetMapping("/api_security/sampling/{i}")
196+
ResponseEntity<String> apiSecuritySamplingWithStatus(@PathVariable final int i) {
197+
return ResponseEntity.status(i).body("Hello!\n");
198+
}
199+
200+
@GetMapping("/api_security_sampling/{i}")
201+
ResponseEntity<String> apiSecuritySampling(@PathVariable final int i) {
202+
return ResponseEntity.status(200).body("OK!\n");
203+
}
204+
195205
@RequestMapping("/waf/**")
196206
String waf() {
197207
return "Hello World!";

utils/build/docker/java/vertx3/src/main/java/com/datadoghq/vertx3/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ public static void main(String[] args) {
9797
.setStatusCode(Integer.parseInt(ctx.pathParam("status_code")))
9898
.end("Value tagged");
9999
});
100+
router.get("/api_security/sampling/:i")
101+
.produces("text/plain")
102+
.handler(ctx -> {
103+
ctx.response()
104+
.setStatusCode(Integer.parseInt(ctx.pathParam("i")))
105+
.end("Hello!\n");
106+
});
107+
router.get("/api_security_sampling/:i")
108+
.produces("text/plain")
109+
.handler(ctx -> {
110+
final int i = Integer.parseInt(ctx.pathParam("i"));
111+
ctx.response()
112+
.setStatusCode(200)
113+
.end("Hello!\n");
114+
});
100115
router.getWithRegex("/params(?:/([^/]*))?(?:/([^/]*))?(?:/([^/]*))?(?:/([^/]*))?(?:/([^/]*))?")
101116
.produces("text/plain")
102117
.handler(ctx ->

utils/build/docker/java/vertx4/src/main/java/com/datadoghq/vertx4/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ public static void main(String[] args) {
9494
.setStatusCode(Integer.parseInt(ctx.pathParam("status_code")))
9595
.end("Value tagged");
9696
});
97+
router.get("/api_security/sampling/:i")
98+
.produces("text/plain")
99+
.handler(ctx -> {
100+
ctx.response()
101+
.setStatusCode(Integer.parseInt(ctx.pathParam("i")))
102+
.end("Hello!\n");
103+
});
104+
router.get("/api_security_sampling/:i")
105+
.produces("text/plain")
106+
.handler(ctx -> {
107+
final int i = Integer.parseInt(ctx.pathParam("i"));
108+
ctx.response()
109+
.setStatusCode(200)
110+
.end("Hello!\n");
111+
});
97112
router.getWithRegex("/params(?:/([^/]*))?(?:/([^/]*))?(?:/([^/]*))?(?:/([^/]*))?(?:/([^/]*))?")
98113
.produces("text/plain")
99114
.handler(ctx ->

0 commit comments

Comments
 (0)