Skip to content
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

Tokenless V3 #533

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Tokenless V3 #533

wants to merge 1 commit into from

Conversation

joseph-sentry
Copy link
Contributor

@joseph-sentry joseph-sentry commented May 1, 2024

Purpose/Motivation

We don't want to make GH API calls when doing tokenless anymore

Links to relevant tickets

codecov/engineering-team#1574

Related: codecov/codecov-action#1406

What does this PR do?

  • Remove GitHub API calls from TokenlessAuthentication

@joseph-sentry joseph-sentry requested a review from a team as a code owner May 1, 2024 15:15
@codecov-qa
Copy link

codecov-qa bot commented May 1, 2024

Codecov Report

Attention: Patch coverage is 82.25806% with 11 lines in your changes are missing coverage. Please review.

Project coverage is 91.45%. Comparing base (5954d0e) to head (fecbe1b).
Report is 14 commits behind head on main.

✅ All tests successful. No failed tests found.

Files Patch % Lines
codecov_auth/authentication/repo_auth.py 81.96% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #533      +/-   ##
==========================================
- Coverage   91.48%   91.45%   -0.03%     
==========================================
  Files         599      602       +3     
  Lines       16268    16394     +126     
==========================================
+ Hits        14882    14993     +111     
- Misses       1386     1401      +15     
Flag Coverage Δ
unit 91.45% <82.25%> (-0.03%) ⬇️
unit-latest-uploader 91.45% <82.25%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

codecov-public-qa bot commented May 1, 2024

Codecov Report

Attention: Patch coverage is 82.25806% with 11 lines in your changes are missing coverage. Please review.

Project coverage is 91.45%. Comparing base (5954d0e) to head (fecbe1b).
Report is 14 commits behind head on main.

✅ All tests successful. No failed tests found ☺️

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #533      +/-   ##
==========================================
- Coverage   91.48%   91.45%   -0.03%     
==========================================
  Files         599      602       +3     
  Lines       16268    16394     +126     
==========================================
+ Hits        14882    14993     +111     
- Misses       1386     1401      +15     
Flag Coverage Δ
unit 91.45% <82.25%> (-0.03%) ⬇️
unit-latest-uploader 91.45% <82.25%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
upload/views/commits.py 100.00% <100.00%> (ø)
upload/views/reports.py 100.00% <ø> (ø)
codecov_auth/authentication/repo_auth.py 93.24% <81.96%> (-5.24%) ⬇️

... and 16 files with indirect coverage changes

Impacted file tree graph

Copy link

codecov bot commented May 1, 2024

Codecov Report

Attention: Patch coverage is 82.25806% with 11 lines in your changes are missing coverage. Please review.

Project coverage is 95.78%. Comparing base (5954d0e) to head (fecbe1b).
Report is 14 commits behind head on main.

Changes have been made to critical files, which contain lines commonly executed in production. Learn more

✅ All tests successful. No failed tests found.

Files Patch % Lines
codecov_auth/authentication/repo_auth.py 81.96% 11 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff           @@
##            main    #533    +/-   ##
======================================
  Coverage   95.78   95.78            
======================================
  Files        774     777     +3     
  Lines      17124   17549   +425     
======================================
+ Hits       16401   16809   +408     
- Misses       723     740    +17     
Flag Coverage Δ
unit 91.45% <82.25%> (-0.03%) ⬇️
unit-latest-uploader 91.45% <82.25%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@giovanni-guidini
Copy link
Contributor

We need to be careful merging these changes to avoid re-introducing the bug of having an upload from a fork branch accidentally overwrite coverage in the upstream branch.

Considering the upload endpoints used by the CLI either we change the CLI to send the branch info every time (maybe through headers) OR we use the info we have to make the validation that the branch name is in the format fork:branch. If that is not the case, reject the upload.

Currently I believe it should be possible to make this check:

  • For the commit creation the branch should be part of the body
  • For the subsequent requests (create report and send the upload) we can pull the commit from the database and check its branch information.

Personally I feel that we should be able to accept / reject a request without having to look at the request body. So I'd opt to make the CLI send specific headers with the information we need.
(Also by removing the current checks maybe X-Tokenless-PR header is useless?)

@joseph-sentry
Copy link
Contributor Author

joseph-sentry commented May 2, 2024

So I'd opt to make the CLI send specific headers with the information we need.
(Also by removing the current checks maybe X-Tokenless-PR header is useless?)

We could just validate that the x-tokenless-pr header is in the format we expect and has the correct repo name (it matches the one in the url)?

@giovanni-guidini
Copy link
Contributor

X-Tokenless-PR is a number [1] that we used to get the correct PR from the provider. That does little for us considering the changes in this PR. If you just change the value the header name will be misleading.

X-Tokenless should not match the repo name in the URL if it comes from a fork [1], cause it's supposed to be the fork slug, while the slug in the URL should be the upstream's.

[1] https://github.com/codecov/codecov-cli/blob/7b028499a521f8df3cf3bb6b642246a19e07bee9/codecov_cli/services/report/__init__.py#L57-L58

- there are now 2 tokenless authentication modes,
  one for the commits endpoint and
  one for the reports/uploads endpoints
- the commit endpoint just requires the X-Tokenless header
  to be present and have the format "<user>:<branch name>"
  the branch of the commit that will be created will be set
  to the value of the X-Tokenless header.
- the reports and uploads enpoints require the X-Tokenless
  header to specify the branch name of the commit that they
  are targetting and it should match the format from the
  commits endpoint
- All endpoints require the repository that is being targetted
  to be public

Signed-off-by: joseph-sentry <[email protected]>
@joseph-sentry joseph-sentry changed the title fix: don't make api calls in tokenless auth Tokenless V3 May 6, 2024
if ":" not in tokenless:
raise exceptions.AuthenticationFailed(tokenless_auth_failed_message)

# make sure it's backwards compatible with the old way that
Copy link
Contributor

Choose a reason for hiding this comment

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

It can't be compatible if validation fails (in the line above) because "the old way" doesn't include a : in the X-Tokenless header, ...right?

Copy link
Contributor

@matt-codecov matt-codecov left a comment

Choose a reason for hiding this comment

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

this probably belongs in another PR, but in addition to getting rid of the get_pull_request_info() check like you do here, we also want to get rid of the "recent CI run" check. that happens in upload/helpers.py. with that gone, i think we can delete the whole upload/tokenless directory

also, the other day you made a great point about the report/upload endpoints being able to get the branch from the commit rather than needing it to be sent by the CLI. i think we can take advantage of that to simplify this logic across the API, CLI, and CI actions

on the CLI side, we already have to send the branch name in the request body for the create-commit step, and we already modify the branch name for forks there. the other two steps include which commit they are working with, so the API will be able to look up the commit and get the branch from there. so none of the commands need to set X-Tokenless, and the action doesn't have to set any extra env vars. (i think this also avoids an edge case where an attacker passes authentication by passing X-Tokenless: fork:hahaha to the do-upload endpoint but the commit SHA they passed is actually a commit on main)

then on the API side, i think the only difference between the commit endpoint and the others is where we get the branch name from? if so, i think we can merge back into one TokenAuthentication class, and if the commit sha is part of the URL we get the branch from the commit DB object, otherwise we look for the branch in the request body because we're creating a new commit

my brain is a little fried right now so this comment might not be very useful. i'll do another pass tomorrow, or maybe we could hop on a call and talk about it

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.

None yet

3 participants