Skip to content

Commit

Permalink
Add a test to ensure client IP is logged
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Apr 8, 2024
1 parent e6cebec commit e118a0b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
26 changes: 26 additions & 0 deletions nginx/tests/ServiceLogsClientIp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import plugins.TestsPlugin.DockerComposeUp
import java.io.ByteArrayOutputStream

tasks.named<DockerComposeUp>("test") {
doLast {
// get the docker logs from our nginx service
val outputStream = ByteArrayOutputStream()
project.exec {
commandLine = baseArguments + listOf("logs")
standardOutput = outputStream
workingDir = project.projectDir
}
val output = outputStream.toString()

// see if the log has a match for the IP we set in the test.sh cURL -H command
val pattern = "nginx-1 | 1.2.3.4"
val matchingLines = output.lines().filter { line ->
line.startsWith(pattern)
}

// fail the test if we didn't find any logs with the IP
if (matchingLines.isEmpty()) {
throw GradleException("No lines found starting with '$pattern'")
}
}
}
20 changes: 20 additions & 0 deletions nginx/tests/ServiceLogsClientIp/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
version: "3.8"

# Common to all services
x-common: &common
restart: "no"

name: nginx-servicelogsclientip
services:
nginx:
<<: *common
image: ${NGINX:-islandora/nginx:local}
# Set realip as trusting only localhost
environment:
- NGINX_REAL_IP_HEADER=X-Forwarded-For
- NGINX_SET_REAL_IP_FROM=127.0.0.1/32
volumes:
- ./test.sh:/test.sh
command:
- /test.sh
13 changes: 13 additions & 0 deletions nginx/tests/ServiceLogsClientIp/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/command/with-contenv bash
# shellcheck shell=bash

set -eou pipefail

# Wait for Nginx to be ready.
s6-svwait -U /run/service/nginx

# hit localhost nginx with the proper header so that IP is logged
curl -s -o /dev/null -H "X-Forwarded-For: 1.2.3.4" http://localhost:80/

# Service must start for us to get to this point.
exit 0

0 comments on commit e118a0b

Please sign in to comment.