Skip to content

Commit 8897515

Browse files
authored
Merge pull request #88 from hyperledger/ip_tables
Ip tables scripts updated
2 parents cb40bd1 + 4300443 commit 8897515

5 files changed

Lines changed: 102 additions & 102 deletions

File tree

git-filter-add-signoff.py

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

run/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ The relevant directories are mounted as
2727

2828
giving direct access to the relevant config files from the host machine, if needed. Note that the `NETWORK_NAME` in `indy_config.py` is overridden at startup with the value from `INDY_NETWORK_NAME` from `.env`.
2929

30+
## Firewall (IP Tables)
31+
32+
If the firewall rules for your indy node are not set elsewhere (on the docker host or upstream), you may want to use the
33+
[set_iptables.sh](./set_iptables.sh) script to set the recommended firewall settings for your node in the DOCKER-USER
34+
chain.
35+
See `./set_iptables.sh -h` for usage information. You will need to provide the list of ip addresses of nodes in your
36+
network in a suitable file. To this end, create a file called `ips` (filename can be changed via variables `IP_FILE=... ./set_iptables.sh`) and put your network's IP addresses into this file, one per line.
37+
38+
39+
3040
## Logging
3141

3242
The log dir is mounted to `./log_indy` by default to ease access to the log files.

run/idu_ips

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

run/set_iptables.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
3+
set +x
4+
set -e
5+
6+
DEFAULT_ADRESS_FILE=ips
7+
echo "INTERNAL_PORT=${INTERNAL_PORT:=9701}"
8+
echo "CLI_PORT=${CLI_PORT:=9702}"
9+
echo "CHAIN=${CHAIN:=DOCKER-USER}"
10+
echo "MAX_CONN=${MAX_CONN:=500}"
11+
12+
usage() {
13+
echo
14+
echo "Usage:"
15+
echo -n "INTERFACE=[your_network_interface] IP_FILE=[path_to_ip_addresses_file, defaults to $DEFAULT_ADRESS_FILE] "
16+
echo -n "INTERNAL_PORT[default 9701] CLI_PORT=[default 9702] CHAIN[default DOCKER-USER] MAX_CONN[default 500]"
17+
echo "$0"
18+
echo
19+
echo "This script will add rules to your ip tables chain CHAIN to allow incoming connections on port INTERNAL_PORT"
20+
echo "only from ips listed in the IP_FILE. It will also restrict the number of connections to port CLI_PORT to MAX_CONN."
21+
echo
22+
echo "The ip adresses file should contain the list of nodes"
23+
echo "in your network. One ip address per line."
24+
echo "The network interface should be the physical one used for incoming connections from the internet"
25+
echo
26+
echo "This script needs to be run as root/via sudo."
27+
echo
28+
}
29+
30+
# skip existing rules to avoid duplicates
31+
add_new_rule() {
32+
RULE="$@"
33+
34+
if iptables -C $RULE 2>/dev/null 1>&2; then
35+
echo "[skip] $RULE already exists"
36+
elif [[ "$RULE" == *"DROP"* ]] || [[ "$RULE" == *"RETURN"* ]]; then
37+
iptables -A $RULE
38+
echo "[ok] $RULE added to the end of the chain"
39+
else
40+
iptables -I $RULE
41+
echo "[ok] $RULE added to the beginning of the chain"
42+
fi
43+
}
44+
45+
make_last_rule() {
46+
RULE="$@"
47+
while iptables -C $RULE 2>/dev/null 1>&2; do
48+
iptables -D $RULE
49+
echo "[ok] $RULE deleted"
50+
done
51+
iptables -A $RULE
52+
echo "[ok] $RULE added to the end of the chain"
53+
}
54+
55+
# -h --help --whatever
56+
if ! [ -z "$*" ]; then
57+
usage
58+
exit 0
59+
fi
60+
61+
echo "INTERFACE=${INTERFACE:=ens18}"
62+
63+
# check if INTERFACE is set to an inet facing interface
64+
if ! ip a | grep inet | grep "$INTERFACE" >/dev/null; then
65+
echo "interface '$INTERFACE' does not seem to be an internet facing interface"
66+
usage
67+
exit 1
68+
fi
69+
70+
echo "IP_FILE=${IP_FILE:=$DEFAULT_ADRESS_FILE}"
71+
72+
if ! [ -f "$IP_FILE" ]; then
73+
echo "file '$IP_FILE' not found"
74+
usage
75+
exit 1
76+
fi
77+
78+
# 9701 whitelist approach: drop all others INCOMING (-i) connections
79+
add_new_rule $CHAIN -p tcp -i $INTERFACE --dport $INTERNAL_PORT -j DROP
80+
81+
# 9701 create IP whitelist from file
82+
while read IP; do
83+
if [[ "$IP" != "#"* ]] && [[ "$IP" != "" ]]; then
84+
add_new_rule $CHAIN -p tcp --dport $INTERNAL_PORT -s $IP -j ACCEPT
85+
fi
86+
done <"$IP_FILE"
87+
88+
# 9702 connlimit
89+
add_new_rule $CHAIN -p tcp --syn --dport $CLI_PORT -m connlimit --connlimit-above $MAX_CONN -j REJECT
90+
91+
# make sure, RETURN ist the last rule
92+
make_last_rule $CHAIN -j RETURN

run/set_iptables_for_idu.sh

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

0 commit comments

Comments
 (0)