Connect To Private Services With WireGuard
Most apps running on Fly will expose some kind of service, usually (but not always) HTTP, to the public Internet. That’s kind of what we’re all about. But did you know you can also expose private services to your developers and ops teams? It’s easy if you try.
We’re going to walk through connecting your organizations — all the applications you’re running — with WireGuard, and how to expose a service only WireGuard can talk to. This is a great way to set up admin interfaces and ops services, giving you a cryptographically secured network path.
Let’s assume for starters that you’re using a MacBook. Later, we’ll talk about how to get this working on Linux (it’s actually easier).
Make Sure You’ve Got A Recent flyctl
Now’s a good time to run flyctl version update
. What you want is a flyctl
that has the flyctl wireguard
command.
Make Sure You’re Logged In
I know, we’re asking if it’s plugged in. But: is it plugged in? Try flyctl apps list
. If you can see your app, you should be able to set up WireGuard.
Install WireGuard
It’s in the app store. Jason Donenfeld, the author of WireGuard and the hardest working person in show business, manages the project himself. It’s great.
When it’s installed, the goofy WireGuard snake will appear in your menu bar. That’s how you know it’s working. We’re moving on.
Generate A WireGuard Configuration
This sounds intimidating, but we’re going to do all the work for you.
Do flyctl wireguard create
. You’ll see something like:
> $ flyctl wireguard create
? Select organization: [Use arrows to move, type to filter]
> Thomas Ptacek (personal)
I’m picking my personal organization, but you should pick the organization where all your apps are.
Now flyctl
has a question for us:
? Select organization: Thomas Ptacek (personal)
? Region in which to add WireGuard peer: ord
Fly runs WireGuard endpoints in a bunch of different places around the world, so you can pick where you connect to our network. If you don’t care, ord
(Chicago), iad
(Dallas) and fra
(Frankfort) are good bets.
One last question:
> $ flyctl wireguard create
? Select organization: Thomas Ptacek (personal)
? Region in which to add WireGuard peer: ord
? New DNS name for WireGuard peer: my-new-network
Give your WireGuard connection a name. We’ll link it to the DNS so your apps can find you (more on that later). This isn’t a life-or-death decision; you can quickly delete and recreate a WireGuard connection if you want to change this, or create a bunch of different ones. Go nuts!
Now, something happens:
> $ flyctl wireguard create
? Select organization: Thomas Ptacek (personal)
? Region in which to add WireGuard peer: ord
? New DNS name for WireGuard peer: my-new-net
Creating WireGuard peer "my-new-net" in region "ord" for organization personal
!!!! WARNING: Output includes private key. Private keys cannot be recovered !!!!
!!!! after creating the peer; if you lose the key, you'll need to remove !!!!
!!!! and re-add the peering connection. !!!!
? Filename to store WireGuard configuration in, or 'stdout':
This is much less scary than it looks; the thing to know here is that if you lose this configuration, we can’t regenerate it for you (we generate your keys clientside). Which isn’t the end of the world! You can delete the connection and make a new one.
Add The Configuration To macOS WireGuard
Click the weird looking snake in your menu bar. Click “Import Tunnels From File”. Import your tunnel, from file.
Connect
The new tunnel should appear in a list in the WireGuard menu drop-down. Click it to connect. It should connect quickly!
You now have connectivity to your organization.
If you look at your new WireGuard configuration, you’ll see it includes a DNS server address. You can use that DNS server to look up names for your applications running on Fly; they’ll all have hostnames following the pattern {my-app}.internal
. You can target specific regions with… you guessed it… {region}.{my-app}.internal
.
All of these hosts will have IPv6 addresses — welcome to the world of tomorrow! — that follow a pattern starting in fdaa::*
. As long as your WireGuard connection is up and running, you should be able to talk to any of these addresses.
Expose A Service On Your App To WireGuard
Each instance of your app running on Fly has a special alias address on that IPv6 fdaa::*
network we just talked about. We record it in /etc/hosts
as fly-local-6pn
.
So the trick to exposing a service to WireGuard, and keeping it off the Internet, is to bind/listen on that fly-local-6pn
service. So for instance, if you have an admin console running on port 8443, you’d want to bind it to fly-local-6pn:8443
. How you do this will depend on your development environment.
Getting This To Work On Linux
Modern Linux has WireGuard built it. Modern Linux is great! If you’re running earlier WireGuard, you can install it; for instance, on Ubuntu, you can apt install -y wireguard
.
Once you’ve got WireGuard installed, just repeat the steps above, and save your WireGuard configuration in a file (say, newconfig.conf
). Then it should just be wg-quick up newconfig.conf
to connect.
We told you it was easier!