In this blog post, we’re going to talk about how we use Cloudflare R2 as an apt/yum repository to bring cloudflared (the Cloudflare Tunnel daemon) to your Debian/Ubuntu and CentOS/RHEL systems and how you can do it for your own distributable in a few easy steps!
I work on Cloudflare Tunnel, a product which enables customers to quickly connect their private networks and services through the Cloudflare global network without needing to expose any public IPs or ports through their firewall. Cloudflare Tunnel is managed for users by cloudflared, a tool that runs on the same network as the private services. It proxies traffic for these services via Cloudflare, and users can then access these services securely through the Cloudflare network.
Our connector, cloudflared, was designed to be lightweight and flexible enough to be effectively deployed on a Raspberry Pi, a router, your laptop, or a server running on a data center with applications ranging from IoT control to private networking. Naturally, this means cloudflared comes built for a myriad of operating systems, architectures and package distributions: You could download the appropriate package from our GitHub releases, brew install it or apt/yum install it (https://pkg.cloudflare.com).
In the rest of this blog post, I’ll use cloudflared as an example of how to create an apt/yum repository backed by Cloudflare’s object storage service R2. Note that this can be any binary/distributable. I simply use cloudflared as an example because this is something we recently did and actually use in production.
How apt-get works
Let’s see what happens when you run something like this on your terminal.
$ apt-get install cloudflared
Let’s also assume that apt sources were already added like so:
$ echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared buster main' | sudo tee /etc/apt/sources.list.d/cloudflared.list
$ apt-get update
From the source.list above, apt first looks up the Release file (or InRelease if it’s a signed package like cloudflared, but we will ignore this for the sake of simplicity).
A Release file contains a list of supported architectures and their md5, sha1 and sha256 checksums. It looks something like this:
$ curl https://pkg.cloudflare.com/cloudflared/dists/buster/Release
Origin: cloudflared
Label: cloudflared
Codename: buster
Date: Thu, 11 Aug 2022 08:40:18 UTC
Architectures: amd64 386 arm64 arm armhf
Components: main
Description: apt repository for cloudflared - buster
MD5Sum:
c14a4a1cbe9437d6575ae788008a1ef4 549 main/binary-amd64/Packages
6165bff172dd91fa658ca17a9556f3c8 374 main/binary-amd64/Packages.gz
9cd622402eabed0b1b83f086976a8e01 128 main/binary-amd64/Release
5d2929c46648ea8dbeb1c5f695d2ef6b 545 main/binary-386/Packages
7419d40e4c22feb19937dce49b0b5a3d 371 main/binary-386/Packages.gz
1770db5634dddaea0a5fedb4b078e7ef 126 main/binary-386/Release
b0f5ccfe3c3acee38ba058d9d78a8f5f 549 main/binary-arm64/Packages
48ba719b3b7127de21807f0dfc02cc19 376 main/binary-arm64/Packages.gz
4f95fe1d9afd0124a32923ddb9187104 128 main/binary-arm64/Release
8c50559a267962a7da631f000afc6e20 545 main/binary-arm/Packages
4baed71e49ae3a5d895822837bead606 372 main/binary-arm/Packages.gz
e472c3517a0091b30ab27926587c92f9 126 main/binary-arm/Release
bb6d18be81e52e57bc3b729cbc80c1b5 549 main/binary-armhf/Packages
31fd71fec8acc969a6128ac1489bd8ee 371 main/binary-armhf/Packages.gz
8fbe2ff17eb40eacd64a82c46114d9e4 128 main/binary-armhf/Release
SHA1:
…
SHA256:
…
Depending on your system’s architecture, Debian picks the appropriate Packages location. A Packages file contains metadata about the binary apt wants to install, including location and its checksum. Let’s say it’s an amd64 machine. This means we’ll go here next:
$ curl https://pkg.cloudflare.com/cloudflared/dists/buster/main/binary-amd64/Packages
Package: cloudflared
Version: 2022.8.0
License: Apache License Version 2.0
Vendor: Cloudflare
Architecture: amd64
Maintainer: Cloudflare <support@cloudflare.com>
Installed-Size: 30736
Homepage: https://github.com/cloudflare/cloudflared
Priority: extra
Section: default
Filename: pool/main/c/cloudflared/cloudflared_2022.8.0_amd64.deb
Size: 15286808
SHA256: c47ca10a3c60ccbc34aa5750ad49f9207f855032eb1034a4de2d26916258ccc3
SHA1: 1655dd22fb069b8438b88b24cb2a80d03e31baea
MD5sum: 3aca53ccf2f9b2f584f066080557c01e
Description: Cloudflare Tunnel daemon
Notice the Filename field. This is where apt gets the deb from before running a dpkg command on it. What all of this means is the apt repository (and the yum repositories too) is basically a structured file system of mostly plaintext files and a deb.
The deb file is a Debian software package that contains two things: installable data (in our case, the cloudflared binary) and metadata about the installable.
Building our own apt repository
Now that we know what happens when an apt-get install runs, let’s work our way backwards to construct the repository.
Create a deb file out of the binary
Note: It is optional but recommended one signs the packages. See the section about how apt verifies packages here.
Debian files can be built by the dpkg-buildpackage tool in a linux or Debian environment. We use a handy command line tool called fpm (https://fpm.readthedocs.io/en/v1.13.1/) to do this because it works for both rpm and deb.
$ fpm -s <dir> -t deb -C /path/to/project -name <project_name> –version <version>
This yields a .deb file.
Create plaintext files needed by apt to lookup downloads
Again, these files can be built by hand, but there are multiple tools available to generate this:
We use reprepro.
Using it is as simple as
$ reprepro buster includedeb <path/to/the/deb>
reprepro neatly creates a bunch of folders in the structure we looked into above.
Upload them to Cloudflare R2
We use Cloudflare R2 to now be the host for this structured file system. R2 lets us upload and serve objects in this structured format. This means, we just need to upload the files in the same structure reprepro created them.
Here is a copyable example of how we do it for cloudflared.
Serve them from an R2 worker
For fine-grained control, we’ll write a very lightweight Cloudflare Worker to be the service we talk to and serve as the front-end API for apt to talk to. For an apt repository, we only need it to perform the GET function.
Here’s an example on how-to do this: https://developers.cloudflare.com/r2/examples/demo-worker/
Putting it all together
Here is a handy script we use to push cloudflared to R2 ready for apt/yum downloads and includes signing and publishing the pubkey.
And that’s it! You now have your own apt/yum repo without needing a server that needs to be maintained, there are no egress fees for downloads, and it is on the Cloudflare global network, protecting it against high request volumes. You can automate many of these steps to make it part of a release process.
Today, this is how cloudflared is distributed on the apt and yum repositories: https://pkg.cloudflare.com/