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

Addresses Issue #5572: Implement PIP_TRUSTED_HOSTS logic... #5615

Merged
merged 2 commits into from
Mar 20, 2023

Conversation

kalebmckale
Copy link
Contributor

@kalebmckale kalebmckale commented Feb 18, 2023

Thank you for contributing to Pipenv!

The issue

What is the thing you want to fix? Is it associated with an issue on GitHub? Please mention it.

Opened issue #5572

The fix

How does this pull request fix your problem? Did you consider any alternatives? Why is this the best solution, in your opinion?

Duplicated logic from import_requirements() to do_install() in core.py to allow users to validate an index specified via --index command-line option against PIP_TRUSTED_HOSTS when determining verify_ssl value written to the Pipfile.

This is the quickest fix to the problem, but ideally, trusted_hosts logic throughout the package should be consolidated and made consistent.

Along with the pip.conf changes, a user can ensure Pipfile is created with correct indices from command-line the first time.

The checklist

  • Associated issue
  • A news fragment in the news/ directory to describe this fix with the extension .bugfix.rst, .feature.rst, .behavior.rst, .doc.rst. .vendor.rst. or .trivial.rst (this will appear in the release changelog). Use semantic line breaks and name the file after the issue number or the PR #.

@matteius
Copy link
Member

Thnaks for opening this @kalebmckale -- we can port the change over to the new file structure -- I've opened a PR tonight that eliminates core.py because its been holding me back from fixing other bugs. #5616

@kalebmckale
Copy link
Contributor Author

kalebmckale commented Feb 20, 2023

I'm happy to look over your branch and make the adjustment there so you at least have one less thing to do, if you like.

Welp... I guess I don't really know how to do that with GitHub apparently. Don't usually do the fork thing and didn't realize how different it was from just being on a project. 🙂

Actually, I figured it out. 🙃

@kalebmckale
Copy link
Contributor Author

kalebmckale commented Feb 20, 2023

@matteius, I thought I'd look through routines/install.py and make some notes about some refactoring to remove some Python anti-patterns, etc. And... well... wow... that do_install() function... it's uh... quite a doozy!

Anyhoo, I made some refactoring notes before stopping. You can view them here: routines/install.py.

@matteius
Copy link
Member

Thank you @kalebmckale -- I'll review them when I have more time. The goal of the routines PR is to split apart the existing code without refactor, and that will make subsequent refactor PRs much more viable. I really appreciate you taking the time to read through the code and look for areas of improvement.

@kalebmckale
Copy link
Contributor Author

kalebmckale commented Feb 20, 2023

No problem. I figured the current split was limited in scope because that's how I'd work on an issue, too. Keep it focused on specific tasks and iteratively make changes over multiple issues to ensure no breakage. 🙂

@matteius
Copy link
Member

matteius commented Mar 1, 2023

@kalebmckale Now that the other PR that splits apart core.py has been merged, could you resolve the conflicts?

@matteius
Copy link
Member

matteius commented Mar 9, 2023

@kalebmckale I am hopeful to do a release come this weekend, if possible I would like to include this.

@kalebmckale
Copy link
Contributor Author

Hey @matteius ! Sorry I’ve been spending a lot of extra time at work lately and haven’t had much time to do coding at home. I’ll see what I can do before this weekend.

@kalebmckale kalebmckale force-pushed the issue-5572 branch 3 times, most recently from 8d3ecae to d7dec99 Compare March 10, 2023 09:34
@kalebmckale
Copy link
Contributor Author

I think that should do it, but I am not able to test it locally right now.

@@ -334,8 +334,21 @@ def do_install(
)
# Add the package to the Pipfile.
if index_url:
trusted_hosts = get_trusted_hosts()
host_and_port = get_host_and_port(index_url)
require_valid_https = not any(
Copy link
Member

Choose a reason for hiding this comment

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

Just thinking through this change -- isn't this saying it requires valid https if any of the hosts in the PIP_TRUSTED_HOSTS? Wouldn't it be better to only check if the specific host of the index_url requires valid https?

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 identical to the code for the requirements file. I initially assumed it was correct as-is and literally just copied it over. Let me look more into this.

POST-UPDATE: The logic seems correct because for each index_url, it checks if either its host with or without port appears in list of trusted_hosts. If it does, it will then set require_valid_https to False and hence verify_ssl=False, which is what we want here. The any() is checking all the trusted hosts against the one index_url host (with and without port) and doesn't set all index_urls. If an index_url's host doesn't appear in trusted_hosts, this will still set verify_ssl=True. In cases when an index_url doesn't have a port, I would assume that any() is just doing one comparison unless I'm missing something.

UPDATE: This is currently only checking against the set environment variable PIP_TRUSTED_HOSTS.

This was primarily a quick patch work-around to allow a way for a user to be able to specify that an index specified at the command line was a trusted host and should set the argument verify_ssl=False.

Truthfully, this has to be just step one since it should also use the data from the pip.conf file, which at the time, I didn't know how to do until you implemented your code.

Furthermore, really there is a need for refactoring the whole trusted-host code throughout the codebase to condense the logic into one source of truth for all the references to it. However, that's a much bigger project and this work-around (assuming the logic is correct) provides something that works until the larger overhaul can be completed.

news/5572.feature.rst Outdated Show resolved Hide resolved
@oz123
Copy link
Contributor

oz123 commented Mar 19, 2023

@matteius, I thought I'd look through routines/install.py and make some notes about some refactoring to remove some Python anti-patterns, etc. And... well... wow... that do_install() function... it's uh... quite a doozy!

Anyhoo, I made some refactoring notes before stopping. You can view them here: routines/install.py.

Yes, unfortunately, pipenv has a very ill code base. But we are working to make it better :-)

Duplicated logic from `import_requirements()` in requirements.py
to `do_install()` in `install.py` to allow users to specify index
via `--index` command-line option and validate against
`PIP_TRUSTED_HOSTS` when determining `verify_ssl` value.

Update news file to be more descriptive.
@kalebmckale
Copy link
Contributor Author

@oz123 No shame or shade intended. I am happy to help. This is my wheelhouse.

@oz123
Copy link
Contributor

oz123 commented Mar 19, 2023

Thanks for the updated documentation. Can you please fix the linting error?

@kalebmckale
Copy link
Contributor Author

Sure! I think I understand what the error is, but just checking: is it saying I need an extra newline at the end of the news file?

@kalebmckale kalebmckale requested review from oz123 and matteius and removed request for oz123 and matteius March 20, 2023 01:00
@kalebmckale kalebmckale requested a review from oz123 March 20, 2023 01:01
@kalebmckale
Copy link
Contributor Author

Sorry for all the confusing messages. This is literally my first time working on a big GitHub project. I’m used to a competitor’s product and am confused about some of the differences. So, I’m probably clicking on a lot of things I shouldn’t be because I don’t know what they do.

@kalebmckale
Copy link
Contributor Author

At home, I have only my iPad to develop and run code. I've been trying to run pre-commit run --all-files --verbose --show-diff-on-failure, but it keeps crashing iSH before it completes. If you can let me know how to interpret the CI output without having to run it locally, that would be great! Sorry!

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

3 participants