<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>nunogrl.com - tunnels</title><link href="https://nunogrl.com/" rel="alternate"></link><link href="https://nunogrl.com/categories/tunnels/atom.xml" rel="self"></link><id>https://nunogrl.com/</id><updated>2024-03-21T00:00:00+00:00</updated><entry><title>Testing Microservices Securely Using SSH Tunnels</title><link href="https://nunogrl.com/articles/testing-microservices-securely-using-ssh-tunnels/" rel="alternate"></link><published>2024-03-21T00:00:00+00:00</published><updated>2024-03-21T00:00:00+00:00</updated><author><name>Nuno Leitao</name></author><id>tag:nunogrl.com,2024-03-21:/articles/testing-microservices-securely-using-ssh-tunnels/</id><summary type="html">&lt;p class="first last"&gt;A practical guide to testing local and staging microservices against production endpoints using SSH tunnels — without exposing anything publicly&lt;/p&gt;
</summary><content type="html">&lt;div class="section" id="problem-solution"&gt;
&lt;h2&gt;🚀 Problem &amp;amp; Solution&lt;/h2&gt;
&lt;div class="section" id="context-backstory"&gt;
&lt;h3&gt;📌 &lt;strong&gt;Context / Backstory&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;This is a solution to connect two different servers on different networks.&lt;/p&gt;
&lt;p&gt;What I was trying to achieve was to get the server from network-A to reach the
port 80 from the network-B.&lt;/p&gt;
&lt;p&gt;These networks are different VPCs on AWS, and connecting the networks is not an
option, because of some conflicts with DNS.&lt;/p&gt;
&lt;p&gt;We needed to test a &lt;strong&gt;local microservice using a production endpoint&lt;/strong&gt;, which wasn't publicly accessible. This endpoint couldn't be mocked or duplicated in staging — and pushing untested code to production would have been reckless.&lt;/p&gt;
&lt;p&gt;Additionally, we had a &lt;strong&gt;staging microservice&lt;/strong&gt; that also needed to interact with the same production service. The risk of breaking production due to untested integration was high, but access was restricted for good reason.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="the-problem"&gt;
&lt;h3&gt;⚠️ &lt;strong&gt;The Problem&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;How do we test these microservices — local and staging — against a &lt;strong&gt;real production service&lt;/strong&gt; without:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Exposing the service publicly&lt;/li&gt;
&lt;li&gt;Deploying unverified code&lt;/li&gt;
&lt;li&gt;Breaking the security model&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="the-solution"&gt;
&lt;h3&gt;💡 &lt;strong&gt;The Solution&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;We used &lt;strong&gt;SSH tunnels&lt;/strong&gt; to create secure, temporary links between the testing environments (local and staging) and the production endpoint. This allowed us to route traffic safely without exposing any services over the internet.&lt;/p&gt;
&lt;p&gt;👥 &lt;strong&gt;Who This Helps&lt;/strong&gt;&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;DevOps engineers needing to test services under real conditions&lt;/li&gt;
&lt;li&gt;Developers in environments with locked-down production APIs&lt;/li&gt;
&lt;li&gt;Homelab or cloud setups requiring secure point-to-point testing&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="technical-implementation"&gt;
&lt;h2&gt;⚙️ Technical Implementation&lt;/h2&gt;
&lt;p&gt;I can reach both networks from my laptop through VPNs, so the solution I'm
sharing here is on how to &lt;strong&gt;forward the port 80 from a server to
localhost:8080 of the other server&lt;/strong&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;SERVER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;server-net-b
&lt;span class="nv"&gt;CANARY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;server-net-a&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;# workstation&lt;/span&gt;
ssh&lt;span class="w"&gt; &lt;/span&gt;-L&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;8080&lt;/span&gt;:&lt;span class="nv"&gt;$SERVER&lt;/span&gt;:80&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$SERVER&lt;/span&gt;

&lt;span class="c1"&gt;# workstation&lt;/span&gt;
ssh&lt;span class="w"&gt; &lt;/span&gt;-R&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;8080&lt;/span&gt;:localhost:8080&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$CANARY&lt;/span&gt;

