Skip to content

Commit 8c5cae1

Browse files
committed
Auto merge of #11533 - weihanglo:issue-11509, r=ehuss
Cargo by default saves credentials to `.cargo/credentials.toml`
2 parents e2ccb3c + cff549b commit 8c5cae1

File tree

8 files changed

+41
-28
lines changed

8 files changed

+41
-28
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl RegistryBuilder {
320320
}
321321

322322
if self.configure_token {
323-
let credentials = paths::home().join(".cargo/credentials");
323+
let credentials = paths::home().join(".cargo/credentials.toml");
324324
match &registry.token {
325325
Token::Plaintext(token) => {
326326
if let Some(alternative) = &self.alternative {

src/cargo/util/config/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,16 +2134,16 @@ pub fn save_credentials(
21342134
Some(name)
21352135
};
21362136

2137-
// If 'credentials.toml' exists, we should write to that, otherwise
2138-
// use the legacy 'credentials'. There's no need to print the warning
2139-
// here, because it would already be printed at load time.
2137+
// If 'credentials' exists, write to that for backward compatibility reasons.
2138+
// Otherwise write to 'credentials.toml'. There's no need to print the
2139+
// warning here, because it would already be printed at load time.
21402140
let home_path = cfg.home_path.clone().into_path_unlocked();
21412141
let filename = match cfg.get_file_path(&home_path, "credentials", false)? {
21422142
Some(path) => match path.file_name() {
21432143
Some(filename) => Path::new(filename).to_owned(),
2144-
None => Path::new("credentials").to_owned(),
2144+
None => Path::new("credentials.toml").to_owned(),
21452145
},
2146-
None => Path::new("credentials").to_owned(),
2146+
None => Path::new("credentials.toml").to_owned(),
21472147
};
21482148

21492149
let mut file = {

src/doc/src/reference/unstable.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ a new `cargo logout` command.
912912

913913
To use this feature, you must pass the `-Z credential-process` flag on the
914914
command-line. Additionally, you must remove any current tokens currently saved
915-
in the [`credentials` file] (which can be done with the new `logout` command).
915+
in the [`credentials.toml` file] (which can be done with the new `logout` command).
916916

917917
#### `credential-process` Configuration
918918

@@ -1049,10 +1049,10 @@ The following environment variables will be provided to the executed command:
10491049
#### `cargo logout`
10501050

10511051
A new `cargo logout` command has been added to make it easier to remove a
1052-
token from storage. This supports both [`credentials` file] tokens and
1052+
token from storage. This supports both [`credentials.toml` file] tokens and
10531053
`credential-process` tokens.
10541054

1055-
When used with `credentials` file tokens, it needs the `-Z unstable-options`
1055+
When used with `credentials.toml` file tokens, it needs the `-Z unstable-options`
10561056
command-line option:
10571057

10581058
```console
@@ -1071,7 +1071,7 @@ cargo logout -Z credential-process
10711071
[`cargo publish`]: ../commands/cargo-publish.md
10721072
[`cargo owner`]: ../commands/cargo-owner.md
10731073
[`cargo yank`]: ../commands/cargo-yank.md
1074-
[`credentials` file]: config.md#credentials
1074+
[`credentials.toml` file]: config.md#credentials
10751075
[crates.io]: https://crates.io/
10761076
[config file]: config.md
10771077

tests/testsuite/alt_registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ fn block_publish_due_to_no_token() {
399399
registry::alt_init();
400400
let p = project().file("src/lib.rs", "").build();
401401

402-
fs::remove_file(paths::home().join(".cargo/credentials")).unwrap();
402+
fs::remove_file(paths::home().join(".cargo/credentials.toml")).unwrap();
403403

404404
// Now perform the actual publish
405405
p.cargo("publish --registry alternative")

tests/testsuite/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2893,7 +2893,7 @@ fn credentials_is_unreadable() {
28932893
.file("src/lib.rs", "")
28942894
.build();
28952895

2896-
let credentials = home().join(".cargo/credentials");
2896+
let credentials = home().join(".cargo/credentials.toml");
28972897
t!(fs::create_dir_all(credentials.parent().unwrap()));
28982898
t!(fs::write(
28992899
&credentials,

tests/testsuite/login.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
//! Tests for the `cargo login` command.
22
33
use cargo_test_support::cargo_process;
4-
use cargo_test_support::install::cargo_home;
54
use cargo_test_support::paths::{self, CargoPathExt};
65
use cargo_test_support::registry::{self, RegistryBuilder};
76
use cargo_test_support::t;
8-
use std::fs::{self};
7+
use std::fs;
98
use std::path::PathBuf;
109
use toml_edit::easy as toml;
1110

1211
const TOKEN: &str = "test-token";
1312
const TOKEN2: &str = "test-token2";
1413
const ORIGINAL_TOKEN: &str = "api-token";
1514

15+
fn credentials_toml() -> PathBuf {
16+
paths::home().join(".cargo/credentials.toml")
17+
}
18+
1619
fn setup_new_credentials() {
17-
let config = cargo_home().join("credentials");
18-
setup_new_credentials_at(config);
20+
setup_new_credentials_at(credentials_toml());
1921
}
2022

2123
fn setup_new_credentials_at(config: PathBuf) {
@@ -27,7 +29,7 @@ fn setup_new_credentials_at(config: PathBuf) {
2729
}
2830

2931
fn check_token(expected_token: &str, registry: Option<&str>) -> bool {
30-
let credentials = cargo_home().join("credentials");
32+
let credentials = credentials_toml();
3133
assert!(credentials.is_file());
3234

3335
let contents = fs::read_to_string(&credentials).unwrap();
@@ -189,15 +191,15 @@ fn login_with_no_cargo_dir() {
189191
cargo_process("login foo -v")
190192
.replace_crates_io(registry.index_url())
191193
.run();
192-
let credentials = fs::read_to_string(paths::home().join(".cargo/credentials")).unwrap();
194+
let credentials = fs::read_to_string(credentials_toml()).unwrap();
193195
assert_eq!(credentials, "[registry]\ntoken = \"foo\"\n");
194196
}
195197

196198
#[cargo_test]
197199
fn login_with_differently_sized_token() {
198200
// Verify that the configuration file gets properly truncated.
199201
let registry = registry::init();
200-
let credentials = paths::home().join(".cargo/credentials");
202+
let credentials = credentials_toml();
201203
fs::remove_file(&credentials).unwrap();
202204
cargo_process("login lmaolmaolmao -v")
203205
.replace_crates_io(registry.index_url())
@@ -215,7 +217,7 @@ fn login_with_differently_sized_token() {
215217
#[cargo_test]
216218
fn login_with_token_on_stdin() {
217219
let registry = registry::init();
218-
let credentials = paths::home().join(".cargo/credentials");
220+
let credentials = credentials_toml();
219221
fs::remove_file(&credentials).unwrap();
220222
cargo_process("login lmao -v")
221223
.replace_crates_io(registry.index_url())
@@ -232,7 +234,7 @@ fn login_with_token_on_stdin() {
232234
#[cargo_test]
233235
fn login_with_asymmetric_token_and_subject_on_stdin() {
234236
let registry = registry::init();
235-
let credentials = paths::home().join(".cargo/credentials");
237+
let credentials = credentials_toml();
236238
fs::remove_file(&credentials).unwrap();
237239
cargo_process("login --key-subject=foo --secret-key -v -Z registry-auth")
238240
.masquerade_as_nightly_cargo(&["registry-auth"])
@@ -253,7 +255,7 @@ k3.public.AmDwjlyf8jAV3gm5Z7Kz9xAOcsKslt_Vwp5v-emjFzBHLCtcANzTaVEghTNEMj9PkQ",
253255
#[cargo_test]
254256
fn login_with_asymmetric_token_on_stdin() {
255257
let registry = registry::init();
256-
let credentials = paths::home().join(".cargo/credentials");
258+
let credentials = credentials_toml();
257259
fs::remove_file(&credentials).unwrap();
258260
cargo_process("login --secret-key -v -Z registry-auth")
259261
.masquerade_as_nightly_cargo(&["registry-auth"])
@@ -272,7 +274,7 @@ k3.public.AmDwjlyf8jAV3gm5Z7Kz9xAOcsKslt_Vwp5v-emjFzBHLCtcANzTaVEghTNEMj9PkQ",
272274
#[cargo_test]
273275
fn login_with_asymmetric_key_subject_without_key() {
274276
let registry = registry::init();
275-
let credentials = paths::home().join(".cargo/credentials");
277+
let credentials = credentials_toml();
276278
fs::remove_file(&credentials).unwrap();
277279
cargo_process("login --key-subject=foo -Z registry-auth")
278280
.masquerade_as_nightly_cargo(&["registry-auth"])
@@ -307,7 +309,7 @@ k3.public.AmDwjlyf8jAV3gm5Z7Kz9xAOcsKslt_Vwp5v-emjFzBHLCtcANzTaVEghTNEMj9PkQ",
307309
#[cargo_test]
308310
fn login_with_generate_asymmetric_token() {
309311
let registry = registry::init();
310-
let credentials = paths::home().join(".cargo/credentials");
312+
let credentials = credentials_toml();
311313
fs::remove_file(&credentials).unwrap();
312314
cargo_process("login --generate-keypair -Z registry-auth")
313315
.masquerade_as_nightly_cargo(&["registry-auth"])

tests/testsuite/logout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ the `cargo logout` command.
2424

2525
/// Checks whether or not the token is set for the given token.
2626
fn check_config_token(registry: Option<&str>, should_be_set: bool) {
27-
let credentials = cargo_home().join("credentials");
27+
let credentials = cargo_home().join("credentials.toml");
2828
let contents = fs::read_to_string(&credentials).unwrap();
2929
let toml: toml::Value = contents.parse().unwrap();
3030
if let Some(registry) = registry {

tests/testsuite/publish.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ fn old_token_location() {
231231
.file("src/main.rs", "fn main() {}")
232232
.build();
233233

234-
let credentials = paths::home().join(".cargo/credentials");
234+
let credentials = paths::home().join(".cargo/credentials.toml");
235235
fs::remove_file(&credentials).unwrap();
236236

237237
// Verify can't publish without a token.
@@ -1614,8 +1614,9 @@ fn credentials_ambiguous_filename() {
16141614
// `publish` generally requires a remote registry
16151615
let registry = registry::RegistryBuilder::new().http_api().build();
16161616

1617+
// Make token in `credentials.toml` incorrect to ensure it is not read.
16171618
let credentials_toml = paths::home().join(".cargo/credentials.toml");
1618-
fs::write(credentials_toml, r#"token = "api-token""#).unwrap();
1619+
fs::write(credentials_toml, r#"token = "wrong-token""#).unwrap();
16191620

16201621
let p = project()
16211622
.file(
@@ -1632,6 +1633,16 @@ fn credentials_ambiguous_filename() {
16321633
.file("src/main.rs", "fn main() {}")
16331634
.build();
16341635

1636+
p.cargo("publish --no-verify")
1637+
.replace_crates_io(registry.index_url())
1638+
.with_status(101)
1639+
.with_stderr_contains("[..]Unauthorized message from server[..]")
1640+
.run();
1641+
1642+
// Favor `credentials` if exists.
1643+
let credentials = paths::home().join(".cargo/credentials");
1644+
fs::write(credentials, r#"token = "sekrit""#).unwrap();
1645+
16351646
p.cargo("publish --no-verify")
16361647
.replace_crates_io(registry.index_url())
16371648
.with_stderr(
@@ -1656,7 +1667,7 @@ fn index_requires_token() {
16561667
// Use local registry for faster test times since no publish will occur
16571668
let registry = registry::init();
16581669

1659-
let credentials = paths::home().join(".cargo/credentials");
1670+
let credentials = paths::home().join(".cargo/credentials.toml");
16601671
fs::remove_file(&credentials).unwrap();
16611672

16621673
let p = project()

0 commit comments

Comments
 (0)