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

Add expansion of environment variables in requirement files #3728

Merged
merged 6 commits into from
Feb 5, 2018

Conversation

BrownTruck
Copy link
Contributor

@BrownTruck BrownTruck commented May 26, 2016

I really like pip and have been using it for quite some time. One scenario that I've come across several times that doesn't seem to be covered is installing from private git (or other) repos without having to store private API keys or other authentication artifacts in the requirements file.

In a 12-factor fashion, I've started thinking about how pip could support private repos by keeping secrets in environment variables. And it turned out that supporting the expansion of environment variables is pretty trivial. Adding a line like this in a requirement file:

https://$GITHUB_TOKEN:[email protected]/user/repo/archive/master.zip

can easily be expanded to by extracting the actual API key from the $GITHUB_TOKEN variable using os.path.expandvars, turning it into:

https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:[email protected]/user/repo/archive/master.zip

I'm not sure if there are any caveats with this implementation around cross-platform support, security or whatever else. But I think this would be a great feature to add to pip and would love to know how to improve on this PR to get it added.

I'm also aware that the tests could be improved since they are not really testing if this actually work. However, mocking the environment for this specific test is not really straight forward. I'd appreciate any input on how this can be tested properly.


This was automatically migrated from #3514 to reparent it to the master branch. Please see original pull request for any previous discussion.

Original Submitter: @elbaschid


This change is Reviewable

@wobeng
Copy link

wobeng commented Feb 21, 2017

Has this been implemented?

assert reqs[0].link.url == expected_url

