Skip to content

Commit 0a4ec29

Browse files
committed
Auto merge of #7774 - giraffate:update_credentials, r=ehuss
Load credentials only when needed Credentials are always loaded, even if these are not used. If access to confidential files such as credentials is not given, `cargo build` fails despite not using credentials. Fixes #7624.
2 parents ad3dbe1 + 438d005 commit 0a4ec29

File tree

10 files changed

+174
-28
lines changed

10 files changed

+174
-28
lines changed

src/bin/cargo/commands/owner.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Explicitly named owners can also modify the set of owners, so take care!
3939
}
4040

4141
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
42+
config.load_credentials()?;
43+
4244
let registry = args.registry(config)?;
4345
let opts = OwnersOptions {
4446
krate: args.value_of("crate").map(|s| s.to_string()),

src/bin/cargo/commands/publish.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub fn cli() -> App {
2626
}
2727

2828
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
29+
config.load_credentials()?;
30+
2931
let registry = args.registry(config)?;
3032
let ws = args.workspace(config)?;
3133
let index = args.index(config)?;

src/bin/cargo/commands/yank.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ crates to be locked to any yanked version.
2929
}
3030

3131
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
32+
config.load_credentials()?;
33+
3234
let registry = args.registry(config)?;
3335

3436
ops::yank(

src/cargo/util/config/mod.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,6 @@ impl Config {
723723
})
724724
.chain_err(|| "could not load Cargo configuration")?;
725725