&lt;span class="c1"&gt;# CANARY&lt;/span&gt;
curl&lt;span class="w"&gt; &lt;/span&gt;localhost:8080
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here's a diagram of the solution:&lt;/p&gt;
&lt;div class="mermaid" id="mermaid-diagram-4774066912988345596"&gt;
flowchart LR

   subgraph local network
   L[Local Machine]
   end

   subgraph vpc-prod
   S[Server
     *webapp-0ef31
     localhost:80*]
   end

   subgraph vpc-dev
   P[Canary]
   end

   L --- |*webapp-0ef31
          localhost:8080*|L
   L --- |Local Port Forward
         ssh -L 8080:Server:80| S
   L --&gt;|Remote Port Forward
         ssh -R 8080:CANARY:8080| P
   P --&gt; |*webapp-0ef31
          localhost:8080*| P&lt;/div&gt;&lt;p&gt;Let's visualize the different types of SSH tunnels we can use:&lt;/p&gt;
&lt;div class="section" id="local-port-forwarding-local-to-production"&gt;
&lt;h3&gt;1️⃣ Local Port Forwarding (Local to Production)&lt;/h3&gt;
&lt;p&gt;To test a local service against a production database or API:&lt;/p&gt;
&lt;div class="mermaid" id="mermaid-diagram--497343567526766339"&gt;
flowchart LR
   L &lt;--&gt;|Local Port Forward
         ssh -L 5432| S
   L --&gt;|localhost:5432| L

   subgraph local network
   L[Local Machine]
   end

   subgraph vpc-dev
   S[Staging Server
     localhost:5432]
   end&lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;ssh&lt;span class="w"&gt; &lt;/span&gt;-L&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;5432&lt;/span&gt;:localhost:5432&lt;span class="w"&gt; &lt;/span&gt;user@prod-host
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This allows you to connect to &lt;cite&gt;localhost:5432&lt;/cite&gt;, which transparently tunnels to the production service on &lt;cite&gt;prod-host&lt;/cite&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="remote-port-forwarding-expose-local-to-remote"&gt;
&lt;h3&gt;2️⃣ Remote Port Forwarding (Expose Local to Remote)&lt;/h3&gt;
&lt;p&gt;To make your local microservice available to a remote server (like staging or prod):&lt;/p&gt;
&lt;div class="mermaid" id="mermaid-diagram-2118793104627592594"&gt;
flowchart LR
   L &lt;--&gt;|Local Port Forward
         ssh -R 8080:localhost:3000| S
   S --&gt;|localhost:8080| S

   subgraph local network
   L[Local Machine
     localhost:3000]
   end

   subgraph vpc-dev
   S[Staging Server]
   end&lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;ssh&lt;span class="w"&gt; &lt;/span&gt;-R&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;8080&lt;/span&gt;:localhost:3000&lt;span class="w"&gt; &lt;/span&gt;user@remote-server
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, &lt;cite&gt;remote-server:8080&lt;/cite&gt; connects to your local microservice running on port &lt;cite&gt;3000&lt;/cite&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="socks-proxy-dynamic-tunnel"&gt;
&lt;h3&gt;3️⃣ SOCKS Proxy (Dynamic Tunnel)&lt;/h3&gt;
&lt;p&gt;To route your web or tool traffic through a secure production jump host:&lt;/p&gt;
&lt;div class="mermaid" id="mermaid-diagram-6601247410485898508"&gt;
flowchart LR
   L --&gt;|web proxy:
         localhost:1080| L

   L &lt;--&gt;|ssh -D 1080| J
   J ==&gt; G

   subgraph local network
   L[Local Machine]
   end
   subgraph network
   J[server]
   G((internet
     gateway))
   end&lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;ssh&lt;span class="w"&gt; &lt;/span&gt;-D&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;1080&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-C&lt;span class="w"&gt; &lt;/span&gt;-N&lt;span class="w"&gt; &lt;/span&gt;user@prod-host
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then configure your browser or curl to use SOCKS proxy at &lt;cite&gt;localhost:1080&lt;/cite&gt;.&lt;/p&gt;
&lt;p&gt;With this, you can access any service that is hosted on the production server, such as a database or API.&lt;/p&gt;
&lt;p&gt;Also, all your traffic will be encrypted and routed through the production server.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="reverse-ssh-tunnel-access-machines-behind-nat"&gt;
&lt;h3&gt;4️⃣ Reverse SSH Tunnel (Access Machines Behind NAT)&lt;/h3&gt;
&lt;p&gt;To allow remote SSH access into a local machine that's behind NAT.&lt;/p&gt;
&lt;p&gt;This is useful when you need to access a machine that is behind a firewall or NAT.
In the situation you need to access a machine that is behind a firewall someone else controls, this is a great solution.&lt;/p&gt;
&lt;p&gt;the person will connect to the jumbox server and then you will connect to the jumbox server to access the machine that is behind the firewall.&lt;/p&gt;
&lt;div class="mermaid" id="mermaid-diagram--7573044980368461621"&gt;
flowchart LR
   H &lt;--&gt;|Remote Port Forward
          ssh -R
          /hooked ssh connection| J
   L --&gt;|ssh
   to jumpbox| J
   J --&gt;|ssh to
         localhost:2222| J

   subgraph local network
   L[Local Machine]
   end
   subgraph Public network
   J[jumpbox]
   end
   subgraph private network
   H[server]
   end&lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;ssh&lt;span class="w"&gt; &lt;/span&gt;-R&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;2222&lt;/span&gt;:localhost:22&lt;span class="w"&gt; &lt;/span&gt;user@jumpbox
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then connect to the local machine from jumpbox:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;ssh&lt;span class="w"&gt; &lt;/span&gt;-p&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;2222&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;user@localhost
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="persistent-tunnels-with-autossh"&gt;
&lt;h3&gt;5️⃣ Persistent Tunnels with autossh&lt;/h3&gt;
&lt;p&gt;To keep a tunnel alive automatically:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;autossh&lt;span class="w"&gt; &lt;/span&gt;-M&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-f&lt;span class="w"&gt; &lt;/span&gt;-N&lt;span class="w"&gt; &lt;/span&gt;-L&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;8080&lt;/span&gt;:localhost:8080&lt;span class="w"&gt; &lt;/span&gt;user@remote-server
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="troubleshooting-debugging"&gt;
&lt;h2&gt;🛠️ Troubleshooting &amp;amp; Debugging&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;strong&gt;Connection hangs&lt;/strong&gt;? Add &lt;cite&gt;-v&lt;/cite&gt; or &lt;cite&gt;-vvv&lt;/cite&gt; to see what SSH is doing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Port not forwarding&lt;/strong&gt;? Make sure &lt;cite&gt;GatewayPorts&lt;/cite&gt; is allowed in sshd config.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Firewall blocking traffic&lt;/strong&gt;? Test with &lt;cite&gt;telnet&lt;/cite&gt;, &lt;cite&gt;nc&lt;/cite&gt;, or &lt;cite&gt;curl&lt;/cite&gt; to confirm.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Service only binds to 127.0.0.1?&lt;/strong&gt; Use &lt;cite&gt;ssh -L&lt;/cite&gt; with explicit hostnames.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="optimizations-alternatives"&gt;
&lt;h2&gt;🔁 Optimizations &amp;amp; Alternatives&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;For longer-term infrastructure, consider &lt;strong&gt;WireGuard&lt;/strong&gt; or &lt;strong&gt;Tailscale&lt;/strong&gt; for persistent tunnels.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;SSH certificates&lt;/strong&gt; to avoid managing multiple authorized keys.&lt;/li&gt;
&lt;li&gt;Run &lt;cite&gt;autossh&lt;/cite&gt; as a systemd or runit service for reliability.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="conclusion-takeaways"&gt;
&lt;h2&gt;✅ Conclusion &amp;amp; Takeaways&lt;/h2&gt;
&lt;p&gt;Using SSH tunnels allowed us to test services against production safely, without deploying code or violating security constraints. This pattern is lightweight, robust, and fits well into environments where:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;VPN is not an option&lt;/li&gt;
&lt;li&gt;Public exposure is forbidden&lt;/li&gt;
&lt;li&gt;Controlled testing against production is required&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="comments-next-steps"&gt;
&lt;h2&gt;💬 Comments &amp;amp; Next Steps&lt;/h2&gt;
&lt;p&gt;Have you used SSH tunnels in your microservices architecture? Share your experience or ask questions below!&lt;/p&gt;
&lt;/div&gt;
</content><category term="Infrastructure Security"></category><category term="ssh"></category><category term="tunnels"></category><category term="secure-access"></category><category term="devops-tools"></category><category term="microservices"></category></entry></feed>