You can build Zero Trust rules to secure connections to MongoDB deployments using Cloudflare Access and Cloudflare Tunnel. Cloudflare Tunnel requires a lightweight daemon,
cloudflared, running alongisde the deployment and as on the client side.
In this tutorial, a client running
cloudflared connects over SSH to a MongoDB deployment running on Kubernetes. The deployment example is structured to connect to the MongoDB instance. The MongoDB Kubernetes deployment runs both the MongoDB database service and
cloudflared as a ingress service that operates like a jump host.
🗺️ This tutorial covers how to:
- Create a Cloudflare Access rule to secure a MongoDB deployment
- Configure a StatefulSet and service definition for the deployment
- Configure an Cloudflare Tunnel connection to Cloudflare’s edge
- Create an SSH configuration file for the client
⏲️ Time to complete:
Configure Cloudflare Access
You can build a rule in Cloudflare Access to control who can connect to your MongoDB deployment. Cloudflare Access rules are built around a hostname; even though this deployment will be accessible over SSH, the resource will be represented in Cloudflare as a hostname. For example, if you have the website
app.com in your Cloudflare account, you can build a rule to secure
Navigate to the
Applicationspage in the
Accesssection of the Zero Trust dashboard.
Click Add an application and choose
Create an application for a subdomain where users will connect to your deployment. Click Next.
Build a rule to determine who can reach the deployment. You can build a rule that allows anyone in your organization to connect or you can build more granular rules based on signals like identity provider groups, , or .
Click Next again and add the application.
Configure the Kubernetes deployment
To be accessible over SSH, the Kubernetes deployment should manage both the MongoDB standalone service and an SSH proxy service. The configuration below will deploy 1 replica of the database service, available at port 27017, as well as an SSH proxy available at port 22.
The corresponding service definition should also specify the the ports and target ports for the containers (in this case, the database service and the SSH proxy service).
The MongoDB pod and the SSH jump host will share a Unix socket over an empty directory volume. The
entrypoint.sh file run by the jump host, example below, will start an OpenSSH server.
#!/bin/shexport TZ=America/Chicagoln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezoneapt-get update -y && apt-get install -y openssh-servermkdir /root/.sshcp /config/ssh/authorized_keys /root/.ssh/authorized_keyschmod 400 /root/.ssh/authorized_keysservice ssh startwhile true;do sleep 30;done;
Configure Cloudflare Tunnel
Once installed, run the following command to authenticate the instance of
cloudflared into your Cloudflare account.
$ cloudflared login
The command will launch a browser window and prompt you to login with your Cloudflare account. Choose a website that you have added into your account.
Once you click one of the sites in your account, Cloudflare will download a certificate file, called
cert.pem to authenticate this instance of
cert.pem file uses a certificate to authenticate your instance of
cloudflared and includes an API key for your account to perform actions like DNS record changes.
You can now use
cloudflared to control Cloudflare Tunnel connections in your Cloudflare account.
Create a Tunnel
Run the following command to create a Tunnel. You can replace
mongodb with any name that you choose. This command requires the
$ cloudflared tunnel create mongodb
Cloudflare will create the Tunnel with that name and generate an ID and credentials file for that Tunnel.
The credentials file is separate from the
cert.pem file. Unlike the
cert.pem file, the credentials file consists of a token that authenticates only the Named Tunnel you just created. Formatted as
JSON, the file cannot make changes to your Cloudflare account or create additional Tunnels.
If you are done creating Tunnels, you can delete the
cert.pem file, leave only the credentials file, and continue to manage DNS records directly in the Cloudflare dashboard or API. For additional information on the different functions of the two files, refer to the list of .
JSON file as a Kubernetes secret.
Configure Cloudflare Tunnel
The previous setps used
cloudflared to generate a credentials file for your Cloudflare account. When run as a service alongside the MongoDB Kubernetes deployment you will need to use a Docker image of
cloudflared. Cloudflare makes an in DockerHub.
The configuration below will run a single replica of
cloudflared as an ingress point alongside the MongoDB and SSH proxy services.
cloudflared will proxy traffic to the SSH proxy service. The
cloudflared instance will run as its own deployment in a different namespace and, if network policy allows, ingress to any service in the Kubernetes node.
Connect from a client
Once deployed, you can run
cloudflared on the client side to connect to the MongoDB deployment. Add the following lines to your SSH configuration file, replacing the examples with your hostname and details. The
--destination value should match the URL of the SSH Proxy service configured previously.
Host mongodbProxyCommand /usr/local/bin/cloudflared access ssh --hostname mongodb.widgetcorp.tech --destination ssh-proxy.mongodb.svc.cluster.local:22LocalForward 27000 /socket/mongodb-27017.sockUser rootIdentityFile /Users/username/.ssh/id_rsa
This is a one-time step. When you next attempt to make an SSH connection to the deployment,
cloudflared will launch a browser window and prompt you to authenticate. Once authenticated, you will be connected if you have a valid session. Once the tunnel is established, all requests to
localhost:27000 on your machine will be forwarded to
/socket/mongodb-27017.sock on the SSH proxy container.
You can then set MongoDB Compass to connect to