Skip to content

Commit 4177bc2

Browse files
Merge pull request #42 from CodeNow/iptables
Iptables
2 parents 41e3c1b + 196f314 commit 4177bc2

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ consul-resources/vault/**/token-03
1010
key/rollbar.token
1111
.stubdata
1212
vault-resources/s3.policy.json
13+
.idea

init.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ else
1919
export CONSUL_HOSTNAME
2020
fi
2121

22+
export DOCKER_NETWORK=172.17.0.0/16
23+
2224
source "${DOCK_INIT_BASE}/lib/consul.sh"
2325
source "${DOCK_INIT_BASE}/lib/aws.sh"
2426
source "${DOCK_INIT_BASE}/lib/dock.sh"
2527
source "${DOCK_INIT_BASE}/lib/container.sh"
28+
source "${DOCK_INIT_BASE}/lib/iptables.sh"
2629
source "${DOCK_INIT_BASE}/lib/util/log.sh"
2730

2831
# Initializes the dock
@@ -35,6 +38,8 @@ main() {
3538
dock::set_hostname
3639
dock::set_config_org
3740
container::start
41+
# rules must be run after docker has started
42+
iptables::run_rules
3843
log::info "Init Done!"
3944
}
4045

lib/container.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ container::_start_cadvisor_container() {
104104

105105
container::_start_node_exporter_container() {
106106
local name="prom/node-exporter"
107-
local version="0.12.0"
107+
local version="v0.13.0"
108108

109109
log::info "Starting ${name}:${version} container"
110110
local docker_logs
@@ -113,10 +113,16 @@ container::_start_node_exporter_container() {
113113
--detach=true \
114114
--restart=always \
115115
--net=host \
116+
--volume=/proc:/host/proc \
117+
--volume=/sys:/host/sys \
118+
--volume=/:/rootfs \
116119
--memory=100mb \
117120
--memory-reservation=50mb \
118121
"${name}:${version}" \
119122
--collectors.enabled=conntrack,diskstats,filefd,filesystem,loadavg,meminfo,netdev,netstat,stat,time \
123+
--collector.procfs=/host/proc \
124+
--collector.sysfs=/host/sys \
125+
--collector.filesystem.ignored-mount-points="/rootfs/docker/aufs|/sys|/etc|/proc|/dev|/rootfs/run|/$" \
120126
--web.listen-address=:29006)
121127

122128
if [[ "$?" -gt "0" ]]; then

lib/iptables.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
source "${DOCK_INIT_BASE}/lib/util/log.sh"
4+
5+
iptables::run_rules() {
6+
log::info "setting up iptable rules"
7+
# drop pings
8+
iptables -I INPUT -p icmp --icmp-type echo-request -m state --state ESTABLISHED -j DROP
9+
10+
# prevent containers from talking to host
11+
iptables -I INPUT -s ${DOCKER_NETWORK} -d 10.0.0.0/8 -m state --state NEW -j DROP
12+
13+
# drop all new traffic from container ip to runnable infra
14+
iptables -I FORWARD -s ${DOCKER_NETWORK} -d 10.0.0.0/8 -m state --state NEW -j DROP
15+
# log container traffic for PSAD
16+
iptables -I FORWARD -s ${DOCKER_NETWORK} -j LOG
17+
# drop all local container to container traffic
18+
iptables -I FORWARD -s ${DOCKER_NETWORK} -d ${DOCKER_NETWORK} -j DROP
19+
# allow consul access (should be before drop)
20+
iptables -I FORWARD -s ${DOCKER_NETWORK} -d ${CONSUL_HOSTNAME} -j ACCEPT
21+
22+
DNS_IP=`iptables::_find_aws_dns_ip`
23+
# allow aws DNS server queries (must be first)
24+
iptables -I FORWARD -s ${DOCKER_NETWORK} -d ${DNS_IP} -j ACCEPT
25+
26+
# drop all new traffic from container to runnable infra
27+
iptables -I OUTPUT -s ${DOCKER_NETWORK} -d 10.0.0.0/8 -m state --state NEW -j DROP
28+
}
29+
30+
iptables::_find_aws_dns_ip() {
31+
cat /etc/resolv.conf | grep name | cut -d' ' -f 2
32+
}

0 commit comments

Comments
 (0)