<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Divya Mamgai</title>
    <description>The latest articles on DEV Community by Divya Mamgai (@divyamamgai).</description>
    <link>https://dev.to/divyamamgai</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F510231%2F6d846d54-9c31-4d4f-a77c-99392e661a35.jpeg</url>
      <title>DEV Community: Divya Mamgai</title>
      <link>https://dev.to/divyamamgai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/divyamamgai"/>
    <language>en</language>
    <item>
      <title>How to Whitelist IP Addresses to Access Desired Docker Containers?</title>
      <dc:creator>Divya Mamgai</dc:creator>
      <pubDate>Sun, 08 Nov 2020 07:18:50 +0000</pubDate>
      <link>https://dev.to/divyamamgai/how-to-whitelist-ip-addresses-to-access-desired-docker-containers-3o32</link>
      <guid>https://dev.to/divyamamgai/how-to-whitelist-ip-addresses-to-access-desired-docker-containers-3o32</guid>
      <description>&lt;h1&gt;
  
  
  Motivation
&lt;/h1&gt;

&lt;p&gt;Recently I had to secure one of my docker setups running in a virtual machine so that only specific ports (or docker containers) are accessible via a specific set of IP addresses on the network. Now, this seems to be a simple task, and mind you it is, but not if you aren’t well versed with iptables and how docker configures it for its network. Since I was unable to find any tutorial for this particular requirement I decided to write one.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**Disclaimer:&lt;/em&gt;* I’m quite new to the networking world of Linux and iptables in general, please let me know in the comments if there is an even easier way to achieve this and or any enclosed information is incorrect.*&lt;/p&gt;

&lt;h1&gt;
  
  
  Exploration
&lt;/h1&gt;

&lt;p&gt;Docker does have a documentation page on how to configure iptables with docker &lt;a href="https://docs.docker.com/network/iptables/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. And it does outline a way to whitelist a single IP. So yay?… job done? Not quite. It only allows single IP to access &lt;strong&gt;ALL&lt;/strong&gt; ports in the Docker network. Not quite what we need. If that is your use case I suggest you stop reading this any further and go through the documentation, finish your task at hand, and come back if you would like to know how to add restrictions on port level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Uncomplicated Firewall made complicated by docker?
&lt;/h2&gt;

&lt;p&gt;UFW is an easier and faster way of managing firewalls rules in Linux. Well for one thing it’s in the name — “uncomplicated”. But as I found out with docker UFW is more like CFW or more colloquially known as TFW. I won’t be getting into the details here but because of the way docker manipulates the iptables normal UFW flow won’t work, in fact, it will cause more problems than it solves — learned that the hard way. There are lots of tutorials out there on how to get this to work and even a &lt;a href="https://github.com/chaifeng/ufw-docker" rel="noopener noreferrer"&gt;github&lt;/a&gt; project dedicated to extending UFW to support docker. If most of your setup already relies on UFW I suggest you check those out, since for my use-case it seems quite the overkill and I didn’t bother exploring it.&lt;/p&gt;

&lt;h2&gt;
  
  
  But what the heck are iptables?
&lt;/h2&gt;

&lt;p&gt;Instead of me explaining what they are, which will be a bad job, you can look at the &lt;a href="https://en.wikipedia.org/wiki/Iptables" rel="noopener noreferrer"&gt;wikipedia page&lt;/a&gt; for iptables and very detailed information in the &lt;a href="https://www.frozentux.net/iptables-tutorial/iptables-tutorial.html" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;. The only particulars needed for this post are the following. Iptables mainly have four types of table — raw, mangle, nat, and filter. And they are processed in that order. For more granularity go through the diagram below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AsSWNI1JSAkrP8auz" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AsSWNI1JSAkrP8auz"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding how docker manipulates iptables
&lt;/h2&gt;

