Skip to content

Commit 6d7531a

Browse files
committed
Add error message for when no credential providers are available
1 parent b28be28 commit 6d7531a

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

Diff for: src/cargo/util/auth/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ fn credential_provider(config: &Config, sid: &SourceId) -> CargoResult<Vec<Vec<S
9999
.collect()
100100
})
101101
.unwrap_or_else(default_providers);
102+
tracing::debug!(?global_providers);
102103

103104
let providers = match cfg {
104105
// If there's a specific provider configured for this registry, use it.
@@ -442,6 +443,7 @@ fn credential_action(
442443
headers,
443444
};
444445
let providers = credential_provider(config, sid)?;
446+
let mut any_not_found = false;
445447
for provider in providers {
446448
let args: Vec<&str> = provider
447449
.iter()
@@ -471,8 +473,8 @@ fn credential_action(
471473
})?;
472474
match provider.perform(&registry, &action, &args[1..]) {
473475
Ok(response) => return Ok(response),
474-
Err(cargo_credential::Error::UrlNotSupported)
475-
| Err(cargo_credential::Error::NotFound) => {}
476+
Err(cargo_credential::Error::UrlNotSupported) => {}
477+
Err(cargo_credential::Error::NotFound) => any_not_found = true,
476478
e => {
477479
return e.with_context(|| {
478480
format!(
@@ -483,7 +485,11 @@ fn credential_action(
483485
}
484486
}
485487
}
486-
Err(cargo_credential::Error::NotFound.into())
488+
if any_not_found {
489+
Err(cargo_credential::Error::NotFound.into())
490+
} else {
491+
anyhow::bail!("no credential providers could handle the request")
492+
}
487493
}
488494

489495
/// Returns the token to use for the given registry.

Diff for: src/doc/src/reference/unstable.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,14 @@ specified in the `registries` table:
10681068

10691069
```toml
10701070
[registries.my-registry]
1071-
global-credential-provider = "/usr/bin/cargo-creds"
1071+
credential-provider = "/usr/bin/cargo-creds"
1072+
```
1073+
1074+
The credential provider for crates.io can be specified as:
1075+
1076+
```toml
1077+
[registry]
1078+
credential-provider = "/usr/bin/cargo-creds"
10721079
```
10731080

10741081
The value can be a string with spaces separating arguments or it can be a TOML

Diff for: tests/testsuite/credential_process.rs

+80
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,86 @@ fn build_provider(name: &str, response: &str) -> String {
320320
toml_bin(&cred_proj, name)
321321
}
322322

323+
#[cargo_test]
324+
fn all_not_found() {
325+
let server = registry::RegistryBuilder::new()
326+
.no_configure_token()
327+
.auth_required()
328+
.http_index()
329+
.build();
330+
let not_found = build_provider("not_found", r#"{"Err": {"kind": "not-found"}}"#);
331+
cargo_util::paths::append(
332+
&paths::home().join(".cargo/config"),
333+
format!(
334+
r#"
335+
[registry]
336+
global-credential-providers = ["not_found"]
337+
[credential-alias]
338+
not_found = ["{not_found}"]
339+
"#,
340+
)
341+
.as_bytes(),
342+
)
343+
.unwrap();
344+
345+
cargo_process("install -v foo -Zcredential-process -Zregistry-auth")
346+
.masquerade_as_nightly_cargo(&["credential-process", "registry-auth"])
347+
.replace_crates_io(server.index_url())
348+
.with_status(101)
349+
.with_stderr(
350+
r#"[UPDATING] [..]
351+
[CREDENTIAL] [..]not_found[..] get crates-io
352+
{"v":1,"registry":{"index-url":"[..]","name":"crates-io","headers":[[..]"WWW-Authenticate: Cargo login_url=\"https://test-registry-login/me\""[..]]},"kind":"get","operation":"read","args":[]}
353+
[ERROR] failed to query replaced source registry `crates-io`
354+
355+
Caused by:
356+
no token found, please run `cargo login`
357+
or use environment variable CARGO_REGISTRY_TOKEN
358+
"#,
359+
)
360+
.run();
361+
}
362+
363+
#[cargo_test]
364+
fn all_not_supported() {
365+
let server = registry::RegistryBuilder::new()
366+
.no_configure_token()
367+
.auth_required()
368+
.http_index()
369+
.build();
370+
let not_supported =
371+
build_provider("not_supported", r#"{"Err": {"kind": "url-not-supported"}}"#);
372+
cargo_util::paths::append(
373+
&paths::home().join(".cargo/config"),
374+
format!(
375+
r#"
376+
[registry]
377+
global-credential-providers = ["not_supported"]
378+
[credential-alias]
379+
not_supported = ["{not_supported}"]
380+
"#,
381+
)
382+
.as_bytes(),
383+
)
384+
.unwrap();
385+
386+
cargo_process("install -v foo -Zcredential-process -Zregistry-auth")
387+
.masquerade_as_nightly_cargo(&["credential-process", "registry-auth"])
388+
.replace_crates_io(server.index_url())
389+
.with_status(101)
390+
.with_stderr(
391+
r#"[UPDATING] [..]
392+
[CREDENTIAL] [..]not_supported[..] get crates-io
393+
{"v":1,"registry":{"index-url":"[..]","name":"crates-io","headers":[[..]"WWW-Authenticate: Cargo login_url=\"https://test-registry-login/me\""[..]]},"kind":"get","operation":"read","args":[]}
394+
[ERROR] failed to query replaced source registry `crates-io`
395+
396+
Caused by:
397+
no credential providers could handle the request
398+
"#,
399+
)
400+
.run();
401+
}
402+
323403
#[cargo_test]
324404
fn multiple_providers() {
325405
let server = registry::RegistryBuilder::new()

0 commit comments

Comments
 (0)