{"id":2112,"date":"2020-04-17T01:00:00","date_gmt":"2020-04-17T01:00:00","guid":{"rendered":"http:\/\/inswwdev.azurewebsites.net\/au\/insights\/uncategorized\/encrypting-container-traffic-with-wireguard\/"},"modified":"2024-12-13T02:23:36","modified_gmt":"2024-12-13T02:23:36","slug":"encrypting-container-traffic-with-wireguard","status":"publish","type":"post","link":"https:\/\/www.insentragroup.com\/au\/insights\/geek-speak\/professional-services\/encrypting-container-traffic-with-wireguard\/","title":{"rendered":"Encrypting Container Traffic with WireGuard"},"content":{"rendered":"<p><span>I was thinking about the container traffic between several nodes and it occurred to me that unless someone uses TLS\/SSL for specific services (like LDAPs or HTTPS), the rest of the services are not encrypted. So, I created this simple setup where it is possible to use WireGuard VPN to secure the traffic between the nodes and send it over the encrypted tunnel. <\/span><\/p>\n<p><span>The process below demonstrates how to install and configure simple WireGuard on Fedora 31 and RHEL 8 servers.<\/span><\/p>\n<h3 style=\"padding-bottom: 15px; margin-bottom: 30px; margin-top: 40px; border-bottom: 1px solid #f16020;\">Install WireGuard on Fedora 31<\/h3>\n<p style=\"padding-left: 40px;\"><span>1. Log into your server and ensure you have sudo or root privileges <\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>2. Update the server<\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>\u200bdnf \u2013-refresh upgrade<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>3. Reboot the server\/workstation to start with the latest kernel<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>4. Enable copr repository (Cool Other Package Repo)<\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>dnf copr enable jdoss\/wireguard<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>5. Install WireGuard packages (tools and dkms). This step should build dynamic modules for WireGuard<\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>dnf install wireguard-dkms wireguard-tools<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>6. Try to add a new interface using ip link add command. This step should load the relevant WireGuard module to the kernel. <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>ip link add wg0 type wireguard <br \/>ip a <\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>7. Verify if the interface is visible<\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>ip address show dev wg0 <\/span><\/p>\n<p style=\"padding-left: 40px;\">\u00a08. Create private and public keys for the WireGuard<\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> mkdir -p \/root\/wireguard<br \/>cd \/root\/wireguard<br \/>umask 077; wg genkey &gt; private; wg pubkey <\/span><\/p>\n<p style=\"padding-left: 40px;\">9. Configure the interface with the internal ip address<\/p>\n<p style=\"padding: 5px 10px; background-color: #eee;\"><span> ip link add wg0 type wireguard<br \/>ip addr add 10.0.0.1\/24 dev wg0<br \/>ip link set up dev wg0 <\/span><\/p>\n<h3 style=\"padding-bottom: 15px; margin-bottom: 30px; margin-top: 40px; border-bottom: 1px solid #f16020;\">Install WireGuard on RHEL 8<\/h3>\n<p style=\"padding-left: 40px;\"><span>1. Log into your server and ensure you have sudo or root privileges <\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>2. Update the server<\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>dnf \u2013-refresh upgrade <\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>3. Reboot the server\/workstation to start with the latest kernel<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>4. Enable repository <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> dnf install https:\/\/dl.fedoraproject.org\/pub\/epel\/epel-release-latest-8.noarch.rpm<br \/>subscription-manager repos &#8211;enable codeready-builder-for-rhel-8-$(arch)-rpms<br \/>dnf copr enable jdoss\/wireguard<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>5. Install WireGuard packages (tools and dkms). This step should build dynamic modules for WireGuard<\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>dnf install wireguard-dkms wireguard-tools<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>6. Try to add a new interface using ip link add command. This step should load the relevant WireGuard module to the kernel. <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> ip link add wg0 type wireguard <br \/>ip a<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>7. Verify if the interface is visible<\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>ip address show dev wg0<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>8. Create private and public keys for the WireGuard<\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>mkdir -p \/root\/wireguard <br \/>cd \/root\/wireguard <br \/>umask 077; wg genkey &gt; private; wg pubkey &lt; private <\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>9. Configure the interface with the internal ip address <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> ip link add wg0 type wireguard<br \/>ip addr add 10.0.0.2\/24 dev wg0<br \/>ip link set up dev wg0 <\/span><\/p>\n<h3 style=\"padding-bottom: 15px; margin-bottom: 30px; margin-top: 40px; border-bottom: 1px solid #f16020;\">Configure WireGuard<\/h3>\n<p style=\"padding-left: 40px;\"><span>1. Log in to both servers<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>2. Ensure the previous steps have been accomplished and there is interface wg0 with IP address assigned<\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>ip addr show dev wg0<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>3. On both servers run the following command: <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>mkdir -p \/etc\/wireguard; touch \/etc\/wireguard\/wg0.conf<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>4. On both servers run the following command: <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>wg<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>5. The command will return the output similar to this: <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>interface: wg0<br \/>listening port: 60316 <\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>6. Run the following command on both servers. This will set the port and set the public certificate<\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>wg set wg0 listen-port 51820 private-key \/root\/wireguard\/private<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>7. Run the following command on both servers: <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> wg <\/p>\n<p>interface: wg0<br \/>public key: 9yOzarwDKiiIxRr+u+OfGb+jpHDwrTPSkZ1lpZ162kE=<br \/>private key: (hidden)<br \/>listening port: 51820 <\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>8. Set the tunnel on both servers (two-way tunnel). Run the following command on the Fedora server. Ensure the peer certificate has been copied from the RHEL server and IP addresses as well. <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>wg set wg0 peer 3BYw0h6PC0d2hEl5fcP5Km3NNTS2DFIjpADbl\/xaZlo= allowed-ips <br \/>10.0.0.0\/24 endpoint 192.168.100.37:51820<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>9. Set the tunnel on both servers (two-way tunnel). Run the following command on the RHEL server. Ensure the peer certificate has been copied from the RHEL server and IP addresses as well. <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee border: 1px solid #d7d7d7;\"><span>wg set wg0 peer 9yOzarwDKiiIxRr+u+OfGb+jpHDwrTPSkZ1lpZ162kE= allowed-ips <br \/>10.0.0.0\/24 endpoint 192.168.100.66:51820<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>10. Open the firewall port on both servers to allow WireGuard communication. Remember WireGuard is using UDP protocol. You can use the following to create a new service: <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> # cat &gt; \/etc\/firewalld\/services\/wireguard.xml &lt;&lt; EOF <\/p>\n<p>WIREGUARD <\/p>\n<p>EOF <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> firewall-cmd &#8211;add-service=wireguard &#8211;permanent <br \/>firewall-cmd &#8211;add-service=wireguard <br \/>firewall-cmd &#8211;reload <\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>11. Verify the communication between the peers <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>ping 10.0.0.2 or ping 10.0.0.1<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>12. The wg show command should provide the following information: <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> wg show<br \/>interface: wg0<br \/>public key: 9yOzarwDKiiIxRr+u+OfGb+jpHDwrTPSkZ1lpZ162kE=<br \/>private key: (hidden)<br \/>listening port: 51820 <\/p>\n<p>peer: 3BYw0h6PC0d2hEl5fcP5Km3NNTS2DFIjpADbl\/xaZlo=<br \/>endpoint: 192.168.100.37:51820<br \/>allowed ips: 10.0.0.0\/24<br \/>latest handshake: 3 minutes, 1 second ago<br \/>transfer: 1.80 KiB received, 1.71 KiB sent <\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>13. Save the WireGuard configuration on both hosts: <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>wg-quick save wg0<\/span><\/p>\n<h3 style=\"padding-bottom: 15px; margin-bottom: 30px; margin-top: 40px; border-bottom: 1px solid #f16020;\">Install Podman and Run the Container on the WireGuard Interface<\/h3>\n<p style=\"padding-left: 40px;\"><span>1. Log into both servers<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>2. On both servers install Podman if it is not installed yet: <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span>dnf install podman -y <\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>3. Run any container image to check the functionality. For example \u2013 nginx on port 80<\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> podman run -dt -p 80:80 &#8211;name mynginx nginx podman ps -a curl http:\/\/localhost:80 <\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>4. Delete all containers: <\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee;\"><span>podman rm -f &#8211;all<\/span><\/p>\n<p style=\"padding-left: 40px;\"><span>5. Create the container listening on the WireGuard interface: <\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><span>Verify the IP address assigned to WireGuard wg0 interface<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> ip addr show dev wg0 | grep inet | awk &#8216;{ print $2 }&#8217; <\/p>\n<p>10.0.0.1\/24 <\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><span>Run the Podman container on the WireGuard interface on Fedora server<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> podman run -dt -p 10.0.0.1:80:80 &#8211;name mynginx Nginx<br \/>podman port mynginx<\/p>\n<p>80\/tcp -&gt; 10.0.0.1:80 <\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><span>Verify if you can still connect to port 80\/tcp on localhost<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> # curl http:\/\/localhost:80<br \/>curl: (7) Failed to connect to localhost port 80: Connection refused <\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><span>Verify if you can connect to port 80\/tcp on 10.0.0.1 (WireGuard interface):<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span><img decoding=\"async\" src=\"https:\/\/www.insentragroup.com\/wp-content\/uploads\/sites\/22\/2021\/02\/insentra-seb-04202020-img-1.jpg\" alt=\"\" data-udi=\"umb:\/\/media\/469bc7312cd2442eb7f8d1eaf729bc7e\" title=\"\"><\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><span>Open port 80\/tcp on the firewall: <\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> firewall-cmd &#8211;add-service=http &#8211;permanent<br \/>firewall-cmd &#8211;add-service=http<br \/>firewall-cmd &#8211;reload <\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><span>From the RHEL node, try to connect to port 80\/tcp using the Fedora host IP (192.168.100.66 in my case): <\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> # curl http:\/\/192.168.100.66:80<br \/>curl: (7) Failed to connect to 192.168.100.66 port 80: No route to host <\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><span>From the RHEL node, try to connect to port 80\/tcp using the WireGuard IP (10.0.0.1 in my case): <\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span><img decoding=\"async\" src=\"https:\/\/www.insentragroup.com\/wp-content\/uploads\/sites\/22\/2021\/02\/insentra-seb-04202020-img-2.jpg\" alt=\"\" data-udi=\"umb:\/\/media\/e7080b3009f24c4fadba4271088026cd\" title=\"\"><\/span><\/p>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span> podman run -dt -p 10.0.0.2:80:80 &#8211;name mynginx Nginx<\/p>\n<p>podman port mynginx<\/p>\n<p>80\/tcp -&gt; 10.0.0.2:80 <\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><span>Start \/bin\/bash in the container on Fedora server<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p style=\"padding: 5px 10px; background-color: #eee; border: 1px solid #d7d7d7;\"><span># podman exec -it mynginx \/bin\/bash<\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><span>In the command line of the container curl with the WireGuard IP address of the RHEL server and the port forwarded to the container: <\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span><img decoding=\"async\" src=\"https:\/\/www.insentragroup.com\/wp-content\/uploads\/sites\/22\/2021\/02\/insentra-seb-04202020-img-3.jpg\" alt=\"\" data-udi=\"umb:\/\/media\/04c7ade157064650a5c46930b37ae5b5\" title=\"\"><\/span><\/p>\n<p><span>Should you have any questions \u2013 please do not hesitate to reach out. <\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was thinking about the container traffic between several nodes and it occurred to me that unless someone uses TLS\/SSL for specific services (like LDAPs or HTTPS), the rest of the services are not encrypted. So, I created this simple setup where it is possible to use WireGuard VPN to secure the traffic between the&hellip; <a class=\"more-link\" href=\"https:\/\/www.insentragroup.com\/au\/insights\/geek-speak\/professional-services\/encrypting-container-traffic-with-wireguard\/\">Continue reading <span class=\"screen-reader-text\">Encrypting Container Traffic with WireGuard<\/span><\/a><\/p>\n","protected":false},"author":67,"featured_media":2113,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[25],"tags":[],"class_list":["post-2112","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-professional-services","entry"],"_links":{"self":[{"href":"https:\/\/www.insentragroup.com\/au\/wp-json\/wp\/v2\/posts\/2112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.insentragroup.com\/au\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.insentragroup.com\/au\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.insentragroup.com\/au\/wp-json\/wp\/v2\/users\/67"}],"replies":[{"embeddable":true,"href":"https:\/\/www.insentragroup.com\/au\/wp-json\/wp\/v2\/comments?post=2112"}],"version-history":[{"count":1,"href":"https:\/\/www.insentragroup.com\/au\/wp-json\/wp\/v2\/posts\/2112\/revisions"}],"predecessor-version":[{"id":23569,"href":"https:\/\/www.insentragroup.com\/au\/wp-json\/wp\/v2\/posts\/2112\/revisions\/23569"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.insentragroup.com\/au\/wp-json\/wp\/v2\/media\/2113"}],"wp:attachment":[{"href":"https:\/\/www.insentragroup.com\/au\/wp-json\/wp\/v2\/media?parent=2112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.insentragroup.com\/au\/wp-json\/wp\/v2\/categories?post=2112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.insentragroup.com\/au\/wp-json\/wp\/v2\/tags?post=2112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}