def test_expand_missing_env_variables(self, tmpdir, finder):
req_url = ('https://${NON_EXISTING_VARIABLE}:$WRONG_FORMAT@'

Choose a reason for hiding this comment

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

While the test is still reasonably accurate, isn't this supposed to be https://${WRONG_FORMAT}:$NON_EXISTING_VARIABLE@ instead?

Copy link
Member

Choose a reason for hiding this comment

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

🎨 NON_EXISTENT_VARIABLE

@chaitan94
Copy link

Would love to see this feature merged. May I ask if there are any plans of doing this anytime soon?

@silverzhaojr
Copy link

This is a wonderful feature! Any update when it will be merged?

@pradyunsg pradyunsg added the type: enhancement Improvements to functionality label Aug 7, 2017
Copy link
Member

@pradyunsg pradyunsg left a comment

Choose a reason for hiding this comment

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

The change looks good to me in general.

That said, before this can be merged, this PR would need to be rebased or merged with the current master and a news fragment needs to be added for this work.

@RonnyPfannschmidt
Copy link
Contributor

also this seems to be a less complete implementation of https://docs.python.org/2/library/os.path.html#os.path.expandvars

@jorng
Copy link

jorng commented Aug 7, 2017

@RonnyPfannschmidt yeah, I'm not sure I understand the reasoning for that, but it was done in response to a comment from @pfmoore last year:

I hadn't spotted the expandvars usage. For this PR to be acceptable, I think that the variable expansion used has to be platform independent (otherwise requirements files become platform dependent which is not acceptable). So the patch needs to do its own expansion, and document the format used.

Isn't os.path.expandvars already platform independent? According to the docs, the $name or ${name} style work on all platforms. Making custom expansion code seems like a poor choice to me.

@benoit-pierre
Copy link
Member

@jediorange: Yes, but os.path.expandvars also does %var% expansion on Windows, and only on that platform.

@jorng
Copy link

jorng commented Aug 7, 2017

@benoit-pierre I see that, but just personally fail to understand how that is a bad thing. The use case for environment variables in requirements files is for using private repos or obscuring other things. This means that they are used for private or proprietary projects, not mainstream things that are going to be downloaded by everyone.

If someone really wants to use Windows style variables for a personal/proprietary project (breaking things for any other OS), why not let them? I would consider it expected behavior.

@pradyunsg
Copy link
Member

this seems to be a less complete implementation of [link to os.path.expandvars docs]

As @jediorange and @benoit-pierre pointed out, the history on this is that the original PR was implemented using os.path.expandvars but in favour of not having too many ways and os-dependent behaviour to specify variable-expansion (like expandvars provides) it was decided to have just one way to it, using the POSIX standard definition. That way, there's consistency across platforms.

Making custom expansion code seems like a poor choice to me.

Above + this custom logic is straightforward enough.

@pradyunsg
Copy link
Member

pradyunsg commented Aug 7, 2017

why not let them?

If we can prevent someone from breaking stuff by mistake (or ignorance) without handicapping their ability to do something (here - expand variables in req-files), I'd say that we should. :)

@jorng
Copy link

jorng commented Aug 7, 2017

If we can prevent someone from breaking stuff by mistake (or ignorance) without handicapping their ability to do something (here - expand variables in req-files), I'd say that we should. :)

@pradyunsg I would argue that more people would be confused by having to adhere to a very specific style of environment variables, rather than using the same styles they can anywhere else (in shell scripts, Procfiles, etc).

To be clear: While I don't see the purpose of making it limited to only ${}, I am in favor of getting this feature added either way.

@pradyunsg
Copy link
Member

pradyunsg commented Aug 7, 2017

@jediorange

I would argue that more people would be confused

I mean, yeah, it's possible. The nice thing about being strict initially is we could expand this behaviour (word-play not intended) later if it actually confuses/annoys too much. :)

While I don't see the purpose of making it limited to only ${}, I am in favor of getting this feature added either way.

Roger that!

@pfmoore
Copy link
Member

pfmoore commented Aug 7, 2017

One outstanding issue from the discussion in the original PR is that this needs documentation - both to note that the requirements file format allows variable expansion, and to describe the permissible form.

The key review comment from the previous discussion was #3514 (comment). Other than that @xavfernandez was lukewarm on the idea, as am I. None of the other @pypa/pip-committers has commented yet.

If @elbaschid wants to take this PR forward by adding docs and fixing up the requested changes, or if someone else wants to take it over, then I'm OK to merge this. But I'd like someone with an actual need for the feature to complete the work.

@avinashak
Copy link

I recently figured out a way to achieve this without changes to pip utilizing git's url rewrite config

So doing something like git config --global "url.https://${GITHUB_TOKEN}@github.aaakk.us.kg.insteadOf" https://github.com will let you install packages from your private repo's if you have the ability to configure git in your environment.

Hope this helps.

@pradyunsg pradyunsg added the resolution: deferred till PR Further discussion will happen when a PR is made label Aug 21, 2017
@BrownTruck
Copy link
Contributor Author

Hello!

I am an automated bot and I have noticed that this pull request is not currently able to be merged. If you are able to either merge the master branch into this pull request or rebase this pull request against master then it will eligible for code review and hopefully merging!

@BrownTruck BrownTruck added the needs rebase or merge PR has conflicts with current master label Sep 1, 2017
@pradyunsg pradyunsg removed the resolution: deferred till PR Further discussion will happen when a PR is made label Oct 12, 2017
@pradyunsg
Copy link
Member

this needs documentation - both to note that the requirements file format allows variable expansion, and to describe the permissible form.

If @elbaschid or anyone else wants to take this forward, this is one thing that needs to be fixed.

@roadsideseb roadsideseb force-pushed the add_expanding_env_vars branch from 6c09626 to a75ef04 Compare November 5, 2017 21:22
@pypa-bot pypa-bot removed the needs rebase or merge PR has conflicts with current master label Nov 5, 2017
@roadsideseb roadsideseb force-pushed the add_expanding_env_vars branch 2 times, most recently from 2e1df66 to bfeb0f7 Compare November 5, 2017 23:11
@roadsideseb
Copy link

@pradyunsg I've rebased and added the NEWS file as requested. Seems like the failed test are the same as in master. I'll add documentation and make sure tests are running as soon as they are fixed on master.

I'm thinking about adding a section in docs/reference/pip_install.rst below "VCS Support". Does that sound like a reasonable place?

@pradyunsg
Copy link
Member

VCS support is probably not the right place. You want to do it at the location where there's discussion on/description of the requirement file format. I think that would in pip install reference but yeah, whereever it is, I think that's the right place.

@roadsideseb roadsideseb force-pushed the add_expanding_env_vars branch from 4bb2465 to 70b4dd8 Compare November 7, 2017 18:39
Copy link
Member

@pradyunsg pradyunsg left a comment

Choose a reason for hiding this comment

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

LGTM.

I'll wait for another person from @pypa/pip-committers to review. :)

@roadsideseb
Copy link

I'm happy to make the 🎨 changes...I fully understand your picky-ness 😃 .

I've also updated the docs according to your suggestions. I hope that makes more sense now and is concise enough. Let me know if you need any other changes made.

@roadsideseb roadsideseb force-pushed the add_expanding_env_vars branch from 70b4dd8 to 4496268 Compare November 15, 2017 23:06
@roadsideseb roadsideseb force-pushed the add_expanding_env_vars branch from 4496268 to 4a040c3 Compare November 18, 2017 15:36
@roadsideseb
Copy link

@pradyunsg I realized the test on appveyor were still failing so I rebased the latest working master and that fixed it. All seems to be green on that front now. Let me know if you need me to make any changes before this can be merged.

@pradyunsg pradyunsg requested a review from a team November 23, 2017 14:27
@venthur
Copy link
Contributor

venthur commented Dec 14, 2017

Thanks for working on this PR! This feature is badly needed in corporate environments where you have to install dependencies from private repositories with auth. This Issue is probably related to this PR.

@n1k0
Copy link

n1k0 commented Jan 18, 2018

What's the status of this PR? Our company would love seeing this available in pip as it would solve tons of headaches.

@nacivida
Copy link

nacivida commented Feb 1, 2018

We also need this feature for security. Any change this could be merged soon?

@eacmen
Copy link

eacmen commented Feb 3, 2018

I concur. We use travis-ci and this feature would make our process much cleaner and more secure with our private pypi repo.

Copy link
Member

@pfmoore pfmoore left a comment

Choose a reason for hiding this comment

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

I remain lukewarm on the functionality, but I'm OK for this to be merged.

@nacivida
Copy link

nacivida commented Feb 5, 2018

Thanks for approvals, we would be grateful if a collaborator merges this in. Thanks.

@pradyunsg
Copy link
Member

Given that none of the @pypa/pip-committers are opposed (or in favour) of this and, that there's multiple people asking for this and the PR is complete, I'm okay with merging this.

I should state that this increases the complexity of the format that is already defined on an as-implemented basis, which I don't like but again, I won't oppose this for that reason.

@lock
Copy link

lock bot commented Jun 2, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot added the auto-locked Outdated issues that have been locked by automation label Jun 2, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jun 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
auto-locked Outdated issues that have been locked by automation type: enhancement Improvements to functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.