Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/sentry/tasks/auth/check_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def check_single_auth_identity(auth_identity_id: int) -> None:
return

prev_is_valid = not getattr(om.flags, "sso:invalid")
is_verified = True

provider = auth_provider.get_provider()
try:
Expand All @@ -133,8 +134,9 @@ def check_single_auth_identity(auth_identity_id: int) -> None:
is_linked = False
is_valid = False
except Exception as exc:
# to ensure security we count any kind of error as an invalidation
# event
# The provider didn't say whether the identity is valid. Leaving `last_verified` as it
# is makes the identity invalid once no refresh has succeeded for a week.
is_verified = False
metrics.incr("auth.identities.refresh_error", skip_internal=False)
logger.exception(
"AuthIdentity(id=%s) returned an error during validation: %s",
Expand All @@ -155,4 +157,7 @@ def check_single_auth_identity(auth_identity_id: int) -> None:
organization_service.update_membership_flags(organization_member=om)

now = timezone.now()
auth_identity.update(last_verified=now, last_synced=now)
if is_verified:
auth_identity.update(last_verified=now, last_synced=now)
else:
auth_identity.update(last_synced=now)
31 changes: 31 additions & 0 deletions tests/sentry/tasks/test_check_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
AUTH_CHECK_INTERVAL,
AUTH_CHECK_SKEW,
check_auth,
check_auth_identities,
check_auth_identity,
)
from sentry.testutils.cases import TestCase
Expand Down Expand Up @@ -111,6 +112,36 @@ def test_simple(self, mock_check_auth_identity: MagicMock) -> None:
assert updated_ai.last_synced != ai.last_synced
assert updated_ai.last_verified != ai.last_verified

@patch("sentry.tasks.auth.check_auth.in_test_environment", return_value=False)
def test_failed_refresh_is_not_a_verification(self, _: MagicMock) -> None:
organization = self.create_organization(name="Test")
user = self.create_user(email="bar@example.com")
auth_provider = AuthProvider.objects.create(
organization_id=organization.id, provider="dummy"
)
om = self.create_member(
user_id=user.id, organization=organization, flags=OrganizationMember.flags["sso:linked"]
)
ai = AuthIdentity.objects.create(
auth_provider=auth_provider,
user=user,
last_verified=timezone.now() - timedelta(days=1),
last_synced=timezone.now() - timedelta(days=1),
)

with patch.object(DummyProvider, "refresh_identity", side_effect=Exception("timed out")):
check_auth_identities(auth_identity_ids=[ai.id])

# The member keeps access until the identity hasn't been verified for a week
with assume_test_silo_mode(SiloMode.CELL):
om.refresh_from_db()
assert om.flags["sso:linked"]
assert not om.flags["sso:invalid"]

updated_ai = AuthIdentity.objects.get(id=ai.id)
assert updated_ai.last_synced != ai.last_synced
assert updated_ai.last_verified == ai.last_verified

def test_skips_provider_that_does_not_require_refresh(self) -> None:
organization = self.create_organization(name="Test")
user = self.create_user(email="bar@example.com")
Expand Down
Loading