726-
self.load_credentials(&mut cfg)?;
727726
match cfg {
728727
CV::Table(map, _) => Ok(map),
729728
_ => unreachable!(),
@@ -956,9 +955,8 @@ impl Config {
956955
Ok(url)
957956
}
958957

959-
/// Loads credentials config from the credentials file into the `ConfigValue` object, if
960-
/// present.
961-
fn load_credentials(&self, cfg: &mut ConfigValue) -> CargoResult<()> {
958+
/// Loads credentials config from the credentials file, if present.
959+
pub fn load_credentials(&mut self) -> CargoResult<()> {
962960
let home_path = self.home_path.clone().into_path_unlocked();
963961
let credentials = match self.get_file_path(&home_path, "credentials", true)? {
964962
Some(credentials) => credentials,
@@ -983,7 +981,19 @@ impl Config {
983981
}
984982
}
985983

986-
cfg.merge(value, true)?;
984+
if let CV::Table(map, _) = value {
985+
let base_map = self.values_mut()?;
986+
for (k, v) in map {
987+
match base_map.entry(k) {
988+
Vacant(entry) => {
989+
entry.insert(v);
990+
}
991+
Occupied(mut entry) => {
992+
entry.get_mut().merge(v, true)?;
993+
}
994+
}
995+
}
996+
}
987997

988998
Ok(())
989999
}

tests/testsuite/build.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,32 @@ fn recompile_space_in_name() {
22372237
foo.cargo("build").with_stdout("").run();
22382238
}
22392239

2240+
#[cfg(unix)]
2241+
#[cargo_test]
2242+
fn credentials_is_unreadable() {
2243+
use cargo_test_support::paths::home;
2244+
use std::os::unix::prelude::*;
2245+
let p = project()
2246+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
2247+
.file("src/lib.rs", "")
2248+
.build();
2249+
2250+
let credentials = home().join(".cargo/credentials");
2251+
t!(fs::create_dir_all(credentials.parent().unwrap()));
2252+
t!(t!(File::create(&credentials)).write_all(
2253+
br#"
2254+
[registry]
2255+
token = "api-token"
2256+
"#
2257+
));
2258+
let stat = fs::metadata(credentials.as_path()).unwrap();
2259+
let mut perms = stat.permissions();
2260+
perms.set_mode(0o000);
2261+
fs::set_permissions(credentials, perms.clone()).unwrap();
2262+
2263+
p.cargo("build").run();
2264+
}
2265+
22402266
#[cfg(unix)]
22412267
#[cargo_test]
22422268
fn ignore_bad_directories() {

tests/testsuite/login.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,28 +111,6 @@ fn credentials_work_with_extension() {
111111
assert!(check_token(TOKEN, None));
112112
}
113113

114-
#[cargo_test]
115-
fn credentials_ambiguous_filename() {
116-
registry::init();
117-
setup_new_credentials();
118-
setup_new_credentials_toml();
119-
120-
cargo_process("login --host")
121-
.arg(registry_url().to_string())
122-
.arg(TOKEN)
123-
.with_stderr_contains(
124-
"\
125-
[WARNING] Both `[..]/credentials` and `[..]/credentials.toml` exist. Using `[..]/credentials`
126-
",
127-
)
128-
.run();
129-
130-
// It should use the value from the one without the extension
131-
// for backwards compatibility. check_token explicitly checks
132-
// 'credentials' without the extension, which is what we want.
133-
assert!(check_token(TOKEN, None));
134-
}
135-
136114
#[cargo_test]
137115
fn login_with_old_and_new_credentials() {
138116
setup_new_credentials();
@@ -161,7 +139,9 @@ fn new_credentials_is_used_instead_old() {
161139
.arg(TOKEN)
162140
.run();
163141

164-
let config = Config::new(Shell::new(), cargo_home(), cargo_home());
142+
let mut config = Config::new(Shell::new(), cargo_home(), cargo_home());
143+
let _ = config.values();
144+
let _ = config.load_credentials();
165145

166146
let token = config.get_string("registry.token").unwrap().map(|p| p.val);
167147
assert_eq!(token.unwrap(), TOKEN);

tests/testsuite/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ mod net_config;
6868
mod new;
6969
mod offline;
7070
mod out_dir;
71+
mod owner;
7172
mod package;
7273
mod patch;
7374
mod path;
@@ -106,6 +107,7 @@ mod verify_project;
106107
mod version;
107108
mod warn_on_failure;
108109
mod workspaces;
110+
mod yank;
109111

110112
#[cargo_test]
111113
fn aaa_trigger_cross_compile_disabled_check() {

tests/testsuite/owner.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//! Tests for the `cargo owner` command.
2+
3+
use std::fs;
4+
5+
use cargo_test_support::paths::CargoPathExt;
6+
use cargo_test_support::project;
7+
use cargo_test_support::registry::{self, api_path, registry_url};
8+
9+
fn setup(name: &str) {
10+
let dir = api_path().join(format!("api/v1/crates/{}", name));
11+
dir.mkdir_p();
12+
fs::write(
13+
dir.join("owners"),
14+
r#"{
15+
"users": [
16+
{
17+
"id": 70,
18+
"login": "github:rust-lang:core",
19+
"name": "Core"
20+
}
21+
]
22+
}"#,
23+
)
24+
.unwrap();
25+
}
26+
27+
#[cargo_test]
28+
fn simple_list() {
29+
registry::init();
30+
setup("foo");
31+
32+
let p = project()
33+
.file(
34+
"Cargo.toml",
35+
r#"
36+
[project]
37+
name = "foo"
38+
version = "0.0.1"
39+
authors = []
40+
license = "MIT"
41+
description = "foo"
42+
"#,
43+
)
44+
.file("src/main.rs", "fn main() {}")
45+
.build();
46+
47+
p.cargo("owner -l --index")
48+
.arg(registry_url().to_string())
49+
.run();
50+
}

tests/testsuite/publish.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,3 +1261,37 @@ repository = "foo"
12611261
)],
12621262
);
12631263
}
1264+
1265+
#[cargo_test]
1266+
fn credentials_ambiguous_filename() {
1267+
registry::init();
1268+
1269+
let credentials_toml = paths::home().join(".cargo/credentials.toml");
1270+
fs::write(credentials_toml, r#"token = "api-token""#).unwrap();
1271+
1272+
let p = project()
1273+
.file(
1274+
"Cargo.toml",
1275+
r#"
1276+
[project]
1277+
name = "foo"
1278+
version = "0.0.1"
1279+
authors = []
1280+
license = "MIT"
1281+
description = "foo"
1282+
"#,
1283+
)
1284+
.file("src/main.rs", "fn main() {}")
1285+
.build();
1286+
1287+
p.cargo("publish --no-verify --index")
1288+
.arg(registry_url().to_string())
1289+
.with_stderr_contains(
1290+
"\
1291+
[WARNING] Both `[..]/credentials` and `[..]/credentials.toml` exist. Using `[..]/credentials`
1292+
",
1293+
)
1294+
.run();
1295+
1296+
validate_upload_foo();
1297+
}

tests/testsuite/yank.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//! Tests for the `cargo yank` command.
2+
3+
use std::fs;
4+
5+
use cargo_test_support::paths::CargoPathExt;
6+
use cargo_test_support::project;
7+
use cargo_test_support::registry::{self, api_path, registry_url};
8+
9+
fn setup(name: &str, version: &str) {
10+
let dir = api_path().join(format!("api/v1/crates/{}/{}", name, version));
11+
dir.mkdir_p();
12+
fs::write(dir.join("yank"), r#"{"ok": true}"#).unwrap();
13+
}
14+
15+
#[cargo_test]
16+
fn simple() {
17+
registry::init();
18+
setup("foo", "0.0.1");
19+
20+
let p = project()
21+
.file(
22+
"Cargo.toml",
23+
r#"
24+
[project]
25+
name = "foo"
26+
version = "0.0.1"
27+
authors = []
28+
license = "MIT"
29+
description = "foo"
30+
"#,
31+
)
32+
.file("src/main.rs", "fn main() {}")
33+
.build();
34+
35+
p.cargo("yank --vers 0.0.1 --index")
36+
.arg(registry_url().to_string())
37+
.run();
38+
}

0 commit comments

Comments
 (0)