Skip to content

Use AppBearerTokenAuth in login_as_app_installation #1009

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

Merged
merged 1 commit into from
Dec 22, 2020

Conversation

dato
Copy link
Contributor

@dato dato commented Dec 20, 2020

Previously, this method used an Authorization header via headers
parameter, instead of an AuthBase instance via auth parameter.

But the requests library has this behavior where it will strip the
Authorization header on redirects, and then pick up fresh credentials
from other sources (~/.netrc in particular, if present).

Any AuthBase object, though, is still attached to the request, so this
makes for an incredibly difficult issue to debug: all of github3.py
uses AuthBase to authenticate, except login_as_app_installation. The
latter thus being the only place in which the issue can manifest, when
~/.netrc has an entry for api.github.com. By using AppBearerTokenAuth,
this can no longer happen.

@opendev-zuul
Copy link

opendev-zuul bot commented Dec 20, 2020

Starting third-party-check jobs.
Warning:
Failed to update check run openstack/third-party-check: 403 Resource not accessible by integration

@dato
Copy link
Contributor Author

dato commented Dec 20, 2020

This took me a long time to track down (and it's no fault of github3, really). But just using authentication consistently throughout the library seems better (behavior is consistent in every method).

I marked it as draft because I'm unsure if there might be any unwanted side-effects (I don't think so, but...).

Many thanks for considering.

@dato
Copy link
Contributor Author

dato commented Dec 20, 2020

Also unsure if the session.no_auth() context manager is still needed, given that we're providing auth=bearer_auth explicitly.

@opendev-zuul
Copy link

opendev-zuul bot commented Dec 20, 2020

Build failed (third-party-check pipeline) integration testing on
OpenDev. For information on how to proceed, see
https://docs.opendev.org/opendev/infra-manual/latest/developers.html#automated-testing

Warning:
Failed to update check run openstack/third-party-check: 403 Resource not accessible by integration
Failed to create check run openstack/third-party-check: 403 Resource not accessible by integration

@@ -171,26 +171,3 @@ def create_token(private_key_pem, app_id, expire_in=TEN_MINUTES_AS_SECONDS):
)
token.make_signed_token(key)
return token.serialize()


def create_jwt_headers(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a publicly named function in a publicly named module. We can't just get rid of it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored

@sigmavirus24
Copy link
Owner

This looks generally fine. The question I have is why on earth is GitHub sending you a redirect here?

@dato dato force-pushed the bearer_auth_login branch from 50573f1 to 898a9d7 Compare December 20, 2020 14:41
@dato dato marked this pull request as ready for review December 20, 2020 14:41
@opendev-zuul
Copy link

opendev-zuul bot commented Dec 20, 2020

Starting third-party-check jobs.
Warning:
Failed to update check run openstack/third-party-check: 403 Resource not accessible by integration

@dato
Copy link
Contributor Author

dato commented Dec 20, 2020

Oops, good point about the public function. I've reverted that part and pushed a new version of the commit.

Re: redirects, I poked it a bit further, and it turns out it wasn't hitting the redirect path, but this other path:

https://github.com/psf/requests/blob/c2b307dbefe21177af03f9feb37181a89a799fcc/requests/sessions.py#L450-L453

(At least, if I comment out that block, an unmodified github3.py works for me.)

I've marked the PR as ready for review, but the commit description could still be amended to reflect this last fact. I could do that if you'd like.

@opendev-zuul
Copy link

opendev-zuul bot commented Dec 20, 2020

Build failed (third-party-check pipeline) integration testing on
OpenDev. For information on how to proceed, see
https://docs.opendev.org/opendev/infra-manual/latest/developers.html#automated-testing

Warning:
Failed to update check run openstack/third-party-check: 403 Resource not accessible by integration
Failed to create check run openstack/third-party-check: 403 Resource not accessible by integration

@sigmavirus24
Copy link
Owner

I've marked the PR as ready for review, but the commit description could still be amended to reflect this last fact. I could do that if you'd like.

Please do. That would make it clearer for future folks what is happening.

Otherwise, this looks great. Thanks!

Previously, this method used an Authorization header via `headers`
parameter, instead of an AuthBase instance via the `auth` parameter.

But the requests library has this behavior where it will try
to pick up authentication credentials from ~/.netrc if neither
session nor request has an associated AuthBase object. (A sole
Authorization header will not prevent this behavior.)

Since all of github3.py uses AuthBase instances to
authenticate, but login_as_app_installation didn't, this makes
for an incredibly difficult issue to debug, since the latter
is the only place in which the issue can manifest, if a user's
~/.netrc has an entry for api.github.com.

By using AppBearerTokenAuth, this can no longer happen, since
the ~/.netrc path is not hit any more.
@dato dato force-pushed the bearer_auth_login branch from 898a9d7 to 655c8e1 Compare December 20, 2020 21:31
@opendev-zuul
Copy link

opendev-zuul bot commented Dec 20, 2020

Starting third-party-check jobs.
Warning:
Failed to update check run openstack/third-party-check: 403 Resource not accessible by integration

@dato
Copy link
Contributor Author

dato commented Dec 20, 2020

Please do. That would make it clearer for future folks what is happening.

Done. (I see you tend to merge PRs, and not squash, so I didn't update the PR description in the web UI.)

Thanks!

@opendev-zuul
Copy link

opendev-zuul bot commented Dec 20, 2020

Build failed (third-party-check pipeline) integration testing on
OpenDev. For information on how to proceed, see
https://docs.opendev.org/opendev/infra-manual/latest/developers.html#automated-testing

Warning:
Failed to update check run openstack/third-party-check: 403 Resource not accessible by integration
Failed to create check run openstack/third-party-check: 403 Resource not accessible by integration

@sigmavirus24
Copy link
Owner

Closing and re-opening to try to retrigger GHA

@opendev-zuul
Copy link

opendev-zuul bot commented Dec 22, 2020

Starting third-party-check jobs.
Warning:
Failed to update check run openstack/third-party-check: 403 Resource not accessible by integration

@opendev-zuul
Copy link

opendev-zuul bot commented Dec 22, 2020

Build failed (third-party-check pipeline) integration testing on
OpenDev. For information on how to proceed, see
https://docs.opendev.org/opendev/infra-manual/latest/developers.html#automated-testing

Warning:
Failed to update check run openstack/third-party-check: 403 Resource not accessible by integration
Failed to create check run openstack/third-party-check: 403 Resource not accessible by integration

@sigmavirus24
Copy link
Owner

Updated our github actions to build on pull requests too which they weren't before. Let me close/reopen one last time.

@opendev-zuul
Copy link

opendev-zuul bot commented Dec 22, 2020

Starting third-party-check jobs.
Warning:
Failed to update check run openstack/third-party-check: 403 Resource not accessible by integration

@opendev-zuul
Copy link

opendev-zuul bot commented Dec 22, 2020

Build failed (third-party-check pipeline) integration testing on
OpenDev. For information on how to proceed, see
https://docs.opendev.org/opendev/infra-manual/latest/developers.html#automated-testing

Warning:
Failed to update check run openstack/third-party-check: 403 Resource not accessible by integration
Failed to create check run openstack/third-party-check: 403 Resource not accessible by integration

@sigmavirus24 sigmavirus24 merged commit f3bf7cb into sigmavirus24:master Dec 22, 2020
@sigmavirus24
Copy link
Owner

Thank you @dato !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants