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

fix(fly): return user with correct auth #56523

Merged
merged 2 commits into from
Sep 20, 2023

Conversation

sentaur-athena
Copy link
Member

When we find 2 users for one email we return the first one so the user experience doesn't break. This is causing issue for fly because one of the users is the one that's setup with fly auth.

This fix returns the user who has the auth setup or the first user if ident is None

@sentaur-athena sentaur-athena requested a review from a team as a code owner September 19, 2023 23:36
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Sep 19, 2023
@sentaur-athena sentaur-athena requested a review from a team September 19, 2023 23:42
Comment on lines 200 to 203
for candid_user in user_query:
identity_query = AuthIdentity.objects.filter(
user=candid_user, ident=ident
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like an N+1 query? Probably want to do something like

AuthIdentity.objects.filter(user__in=user_query, ident=ident)

and then loop over the result.

It might also be worth logging if that query returns multiple AuthIdentity objects.

Copy link
Contributor

@RyanSkonnord RyanSkonnord left a comment

Choose a reason for hiding this comment

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

LGTM as auth behavior; just a small implementation note.

@codecov
Copy link

codecov bot commented Sep 20, 2023

Codecov Report

Merging #56523 (14265da) into master (69cc8ff) will decrease coverage by 0.02%.
Report is 78 commits behind head on master.
The diff coverage is 66.66%.

@@            Coverage Diff             @@
##           master   #56523      +/-   ##
==========================================
- Coverage   78.64%   78.62%   -0.02%     
==========================================
  Files        5078     5083       +5     
  Lines      218266   219131     +865     
  Branches    36952    37104     +152     
==========================================
+ Hits       171646   172291     +645     
- Misses      41085    41265     +180     
- Partials     5535     5575      +40     
Files Changed Coverage
src/sentry/services/hybrid_cloud/user/impl.py 63.63%
src/sentry/services/hybrid_cloud/user/service.py 100.00%

@@ -172,7 +174,9 @@ def get_first_superuser(self) -> Optional[RpcUser]:
return None
return serialize_rpc_user(user)

def get_or_create_user_by_email(self, *, email: str) -> RpcUser:
def get_or_create_user_by_email(
Copy link
Member

Choose a reason for hiding this comment

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

@sentaur-athena Since get_or_create_user_by_email() is called in getsentry, and the function signature has changed, it might be better to use a new function name (e.g. get_or_create_user_by_email2) so that it's easier to make the switch on getsentry.

Otherwise, the get_or_create_user_by_email() call will error out on getsentry.

Copy link
Member Author

@sentaur-athena sentaur-athena Sep 20, 2023

Choose a reason for hiding this comment

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

Interesting. @dashed would it still error although I defined the new parameters as optional? It doesn't error locally without changes in getsentry

Copy link
Member

Choose a reason for hiding this comment

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

@sentaur-athena I think it'll be fine if the new arguments are optional.

user = user_query[0]
if ident:
for candid_user in user_query:
identity_query = AuthIdentity.objects.filter(
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

@dashed I'm aware of that. The problem I'm facing is for the people who have been in this flow before and already have auth_identity created for them. For example:

  1. Athena has 2 users, Athena1 and Athena2
  2. She provisions a new org and in that process we create Athena1 an auth identity related to that org
  3. Later she provisions a project get_or_create_new_user() returns Athena2 so then auth_service.create_auth_identity() tries to create auth_identity because Athena2 doesn't have one. That would throw an error because Athena1 is using the same ident.

In this fix I want to make sure that if a user already has an ident return that one from get_or_create_new_user() otherwise it doesn't matter.

Copy link
Member

Choose a reason for hiding this comment

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

@sentaur-athena that makes sense. The case you outlined might be good to add to https://github.com/getsentry/getsentry/blob/master/tests/getsentry/web/channel_provisioning/test_process.py in case you haven't done so. 👍

@sentaur-athena sentaur-athena merged commit c9edcf3 into master Sep 20, 2023
49 of 50 checks passed
@sentaur-athena sentaur-athena deleted the athena/fix-duplicate-auth branch September 20, 2023 18:34
michellewzhang pushed a commit that referenced this pull request Sep 21, 2023
When we find 2 users for one email we return the first one so the user
experience doesn't break. This is causing issue for fly because one of
the users is the one that's setup with fly auth.

This fix returns the user who has the auth setup or the first user if
ident is None
@github-actions github-actions bot locked and limited conversation to collaborators Oct 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants