Skip to content

Commit 395edf2

Browse files
committed
Change behavior when both token and credential-process is specified.
Change it so that if both are specified, it is an error just to be safer for now. If token is specified for a registry, ignore the global credential-process. I'm still uncertain if this is the best behavior, but I think we can tweak it later if needed.
1 parent 4b12011 commit 395edf2

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

src/cargo/ops/registry.rs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -354,52 +354,55 @@ pub fn registry_configuration(
354354
config: &Config,
355355
registry: Option<&str>,
356356
) -> CargoResult<RegistryConfig> {
357+
let err_both = |token_key: &str, proc_key: &str| {
358+
Err(format_err!(
359+
"both `{TOKEN_KEY}` and `{PROC_KEY}` \
360+
were specified in the config\n\
361+
Only one of these values may be set, remove one or the other to proceed.",
362+
TOKEN_KEY = token_key,
363+
PROC_KEY = proc_key,
364+
))
365+
};
357366
// `registry.default` is handled in command-line parsing.
358-
let (index, token, process, token_key, proc_key) = match registry {
367+
let (index, token, process) = match registry {
359368
Some(registry) => {
360369
validate_package_name(&registry, "registry name", "")?;
361370
let index = Some(config.get_registry_index(&registry)?.to_string());
362371
let token_key = format!("registries.{}.token", registry);
363372
let token = config.get_string(&token_key)?.map(|p| p.val);
364-
let mut proc_key = format!("registries.{}.credential-process", registry);
365-
let mut process = config.get::<Option<config::PathAndArgs>>(&proc_key)?;
366-
if process.is_none() {
367-
proc_key = String::from("registry.credential-process");
368-
process = config.get::<Option<config::PathAndArgs>>(&proc_key)?;
369-
}
370-
(index, token, process, token_key, proc_key)
373+
let process = if config.cli_unstable().credential_process {
374+
let mut proc_key = format!("registries.{}.credential-process", registry);
375+
let mut process = config.get::<Option<config::PathAndArgs>>(&proc_key)?;
376+
if process.is_none() && token.is_none() {
377+
// This explicitly ignores the global credential-process if
378+
// the token is set, as that is "more specific".
379+
proc_key = String::from("registry.credential-process");
380+
process = config.get::<Option<config::PathAndArgs>>(&proc_key)?;
381+
} else if process.is_some() && token.is_some() {
382+
return err_both(&token_key, &proc_key);
383+
}
384+
process
385+
} else {
386+
None
387+
};
388+
(index, token, process)
371389
}
372390
None => {
373391
// Use crates.io default.
374392
config.check_registry_index_not_set()?;
375393
let token = config.get_string("registry.token")?.map(|p| p.val);
376-
let process =
377-
config.get::<Option<config::PathAndArgs>>("registry.credential-process")?;
378-
(
379-
None,
380-
token,
381-
process,
382-
String::from("registry.token"),
383-
String::from("registry.credential-process"),
384-
)
385-
}
386-
};
387-
388-
let process = if config.cli_unstable().credential_process {
389-
if token.is_some() && process.is_some() {
390-
config.shell().warn(format!(
391-
"both `{TOKEN_KEY}` and `{PROC_KEY}` \
392-
were specified in the config, only `{TOKEN_KEY}` will be used\n\
393-
Specify only one value to silence this warning.",
394-
TOKEN_KEY = token_key,
395-
PROC_KEY = proc_key,
396-
))?;
397-
None
398-
} else {
399-
process
394+
let process = if config.cli_unstable().credential_process {
395+
let process =
396+
config.get::<Option<config::PathAndArgs>>("registry.credential-process")?;
397+
if token.is_some() && process.is_some() {
398+
return err_both("registry.token", "registry.credential-process");
399+
}
400+
process
401+
} else {
402+
None
403+
};
404+
(None, token, process)
400405
}
401-
} else {
402-
None
403406
};
404407

405408
let credential_process =

tests/testsuite/credential_process.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,18 @@ fn warn_both_token_and_process() {
9292

9393
p.cargo("publish --no-verify --registry alternative -Z credential-process")
9494
.masquerade_as_nightly_cargo()
95+
.with_status(101)
9596
.with_stderr(
9697
"\
97-
[WARNING] both `registries.alternative.token` and `registries.alternative.credential-process` \
98-
were specified in the config, only `registries.alternative.token` will be used
99-
Specify only one value to silence this warning.
100-
[UPDATING] [..]
101-
[PACKAGING] foo v0.1.0 [..]
102-
[UPLOADING] foo v0.1.0 [..]
98+
[ERROR] both `registries.alternative.token` and `registries.alternative.credential-process` \
99+
were specified in the config\n\
100+
Only one of these values may be set, remove one or the other to proceed.
103101
",
104102
)
105103
.run();
106104

107105
// Try with global credential-process, and registry-specific `token`.
106+
// This should silently use the config token, and not run the "false" exe.
108107
p.change_file(
109108
".cargo/config",
110109
r#"
@@ -119,9 +118,6 @@ Specify only one value to silence this warning.
119118
.masquerade_as_nightly_cargo()
120119
.with_stderr(
121120
"\
122-
[WARNING] both `registries.alternative.token` and `registry.credential-process` \
123-
were specified in the config, only `registries.alternative.token` will be used
124-
Specify only one value to silence this warning.
125121
[UPDATING] [..]
126122
[PACKAGING] foo v0.1.0 [..]
127123
[UPLOADING] foo v0.1.0 [..]

0 commit comments

Comments
 (0)