Skip to content

Log user id and token id when applicable. #2644

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 2 commits into from
Jul 21, 2020
Merged

Conversation

jsha
Copy link
Contributor

@jsha jsha commented Jul 20, 2020

In rust-lang/rfcs#2947 (comment),
@pietroalbini says, with regards to restricting the default
authorization of API tokens:

this would break everyone using the token to authenticate to endpoints
not used by Cargo. Even though we're not providing stability guarantees
for them I'd be wary of blindly breaking them. At least I'd like some
stats on which endpoints are accessed with the cookie and which are used
with tokens.

This change attempts to provide those stats via the logs. All
authenticated requests receive a uid field in the log output. If the
request was authenticated via an API token, the log output additionally
contains a tokenid field.

Because the method I used, log_request::add_custom_metadata, requires a
mutable request, I had to make req.authenticate() take a mutable ref.
Since that conflicted with many call sites that were already holding an
immutable ref to the request's DB connection, I moved the taking of the
DB connection reference after the authenticate call.

In crate_owner_invitations.rs and follow.rs, this also meant removing
duplicate authenticate calls and passing through the
already-authenticated user ID from a calling function instead.

In a few places, req.authenticate(&conn)?.find_user(&conn)? has been
replaced with two lines, one to do the authentication, and one to do the
database lookup for the user object, after let conn = req.db_conn()?

jsha added 2 commits July 19, 2020 21:58
In rust-lang/rfcs#2947 (comment),
@pietroalbini says, with regards to restricting the default
authorization of API tokens:
> this would break everyone using the token to authenticate to endpoints
> not used by Cargo. Even though we're not providing stability guarantees
> for them I'd be wary of blindly breaking them. At least I'd like some
> stats on which endpoints are accessed with the cookie and which are used
> with tokens.

This change attempts to provide those stats via the logs. All
authenticated requests receive a `uid` field in the log output. If the
request was authenticated via an API token, the log output additionally
contains a `tokenid` field.

Because the method I used, log_request::add_custom_metadata, requires a
mutable request, I had to make req.authenticate() take a mutable ref.
Since that conflicted with many call sites that were already holding an
immutable ref to the request's DB connection, I moved the taking of the
DB connection reference after the authenticate call.

In crate_owner_invitations.rs and follow.rs, this also meant removing
duplicate authenticate calls and passing through the
already-authenticated user ID from a calling function instead.

In a few places, `req.authenticate(&conn)?.find_user(&conn)?` has been
replaced with two lines, one to do the authentication, and one to do the
database lookup for the user object, after `let conn = req.db_conn()?`
@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @smarnach (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@pietroalbini
Copy link
Member

Thanks!

r? @pietroalbini
@bors r+

@bors
Copy link
Contributor

bors commented Jul 21, 2020

📌 Commit bbadd1e has been approved by pietroalbini

@bors
Copy link
Contributor

bors commented Jul 21, 2020

⌛ Testing commit bbadd1e with merge 03ac789...

@bors
Copy link
Contributor

bors commented Jul 21, 2020

☀️ Test successful - checks-travis
Approved by: pietroalbini
Pushing 03ac789 to master...

@bors bors merged commit 03ac789 into rust-lang:master Jul 21, 2020
@jsha jsha deleted the log-auth-type branch July 21, 2020 16:46
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.

5 participants