Skip to content

Commit 711e323

Browse files
committed
Auto merge of #11661 - arlosi:no-error-for-auth-required, r=Eh2406
Do not error for `auth-required: true` without `-Z sparse-registry` Registries that include `auth-required: true` in their `config.json` currently hit the following error on stable: ``` authenticated registries require `-Z registry-auth` ``` This situation makes it difficult for a registry to optionally offer the `auth-required: true` feature, since it forces users on to the nightly toolchain. This PR changes the behavior to ignore the `auth-required: true` field of `config.json` without `-Z registry-auth`. The downside to this change is that it makes it harder to discover why a registry isn't working, since the user will get an HTTP 401 error from the server, rather than a message from Cargo suggesting adding `-Z registry-auth`. r? `@Eh2406`
2 parents 4ae3576 + f1dadb6 commit 711e323

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

src/cargo/sources/registry/http_remote.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,6 @@ impl<'cfg> HttpRegistry<'cfg> {
334334
}
335335
}
336336

337-
fn check_registry_auth_unstable(&self) -> CargoResult<()> {
338-
if self.auth_required && !self.config.cli_unstable().registry_auth {
339-
anyhow::bail!("authenticated registries require `-Z registry-auth`");
340-
}
341-
Ok(())
342-
}
343-
344337
/// Get the cached registry configuration, if it exists.
345338
fn config_cached(&mut self) -> CargoResult<Option<&RegistryConfig>> {
346339
if self.registry_config.is_some() {
@@ -486,7 +479,9 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
486479
return Poll::Ready(Ok(LoadResponse::NotFound));
487480
}
488481
StatusCode::Unauthorized
489-
if !self.auth_required && path == Path::new("config.json") =>
482+
if !self.auth_required
483+
&& path == Path::new("config.json")
484+
&& self.config.cli_unstable().registry_auth =>
490485
{
491486
debug!("re-attempting request for config.json with authorization included.");
492487
self.fresh.remove(path);
@@ -542,6 +537,10 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
542537
}
543538
}
544539

540+
if !self.config.cli_unstable().registry_auth {
541+
self.auth_required = false;
542+
}
543+
545544
// Looks like we're going to have to do a network request.
546545
self.start_fetch()?;
547546

@@ -587,7 +586,6 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
587586
}
588587
}
589588
if self.auth_required {
590-
self.check_registry_auth_unstable()?;
591589
let authorization =
592590
auth::auth_token(self.config, &self.source_id, self.login_url.as_ref(), None)?;
593591
headers.append(&format!("Authorization: {}", authorization))?;
@@ -660,8 +658,10 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
660658
}
661659

662660
fn config(&mut self) -> Poll<CargoResult<Option<RegistryConfig>>> {
663-
let cfg = ready!(self.config()?).clone();
664-
self.check_registry_auth_unstable()?;
661+
let mut cfg = ready!(self.config()?).clone();
662+
if !self.config.cli_unstable().registry_auth {
663+
cfg.auth_required = false;
664+
}
665665
Poll::Ready(Ok(Some(cfg)))
666666
}
667667

src/cargo/sources/registry/remote.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,9 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
245245
match ready!(self.load(Path::new(""), Path::new("config.json"), None)?) {
246246
LoadResponse::Data { raw_data, .. } => {
247247
trace!("config loaded");
248-
let cfg: RegistryConfig = serde_json::from_slice(&raw_data)?;
249-
if cfg.auth_required && !self.config.cli_unstable().registry_auth {
250-
return Poll::Ready(Err(anyhow::anyhow!(
251-
"authenticated registries require `-Z registry-auth`"
252-
)));
248+
let mut cfg: RegistryConfig = serde_json::from_slice(&raw_data)?;
249+
if !self.config.cli_unstable().registry_auth {
250+
cfg.auth_required = false;
253251
}
254252
Poll::Ready(Ok(Some(cfg)))
255253
}

tests/testsuite/registry_auth.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,26 @@ static SUCCESS_OUTPUT: &'static str = "\
4242

4343
#[cargo_test]
4444
fn requires_nightly() {
45-
let _registry = RegistryBuilder::new().alternative().auth_required().build();
45+
let _registry = RegistryBuilder::new()
46+
.alternative()
47+
.auth_required()
48+
.http_api()
49+
.build();
4650

4751
let p = make_project();
4852
p.cargo("build")
4953
.with_status(101)
50-
.with_stderr_contains(" authenticated registries require `-Z registry-auth`")
54+
.with_stderr(
55+
r#"[UPDATING] `alternative` index
56+
[DOWNLOADING] crates ...
57+
error: failed to download from `[..]/dl/bar/0.0.1/download`
58+
59+
Caused by:
60+
failed to get successful HTTP response from `[..]`, got 401
61+
body:
62+
Unauthorized message from server.
63+
"#,
64+
)
5165
.run();
5266
}
5367

0 commit comments

Comments
 (0)