Skip to content

Commit

Permalink
enh(Zendesk) - add a check for user being an admin upon update (#9876)
Browse files Browse the repository at this point in the history
* add a check for user being an admin upon update

* rename the message code

* update the error type

* fix the error type caught and add a comment

* make the error type connector_oauth_user_missing_rights reusable

* update the error message
  • Loading branch information
aubin-tchoi authored Jan 10, 2025
1 parent 2eb779b commit e157911
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
8 changes: 8 additions & 0 deletions connectors/src/api/update_connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ const _postConnectorUpdateAPIHandler = async (
message: updateRes.error.message,
},
});
case "CONNECTOR_OAUTH_USER_MISSING_RIGHTS":
return apiError(req, res, {
status_code: 401,
api_error: {
type: "connector_oauth_user_missing_rights",
message: updateRes.error.message,
},
});
case "INVALID_CONFIGURATION":
return apiError(req, res, {
status_code: 400,
Expand Down
5 changes: 3 additions & 2 deletions connectors/src/connectors/interface.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import type {
ConnectorConfiguration,
ConnectorPermission,
ContentNode,
ContentNodesViewType,
ModelId,
Result,
} from "@dust-tt/types";
import type { ConnectorConfiguration } from "@dust-tt/types";

import type { DataSourceConfig } from "@connectors/types/data_source_config";

export type CreateConnectorErrorCode = "INVALID_CONFIGURATION";

export type UpdateConnectorErrorCode =
| "INVALID_CONFIGURATION"
| "CONNECTOR_OAUTH_TARGET_MISMATCH";
| "CONNECTOR_OAUTH_TARGET_MISMATCH"
| "CONNECTOR_OAUTH_USER_MISSING_RIGHTS";

export type RetrievePermissionsErrorCode =
| "INVALID_PARENT_INTERNAL_ID"
Expand Down
22 changes: 19 additions & 3 deletions connectors/src/connectors/zendesk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ import {
forbidSyncZendeskTickets,
} from "@connectors/connectors/zendesk/lib/ticket_permissions";
import { getZendeskSubdomainAndAccessToken } from "@connectors/connectors/zendesk/lib/zendesk_access_token";
import { fetchZendeskCurrentUser } from "@connectors/connectors/zendesk/lib/zendesk_api";
import {
fetchZendeskCurrentUser,
isUserAdmin,
} from "@connectors/connectors/zendesk/lib/zendesk_api";
import {
launchZendeskFullSyncWorkflow,
launchZendeskGarbageCollectionWorkflow,
Expand Down Expand Up @@ -74,7 +77,7 @@ export class ZendeskConnectorManager extends BaseConnectorManager<null> {
subdomain,
accessToken,
});
if (!zendeskUser.active || zendeskUser.role !== "admin") {
if (!isUserAdmin(zendeskUser)) {
throw new ExternalOAuthTokenError(
new Error(`Zendesk user is not an admin: connectionId=${connectionId}`)
);
Expand Down Expand Up @@ -145,7 +148,7 @@ export class ZendeskConnectorManager extends BaseConnectorManager<null> {
if (connectionId) {
const newConnectionId = connectionId;

const { subdomain: newSubdomain } =
const { accessToken, subdomain: newSubdomain } =
await getZendeskSubdomainAndAccessToken(newConnectionId);

if (configuration.subdomain !== newSubdomain) {
Expand All @@ -157,6 +160,19 @@ export class ZendeskConnectorManager extends BaseConnectorManager<null> {
);
}

const zendeskUser = await fetchZendeskCurrentUser({
subdomain: newSubdomain,
accessToken,
});
if (!isUserAdmin(zendeskUser)) {
return new Err(
new ConnectorManagerError(
"CONNECTOR_OAUTH_USER_MISSING_RIGHTS",
"New authenticated user is not an admin"
)
);
}

await connector.update({ connectionId: newConnectionId });

// if the connector was previously paused, unpause it.
Expand Down
4 changes: 4 additions & 0 deletions connectors/src/connectors/zendesk/lib/zendesk_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ export async function fetchZendeskCurrentUser({
return response.user;
}

export function isUserAdmin(user: ZendeskFetchedUser): boolean {
return user.active && user.role === "admin";
}

/**
* Fetches a multiple users at once from the Zendesk API.
* May run multiple queries, more precisely we need userCount // 100 + 1 API calls.
Expand Down
32 changes: 19 additions & 13 deletions front/components/ConnectorPermissionsModal.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
import type { NotificationType } from "@dust-tt/sparkle";
import {
NewDialog,
NewDialogContainer,
NewDialogContent,
NewDialogFooter,
NewDialogHeader,
NewDialogTitle,
NewDialogTrigger,
Spinner,
} from "@dust-tt/sparkle";
import { SheetContainer, SheetTitle } from "@dust-tt/sparkle";
import { SheetHeader } from "@dust-tt/sparkle";
import {
Avatar,
Button,
Expand All @@ -21,12 +9,23 @@ import {
Input,
LockIcon,
Modal,
NewDialog,
NewDialogContainer,
NewDialogContent,
NewDialogFooter,
NewDialogHeader,
NewDialogTitle,
NewDialogTrigger,
Page,
Sheet,
SheetContainer,
SheetContent,
SheetHeader,
SheetTitle,
Spinner,
TrashIcon,
useSendNotification,
} from "@dust-tt/sparkle";
import { useSendNotification } from "@dust-tt/sparkle";
import type {
APIError,
BaseContentNode,
Expand Down Expand Up @@ -205,6 +204,13 @@ async function updateConnectorConnectionId(
error: CONNECTOR_TYPE_TO_MISMATCH_ERROR[provider as ConnectorProvider],
};
}
if (error.type === "connector_oauth_user_missing_rights") {
return {
success: false,
error:
"The authenticated user needs higher permissions from your service provider.",
};
}
return {
success: false,
error: `Failed to update the permissions of the Data Source: (contact support@dust.tt for assistance)`,
Expand Down
1 change: 1 addition & 0 deletions types/src/connectors/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type ConnectorsAPIErrorType =
| "connector_update_error"
| "connector_update_unauthorized"
| "connector_oauth_target_mismatch"
| "connector_oauth_user_missing_rights"
| "connector_oauth_error"
| "connector_authorization_error"
| "slack_channel_not_found"
Expand Down

0 comments on commit e157911

Please sign in to comment.