jelmer / dulwich

Pure-Python Git implementation
https://www.dulwich.io/
Other
2.07k stars 402 forks source link

Support for extra header authentication #882

Open 0x2b3bfa0 opened 3 years ago

0x2b3bfa0 commented 3 years ago

It would be really nice to honor the extra header configuration option in order to allow authentication against HTTP remotes that support basic authentication , like in ephemeral headless environments like continous integration runners.

For example, the official actions/checkout@v2 GitHub Action relies on a custom header set through the local repository configuration to authenticate against the remote:

[http "https://github.com/"]
        extraheader = AUTHORIZATION: basic ···

Minimal example

>>> from dulwich.porcelain import ls_remote
>>> ls_remote("https://github.com/user/repository")

Expected output

{b'HEAD': b'···', b'refs/heads/main': b'···'}

Actual output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/···/site-packages/dulwich/porcelain.py", line 1618, in ls_remote
    return client.get_refs(host_path)
  File "/···/site-packages/dulwich/client.py", line 2089, in get_refs
    refs, _, _ = self._discover_references(b"git-upload-pack", url)
  File "/···/site-packages/dulwich/client.py", line 1906, in _discover_references
    resp, read = self._http_request(url, headers, allow_compression=True)
  File "/···/site-packages/dulwich/client.py", line 1875, in _http_request
    raise HTTPUnauthorized(resp.getheader("WWW-Authenticate"), url)
dulwich.client.HTTPUnauthorized: No valid credentials provided

System configuration

$ uname -a
Linux fv-az132-116 5.8.0-1033-azure #35~20.04.1-Ubuntu SMP Wed May 19 06:46:04 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
$ python --version
Python 3.8.5
$ pip show dulwich
Name: dulwich
Version: 0.20.23
Summary: Python Git Library
Home-page: https://www.dulwich.io/
Author: Jelmer Vernooij
Author-email: jelmer@jelmer.uk
License: Apachev2 or later or GPLv2
Location: /···/site-packages
Requires: urllib3, certifi
Required-by: dvc
$ git config --local http.https://github.com/.extraheader
AUTHORIZATION: basic eC1hY2Nlc3MtdG9rZW46Z2hzXyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKg==
0x2b3bfa0 commented 3 years ago

Could be loosely related to https://github.com/dulwich/dulwich/issues/822

jelmer commented 3 years ago

This depends on #718, if you wanted to set multiple headers.

-- Jelmer Vernooij @.***> PGP Key: https://www.jelmer.uk/D729A457.asc