View as .md

Authenticate Cargo to AWS CodeArtifact

Authenticate Cargo to AWS CodeArtifact registries using Vouch

Vouch acts as a Cargo credential provider (opens in new tab) for AWS CodeArtifact Cargo registries. Instead of a plaintext token in ~/.cargo/credentials.toml, Cargo asks Vouch for a token, and Vouch exchanges your hardware-backed session for a short-lived AWS CodeArtifact authorization token through AWS STS. AWS CodeArtifact is the only Cargo registry that Vouch supports.

Known issue: The Cargo credential provider in current Vouch CLI releases fails the Cargo handshake with failed to deserialize hello, so Cargo cannot obtain a token from Vouch for any registry. The fix is tracked in vouch-sh/vouch#1841 (opens in new tab). This page describes the intended setup.

TL;DR

  • Prerequisites: Getting Started → AWS integration → this page.
  • Admin, once: add codeartifact:GetAuthorizationToken, codeartifact:GetRepositoryEndpoint, codeartifact:ReadFromRepository, and sts:GetServiceBearerToken to the Vouch IAM role.
  • Each developer: vouch setup codeartifact --tool cargo --repository <REPO>, then use Cargo with --registry codeartifact-<REPO>.

Prerequisites

Admin task

Before developers can configure Cargo:

  • The AWS integration must be configured (OIDC provider and IAM role)
  • An AWS CodeArtifact domain and Cargo repository must exist in your AWS account
  • The IAM role must have codeartifact:GetAuthorizationToken, codeartifact:GetRepositoryEndpoint, codeartifact:ReadFromRepository, and sts:GetServiceBearerToken permissions

Each developer needs Cargo installed via rustup (opens in new tab) (version 1.74 or later, which includes credential provider protocol support).


Step 1 – Configure the Cargo registry

Developer task

Run the AWS CodeArtifact setup command with --tool cargo:

vouch setup codeartifact --tool cargo --repository my-repo [--domain my-domain] [--domain-owner 123456789012] [--region us-east-1] [--domain-profile my-profile]

The flags are the same as for every other package manager; see AWS CodeArtifact for the full table, including domain profiles and --profile.

The command adds a registry named codeartifact-<repository> to ~/.cargo/config.toml:

[registries.codeartifact-my-repo]
index = "sparse+https://my-domain-123456789012.d.codeartifact.us-east-1.amazonaws.com/cargo/my-repo/"
credential-provider = ["/path/to/vouch", "credential", "cargo", "--"]

Vouch is configured only for this registry. Other registries, including crates.io, keep their existing credential providers.


Step 2 – Use Cargo normally

Developer task

If you are not logged in, run vouch login — one YubiKey tap starts an 8-hour session that every command below uses automatically.

With the registry configured and an active session, Cargo commands work without cargo login or token environment variables:

# Publish a crate to the AWS CodeArtifact registry
cargo publish --registry codeartifact-my-repo

# Add a dependency from the AWS CodeArtifact registry
cargo add my-crate --registry codeartifact-my-repo

To depend on crates from the registry, reference it in Cargo.toml:

[dependencies]
my-crate = { version = "1.0", registry = "codeartifact-my-repo" }

How it works

  1. Cargo requests a token – Cargo calls the Vouch credential provider when it needs to authenticate to the codeartifact-<repository> registry.
  2. OIDC to STS – Vouch exchanges your active hardware-backed session for temporary AWS STS credentials via AssumeRoleWithWebIdentity.
  3. STS to AWS CodeArtifact – Vouch calls codeartifact:GetAuthorizationToken with the STS credentials to obtain an AWS CodeArtifact authorization token.
  4. Cargo authenticates – Vouch returns the token to Cargo along with its expiry. Cargo sends it to AWS CodeArtifact in the Authorization header. The token is never written to disk.

Troubleshooting

Not authenticated

error: failed to get token for registry `codeartifact-my-repo`
  • Verify you have an active Vouch session: vouch login
  • Check that the Vouch agent is running: vouch status
  • Confirm the registry is configured: inspect ~/.cargo/config.toml and verify the [registries.codeartifact-my-repo] section has a credential-provider entry pointing to Vouch.
  • Confirm the IAM role has the permissions listed under Prerequisites.

Cargo not using Vouch

If Cargo prompts you for a token or uses a different credential provider:

  1. Ensure no CARGO_REGISTRY_TOKEN or CARGO_REGISTRIES_*_TOKEN environment variables are set, as these override credential providers:
    env | grep CARGO_REGISTR
    
  2. Re-run vouch setup codeartifact --tool cargo --repository <REPO> to ensure the configuration is correct.

Unsupported protocol version

error: credential provider `vouch` failed: unsupported credential provider protocol version

The Cargo credential provider protocol requires Cargo 1.74 or later. Check your Cargo version:

cargo --version

If your version is older than 1.74, update via rustup:

rustup update stable

For AWS-side errors (access denied, wrong region, cross-account domains), see AWS CodeArtifact troubleshooting.