Ok, so i’ve got a setup here to propose, that i’d like some feedback on. I’ve come up with an infrastructure plan of sorts here that I think might be a good idea… but i’m not sure. So here go’s.
My setup for now is simple, and it includes the following:
- Frontend static Next.js application (That is deployed to Workers Sites … ezpz)
- Dgraph instance (GitHub - dgraph-io/dgraph: Native GraphQL Database with graph backend) (Graph database) (Running on DigitalOcean droplet for now… will eventually move to Kubernetes)
- REST API (Also deployed to Cloudflare Workers)
So most of my setup is running on Cloudflare Workers, which is great because I don’t have to manage that part of it. The only thing that I need to manage is my Dgraph instance, which is built with Go (and requires a garbage collector)… so it needs to be run on it’s own server. So let’s get into that a little bit.
My Dgraph server (DigitalOcean droplet) is running Cloudflare’s Cloudflare Tunnel , which makes a secure tunnel to Cloudflare and exposes a URL for my Dgraph instance (https://query.mydomain.com let’s say). To make queries against my Dgraph instance, it exposes port 8080… which is what cloudflared
connects to on the server.
The command to accept TLS connections on port 8080 is: ./dgraph alpha --lru_mb=32784 --tls_dir=tls --acl_secret_file=acl_secret_file --encryption_key_file=enc_key_file
My command for setting up cloudflared
is: cloudflared --origin-ca-pool ~/dgraph/dgraph/tls/ca.crt --origin-server-name localhost --hostname query.mydomain.com https://localhost:8080
I’ve also setup on Cloudflare Access on my query.mydomain.com
URL because i’d like to only allow my Cloudflare Workers API to be able to query the endpoint. I created a service token that I will make requests to.
My API Worker (https://api.mydomain.com) look’s something like this (for now… but will eventually be turned into a REST API):
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
return await fetch('https://query.mydomain.com', {
headers: {
'CF-Access-Client-Id': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'CF-Access-Client-Secret': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
}
})
}
Note: I will obviously not have the Access credentials inlined into the Worker script in the production version. I’ll use secret environment variables. Commands · Cloudflare Workers docs
This is just to test the Access connection to my Dgraph server/query endpoint Essentially what i’m doing here… is making is so only my Worker API can access the Dgraph query endpoint (as I don’t want my users making requests to it)
Ok, I know this was alot to read…and might be confusing, so here’s a TLDR version:
My frontend and REST API are both deployed to Cloudflare Workers. I have a Dgraph instance running on a DigitalOcean Droplet in which i’ve set up a Cloudflare Cloudflare Tunnel . I access this Dgraph instance by the URL https://query.mydomain.com. I’d like to make it so that only my Worker API (https://api.mydomain.com) can make requests to my Dgraph URL. So I setup Cloudflare Access on (https://query.mydomain.com) so that you need to supply service token credentials to do so.
Basically i’m asking if anyone else has tried this setup, and whether people think it’s a viable solution. Does anyone else have any other ideas, or concerns?
Note: It would be awesome to see Cloudflare Access integration into Workers themselves. So we could setup access controls, so we could limit who can access the Worker, etc.