Skip to content

Commit 84d7425

Browse files
committed
chore(bigquery-jdbc): dockerized proxy environment to run integration tests
1 parent fa81a5e commit 84d7425

4 files changed

Lines changed: 91 additions & 5 deletions

File tree

java-bigquery-jdbc/Dockerfile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM gcr.io/cloud-devrel-public-resources/java11
1+
FROM gcr.io/cloud-devrel-public-resources/java11 AS base
22
ARG BRANCH=main
33
ENV JDBC_DOCKER_ENV=true
44

@@ -17,8 +17,19 @@ RUN bash -c " \
1717
&& install_modules java-bigquery \
1818
&& rm -rf /git"
1919

20-
# This will ensure all deps are present
20+
# This will ensure all deps are present, including integration test deps, while skipping shade
2121
WORKDIR /src
22-
RUN mvn install
22+
RUN mvn install -Penable-integration-tests -Dtest=NoSuchTest -Dit.test=NoSuchTest -DfailIfNoTests=false -Dsurefire.failIfNoSpecifiedTests=false -Dit.failIfNoSpecifiedTests=false -Dmaven.shade.skip=true -DskipShade=true
2323

2424
ENTRYPOINT []
25+
26+
# Proxy stage: configured squid proxy and iptables to force all traffic through it
27+
FROM base AS proxy
28+
RUN apt-get update && apt-get install -y squid iptables iproute2 curl && rm -rf /var/lib/apt/lists/*
29+
COPY tools/environments/proxy/start-proxy.sh /usr/local/bin/start-proxy.sh
30+
RUN chmod +x /usr/local/bin/start-proxy.sh
31+
ENTRYPOINT ["/usr/local/bin/start-proxy.sh"]
32+
33+
# Regular stage: same as base, default stage
34+
FROM base AS regular
35+

java-bigquery-jdbc/Makefile

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
SHELL := /bin/bash # Default 'sh' doesn't support 'source'
22
BUILD_BRANCH=main
33
CONTAINER_NAME=jdbc
4+
PROXY_CONTAINER_NAME=$(CONTAINER_NAME)-proxy
45
PACKAGE_DESTINATION=$(PWD)/drivers
56
SRC="$(PWD)"
67
skipSurefire ?= true
8+
skipShade ?= true
79
JDBC_DRIVER_VERSION = $(shell mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
810
JDBC_JAR = $(PACKAGE_DESTINATION)/google-cloud-bigquery-jdbc-$(JDBC_DRIVER_VERSION)-all.jar
911

@@ -33,13 +35,14 @@ unittest: |
3335
-Dtest=$(test) \
3436
test
3537

36-
# Important: By default, this command will skip unittests.
38+
# Important: By default, this command will skip unittests & uberjar build.
3739
# To include unit tests, run: make integration-test skipSurefire=false
3840
integration-test:
3941
mvn -B -ntp \
4042
-Penable-integration-tests \
4143
-DtrimStackTrace=false \
4244
-DskipSurefire=$(skipSurefire) \
45+
-DskipShade=$(skipShade) \
4346
-Dclirr.skip=true \
4447
-Denforcer.skip=true \
4548
-Dit.failIfNoSpecifiedTests=true \
@@ -76,21 +79,30 @@ run-it-standalone:
7679
# Commands for dockerized environments
7780
.docker-run: |
7881
docker run -it \
82+
--cap-add=NET_ADMIN \
7983
-v $(GOOGLE_APPLICATION_CREDENTIALS):/auth/application_creds.json \
8084
-v "$(GOOGLE_APPLICATION_CREDENTIALS).p12":/auth/application_creds.p12 \
8185
-e "GOOGLE_APPLICATION_CREDENTIALS=/auth/application_creds.json" \
8286
-v $(SRC):/src \
8387
-e "SA_EMAIL=test_email" \
8488
-e "SA_SECRET=/auth/application_creds.json" \
8589
-e "SA_SECRET_P12=/auth/application_creds.p12" \
90+
-e "BIGQUERY_BASE_URL=$(BIGQUERY_BASE_URL)" \
91+
-e "BIGQUERY_URL_FLAGS=$(BIGQUERY_URL_FLAGS)" \
8692
$(CONTAINER_NAME) $(args)
8793

8894
docker-build:
89-
docker build -t $(CONTAINER_NAME) -f Dockerfile --build-arg BRANCH=${BUILD_BRANCH} $(SRC)
95+
docker build --target regular -t $(CONTAINER_NAME) -f Dockerfile --build-arg BRANCH=${BUILD_BRANCH} $(SRC)
96+
97+
docker-proxy-build:
98+
docker build --target proxy -t $(PROXY_CONTAINER_NAME) -f Dockerfile --build-arg BRANCH=${BUILD_BRANCH} $(SRC)
9099

91100
docker-session:
92101
$(MAKE) .docker-run args="bash"
93102

103+
docker-proxy-session:
104+
$(MAKE) .docker-run-proxy args="bash"
105+
94106
docker-package-all-dependencies: docker-build
95107
mkdir -p $(PACKAGE_DESTINATION)
96108
docker run \
@@ -134,6 +146,9 @@ docker-unittest: |
134146
docker-integration-test: .check-env
135147
$(MAKE) .docker-run args="make integration-test test=$(test) skipSurefire=$(skipSurefire)"
136148

149+
docker-proxy-integration-test: .check-env docker-proxy-build
150+
$(MAKE) docker-integration-test CONTAINER_NAME=$(PROXY_CONTAINER_NAME) BIGQUERY_URL_FLAGS="ProxyHost=127.0.0.1;ProxyPort=3128;"
151+
137152
docker-coverage:
138153
$(MAKE) .docker-run args="make unit-test-coverage"
139154
$(MAKE) .docker-run args="make full-coverage"

java-bigquery-jdbc/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
3232
<github.global.server>github</github.global.server>
3333
<site.installationModule>google-cloud-bigquery-jdbc</site.installationModule>
34+
<skipShade>false</skipShade>
3435
</properties>
3536

3637
<build>
@@ -96,6 +97,7 @@
9697
<goal>shade</goal>
9798
</goals>
9899
<configuration>
100+
<skip>${skipShade}</skip>
99101
<shadedArtifactAttached>true</shadedArtifactAttached>
100102
<createSourcesJar>true</createSourcesJar>
101103
<shadeSourcesContent>true</shadeSourcesContent>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
# start-proxy.sh
3+
4+
set -e
5+
6+
echo "Starting Squid proxy..."
7+
# Run squid in background.
8+
# On Debian, /usr/sbin/squid is the binary.
9+
# -s sends errors to syslog. -Y during rebuild. -C do not catch fatal signals.
10+
/usr/sbin/squid -sYC
11+
12+
# Wait for squid to be ready and listen on 3128
13+
echo "Waiting for Squid to listen on port 3128..."
14+
timeout=30
15+
while ! curl -s -I -x http://127.0.0.1:3128 https://www.google.com >/dev/null; do
16+
sleep 1
17+
timeout=$((timeout - 1))
18+
if [ $timeout -le 0 ]; then
19+
echo "Squid failed to start or cannot access the internet."
20+
exit 1
21+
fi
22+
done
23+
echo "Squid is ready and working."
24+
25+
# Configure iptables to restrict network access
26+
echo "Configuring iptables rules..."
27+
28+
# 1. Allow loopback traffic
29+
iptables -A OUTPUT -o lo -j ACCEPT
30+
31+
# 2. Allow squid user (proxy) to access the network
32+
iptables -A OUTPUT -m owner --uid-owner proxy -j ACCEPT
33+
34+
# 3. Allow DNS (port 53) for everyone
35+
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
36+
iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
37+
38+
# 4. Allow outgoing traffic to port 3128 (proxies) for testing external proxies
39+
iptables -A OUTPUT -p tcp --dport 3128 -j ACCEPT
40+
41+
# 4.5 Allow raw access to Maven Central (repo.maven.apache.org) for dynamic dependency downloads
42+
echo "Resolving repo.maven.apache.org and allowing raw access..."
43+
for ip in $(getent ahosts repo.maven.apache.org | awk '{print $1}' | sort -u); do
44+
if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
45+
echo "Allowing outbound to $ip"
46+
iptables -A OUTPUT -d "$ip" -p tcp --dport 443 -j ACCEPT
47+
iptables -A OUTPUT -d "$ip" -p tcp --dport 80 -j ACCEPT
48+
fi
49+
done
50+
51+
# 5. Reject all other outgoing TCP/UDP traffic
52+
iptables -A OUTPUT -p tcp -j REJECT --reject-with tcp-reset
53+
iptables -A OUTPUT -p udp -j REJECT
54+
55+
echo "Raw network access is now disabled. All traffic must go through the proxy."
56+
57+
# Execute the main command
58+
exec "$@"

0 commit comments

Comments
 (0)