Configure Cargo to use the Varnish Virtual Registry

This tutorial points cargo at a Virtual Registry that caches crates.io, and then puts the Artifact Firewall in front of the crates.

Prerequisites

  • Rust and cargo are installed.
  • The Virtual Registry is installed.

Configure the registry

virtual_registry:
  registries:
  - name: cargo
    remotes:
    - url: https://index.crates.io

The examples below reach the registry as https://cargo.example.com, a host that the registry named cargo answers (see name).

The remote is the sparse index. Crates download from another host, static.crates.io, and the registry rewrites the download URL in the index’s config.json so that they come through it too. With deny_unknown_hosts set, that host has to be listed:

virtual_registry:
  redirects:
    deny_unknown_hosts: true
    extra_known_hosts:
    - static.crates.io

Package types describes how the index and the crates are cached.

Point cargo at the registry

Replace crates.io with the registry in $CARGO_HOME/config.toml, or in .cargo/config.toml inside a project:

[source.crates-io]
replace-with = "virtual-registry"

[source.virtual-registry]
registry = "sparse+https://cargo.example.com/"

CARGO_HOME is ~/.cargo unless it is set, and the rust container image sets it to /usr/local/cargo. Keep the sparse+ prefix. Without it, cargo reads the URL as a Git index.

Builds now resolve and download through the registry:

cargo fetch

Enable the Artifact Firewall

The firewall is licensed separately, see enable_firewall.

virtual_registry:
  registries:
  - name: cargo
    enable_firewall: true
    remotes:
    - url: https://index.crates.io

firewall:
  rulesets:
  - path: /etc/varnish-supervisor/rulesets

A rule names a crate as pkg:cargo/<crate>, in the casing it was published with. This ruleset, in /etc/varnish-supervisor/rulesets/cargo.yaml, denies every version of failure and holds rand below 0.9:

id: cargo-policy
rules:
- id: deny-failure
  action: deny
  reason: failure is unmaintained
  match:
  - purl: pkg:cargo/failure
- id: hold-rand-09
  action: hide
  reason: rand 0.9 changes the API
  match:
  - purl: pkg:cargo/rand
    version: vers:cargo/>=0.9.0

Restart the Supervisor. A reload that turns on enable_firewall is refused, since only a restart starts the firewall:

sudo systemctl restart varnish-supervisor

The firewall marks each denied or hidden version as yanked in the index, so cargo resolves rand = "0.9" to nothing and rand = "*" to the newest 0.8 release. A crate that Cargo.lock already pins to a hidden version still downloads. One pinned to a denied version is refused with a 403 that names the rule, and a crate denied as a whole, like failure, fails before cargo chooses a version. Each decision is written to the audit log.

The firewall checks a download only when its URL names the crate and the version, as crates.io’s, Artifactory’s and, from 0.24.0, Nexus’s do. Cargo enforcement describes what each action and the quarantine window do.


®Varnish Software, Wallingatan 12, 111 60 Stockholm, Organization nr. 556805-6203