Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 364
fix: surface permission sync issues requiring user action#1484
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
936433f79189169bef7dfa3c540aa962821File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| import * as Sentry from "@sentry/node"; | ||
| import { PrismaClient, AccountPermissionSyncJobStatus, Account, PermissionSyncSource} from "@sourcebot/db"; | ||
| import { | ||
| PrismaClient, | ||
| AccountPermissionSyncIssue, | ||
| AccountPermissionSyncJobStatus, | ||
| Account, | ||
| PermissionSyncSource, | ||
| } from "@sourcebot/db"; | ||
| import { env, createLogger, getIdentityProviderConfig, PERMISSION_SYNC_SUPPORTED_IDENTITY_PROVIDERS } from "@sourcebot/shared"; | ||
| import { hasEntitlement } from "../entitlements.js"; | ||
| import { ensureFreshAccountToken, TokenRefreshError } from "./tokenRefresh.js"; | ||
| @@ -46,10 +52,22 @@ export type PermissionCleanupDecision = | ||
| action: 'preserve_permissions'; | ||
| }; | ||
| const PERMISSION_CLEANUP_REASON_MESSAGES: Record<PermissionCleanupReason, string> = { | ||
| oauth_refresh_token_rejected: 'OAuth refresh token rejection', | ||
| upstream_credential_rejected: 'upstream credential rejection', | ||
| upstream_insufficient_scope: 'insufficient OAuth scope', | ||
| const PERMISSION_CLEANUP_DETAILS: Record<PermissionCleanupReason, { | ||
| message: string; | ||
| issue: AccountPermissionSyncIssue; | ||
| }> = { | ||
| oauth_refresh_token_rejected: { | ||
| message: 'OAuth refresh token rejection', | ||
| issue: AccountPermissionSyncIssue.REAUTHENTICATION_REQUIRED, | ||
| }, | ||
| upstream_credential_rejected: { | ||
| message: 'upstream credential rejection', | ||
| issue: AccountPermissionSyncIssue.REAUTHENTICATION_REQUIRED, | ||
| }, | ||
| upstream_insufficient_scope: { | ||
| message: 'insufficient OAuth scope', | ||
| issue: AccountPermissionSyncIssue.INSUFFICIENT_SCOPE, | ||
| }, | ||
| }; | ||
| export const classifyPermissionSyncFailure = (error: unknown): PermissionCleanupDecision => { | ||
| @@ -238,12 +256,21 @@ export class AccountPermissionSyncer { | ||
| const cleanupDecision = classifyPermissionSyncFailure(error); | ||
| if (cleanupDecision.action === 'clear_permissions') { | ||
| const { count } = await this.db.accountToRepoPermission.deleteMany({ | ||
| where: { accountId: account.id }, | ||
| }); | ||
| const details = PERMISSION_CLEANUP_DETAILS[cleanupDecision.reason]; | ||
| const [{ count }] = await this.db.$transaction([ | ||
| this.db.accountToRepoPermission.deleteMany({ | ||
| where: { accountId: account.id }, | ||
| }), | ||
| this.db.account.update({ | ||
| where: { id: account.id }, | ||
| data: { | ||
| permissionSyncIssue: details.issue, | ||
| permissionSyncIssueAt: new Date(), | ||
| }, | ||
| }), | ||
| ]); | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| const reason = PERMISSION_CLEANUP_REASON_MESSAGES[cleanupDecision.reason]; | ||
| logger.warn(`Cleared ${count} permission row(s) for account ${account.id} (user ${account.user.email ?? 'unknown'}) — fail-closed cleanup triggered by ${reason}: ${message}`); | ||
| logger.warn(`Cleared ${count} permission row(s) for account ${account.id} (user ${account.user.email ?? 'unknown'}) — fail-closed cleanup triggered by ${details.message}: ${message}`); | ||
| } | ||
| throw error; | ||
| } | ||
| @@ -451,6 +478,8 @@ export class AccountPermissionSyncer { | ||
| account: { | ||
| update: { | ||
| permissionSyncedAt: new Date(), | ||
| permissionSyncIssue: null, | ||
| permissionSyncIssueAt: null, | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }, | ||
| }, | ||
| completedAt: new Date(), | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| -- CreateEnum | ||
| CREATE TYPE "AccountPermissionSyncIssue" AS ENUM ('REAUTHENTICATION_REQUIRED', 'INSUFFICIENT_SCOPE'); | ||
| -- AlterTable | ||
| ALTER TABLE "Account" | ||
| ADD COLUMN "permissionSyncIssue" "AccountPermissionSyncIssue", | ||
| ADD COLUMN "permissionSyncIssueAt" TIMESTAMP(3); | ||
brendan-kellam marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.