&lt;p&gt;Docker creates two custom iptables chains in the &lt;strong&gt;filter&lt;/strong&gt; table named &lt;strong&gt;DOCKER&lt;/strong&gt; and &lt;strong&gt;DOCKER-USER&lt;/strong&gt;. And all of the communication to docker is validated by these rules. They recommend not changing the &lt;strong&gt;DOCKER&lt;/strong&gt; chain since it is populated by docker networking modules. And prepend (not append) all of the custom validation you might require to the &lt;strong&gt;DOCKER-USER&lt;/strong&gt; chain. The important piece of information which the docker’s documentation page leaves out is that all of the port forwarding rules for the containers are added in the &lt;strong&gt;nat&lt;/strong&gt; table of iptables, and this will be a big gotcha later. To see how the filter table is configured let’s execute the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;iptables &lt;span class="nt"&gt;-L&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All of the incoming connections related to docker are routed to docker network interfaces, hence any filtering of these connections (or packets) will take place in the &lt;strong&gt;FORWARD&lt;/strong&gt; filter chain. Hence check the &lt;strong&gt;FORWARD&lt;/strong&gt; chain, you will see that the first entry is &lt;strong&gt;DOCKER-USER&lt;/strong&gt;. This means all (because there are no constraints added to the rule) of the packets received to this chain will be passed to the &lt;strong&gt;DOCKER-USER&lt;/strong&gt; chain. So let’s jump to the &lt;strong&gt;DOCKER-USER&lt;/strong&gt; chain, there (by default) you should see just a single &lt;strong&gt;RETURN&lt;/strong&gt; rule with no constraints, which means that filtering will start again from the next rule in the parent chain. If we don’t want to process any packet we will have to filter it before the &lt;strong&gt;RETURN&lt;/strong&gt; rule.&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting to the point
&lt;/h1&gt;

&lt;p&gt;Let’s recap our objective — only allow a specific ip to communicate with a specific service running on a specific port. You might need this if you have developed a black box that just takes an input and produces output without exposing any implementation details. You would probably want to block all access to the box apart from input and output streams. So let’s say we have two docker containers running — django (at 8080) and postgres (at 5432). And we only want to allow incoming connections to the django server at 8080 and not allow any outside connections to the database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3748%2F1%2AMTR0ZrxnKX1dcqrD1O6FGQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3748%2F1%2AMTR0ZrxnKX1dcqrD1O6FGQ.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Don’t allow any outside connections by default
&lt;/h2&gt;

&lt;p&gt;To drop all incoming and forwarded connections by default but allow established connections and all ssh connections execute the following commands. We are assuming that there is only one external network interface (eth0) in the system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="c"&gt;# Allow loopback connections.*&lt;/span&gt;
iptables &lt;span class="nt"&gt;-A&lt;/span&gt; INPUT &lt;span class="nt"&gt;-i&lt;/span&gt; lo &lt;span class="nt"&gt;-j&lt;/span&gt; ACCEPT
iptables &lt;span class="nt"&gt;-A&lt;/span&gt; OUTPUT &lt;span class="nt"&gt;-o&lt;/span&gt; lo &lt;span class="nt"&gt;-j&lt;/span&gt; ACCEPT

&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="c"&gt;# Allow established and related incoming connections.*&lt;/span&gt;
iptables &lt;span class="nt"&gt;-A&lt;/span&gt; INPUT &lt;span class="nt"&gt;-m&lt;/span&gt; conntrack &lt;span class="nt"&gt;--ctstate&lt;/span&gt; ESTABLISHED,RELATED &lt;span class="nt"&gt;-j&lt;/span&gt; ACCEPT

&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="c"&gt;# Allow established outgoing connections.*&lt;/span&gt;
iptables &lt;span class="nt"&gt;-A&lt;/span&gt; OUTPUT &lt;span class="nt"&gt;-m&lt;/span&gt; conntrack &lt;span class="nt"&gt;--ctstate&lt;/span&gt; ESTABLISHED &lt;span class="nt"&gt;-j&lt;/span&gt; ACCEPT

&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="c"&gt;# Allow all incoming and outgoing SSH connections.*&lt;/span&gt;
iptables &lt;span class="nt"&gt;-A&lt;/span&gt; INPUT &lt;span class="nt"&gt;-p&lt;/span&gt; tcp &lt;span class="nt"&gt;--dport&lt;/span&gt; 22 &lt;span class="nt"&gt;-m&lt;/span&gt; conntrack &lt;span class="nt"&gt;--ctstate&lt;/span&gt; NEW,ESTABLISHED &lt;span class="nt"&gt;-j&lt;/span&gt; ACCEPT
iptables &lt;span class="nt"&gt;-A&lt;/span&gt; OUTPUT &lt;span class="nt"&gt;-p&lt;/span&gt; tcp &lt;span class="nt"&gt;--sport&lt;/span&gt; 22 &lt;span class="nt"&gt;-m&lt;/span&gt; conntrack &lt;span class="nt"&gt;--ctstate&lt;/span&gt; ESTABLISHED &lt;span class="nt"&gt;-j&lt;/span&gt; ACCEPT

&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="c"&gt;# Drop all incoming and forwarding connections by default.*&lt;/span&gt;
iptables &lt;span class="nt"&gt;-P&lt;/span&gt; INPUT DROP
iptables &lt;span class="nt"&gt;-P&lt;/span&gt; FORWARD DROP

