feat(auth): add URL-dispatched session auth for GitHub and GitLab#1173
Conversation
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughThis PR refactors Fromager's GitHub and GitLab API authentication from scattered caller-side token management to a centralized Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
The new code generalized authentication for GitHub and GitLab. We can drop a bunch of custom code from downstream. |
There was a problem hiding this comment.
The caching/lazy mechanism makes sense to me as I read it, but I'm wondering if the complexity is worth it in terms of what we're saving/avoiding here. I had to step through this and think about it to make sure I understood - for example - the cache invalidation.
Without the caching, we're basically re-reading a .netrc on every request. Is that it?
If so, would it be simpler to just make that file read a singleton and change our callback signature so that we pass in the parsed netrc as a parameter (psuedo-code ahead)
class SessionAuth(requests.auth.AuthBase):
def __init__(self) -> None:
self._netrc = get_netrc_auth()
def get(self, url: str) -> dict[str, str]:
...
callback = self._callbacks.get(key)
return callback(*key, self._netrc) if callback else {}|
I should also say, @tiran I don't feel particularly strongly about #1173 (review) If you're happy with the implementation as-is, I'm happy to give a thumbs up 👍 (though my approval is non-voting on merge). This in particular:
...makes me inclined to say "ship it". |
rd4398
left a comment
There was a problem hiding this comment.
Looks overall good. I have left a couple of comments
Kind of. The two callbacks use requests' utils to locate, load, and parse a netrc file, then find a matching entry for an URL. In the future, callbacks may do other things that do not involve a netrc file. |
+1 to what @ryanpetrello commented. Alternate suggestion: |
|
What profiling? We don't do performance profiling regularly. |
Yeah, I missed that part. |
|
@Mergifyio rebase |
Add `SessionAuth` class that dispatches authentication by (scheme, hostname). Lazy callbacks resolve credentials from netrc or environment variables on first request and cache the result. `create_session()` returns the session and auth handler so callers can register additional hosts. Move authentication documentation to docs/how-tos/authentication.md and update http-retry.md to reference the new guide. Co-Authored-By: Claude <claude@anthropic.com> Signed-off-by: Christian Heimes <cheimes@redhat.com>
|
Deprecation notice: This pull request comes from a fork and was rebased using |
✅ Branch has been successfully rebased |
Pull Request Description
What
Add
SessionAuthclass that dispatches authentication by (scheme, hostname). Lazy callbacks resolve credentials from netrc or environment variables on first request and cache the result.create_session()returns the session and auth handler so callers can register additional hosts.Move authentication documentation to docs/how-tos/authentication.md and update http-retry.md to reference the new guide.
Why
see #1168