Skip to content

Commit fc29c9c

Browse files
committed
Auto merge of #7708 - giraffate:fix_overwriting_credentials, r=ehuss
Fix overwriting alternate registry token When executing `cargo login`, 2nd alternate registry token overwrites 1st alternate registry token. Fixes #7701.
2 parents ad4122a + b7bc069 commit fc29c9c

File tree

3 files changed

+86
-40
lines changed

3 files changed

+86
-40
lines changed

crates/cargo-test-support/src/registry.rs

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,59 @@ use url::Url;
1515
/// initialized with a `config.json` file pointing to `dl_path` for downloads
1616
/// and `api_path` for uploads.
1717
pub fn registry_path() -> PathBuf {
18-
paths::root().join("registry")
18+
generate_path("registry")
1919
}
2020
pub fn registry_url() -> Url {
21-
Url::from_file_path(registry_path()).ok().unwrap()
21+
generate_url("registry")
2222
}
2323
/// Gets the path for local web API uploads. Cargo will place the contents of a web API
2424
/// request here. For example, `api/v1/crates/new` is the result of publishing a crate.
2525
pub fn api_path() -> PathBuf {
26-
paths::root().join("api")
26+
generate_path("api")
2727
}
2828
pub fn api_url() -> Url {
29-
Url::from_file_path(api_path()).ok().unwrap()
29+
generate_url("api")
3030
}
3131
/// Gets the path where crates can be downloaded using the web API endpoint. Crates
3232
/// should be organized as `{name}/{version}/download` to match the web API
3333
/// endpoint. This is rarely used and must be manually set up.
3434
pub fn dl_path() -> PathBuf {
35-
paths::root().join("dl")
35+
generate_path("dl")
3636
}
3737
pub fn dl_url() -> Url {
38-
Url::from_file_path(dl_path()).ok().unwrap()
38+
generate_url("dl")
3939
}
4040
/// Gets the alternative-registry version of `registry_path`.
4141
pub fn alt_registry_path() -> PathBuf {
42-
paths::root().join("alternative-registry")
42+
generate_path("alternative-registry")
4343
}
4444
pub fn alt_registry_url() -> Url {
45-
Url::from_file_path(alt_registry_path()).ok().unwrap()
45+
generate_url("alternative-registry")
4646
}
4747
/// Gets the alternative-registry version of `dl_path`.
4848
pub fn alt_dl_path() -> PathBuf {
49-
paths::root().join("alt_dl")
49+
generate_path("alt_dl")
5050
}
5151
pub fn alt_dl_url() -> String {
52-
let base = Url::from_file_path(alt_dl_path()).ok().unwrap();
53-
format!("{}/{{crate}}/{{version}}/{{crate}}-{{version}}.crate", base)
52+
generate_alt_dl_url("alt_dl")
5453
}
5554
/// Gets the alternative-registry version of `api_path`.
5655
pub fn alt_api_path() -> PathBuf {
57-
paths::root().join("alt_api")
56+
generate_path("alt_api")
5857
}
5958
pub fn alt_api_url() -> Url {
60-
Url::from_file_path(alt_api_path()).ok().unwrap()
59+
generate_url("alt_api")
60+
}
61+
62+
pub fn generate_path(name: &str) -> PathBuf {
63+
paths::root().join(name)
64+
}
65+
pub fn generate_url(name: &str) -> Url {
66+
Url::from_file_path(generate_path(name)).ok().unwrap()
67+
}
68+
pub fn generate_alt_dl_url(name: &str) -> String {
69+
let base = Url::from_file_path(generate_path(name)).ok().unwrap();
70+
format!("{}/{{crate}}/{{version}}/{{crate}}-{{version}}.crate", base)
6171
}
6272

6373
/// A builder for creating a new package in a registry.
@@ -184,34 +194,36 @@ pub fn init() {
184194
));
185195

186196
// Initialize a new registry.
187-
let _ = repo(&registry_path())
188-
.file(
189-
"config.json",
190-
&format!(
191-
r#"
192-
{{"dl":"{}","api":"{}"}}
193-
"#,
194-
dl_url(),
195-
api_url()
196-
),
197-
)
198-
.build();
199-
fs::create_dir_all(api_path().join("api/v1/crates")).unwrap();
197+
init_registry(
198+
registry_path(),
199+
dl_url().into_string(),
200+
api_url(),
201+
api_path(),
202+
);
200203

201204
// Initialize an alternative registry.
202-
repo(&alt_registry_path())
205+
init_registry(
206+
alt_registry_path(),
207+
alt_dl_url(),
208+
alt_api_url(),
209+
alt_api_path(),
210+
);
211+
}
212+
213+
pub fn init_registry(registry_path: PathBuf, dl_url: String, api_url: Url, api_path: PathBuf) {
214+
// Initialize a new registry.
215+
repo(&registry_path)
203216
.file(
204217
"config.json",
205218
&format!(
206219
r#"
207220
{{"dl":"{}","api":"{}"}}
208221
"#,
209-
alt_dl_url(),
210-
alt_api_url()
222+
dl_url, api_url
211223
),
212224
)
213225
.build();
214-
fs::create_dir_all(alt_api_path().join("api/v1/crates")).unwrap();
226+
fs::create_dir_all(api_path.join("api/v1/crates")).unwrap();
215227
}
216228

217229
impl Package {

src/cargo/util/config/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,14 +1314,14 @@ pub fn save_credentials(cfg: &Config, token: String, registry: Option<String>) -
13141314
.open_rw(filename, cfg, "credentials' config file")?
13151315
};
13161316

1317-
let (key, value) = {
1317+
let (key, mut value) = {
13181318
let key = "token".to_string();
13191319
let value = ConfigValue::String(token, file.path().to_path_buf());
13201320
let mut map = HashMap::new();
13211321
map.insert(key, value);
13221322
let table = CV::Table(map, file.path().to_path_buf());
13231323

1324-
if let Some(registry) = registry {
1324+
if let Some(registry) = registry.clone() {
13251325
let mut map = HashMap::new();
13261326
map.insert(registry, table);
13271327
(
@@ -1352,6 +1352,12 @@ pub fn save_credentials(cfg: &Config, token: String, registry: Option<String>) -
13521352
.insert("registry".into(), map.into());
13531353
}
13541354

1355+
if let Some(_) = registry {
1356+
if let Some(table) = toml.as_table_mut().unwrap().remove("registries") {
1357+
let v = CV::from_toml(file.path(), table)?;
1358+
value.merge(v)?;
1359+
}
1360+
}
13551361
toml.as_table_mut().unwrap().insert(key, value.into_toml());
13561362

13571363
let contents = toml.to_string();

tests/testsuite/login.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
//! Tests for the `cargo login` command.
22
3-
use std::fs::{self, File};
3+
use std::fs::{self, File, OpenOptions};
44
use std::io::prelude::*;
55
use std::path::PathBuf;
66

77
use cargo::core::Shell;
88
use cargo::util::config::Config;
99
use cargo_test_support::install::cargo_home;
1010
use cargo_test_support::registry::{self, registry_url};
11-
use cargo_test_support::{cargo_process, t};
11+
use cargo_test_support::{cargo_process, paths, t};
1212
use toml;
1313

1414
const TOKEN: &str = "test-token";
15+
const TOKEN2: &str = "test-token2";
1516
const ORIGINAL_TOKEN: &str = "api-token";
1617

1718
fn setup_new_credentials() {
@@ -169,20 +170,47 @@ fn new_credentials_is_used_instead_old() {
169170
#[cargo_test]
170171
fn registry_credentials() {
171172
registry::init();
173+
174+
let config = paths::home().join(".cargo/config");
175+
let mut f = OpenOptions::new().append(true).open(config).unwrap();
176+
t!(f.write_all(
177+
format!(
178+
r#"
179+
[registries.alternative2]
180+
index = '{}'
181+
"#,
182+
registry::generate_url("alternative2-registry")
183+
)
184+
.as_bytes(),
185+
));
186+
187+
registry::init_registry(
188+
registry::generate_path("alternative2-registry"),
189+
registry::generate_alt_dl_url("alt2_dl"),
190+
registry::generate_url("alt2_api"),
191+
registry::generate_path("alt2_api"),
192+
);
172193
setup_new_credentials();
173194

174195
let reg = "alternative";
175196

176-
cargo_process("login --registry")
177-
.arg(reg)
178-
.arg(TOKEN)
179-
.arg("-Zunstable-options")
180-
.masquerade_as_nightly_cargo()
181-
.run();
197+
cargo_process("login --registry").arg(reg).arg(TOKEN).run();
182198

183199
// Ensure that we have not updated the default token
184200
assert!(check_token(ORIGINAL_TOKEN, None));
185201

186202
// Also ensure that we get the new token for the registry
187203
assert!(check_token(TOKEN, Some(reg)));
204+
205+
let reg2 = "alternative2";
206+
cargo_process("login --registry")
207+
.arg(reg2)
208+
.arg(TOKEN2)
209+
.run();
210+
211+
// Ensure not overwriting 1st alternate registry token with
212+
// 2nd alternate registry token (see rust-lang/cargo#7701).
213+
assert!(check_token(ORIGINAL_TOKEN, None));
214+
assert!(check_token(TOKEN, Some(reg)));
215+
assert!(check_token(TOKEN2, Some(reg2)));
188216
}

0 commit comments

Comments
 (0)