Shorewall+Docker: Two Great Tastes That Taste Great Together

As has been mentioned previously, we lurve us some Docker here at Discourse. We also lurve us some security, and I’ve recently been replacing our “artisinally handcrafted iptables firewall rules” with a Shorewall-managed configuration, which plays better with Puppet. Unfortunately, as it stands, like my twin three year olds, they don’t always play well. The…


This topic is for comments on the original blog entry at: http://blog.discourse.org/2015/11/shorewalldocker-two-great-tastes-that-taste-great-together/

「いいね!」 8

Big fan of Shorewall, been using it for a looong time.

「いいね!」 2

Docker has changed their iptables implementation so that the rules captured and replaced are no longer complete.

As a suggestion the revisions posted below should correct the issue and be more tolerant of future changes

/etc/shorewall/init and /etc/shorewall/stop should become

if iptables -t nat -L DOCKER >/dev/null 2>&1; then
    echo '*nat' > /etc/shorewall/docker_rules
    iptables -t nat -S | grep -i docker >> /etc/shorewall/docker_rules
    echo 'COMMIT' >> /etc/shorewall/docker_rules

    echo '*filter' >> /etc/shorewall/docker_rules
    iptables -t filter -S | grep -i docker >> /etc/shorewall/docker_rules
    echo 'COMMIT' >> /etc/shorewall/docker_rules
fi

and /etc/shorewall/start should be

if [ -f /etc/shorewall/docker_rules ]; then
    iptables-restore -n < /etc/shorewall/docker_rules

    rm -f /etc/shorewall/docker_rules
fi
「いいね!」 4

スレッドを掘り起こすようで恐縮ですが、この情報が私の Swarm サービスを Shorewall と連携させるのに役立ったため、他の誰かにもこれらの追加メモが役立つかもしれません。

上記のスクリプトは、ご自身の環境で期待通りに動作するとは限りません。なぜなら、iptables -S を実行すると、ルールが*追加(append)*形式で返されるからです。私の場合のように Shorewall のルールがかなり厳格な場合、単純な追加では、Docker 固有の iptables チェーンに到達するずっと前に DROP されてしまう可能性が高いです。

そこで、ルールを*先頭に挿入(prepend)*するように修正したものを以下に示します。

/etc/shorewall/{init,stop} の内容:

rules=/etc/shorewall/.docker_rules
if iptables -t nat -L DOCKER >/dev/null 2>&1; then
    echo '*nat' > $rules
    iptables -t nat -S | grep -i docker > $rules.nat
    grep '^-N' $rules.nat >> $rules
    tac $rules.nat | sed -n 's/^-A \([^ ]\+\) /-I \1 1 /p' >> $rules
    rm -f $rules.nat
    echo 'COMMIT' >> $rules

    echo '*filter' >> $rules
    iptables -t filter -S | grep -i docker > $rules.filter
    grep '^-N' $rules.filter >> $rules
    tac $rules.filter | sed -n 's/^-A \([^ ]\+\) /-I \1 1 /p' >> $rules
    rm -f $rules.filter
    echo 'COMMIT' >> $rules
fi

また、/etc/shorewall/start は以下の通りです:

rules=/etc/shorewall/.docker_rules
if [ -f $rules ]; then
    iptables-restore -n < $rules
    rm -f $rules
fi