Skip to content

Additional Ways to Pass Registry Auth Token to Cargo at Runtime #11136

Open
@jonathanstrong

Description

@jonathanstrong

Problem

While testing out the RFC 3139 implementation for authenticated crate downloads (PR), I came across a small sticking point: it is currently (relatively) difficult to pass a registry auth token to cargo at runtime for commands other than publish.

As you may know, cargo publish provides a --token option for passing an auth token at runtime. Previously, this was the only subcommand where that made sense. Now that we are introducing authenticated crate downloads, the existing methods of providing an auth token at runtime for other commands could use improvement.

cargo login and credentials stored on local file system works great for terminal usage by a person, including for non-publish subcommands. But for containers, CI, sandboxed build environments, etc. there are often situations where you don't want to leave an auth token stored in a local file, which presents the need to pass the token to cargo at runtime.

My interest in this is, for example, for the purpose of auto-generating cargo doc output for crate versions published at a private registry that requires authentication, and wanting to avoid storing the registry auth token in a file (i.e. ~/.cargo/credentials) in the build environment.

The only option I have found for passing the token for a private registry for cargo fetch, etc. is the CARGO_REGISTRIES_<name>_TOKEN env var, which I was able to get to work.

However, it took some effort to discover how to format the <name> portion of the env var. In my case, I was testing using a private registry configured under the name "shipyard-rs". The hyphen/dash in the name made things difficult. My first attempt was CARGO_REGISTRIES_shipyard-rs_TOKEN=xxx and that did not work, as variable names with dashes don't work in shell. I tried using the env program (env "CARGO_REGISTRIES_shipyard-rs_TOKEN=xxx" cargo ... before realizing that formatting the registry in screaming snake case would be converted and matched (i.e. CARGO_REGISTRIES_SHIPYARD_RS_TOKEN=xxx works).

I may have missed where it is, but when I was trying this out I didn't find any documentation about the formatting of the <name> portion of the env var.

For cargo fetch et al., there is no --token command line option, so that can't be used.

I was also unable to use --config to work around this, i.e.:

$ cargo fetch -Z registry-auth --config 'registries.shipyard-rs.token=xxx' 

failed with error: registries.shipyard-rs.token cannot be set through --config for security reasons (is there some other way to pass this which would not trigger this error?)

To be clear, this situation was never a problem when publish was the only subcommand that needed auth tokens. But now that we are moving forward with fully authenticated cargo, I think the status quo on passing a token at runtime is not at the high level I would expect from Cargo.

Proposed Solution

  • provide a --token flag/option for subcommands other than publish
  • allow using --config for settingregistries.<name>.token
  • improve documentation about the CARGO_REGISTRIES_<name>_TOKEN to avoid possible confusion

Notes

This issue was initially raised in the issue for the RFC implementation PR, but @Eh2406 suggested creating a separate issue on the matter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-registriesArea: registriesA-registry-authenticationArea: registry authentication and authorization (authn authz)C-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`S-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions