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 support for config vars in Pip requirements files #1451

Open
ericwb opened this issue May 26, 2023 · 4 comments
Open

Add support for config vars in Pip requirements files #1451

ericwb opened this issue May 26, 2023 · 4 comments

Comments

@ericwb
Copy link

ericwb commented May 26, 2023

Unsure if I'm somehow doing something wrong or this is by design. According to Pip's documentation, I can include a private repo in my requirements.txt as a dependency. Since the repo is private and hosted on GitHub, I need to provide a personal access token. Of course, I do not want that token included in source code, so I configure it in Heroku's Config Vars. But it doesn't seem to work for me. However works fine when running locally.

For example, requirements.txt:

aiohttp
cryptography
cachetools
sqlalchemy==1.4.46
git+https://${GITHUB_TOKEN}@github.com/user/repo.git@main

This is the error I get from Heroku. It appears, it does not resolve the GITHUB_TOKEN environmental variable that is defined in the Config Vars.

-----> Installing requirements with pip
       Collecting git+https://****@github.com/user/repo.git@main (from -r requirements.txt (line 9))
         Cloning https://****@github.com/user/repo.git (to revision main) to /tmp/pip-req-build-imw3lwma
         Running command git clone --filter=blob:none --quiet 'https://****@github.com/user/repo.git' /tmp/pip-req-build-imw3lwma
         fatal: could not read Password for 'https://${GITHUB_TOKEN}@github.com': No such device or address
         error: subprocess-exited-with-error

Is there a workaround or something I'm missing? Thanks

https://pip.pypa.io/en/stable/reference/requirements-file-format/#using-environment-variables

@ericwb
Copy link
Author

ericwb commented May 26, 2023

I should also add that the Heroku docs state:
"Anything that works with a standard pip requirements file will work as expected on Heroku."

However, following that statement, it gives an example that is unsupported on GitHub. GitHub no longer supports user/password based authentication.

git+https://user:[email protected]/nsa/secret.git
Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.

https://devcenter.heroku.com/articles/python-pip#git-backed-distributions

@ericwb
Copy link
Author

ericwb commented May 27, 2023

I guess this issue has come up several times in different forms:
#417
#467
#639

The only workaround I seem to have is hosting another python package index and using PIP_EXTRA_INDEX_URL. Which is a hacky option for one repo.

@ericwb
Copy link
Author

ericwb commented May 27, 2023

This link seems to provide a good option on how to modify heroku-buildpack-python to utilize and acceptlist and denylist for env vars. Maybe this would be a worthy consideration as a fix?

https://devcenter.heroku.com/articles/buildpack-api#bin-compile-usage

export_env_dir() {
  env_dir=$1
  acceptlist_regex=${2:-''}
  denylist_regex=${3:-'^(PATH|GIT_DIR|CPATH|CPPATH|LD_PRELOAD|LIBRARY_PATH)$'}
  if [ -d "$env_dir" ]; then
    for e in $(ls $env_dir); do
      echo "$e" | grep -E "$acceptlist_regex" | grep -qvE "$denylist_regex" &&
      export "$e=$(cat $env_dir/$e)"
      :
    done
  fi
}

@edmorley
Copy link
Member

edmorley commented Jun 23, 2023

@ericwb Hi! Thank you for filing this, and sorry for the delay (I was away).

You are correct that env vars are filtered out in this buildpack at present for certain subprocesses, with pip install being one of them.

I'd like to revisit that decision in the new Python Cloud Native Buildpack (that's set to replace this one) - I've filed:
heroku/buildpacks-python#52

In the meantime, I'd recommend trying out .netrc file to configure auth - see:
https://pip.pypa.io/en/stable/topics/authentication/#netrc-support

Since you are using GitHub for the package location, the .netrc file can be created using:
https://github.com/heroku/heroku-buildpack-github-netrc

(For anyone else not using GitHub for the package location, then you will need to either fork the above buildpack, or else manually create a .netrc file using eg https://github.com/heroku/heroku-buildpack-inline)

@edmorley edmorley changed the title Trouble using config vars in requirements.txt Add support for config vars in requirements.txt Aug 25, 2023
@edmorley edmorley changed the title Add support for config vars in requirements.txt Add support for config vars in Pip requirements files Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants