Skip to content

Commit 2fa84c5

Browse files
author
Alex Fraser
committed
Reusing CA certificate
Using a volume to persist the CA certificate so it doesn't need to be reinstalled in the clients when the proxy is restarted.
1 parent f29fc28 commit 2fa84c5

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

Dockerfile

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ FROM ubuntu:14.04
22

33
MAINTAINER Alex Fraser <alex@vpac-innovations.com.au>
44

5+
# Install base dependencies.
56
# Run a caching proxy on the host and bind a port to APT_PROXY_PORT to cache
67
# apt requests. Build with `docker build --build-arg APT_PROXY_PORT=[X] [...]`.
8+
# Not required if you're using a transparent proxy (like the one built by
9+
# this project).
710
WORKDIR /root
811
ARG APT_PROXY_PORT=
912
COPY detect-apt-proxy.sh /root/
@@ -24,25 +27,26 @@ RUN export DEBIAN_FRONTEND=noninteractive TERM=linux \
2427
# rm -rf /var/lib/apt/lists/* \
2528
# /etc/apt/apt.conf.d/30proxy \
2629

30+
# Customise and build Squid.
2731
# It's silly, but run dpkg-buildpackage again if it fails the first time. This
2832
# is needed because sometimes the `configure` script is busy when building in
2933
# Docker after autoconf sets its mode +x.
30-
COPY squid3.patch /root/
34+
COPY squid3.patch mime.conf /root/
3135
RUN cd squid3-3.?.? \
3236
&& patch -p1 < /root/squid3.patch \
3337
&& export NUM_PROCS=`grep -c ^processor /proc/cpuinfo` \
34-
&& (dpkg-buildpackage -b -j${NUM_PROCS} || dpkg-buildpackage -b -j${NUM_PROCS})
35-
COPY mime.conf /root/
36-
RUN dpkg -i \
37-
squid3-common_3.?.?-?ubuntu?.?_all.deb \
38-
squid3_3.?.?-?ubuntu?.?_*.deb \
38+
&& (dpkg-buildpackage -b -j${NUM_PROCS} \
39+
|| dpkg-buildpackage -b -j${NUM_PROCS}) \
40+
&& DEBIAN_FRONTEND=noninteractive TERM=linux dpkg -i \
41+
../squid3-common_3.?.?-?ubuntu?.?_all.deb \
42+
../squid3_3.?.?-?ubuntu?.?_*.deb \
3943
&& mkdir -p /etc/squid3/ssl_cert \
40-
&& cat mime.conf >> /usr/share/squid3/mime.conf
44+
&& cat /root/mime.conf >> /usr/share/squid3/mime.conf
4145

4246
COPY squid.conf /etc/squid3/squid.conf
4347
COPY start_squid.sh /usr/local/bin/start_squid.sh
4448

45-
VOLUME /var/spool/squid3
49+
VOLUME /var/spool/squid3 /etc/squid3/ssl_cert
4650
EXPOSE 3128 3129 3130
4751

4852
CMD ["/usr/local/bin/start_squid.sh"]

run.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# proxy server for docker.
55

66
CACHEDIR=${CACHEDIR:-/tmp/squid3}
7+
CERTDIR=${CACHEDIR:-/tmp/squid3_cert}
78
CONTAINER_NAME=${CONTAINER_NAME:-docker-proxy}
89

910
set -e
@@ -30,8 +31,10 @@ start_routing () {
3031
sudo ip route add default via "${IPADDR}" dev docker0 table TRANSPROXY
3132
# Mark packets to port 80 and 443 external, so they route through the new
3233
# route table
33-
sudo iptables -t mangle -I PREROUTING -p tcp --dport 80 \! -s "${IPADDR}" -i docker0 -j MARK --set-mark 1
34-
sudo iptables -t mangle -I PREROUTING -p tcp --dport 443 \! -s "${IPADDR}" -i docker0 -j MARK --set-mark 1
34+
COMMON_RULES="-t mangle -I PREROUTING -p tcp -i docker0 ! -s ${IPADDR}
35+
-j MARK --set-mark 1"
36+
sudo iptables $COMMON_RULES --dport 80
37+
sudo iptables $COMMON_RULES --dport 443
3538
# Exemption rule to stop docker from masquerading traffic routed to the
3639
# transparent proxy
3740
sudo iptables -t nat -I POSTROUTING -o docker0 -s 172.17.0.0/16 -j ACCEPT
@@ -78,14 +81,15 @@ terminated () {
7881
run () {
7982
# Make sure we have a cache dir - if you're running in vbox you should
8083
# probably map this through to the host machine for persistence
81-
mkdir -p "${CACHEDIR}"
84+
mkdir -p "${CACHEDIR}" "${CERTDIR}"
8285
# Because we're named, make sure the container doesn't already exist
8386
stop
8487
# Run and find the IP for the running container. Bind the forward proxy port
8588
# so clients can get the CA certificate.
8689
CID=$(sudo docker run --privileged -d \
8790
--name ${CONTAINER_NAME} \
8891
--volume="${CACHEDIR}":/var/spool/squid3 \
92+
--volume="${CERTDIR}":/etc/squid3/ssl_cert \
8993
--publish=3128:3128 \
9094
${CONTAINER_NAME})
9195
IPADDR=$(sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${CID})

start_squid.sh

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
#!/bin/bash
22

33
function gen-cert() {
4-
pushd /etc/squid3/ssl_cert
5-
openssl req -new -newkey rsa:2048 -sha256 -days 365 -nodes -x509 \
6-
-keyout privkey.pem -out ca.pem \
7-
-subj '/CN=squid-ssl/O=NULL/C=AU'
8-
chown proxy.proxy privkey.pem
9-
chmod 600 privkey.pem
10-
openssl x509 -in ca.pem -outform DER -out ca.der
4+
pushd /etc/squid3/ssl_cert > /dev/null
5+
if [ ! -f ca.pem ]; then
6+
openssl req -new -newkey rsa:2048 -sha256 -days 365 -nodes \
7+
-x509 -keyout privkey.pem -out ca.pem \
8+
-subj '/CN=docker-proxy/O=NULL/C=AU'
9+
chown proxy.proxy privkey.pem
10+
chmod 600 privkey.pem
11+
openssl x509 -in ca.pem -outform DER -out ca.der
12+
else
13+
echo "Reusing existing certificate"
14+
fi
15+
openssl x509 -sha1 -in ca.pem -noout -fingerprint
1116
# Make CA certificate available for download via HTTP Forwarding port
1217
# e.g. GET http://docker-proxy:3128/squid-internal-static/icons/ca.pem
1318
cp `pwd`/ca.* /usr/share/squid3/icons/
14-
popd
19+
popd > /dev/null
1520
return $?
1621
}
1722

0 commit comments

Comments
 (0)