-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Have docker nginx logs print the client IP (#328)
* have docker nginx logs print the client IP * Add a test to ensure client IP is logged
- Loading branch information
Showing
6 changed files
with
82 additions
and
16 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
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
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,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'") | ||
} | ||
} | ||
} |
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,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 |
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,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 |