Skip to content

Commit 45b8930

Browse files
committed
docs(advanced): document new netrc behavior
BREAKING CHANGE: python-gitlab now explicitly passes auth to requests, meaning it will only read netrc credentials if no token is provided, fixing a bug where netrc credentials took precedence over OAuth tokens. This also affects the CLI, where all environment variables now take precedence over netrc files.
1 parent 5f46cfd commit 45b8930

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

docs/api-usage-advanced.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ properly closed when you exit a ``with`` block:
4444
netrc authentication
4545
--------------------
4646

47-
python-gitlab reads credentials from ``.netrc`` files via the ``requests`` backend by default,
48-
which may override authentication headers you set on your client.
47+
python-gitlab reads credentials from ``.netrc`` files via the ``requests`` backend
48+
only if you do not provide any other type of authentication yourself.
4949

50-
For more granular control, you can disable this `Using a custom session`_
51-
and explicitly setting ``trust_env=False`` as described in the ``requests`` documentation.
50+
If you'd like to disable reading netrc files altogether, you can follow `Using a custom session`_
51+
and explicitly set ``trust_env=False`` as described in the ``requests`` documentation.
5252

5353
.. code-block:: python
5454

tests/unit/test_gitlab_auth.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1+
import pathlib
2+
13
import pytest
24
import requests
5+
import responses
36
from requests import PreparedRequest
47

58
from gitlab import Gitlab
69
from gitlab._backends import JobTokenAuth, OAuthTokenAuth, PrivateTokenAuth
710
from gitlab.config import GitlabConfigParser
811

912

13+
@pytest.fixture
14+
def netrc(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path):
15+
netrc_file = tmp_path / ".netrc"
16+
netrc_file.write_text("machine localhost login test password test")
17+
monkeypatch.setenv("NETRC", str(netrc_file))
18+
19+
1020
def test_invalid_auth_args():
1121
with pytest.raises(ValueError):
1222
Gitlab(
@@ -101,6 +111,30 @@ def test_http_auth():
101111
assert "JOB-TOKEN" not in p.headers
102112

103113

114+
@responses.activate
115+
def test_with_no_auth_uses_netrc_file(netrc):
116+
responses.get(
117+
url="http://localhost/api/v4/test",
118+
match=[
119+
responses.matchers.header_matcher({"Authorization": "Basic dGVzdDp0ZXN0"})
120+
],
121+
)
122+
123+
gl = Gitlab("http://localhost")
124+
gl.http_get("/test")
125+
126+
127+
@responses.activate
128+
def test_with_auth_ignores_netrc_file(netrc):
129+
responses.get(
130+
url="http://localhost/api/v4/test",
131+
match=[responses.matchers.header_matcher({"Authorization": "Bearer test"})],
132+
)
133+
134+
gl = Gitlab("http://localhost", oauth_token="test")
135+
gl.http_get("/test")
136+
137+
104138
@pytest.mark.parametrize(
105139
"options,config,expected_private_token,expected_oauth_token,expected_job_token",
106140
[

0 commit comments

Comments
 (0)