Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support login tokens for multiple registries #4680

Merged
merged 9 commits into from
Nov 1, 2017

Conversation

cswindle
Copy link
Contributor

This pull request builds on #4206 to support login using the the registry from alternate registries (RFC 2141). This includes the following changes:

  • allow credentials to be stored based on the registry
  • allow passing the registry to run cargo commands against using --registry

Note that this does not include a feature gate on the use of --registry as the publish code blocks publish if we use any features. @alexcrichton, are you happy with this approach, or is there a way that you would recommend this should be relaxed for testing purposes?

fa7ca7 and others added 3 commits June 22, 2017 17:21
Now `cargo login` stores a token per host.
If the host parameter is omitted cargo stores a token as default, i.e.
as a token for crates.io.
…gistry-login

Conflicts:
	src/bin/login.rs
	src/cargo/ops/registry.rs
	tests/login.rs
	tests/publish.rs
@rust-highfive
Copy link

r? @matklad

(rust_highfive has picked a reviewer for you, use r? to override)

let config = src.config()?.unwrap();
options.flag_host.clone().unwrap_or(config.api.unwrap())
}
};
println!("please visit {}me and paste the API Token below", host);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm I think this should only get printed for the crates.io registry, right? We may not want to assume the layout of another registry per se just yet, and instead we could probably just return an error if no token was passed and --registry was passed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if the /me API should be required for publish, I am happy to switch to just return an error stating that the token should be provided.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is one where we haven't done much to sketch out the path towards "what does a custom registry need HTTP-wise", so I think it's ok to err on the side of caution for now and just return an error

let (index, token) = match registry {
Some(registry) => {
let index = Some(config.get_registry_index(&registry)?);
let table = config.get_table(&format!("registry.{}", registry))?.map(|t| t.val);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this use get_string perhaps? That'll allow overrides via env var (table support isn't implemented there yet)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did originally try using get_string, but that caused problems if there was a '.' in the registry name (which I think is allowed). I will have a play and see if I can get something working.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out it does just work switching to get_string, so I have made the change.

@@ -545,6 +545,14 @@ impl Config {
}
}

/// Gets the index for a registry.
pub fn get_registry_index(&self, registry: &str) -> CargoResult<String> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this could bake in returning a Url instead of a String?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable, I will update.

@alexcrichton
Copy link
Member

Looking great! Could this also be sure to place the support behind a feature gate? To do something like rustc it means we'd add a -Z unstable-options "feature" which would unlock other unstable flags like --registry

@cswindle
Copy link
Contributor Author

@alexcrichton, I have now made all the changes that you requested as part of your review.

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great! Lemme know if anything doesn't make sense though.

// Now perform the actual publish
assert_that(p.cargo("publish").masquerade_as_nightly_cargo()
.arg("--registry").arg("alternative").arg("-Zunstable-options"),
execs().with_status(101));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add an assertion about stderr here to ensure it prints the right error message?

// Now perform the actual publish
assert_that(p.cargo("publish").masquerade_as_nightly_cargo()
.arg("--registry").arg("alternative").arg("-Zunstable-options"),
execs().with_status(0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm could you describe the behavior of this test a bit? Presumably this is sending a POST which I guess is "uploading" a file to the local filesystem?

If that's the case, can this assert that the file was created? I think the binary format is pretty crazy so it's ok to not actually look inside the file, but asserting at least that it exists may be good?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just performing an upload to the local registry in the same way that publish_clean currently works (with the only change that we are going to an alternate registry), I am not too keen on adding too much extra checking of the actual registry contents as that will likely be a remote server which we will be receiving the package normally. Are you happy for me to not make this change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah I was just thinking we could assert that a file exists on the filesystem somewhere. Basically assert that the upload happened to the alternative registry here and not the "main registry"

@@ -7,7 +7,7 @@ extern crate toml;
use std::io::prelude::*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and I think at least one other file?) I think changed from 644 permissions to 755, mind reverting back?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will revert those.

tests/login.rs Outdated
[registries.test-reg]
index = "http://dummy_index/"

[registries.test.reg]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually reminds me, at some point we're going to want validation on the names of registries I think? Probably for now (to start conservative at least) we probably want to restrict registries to [a-zA-Z0-9_-] probably?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restrict the identifiers of the registry, that is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case I will remove the test for test.reg and remove this entry. I will also add this to the issue list for stabilization.

if let Some(registry) = registry {
let mut map = HashMap::new();
map.insert(key, value);
(registry, CV::Table(map, file.path().to_path_buf()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is building up a TOML file like:

[my-registry-name]
token = "..."

right?

That seems fine by me for now (although maybe registry.foo for consistency with specifying the index?), but one thing I'm a little worried about is that we're making a lot of decisions in a bit of an ad-hoc fashion. I think all the conclusions are fine so far, but it'd be good to have a list of "here's the design decisions that were made" when we go for stabilizing custom registries.

Mind opening a tracking issue for custom registries (or appending to one that already exists?). I'm thinking we could track things like the --registry flag, my concern below for the naming of the registry identifiers, the format of the credentials file, the configuration syntax in .cargo/config for custom registries, etc.

Basically I'm thinking we should just track somewhere "here's what we need to be sure about for stabilization" and we can throw concerns/thoughts/implementation details on that as they arise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will raise an issue to cover this.

@alexcrichton
Copy link
Member

Ok looks great! r=me with an assertion that the publish happens to the right registry (asserting that a file exists)

@cswindle
Copy link
Contributor Author

@alexcrichton, I have added the additional test that you asked for.

@cswindle
Copy link
Contributor Author

cswindle commented Nov 1, 2017

Fixes #4498 and #3365.

@alexcrichton
Copy link
Member

@bors: r+

Looks great!

@bors
Copy link
Collaborator

bors commented Nov 1, 2017

📌 Commit 4ef8f55 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Nov 1, 2017

⌛ Testing commit 4ef8f55 with merge 551b62b...

bors added a commit that referenced this pull request Nov 1, 2017
Support login tokens for multiple registries

This pull request builds on #4206 to support login using the the registry from alternate registries (RFC 2141). This includes the following changes:

 - allow credentials to be stored based on the registry
 - allow passing the registry to run cargo commands against using --registry

Note that this does not include a feature gate on the use of --registry as the publish code blocks publish if we use any features. @alexcrichton, are you happy with this approach, or is there a way that you would recommend this should be relaxed for testing purposes?
@bors
Copy link
Collaborator

bors commented Nov 1, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing 551b62b to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants