-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsetup-firewall.sh
executable file
·94 lines (80 loc) · 2.61 KB
/
setup-firewall.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash
set -o errexit
set -o nounset
SCRIPT_NAME="${0}"
function setup_firewall {
local container_iface="${1}"
local proxy_port="${2}"
local metadata_ip="${3}"
local metadata_port="${4}"
echo "Drop traffic to ${proxy_port} not from container interface ${container_iface}"
iptables \
-I INPUT \
-p tcp \
--dport "${proxy_port}" \
! -i "${container_iface}" \
-j DROP
echo "Redirect any metadata requests from containers to the proxy service"
local proxy_ip=$(ifconfig "${container_iface}" | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}')
iptables \
-t nat \
-I PREROUTING \
-p tcp \
-d "${metadata_ip}" --dport "${metadata_port}" \
-j DNAT \
--to-destination "${proxy_ip}:${proxy_port}" \
-i "${container_iface}"
}
function error {
echo "${@:-}" 1>&2
}
function print_help {
error "${SCRIPT_NAME} [options]"
error
error "Options:"
error " --container-iface: [required] container bridge network interface (example: docker0)"
error " --proxy-port: port on the container interface that the metadata proxy is bound to"
error " (default: 18000)"
error " --metadata-ip: IP of the EC2 metadata service (default: 169.254.169.254)"
error " --metadata-port: Port of the EC2 metadata service (default: 80)"
}
function main {
local container_iface="" # docker0, flynn0, etc
local proxy_port="18000"
local metadata_ip="169.254.169.254"
local metadata_port="80"
if [[ $EUID -ne 0 ]]; then
error "This script must be run as root"
exit 1
fi
while [[ ${#} -gt 0 ]]; do
case "${1}" in
--container-iface) container_iface="${2}"; shift;;
--proxy-port) proxy_port="${2}"; shift;;
--metadata-ip) metadata_ip="${2}"; shift;;
--metadata-port) metadata_port="${2}"; shift;;
-h|--help)
print_help
exit 0;;
*)
if [[ -n "${1}" ]]; then
error "Unknown option: ${1}"
print_help
exit 1
fi
;;
esac
shift
done
if [[ -z "${container_iface}" ]]; then
error "ERROR: --container-iface is required (example: docker0)"
print_help
exit 1
fi
setup_firewall \
"${container_iface}" \
"${proxy_port}" \
"${metadata_ip}" \
"${metadata_port}"
}
main "${@:-}"