Skip to content

Commit fce21c9

Browse files
authored
Merge pull request #119 from UKHomeOffice/geoip2
Geoip2
2 parents afcb10e + 8f029b9 commit fce21c9

File tree

12 files changed

+134
-163
lines changed

12 files changed

+134
-163
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
2-
/tmp/*
2+
/tmp/*
3+
*.swp

Dockerfile

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
FROM quay.io/ukhomeofficedigital/centos-base:latest
2-
32
MAINTAINER Lewis Marshall <[email protected]>
43

54
WORKDIR /root
@@ -18,10 +17,8 @@ RUN yum install -y bind-utils dnsmasq && \
1817
yum clean all
1918

2019
ADD ./naxsi/location.rules /usr/local/openresty/naxsi/location.template
21-
2220
ADD ./nginx*.conf /usr/local/openresty/nginx/conf/
23-
RUN mkdir /usr/local/openresty/nginx/conf/locations
24-
RUN mkdir -p /usr/local/openresty/nginx/lua
21+
RUN mkdir -p /usr/local/openresty/nginx/conf/locations /usr/local/openresty/nginx/lua
2522
ADD ./lua/* /usr/local/openresty/nginx/lua/
2623
RUN md5sum /usr/local/openresty/nginx/conf/nginx.conf | cut -d' ' -f 1 > /container_default_ngx
2724
ADD ./defaults.sh /
@@ -32,21 +29,21 @@ ADD ./logging.conf /usr/local/openresty/nginx/conf/
3229
ADD ./html/ /usr/local/openresty/nginx/html/
3330
ADD ./readyness.sh /
3431
ADD ./helper.sh /
35-
ADD ./refresh_GeoIP.sh /
32+
ADD ./refresh_geoip.sh /
3633

3734
RUN yum remove -y kernel-headers && \
3835
yum clean all
3936

4037
RUN useradd -u 1000 nginx && \
41-
install -o nginx -g nginx -d /usr/local/openresty/naxsi/locations \
42-
/usr/local/openresty/nginx/{client_body,fastcgi,proxy,scgi,uwsgi}_temp && \
43-
chown -R nginx:nginx /usr/local/openresty/nginx/{conf,logs} \
44-
/usr/share/GeoIP
38+
install -o nginx -g nginx -d \
39+
/usr/local/openresty/naxsi/locations \
40+
/usr/local/openresty/nginx/{client_body,fastcgi,proxy,scgi,uwsgi}_temp && \
41+
chown -R nginx:nginx /usr/local/openresty/nginx/{conf,logs} /usr/share/GeoIP
4542

4643
WORKDIR /usr/local/openresty
4744

48-
ENTRYPOINT ["/go.sh"]
45+
EXPOSE 10080 10443
4946

50-
EXPOSE 10080
51-
EXPOSE 10443
5247
USER 1000
48+
49+
ENTRYPOINT [ "/go.sh" ]

build.sh

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44
set -eu
55
set -o pipefail
66

7-
OPEN_RESTY_URL='http://openresty.org/download/openresty-1.11.2.4.tar.gz'
7+
GEOIP_CITY_URL='http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz'
8+
GEOIP_COUNTRY_URL='http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz'
9+
GEOIP_MOD_URL='https://github.com/leev/ngx_http_geoip2_module/archive/3.0.tar.gz'
10+
GEOIP_UPDATE_CLI='https://github.com/maxmind/geoipupdate/releases/download/v3.1.1/geoipupdate-3.1.1.tar.gz'
11+
GEOIP_URL='https://github.com/maxmind/libmaxminddb/releases/download/1.3.2/libmaxminddb-1.3.2.tar.gz'
812
LUAROCKS_URL='http://luarocks.org/releases/luarocks-2.4.2.tar.gz'
913
NAXSI_URL='https://github.com/nbs-system/naxsi/archive/0.56.tar.gz'
14+
OPEN_RESTY_URL='http://openresty.org/download/openresty-1.11.2.4.tar.gz'
1015
STATSD_URL='https://github.com/UKHomeOffice/nginx-statsd/archive/0.0.1.tar.gz'
11-
GEOIP_URL='https://github.com/maxmind/geoip-api-c/releases/download/v1.6.11/GeoIP-1.6.11.tar.gz'
16+
17+
MAXMIND_PATH='/usr/share/GeoIP'
1218

1319
# Install dependencies to build from source
1420
yum -y install \
1521
gcc-c++ \
1622
gcc \
23+
git \
1724
make \
25+
libcurl-devel \
1826
openssl-devel \
1927
openssl \
2028
perl \
@@ -25,57 +33,69 @@ yum -y install \
2533
unzip \
2634
wget
2735

28-
mkdir -p openresty luarocks naxsi nginx-statsd geoip
36+
mkdir -p openresty luarocks naxsi nginx-statsd geoip geoipupdate ngx_http_geoip2_module
2937

3038
# Prepare
31-
wget -qO - "$OPEN_RESTY_URL" | tar xzv --strip-components 1 -C openresty/
32-
wget -qO - "$LUAROCKS_URL" | tar xzv --strip-components 1 -C luarocks/
33-
wget -qO - "$NAXSI_URL" | tar xzv --strip-components 1 -C naxsi/
34-
wget -qO - "$STATSD_URL" | tar xzv --strip-components 1 -C nginx-statsd/
35-
wget -qO - "$GEOIP_URL" | tar xzv --strip-components 1 -C geoip/
39+
wget -qO - "$OPEN_RESTY_URL" | tar xzv --strip-components 1 -C openresty/
40+
wget -qO - "$LUAROCKS_URL" | tar xzv --strip-components 1 -C luarocks/
41+
wget -qO - "$NAXSI_URL" | tar xzv --strip-components 1 -C naxsi/
42+
wget -qO - "$STATSD_URL" | tar xzv --strip-components 1 -C nginx-statsd/
43+
wget -qO - "$GEOIP_URL" | tar xzv --strip-components 1 -C geoip/
44+
wget -qO - "$GEOIP_UPDATE_CLI" | tar xzv --strip-components 1 -C geoipupdate/
45+
wget -qO - "$GEOIP_MOD_URL" | tar xzv --strip-components 1 -C ngx_http_geoip2_module/
3646

37-
# Build!
47+
# Build
3848
pushd geoip
49+
mkdir -p ${MAXMIND_PATH}
3950
./configure
40-
make
4151
make check install
52+
echo "/usr/local/lib" >> /etc/ld.so.conf.d/libmaxminddb.conf
53+
curl -fSL ${GEOIP_COUNTRY_URL} | gzip -d > ${MAXMIND_PATH}/GeoLite2-Country.mmdb
54+
curl -fSL ${GEOIP_CITY_URL} | gzip -d > ${MAXMIND_PATH}/GeoLite2-City.mmdb
55+
chown -R 1000:1000 ${MAXMIND_PATH}
4256
popd
43-
rm -fr geoip
57+
58+
pushd geoipupdate
59+
./configure
60+
make check install
61+
popd
62+
63+
# check maxmind module
64+
echo "Checking libmaxminddb module"
65+
ldconfig && ldconfig -p | grep libmaxminddb
4466

4567
pushd openresty
46-
./configure --add-module="../naxsi/naxsi_src" \
68+
./configure --add-dynamic-module="/root/ngx_http_geoip2_module" \
69+
--add-module="../naxsi/naxsi_src" \
4770
--add-module="../nginx-statsd" \
4871
--with-http_realip_module \
49-
--with-http_geoip_module \
5072
--with-http_stub_status_module
51-
make
5273
make install
5374
popd
5475

5576
# Install NAXSI default rules...
5677
mkdir -p /usr/local/openresty/naxsi/
5778
cp "./naxsi/naxsi_config/naxsi_core.rules" /usr/local/openresty/naxsi/
5879

59-
rm -fr openresty naxsi nginx-statsd
60-
6180
pushd luarocks
6281
./configure --with-lua=/usr/local/openresty/luajit \
63-
--lua-suffix=jit-2.1.0-beta2 \
64-
--with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
82+
--lua-suffix=jit-2.1.0-beta2 \
83+
--with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
6584
make build install
6685
popd
67-
rm -fr luarocks
6886

6987
luarocks install uuid
7088
luarocks install luasocket
71-
luarocks install lua-geoip
7289

7390
# Remove the developer tooling
91+
rm -fr openresty naxsi nginx-statsd geoip luarocks ngx_http_geoip2_module
7492
yum -y remove \
7593
gcc-c++ \
7694
gcc \
95+
git \
7796
make \
7897
openssl-devel \
98+
libcurl-devel \
7999
perl \
80100
pcre-devel \
81101
readline-devel

ci-build.sh

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ MUTUAL_TLS="mutual-tls-${BUILD_NUMBER}"
1414
STANDARD_TLS="standard-tls-${BUILD_NUMBER}"
1515
MOCKSERVER_PORT=9000
1616
SLOWMOCKSERVER_PORT=9001
17-
WORKDIR="/workdir/src/github.com/UKHomeOffice/docker-nginx-proxy"
17+
WORKDIR="${PWD}"
1818

1919
function tear_down_container() {
2020
container=$1
@@ -91,7 +91,7 @@ docker build -t ${TAG} .
9191
echo "travis_fold:end:BUILD"
9292

9393
echo "Running mocking-server..."
94-
docker build -t mockserver:latest /workdir/src/github.com/UKHomeOffice/docker-nginx-proxy -f docker-config/Dockerfile.mockserver
94+
docker build -t mockserver:latest ${WORKDIR} -f docker-config/Dockerfile.mockserver
9595
${STD_CMD} -d \
9696
--name="${MOCKSERVER}" mockserver:latest \
9797
-config=/test-servers.yaml \
@@ -100,7 +100,7 @@ ${STD_CMD} -d \
100100
docker run --rm --link "${MOCKSERVER}:${MOCKSERVER}" martin/wait -c "${MOCKSERVER}:${MOCKSERVER_PORT}"
101101

102102
echo "Running slow-mocking-server..."
103-
docker build -t slowmockserver:latest /workdir/src/github.com/UKHomeOffice/docker-nginx-proxy -f docker-config/Dockerfile.slowmockserver
103+
docker build -t slowmockserver:latest ${WORKDIR} -f docker-config/Dockerfile.slowmockserver
104104
${STD_CMD} -d \
105105
--name="${SLOWMOCKSERVER}" slowmockserver:latest \
106106
-config=/test-servers.yaml \
@@ -147,19 +147,12 @@ start_test "Test GEODB settings can reject..." "${STD_CMD} \
147147
-e \"ADD_NGINX_LOCATION_CFG=error_page 403 /nginx-proxy/50x.shtml;\" \
148148
--link \"${MOCKSERVER}:${MOCKSERVER}\" "
149149
echo "Test GeoIP config IS rejected..."
150-
if ! curl -v -k https://${DOCKER_HOST_NAME}:${PORT}/ 2>&1 \/ | grep '403 Forbidden' ; then
150+
if ! curl -v -k -H "X-Forwarded-For: 1.1.1.1" https://${DOCKER_HOST_NAME}:${PORT}/ 2>&1 \/ | grep '403 Forbidden' ; then
151151
echo "We were expecting to be rejected with 403 error here - we are not in the Congo!"
152152
exit 2
153153
else
154154
echo "Rejected as expected - we are not in the Congo!"
155155
fi
156-
if ! curl -v -k https://${DOCKER_HOST_NAME}:${PORT}/ 2>&1 \
157-
| grep 'An error occurred' ; then
158-
echo "We were expecting to be rejected specific content for invalid country - we are not in the Congo!"
159-
exit 2
160-
else
161-
echo "Rejected with correct content as expected."
162-
fi
163156

164157
start_test "Test rate limits 1 per second" "${STD_CMD} \
165158
-e \"PROXY_SERVICE_HOST=http://${MOCKSERVER}\" \
@@ -283,7 +276,7 @@ wget -O /dev/null --quiet --no-check-certificate https://${DOCKER_HOST_NAME}:${P
283276
--private-key=./client_certs/client.key
284277

285278
echo "Test upstream client certs..."
286-
docker build -t mutual-tls:latest /workdir/src/github.com/UKHomeOffice/docker-nginx-proxy -f docker-config/Dockerfile.mutual-tls
279+
docker build -t mutual-tls:latest ${WORKDIR} -f docker-config/Dockerfile.mutual-tls
287280
${STD_CMD} -d \
288281
-e "PROXY_SERVICE_HOST=http://www.w3.org" \
289282
-e "PROXY_SERVICE_PORT=80" \
@@ -306,7 +299,7 @@ wget -O /dev/null --quiet --no-check-certificate https://${DOCKER_HOST_NAME}:${P
306299
tear_down_container "${MUTUAL_TLS}"
307300

308301
echo "Test failure to verify upstream server cert..."
309-
docker build -t standard-tls:latest /workdir/src/github.com/UKHomeOffice/docker-nginx-proxy -f docker-config/Dockerfile.standard-tls
302+
docker build -t standard-tls:latest ${WORKDIR} -f docker-config/Dockerfile.standard-tls
310303
${STD_CMD} -d \
311304
-e "PROXY_SERVICE_HOST=http://www.w3.org" \
312305
-e "PROXY_SERVICE_PORT=80" \

defaults.sh

100644100755
File mode changed.

go.sh

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ cat > ${NGIX_CONF_DIR}/server_certs.conf <<-EOF_CERT_CONF
2121
ssl_dhparam ${NGIX_CONF_DIR}/dhparam.pem;
2222
EOF_CERT_CONF
2323

24-
2524
if [ "${LOCATIONS_CSV}" == "" ]; then
2625
LOCATIONS_CSV=/
2726
fi
@@ -113,10 +112,10 @@ fi
113112

114113
if [ -f /etc/keys/client-ca ]; then
115114
msg "Loading client certs."
116-
cat > ${NGIX_CONF_DIR}/client_certs.conf <<-EOF_CLIENT_CONF
117-
ssl_client_certificate /etc/keys/client-ca;
118-
ssl_verify_client optional;
119-
EOF_CLIENT_CONF
115+
cat > ${NGIX_CONF_DIR}/client_certs.conf <<-EOF_CLIENT_CONF
116+
ssl_client_certificate /etc/keys/client-ca;
117+
ssl_verify_client optional;
118+
EOF_CLIENT_CONF
120119
else
121120
msg "No client certs mounted - not loading..."
122121
fi
@@ -148,7 +147,7 @@ case "${LOG_FORMAT_NAME}" in
148147
ngx.var.response_body = ngx.ctx.buffered
149148
end
150149
';
151-
EOF-LOGGING-BODY-TRUE
150+
EOF-LOGGING-BODY-TRUE
152151
fi
153152

154153
echo "map \$request_uri \$loggable { ~^/nginx_status/ 0; default 1;}">>${NGIX_CONF_DIR}/logging.conf #remove logging for the sysdig agent.
@@ -171,24 +170,54 @@ if [ "${ADD_NGINX_HTTP_CFG}" != "" ]; then
171170
fi
172171

173172
GEO_CFG="${NGIX_CONF_DIR}/nginx_geoip.conf"
173+
GEO_CFG_INIT="${NGIX_CONF_DIR}/nginx_geoip_init.conf"
174+
GEO_CFG_CONFIG="${NGIX_CONF_DIR}/nginx_geoip.conf"
175+
174176
if [ "${ALLOW_COUNTRY_CSV}" != "" ]; then
175-
msg "Enabling Country codes detection:${ALLOW_COUNTRY_CSV}..."
176-
cat > ${NGIX_CONF_DIR}/nginx_geoip_init.conf <<-EOF-GEO-INIT
177-
init_by_lua '
178-
country = require "country"
179-
country:init()
180-
';
181-
EOF-GEO-INIT
182-
echo "set_by_lua_file \$country_code /usr/local/openresty/nginx/lua/get_country.lua \"\$${REMOTE_IP_VAR}\";">>${GEO_CFG}
183-
184-
# Set up base data as that from yum package...
185-
ln -s /usr/share/GeoIP/GeoIP.dat /usr/share/GeoIP/GeoLiteCountry.dat
186-
187-
# Refresh in background...
188-
/refresh_GeoIP.sh &
177+
msg "Enabling Country codes detection: ${ALLOW_COUNTRY_CSV}"
178+
179+
cat > $GEO_CFG_INIT <<-EOF
180+
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
181+
auto_reload 21600;
182+
\$geoip2_metadata_country_build metadata build_epoch;
183+
\$geoip2_data_country_code default=NA source=\$realip country iso_code;
184+
\$geoip2_data_country_name country names en;
185+
}
186+
187+
geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
188+
\$geoip2_data_city_name default=NA city names en;
189+
}
190+
191+
geoip2_proxy_recursive on;
192+
geoip2_proxy 0.0.0.0/0;
193+
194+
map \$geoip2_data_country_code \$allowed_country {
195+
default no;
196+
NA yes;
197+
$(echo -n "${ALLOW_COUNTRY_CSV}" | awk -F',' "{ for (i=1; i<=NF; i++) { printf \"%s yes;\n\", \$i; }}")
198+
}
199+
200+
EOF
201+
cat > $GEO_CFG_CONFIG <<EOF
202+
# use either the remote addr or the x-forwarded-for header
203+
set \$realip \$remote_addr;
204+
if (\$http_x_forwarded_for ~ "^(\d+\.\d+\.\d+\.\d+)") {
205+
set \$realip \$1;
206+
}
207+
set \$country_code \$geoip2_data_country_code;
208+
209+
# check if the country is allowed and deny
210+
if (\$allowed_country = no) {
211+
return 403;
212+
}
213+
214+
EOF
215+
/refresh_geoip.sh&
216+
msg "Enabling the geoip refresh background job"
189217
else
218+
touch ${GEO_CFG_CONFIG}
219+
touch ${GEO_CFG_INIT}
190220
touch ${GEO_CFG}
191-
touch ${NGIX_CONF_DIR}/nginx_geoip_init.conf
192221
fi
193222

194223
if [ "${STATSD_METRICS_ENABLED}" = "TRUE" ]; then
@@ -197,5 +226,4 @@ if [ "${STATSD_METRICS_ENABLED}" = "TRUE" ]; then
197226
echo "statsd_count \"waf.status.\$status\" 1;" > ${NGIX_CONF_DIR}/nginx_statsd_metrics.conf
198227
fi
199228

200-
201229
eval "${NGINX_BIN} -g \"daemon off;\""

helper.sh

100644100755
File mode changed.

lua/country.lua

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)