&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="c"&gt;# Drop all forwarded connections from the external interface in the DOCKER-USER chain.*&lt;/span&gt;
iptables &lt;span class="nt"&gt;-I&lt;/span&gt; DOCKER-USER &lt;span class="nt"&gt;-i&lt;/span&gt; eth0 &lt;span class="nt"&gt;-j&lt;/span&gt; DROP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;For more information and reference for these commands visit &lt;a href="https://www.digitalocean.com/community/tutorials/iptables-essentials-common-firewall-rules-and-commands" rel="noopener noreferrer"&gt;this&lt;/a&gt; handy blog post over at DigitalOcean.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So now if you again check the output of sudo iptables -L -v you will see that for &lt;strong&gt;INPUT&lt;/strong&gt; and &lt;strong&gt;FORWARD&lt;/strong&gt; chain default policy (shown in brackets after the name of the chain) is now set to &lt;strong&gt;DROP&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;INPUT&lt;/strong&gt; chain should look something like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  555  416K ACCEPT     all  --  lo     any     anywhere             anywhere            
  147 11595 ACCEPT     all  --  any    any     anywhere             anywhere             ctstate RELATED,ESTABLISHED
    1    44 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:ssh ctstate NEW,ESTABLISHED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;DOCKER-USER&lt;/strong&gt; chain should look something like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Chain DOCKER-USER (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  eth0   any     anywhere             anywhere            
   16   864 RETURN     all  --  any    any     anywhere             anywhere
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now &lt;strong&gt;all of the packets coming to this chain from the **eth0&lt;/strong&gt; interface will be dropped**.&lt;/p&gt;

&lt;h2&gt;
  
  
  It’s quite lonely here, let someone talk to me
&lt;/h2&gt;

&lt;p&gt;Assuming that we have a static IP (192.168.0.69) from where we would like to send requests to our django server running at port 8080. To allow these requests to be forwarded to the django container execute the following commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="c"&gt;# Allow inbound and outbound traffic for 192.168.0.69 IP on 8080 port.*&lt;/span&gt;
iptables &lt;span class="nt"&gt;-I&lt;/span&gt; DOCKER-USER &lt;span class="nt"&gt;-i&lt;/span&gt; eth0 &lt;span class="nt"&gt;-s&lt;/span&gt; 192.168.0.69 &lt;span class="nt"&gt;-p&lt;/span&gt; tcp &lt;span class="nt"&gt;--dport&lt;/span&gt; 8080 &lt;span class="nt"&gt;-j&lt;/span&gt; RETURN
iptables &lt;span class="nt"&gt;-I&lt;/span&gt; DOCKER-USER &lt;span class="nt"&gt;-o&lt;/span&gt; eth0 &lt;span class="nt"&gt;-d&lt;/span&gt; 192.168.0.69 &lt;span class="nt"&gt;-p&lt;/span&gt; tcp &lt;span class="nt"&gt;--sport&lt;/span&gt; 8080 &lt;span class="nt"&gt;-j&lt;/span&gt; RETURN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above commands add two rules stating that any forwarded tcp packets either incoming or outgoing from 192.168.0.69 on port 8080 should &lt;strong&gt;RETURN&lt;/strong&gt; from the &lt;strong&gt;DOCKER-USER&lt;/strong&gt; chain to its parent chain (&lt;strong&gt;FORWARD&lt;/strong&gt;) for further processing and skip any subsequent rules in this chain. &lt;strong&gt;Hence those packets are not dropped since the dropping rule comes after them&lt;/strong&gt;. Now django server should be able to receive requests from that IP address. You can prepend as many rules as you want to allow different IP addresses or complete subnets. The job is done.&lt;/p&gt;

&lt;h2&gt;
  
  
  But wait there’s a gotcha?
&lt;/h2&gt;

&lt;p&gt;Remember how I told you docker modifies the &lt;strong&gt;nat&lt;/strong&gt; table of iptables as well? Docker handles all of the exposed ports from your containers using it. Let’s see what exactly it does by running the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;iptables &lt;span class="nt"&gt;-t&lt;/span&gt; nat &lt;span class="nt"&gt;-L&lt;/span&gt; DOCKER
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see an output similar to this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Chain DOCKER (2 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere            
RETURN     all  --  anywhere             anywhere            
DNAT       tcp  --  anywhere             anywhere             tcp dpt:5432 to:172.18.0.12:5432
DNAT       tcp  --  anywhere             anywhere             tcp dpt:8080 to:172.18.0.7:8080
DNAT       tcp  --  anywhere             anywhere             tcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This allows all of the connections made on the exposed ports on the host network to get forwarded to the docker network and its corresponding container.&lt;/p&gt;

&lt;p&gt;Do you see the problem here? Let me explain. Since the &lt;strong&gt;nat&lt;/strong&gt; table is processed before the &lt;strong&gt;filter&lt;/strong&gt; table in iptables, &lt;strong&gt;if we have specified a different exposed port than the port of the process running inside the docker container&lt;/strong&gt; we will run into issues while writing our filtering rules.&lt;/p&gt;

&lt;p&gt;Let’s say in our example above if the django server was running on port 8080 but we exposed (and or mapped) it as 8081, to not get any interference during development with other django based servers which might be running as well. In this case, if you were to run sudo iptables -t nat -L DOCKER you should see output similar to this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Chain DOCKER (2 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere            
RETURN     all  --  anywhere             anywhere            
DNAT       tcp  --  anywhere             anywhere             tcp dpt:5432 to:172.18.0.12:5432
DNAT       tcp  --  anywhere             anywhere             tcp dpt:8081 to:172.18.0.7:8080
DNAT       tcp  --  anywhere             anywhere             tcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We would expect to use port 8081 while writing our IP whitelisting rule as described in the previous section. But that won’t work since those packets would have already been forwarded to port 8080 and will violate the constraints of the whitelisting rule. Figuring this out made me scratch my head for a long while. Hence while writing IP whitelisting rules as mentioned above you should use the port number of the underlying docker container and not the host mapped port as you would expect. Even though you would access the django server using port 8081 on the whitelisted IP.&lt;/p&gt;

&lt;h1&gt;
  
  
  And why should I care about this?
&lt;/h1&gt;

&lt;p&gt;Cause it’s secure.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This is a re-post of my original medium post &lt;a href="https://medium.com/swlh/how-to-whitelist-ip-addresses-to-access-desired-docker-containers-5f6c8fcfa7f6" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>linux</category>
      <category>security</category>
    </item>
  </channel>
</rss>
