Support .netrc by trust_env#2584
Conversation
aiohttp/helpers.py
Outdated
| netrc_obj = None | ||
| netrc_path = os.environ.get('netrc') | ||
| if netrc_path is None: | ||
| home_dir = os.path.expanduser('~') |
There was a problem hiding this comment.
Could you use pathlib instead of os.path?
E.g. https://docs.python.org/3/library/pathlib.html#pathlib.Path.home for getting user home folder.
aiohttp/helpers.py
Outdated
|
|
||
| def proxies_from_env(): | ||
| netrc_obj = None | ||
| netrc_path = os.environ.get('netrc') |
There was a problem hiding this comment.
Environment names are case sensitive, according to https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html the name should be upper-cased: NETRC.
|
|
tests/test_proxy_functional.py
Outdated
| url = 'http://aiohttp.io/path' | ||
| proxy = await proxy_test_server() | ||
| auth = aiohttp.BasicAuth('user', 'pass') | ||
| netrc_file = tempfile.mktemp() |
There was a problem hiding this comment.
Why you use tempfile module? Is tmpdir fixture functionality not enough?
- Change environment name to standard uppercase - Change to use pathlib to get home dir
- use tmpdir fixture
- Remove the unused import
Codecov Report
@@ Coverage Diff @@
## master #2584 +/- ##
==========================================
+ Coverage 97.87% 97.88% +<.01%
==========================================
Files 38 38
Lines 7304 7327 +23
Branches 1263 1268 +5
==========================================
+ Hits 7149 7172 +23
Misses 51 51
Partials 104 104
Continue to review full report at Codecov.
|
|
I'm so sorry, take so long to changes code. not familiar to pathlib, I should take more time to think then code. |
|
No problem |
- full cover tests
_ Extract `netrc_from_env` from `proxies_from_env` - make real netrc file invisible in home dif for test
|
if I use Lines 252 to 265 in dbb8d9a |
|
Sorry, had no time to look on changes. |
aiohttp/helpers.py
Outdated
| if netrc_path and netrc_path.is_file(): | ||
| try: | ||
| netrc_obj = netrc.netrc(str(netrc_path)) | ||
| except (netrc.NetrcParseError, IOError) as e: |
There was a problem hiding this comment.
Rename IOError to OSError -- they are aliases but OSError is the preferable name.
aiohttp/helpers.py
Outdated
| auth_from_netrc = netrc_obj.authenticators(proxy.host) | ||
| if auth_from_netrc is not None: | ||
| *logins, password = auth_from_netrc | ||
| auth = BasicAuth(logins[0] if logins[0] else logins[1], |
There was a problem hiding this comment.
logins[-1] should be enough.
Please add a comment when logins[0] can be empty (file is started with tab I guess).
There was a problem hiding this comment.
https://github.com/requests/requests/blob/24092b11d74af0a766d9cc616622f38adb0044b9/requests/utils.py#L203-L207
Requests code shows me how to handle it, and i saw the netrc source code.
netrc.authenticators method return a (user, account, password) tuple.
I guess user and account both can be username.
if netrc_obj and auth is None:
auth_from_netrc = netrc_obj.authenticators(proxy.host)
if auth_from_netrc is not None:
# auth_from_netrc is a (`user`, `account`, `password`) tuple,
# `user` and `account` both can be username,
# if `user` is None, use `account`
*logins, password = auth_from_netrc
auth = BasicAuth(logins[0] if logins[0] else logins[-1],
password)|
Create a .netrc or _netrc file says the netrc file should be set to at least Read (400), or Read/Write (600), unreadable to everyone else. |
|
No, |
|
Thanks! |
|
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. |
What do these changes do?
Support .netrc by trust_env
Are there changes in behavior for the user?
None
Related issue number
#2581
Checklist
CONTRIBUTORS.txtCHANGESfolder<issue_id>.<type>for example (588.bug)issue_idchange it to the pr id after creating the pr.feature: Signifying a new feature..bugfix: Signifying a bug fix..doc: Signifying a documentation improvement..removal: Signifying a deprecation or removal of public API..misc: A ticket has been closed, but it is not of interest to users.