Skip to content

Commit

Permalink
Auto merge of #12626 - arlosi:cred-error-both, r=weihanglo
Browse files Browse the repository at this point in the history
fix: improve warning for both token & credential-provider

Cargo issues a warning when both a `credential-provider` and a `token` are configured for a registry.

This change removes the warning if the `credential-provider` is `cargo:token` since that *will* use the token. The warning message text is also tweaked to include the name of the `credential-provider` that's overriding the token.
  • Loading branch information
bors committed Sep 6, 2023
2 parents de7537e + 3f004c6 commit d9a4cf1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
27 changes: 16 additions & 11 deletions src/cargo/util/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,26 @@ fn credential_provider(config: &Config, sid: &SourceId) -> CargoResult<Vec<Vec<S
secret_key,
..
}) if config.cli_unstable().credential_process => {
let provider = resolve_credential_alias(config, provider);
if let Some(token) = token {
config.shell().warn(format!(
"{sid} has a token configured in {} that will be ignored \
because a credential-provider is configured for this registry`",
token.definition
))?;
if provider[0] != "cargo:token" {
config.shell().warn(format!(
"{sid} has a token configured in {} that will be ignored \
because this registry is configured to use credential-provider `{}`",
token.definition, provider[0],
))?;
}
}
if let Some(secret_key) = secret_key {
config.shell().warn(format!(
"{sid} has a secret-key configured in {} that will be ignored \
because a credential-provider is configured for this registry`",
secret_key.definition
))?;
if provider[0] != "cargo:paseto" {
config.shell().warn(format!(
"{sid} has a secret-key configured in {} that will be ignored \
because this registry is configured to use credential-provider `{}`",
secret_key.definition, provider[0],
))?;
}
}
vec![resolve_credential_alias(config, provider)]
vec![provider]
}

// Warning for both `token` and `secret-key`, stating which will be ignored
Expand Down
22 changes: 20 additions & 2 deletions tests/testsuite/credential_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,31 @@ fn multiple_providers() {

#[cargo_test]
fn both_token_and_provider() {
let server = registry::RegistryBuilder::new()
.credential_provider(&["cargo:paseto"])
.build();

cargo_process("login -Z credential-process -Z asymmetric-token")
.masquerade_as_nightly_cargo(&["credential-process", "asymmetric-token"])
.replace_crates_io(server.index_url())
.with_stderr(
r#"[UPDATING] [..]
[WARNING] registry `crates-io` has a token configured in [..] that will be ignored because this registry is configured to use credential-provider `cargo:paseto`
k3.public[..]
"#,
)
.run();
}

#[cargo_test]
fn registry_provider_overrides_global() {
let server = registry::RegistryBuilder::new().build();
cargo_util::paths::append(
&paths::home().join(".cargo/config"),
format!(
r#"
[registry]
credential-provider = ["cargo:token"]
global-credential-providers = ["should-not-be-called"]
"#,
)
.as_bytes(),
Expand All @@ -462,10 +480,10 @@ fn both_token_and_provider() {

cargo_process("login -Z credential-process -v abcdefg")
.masquerade_as_nightly_cargo(&["credential-process"])
.env("CARGO_REGISTRY_CREDENTIAL_PROVIDER", "cargo:token")
.replace_crates_io(server.index_url())
.with_stderr(
r#"[UPDATING] [..]
[WARNING] registry `crates-io` has a token configured in [..]credentials.toml that will be ignored because a credential-provider is configured for this registry`
[CREDENTIAL] cargo:token login crates-io
[LOGIN] token for `crates-io` saved
"#,
Expand Down

0 comments on commit d9a4cf1

Please sign in to comment.