Skip to content

Commit

Permalink
Revert "[front] Allow multiple wrappers to run and fetch resources (#…
Browse files Browse the repository at this point in the history
…9682)" (#9738)

This reverts commit 4030dd0.
  • Loading branch information
tdraier authored Jan 3, 2025
1 parent 6c3afac commit 9fc6c81
Show file tree
Hide file tree
Showing 35 changed files with 302 additions and 472 deletions.
389 changes: 96 additions & 293 deletions front/lib/api/resource_wrappers.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<RunAppResponseType>>,
auth: Authenticator,
{ space }: { space: SpaceResource }
space: SpaceResource
): Promise<void> {
const owner = auth.getNonNullableWorkspace();

Expand Down Expand Up @@ -145,5 +145,5 @@ async function handler(
}

export default withPublicAPIAuthentication(
withResourceFetchingFromRoute(handler, { space: { requireCanRead: true } })
withResourceFetchingFromRoute(handler, "space")
);
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<RunAppResponseType>>,
auth: Authenticator,
{ space }: { space: SpaceResource },
space: SpaceResource,
keyAuth: Authenticator
): Promise<void> {
const owner = auth.getNonNullableWorkspace();
Expand Down Expand Up @@ -506,8 +506,7 @@ async function handler(
}

export default withPublicAPIAuthentication(
// Check read on the workspace authenticator - for public space, everybody can read
withResourceFetchingFromRoute(handler, { space: { requireCanRead: true } }),
withResourceFetchingFromRoute(handler, "space"),
{
allowUserOutsideCurrentWorkspace: true,
}
Expand Down
4 changes: 2 additions & 2 deletions front/pages/api/v1/w/[wId]/spaces/[spaceId]/apps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<GetAppsResponseType>>,
auth: Authenticator,
{ space }: { space: SpaceResource }
space: SpaceResource
): Promise<void> {
if (!space.canList(auth)) {
return apiError(req, res, {
Expand Down Expand Up @@ -120,5 +120,5 @@ async function handler(
}

export default withPublicAPIAuthentication(
withResourceFetchingFromRoute(handler, { space: { requireCanList: true } })
withResourceFetchingFromRoute(handler, "space")
);
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { withPublicAPIAuthentication } from "@app/lib/api/auth_wrappers";
import { handlePatchDataSourceView } from "@app/lib/api/data_source_view";
import { withResourceFetchingFromRoute } from "@app/lib/api/resource_wrappers";
import type { Authenticator } from "@app/lib/auth";
import type { DataSourceViewResource } from "@app/lib/resources/data_source_view_resource";
import { DataSourceViewResource } from "@app/lib/resources/data_source_view_resource";
import type { SpaceResource } from "@app/lib/resources/space_resource";
import { apiError } from "@app/logger/withlogging";

/**
Expand Down Expand Up @@ -151,9 +152,35 @@ async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<DataSourceViewsResponseType>>,
auth: Authenticator,
{ dataSourceView }: { dataSourceView: DataSourceViewResource }
space: SpaceResource
): Promise<void> {
if (!dataSourceView.canList(auth)) {
const { dsvId } = req.query;
if (typeof dsvId !== "string") {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: "Invalid path parameters.",
},
});
}

const dataSourceView = await DataSourceViewResource.fetchById(auth, dsvId);
if (!dataSourceView || dataSourceView.space.sId !== space.sId) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "data_source_view_not_found",
message: "The data source view you requested was not found.",
},
});
}

if (
!dataSourceView ||
req.query.spaceId !== dataSourceView.space.sId ||
!dataSourceView.canList(auth)
) {
return apiError(req, res, {
status_code: 404,
api_error: {
Expand Down Expand Up @@ -228,7 +255,5 @@ async function handler(
}

export default withPublicAPIAuthentication(
withResourceFetchingFromRoute(handler, {
dataSourceView: { requireCanList: true },
})
withResourceFetchingFromRoute(handler, "space")
);
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { withPublicAPIAuthentication } from "@app/lib/api/auth_wrappers";
import { handleDataSourceSearch } from "@app/lib/api/data_sources";
import { withResourceFetchingFromRoute } from "@app/lib/api/resource_wrappers";
import type { Authenticator } from "@app/lib/auth";
import type { DataSourceViewResource } from "@app/lib/resources/data_source_view_resource";
import { DataSourceViewResource } from "@app/lib/resources/data_source_view_resource";
import type { SpaceResource } from "@app/lib/resources/space_resource";
import { apiError } from "@app/logger/withlogging";

/**
Expand Down Expand Up @@ -151,9 +152,25 @@ async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<DataSourceSearchResponseType>>,
auth: Authenticator,
{ dataSourceView }: { dataSourceView: DataSourceViewResource }
space: SpaceResource
): Promise<void> {
if (!dataSourceView.canRead(auth)) {
const { dsvId } = req.query;
if (typeof dsvId !== "string") {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: "Invalid path parameters.",
},
});
}

const dataSourceView = await DataSourceViewResource.fetchById(auth, dsvId);
if (
!dataSourceView ||
dataSourceView.space.sId !== space.sId ||
!dataSourceView.canRead(auth)
) {
return apiError(req, res, {
status_code: 404,
api_error: {
Expand Down Expand Up @@ -226,7 +243,5 @@ async function handler(
}

export default withPublicAPIAuthentication(
withResourceFetchingFromRoute(handler, {
dataSourceView: { requireCanRead: true },
})
withResourceFetchingFromRoute(handler, "space")
);
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<DataSourceViewsListResponseType>>,
auth: Authenticator,
{ space }: { space: SpaceResource }
space: SpaceResource
): Promise<void> {
if (!space.canList(auth)) {
return apiError(req, res, {
Expand Down Expand Up @@ -95,5 +95,5 @@ async function handler(
}

export default withPublicAPIAuthentication(
withResourceFetchingFromRoute(handler, { space: { requireCanList: true } })
withResourceFetchingFromRoute(handler, "space")
);
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function handler(
>
>,
auth: Authenticator,
{ dataSource }: { dataSource: DataSourceResource }
dataSource: DataSourceResource
): Promise<void> {
const { fId } = req.query;

Expand Down Expand Up @@ -59,16 +59,6 @@ async function handler(

switch (req.method) {
case "GET":
if (!dataSource.canList(auth)) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "data_source_not_found",
message: "The data source you requested was not found.",
},
});
}

const docRes = await coreAPI.getDataSourceFolder({
projectId: dataSource.dustAPIProjectId,
dataSourceId: dataSource.dustAPIDataSourceId,
Expand All @@ -92,16 +82,6 @@ async function handler(
return;

case "POST":
if (!dataSource.canWrite(auth)) {
return apiError(req, res, {
status_code: 403,
api_error: {
type: "data_source_auth_error",
message: "You are not allowed to update data in this data source.",
},
});
}

const r = UpsertDataSourceFolderRequestSchema.safeParse(req.body);

if (r.error) {
Expand Down Expand Up @@ -173,16 +153,6 @@ async function handler(
return;

case "DELETE":
if (!dataSource.canWrite(auth)) {
return apiError(req, res, {
status_code: 403,
api_error: {
type: "data_source_auth_error",
message: "You are not allowed to update data in this data source.",
},
});
}

const delRes = await coreAPI.deleteDataSourceFolder({
projectId: dataSource.dustAPIProjectId,
dataSourceId: dataSource.dustAPIDataSourceId,
Expand Down Expand Up @@ -220,7 +190,5 @@ async function handler(
}

export default withPublicAPIAuthentication(
withResourceFetchingFromRoute(handler, {
dataSource: { requireCanList: true },
})
withResourceFetchingFromRoute(handler, "dataSource")
);
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<GetFoldersResponseType>>,
auth: Authenticator,
{ dataSource }: { dataSource: DataSourceResource }
dataSource: DataSourceResource
): Promise<void> {
const coreAPI = new CoreAPI(config.getCoreAPIConfig(), logger);
if (!auth.isSystemKey()) {
Expand All @@ -34,16 +34,6 @@ async function handler(

switch (req.method) {
case "GET":
if (!dataSource.canList(auth)) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "data_source_not_found",
message: "The data source you requested was not found.",
},
});
}

const limit = req.query.limit ? parseInt(req.query.limit as string) : 10;
const offset = req.query.offset
? parseInt(req.query.offset as string)
Expand Down Expand Up @@ -87,7 +77,5 @@ async function handler(
}

export default withPublicAPIAuthentication(
withResourceFetchingFromRoute(handler, {
dataSource: { requireCanList: true },
})
withResourceFetchingFromRoute(handler, "dataSource")
);
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<GetDataSourcesResponseType>>,
auth: Authenticator,
{ space }: { space: SpaceResource }
space: SpaceResource
): Promise<void> {
const dataSources = await DataSourceResource.listBySpace(auth, space);

Expand Down Expand Up @@ -87,5 +87,5 @@ async function handler(
}

export default withPublicAPIAuthentication(
withResourceFetchingFromRoute(handler, { space: { requireCanList: true } })
withResourceFetchingFromRoute(handler, "space")
);
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ export type GetDatasetResponseBody = { dataset: DatasetType };
async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<GetDatasetResponseBody>>,

auth: Authenticator,
{ space }: { space: SpaceResource }
space: SpaceResource
): Promise<void> {
const owner = auth.workspace();
if (!owner) {
Expand Down Expand Up @@ -232,5 +233,5 @@ async function handler(
}

export default withSessionAuthenticationForWorkspace(
withResourceFetchingFromRoute(handler, { space: { requireCanRead: true } })
withResourceFetchingFromRoute(handler, "space")
);
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ async function handler(
res: NextApiResponse<
WithAPIErrorResponse<GetDatasetsResponseBody | PostDatasetResponseBody>
>,

auth: Authenticator,
{ space }: { space: SpaceResource }
space: SpaceResource
): Promise<void> {
const { aId } = req.query;
if (typeof aId !== "string") {
Expand Down Expand Up @@ -211,7 +212,5 @@ async function handler(
}

export default withSessionAuthenticationForWorkspace(
// Interacting with datasets requires write access to the app's space.
// Read permission is not enough as it's available to all space users (or everybody for public spaces)
withResourceFetchingFromRoute(handler, { space: { requireCanWrite: true } })
withResourceFetchingFromRoute(handler, "space")
);
5 changes: 3 additions & 2 deletions front/pages/api/w/[wId]/spaces/[spaceId]/apps/[aId]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ const PatchAppBodySchema = t.type({
async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<GetOrPostAppResponseBody>>,

auth: Authenticator,
{ space }: { space: SpaceResource }
space: SpaceResource
): Promise<void> {
const { aId } = req.query;
if (typeof aId !== "string") {
Expand Down Expand Up @@ -134,5 +135,5 @@ async function handler(
}

export default withSessionAuthenticationForWorkspace(
withResourceFetchingFromRoute(handler, { space: { requireCanRead: true } })
withResourceFetchingFromRoute(handler, "space")
);
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ export type GetRunBlockResponseBody = {
async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<GetRunBlockResponseBody>>,

auth: Authenticator,
{ space }: { space: SpaceResource }
space: SpaceResource
): Promise<void> {
const { aId } = req.query;
if (typeof aId !== "string") {
Expand Down Expand Up @@ -107,7 +108,5 @@ async function handler(
}

export default withSessionAuthenticationForWorkspace(
// App block runs contain sensitive data and requires write access to the app's space.
// Read permission is not enough as it's available to all space users (or everybody for public spaces)
withResourceFetchingFromRoute(handler, { space: { requireCanWrite: true } })
withResourceFetchingFromRoute(handler, "space")
);
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export type GetRunStatusResponseBody = {
async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<GetRunStatusResponseBody>>,

auth: Authenticator,
{ space }: { space: SpaceResource }
space: SpaceResource
) {
const { aId } = req.query;
if (typeof aId !== "string") {
Expand Down Expand Up @@ -108,5 +109,5 @@ async function handler(
}

export default withSessionAuthenticationForWorkspace(
withResourceFetchingFromRoute(handler, { space: { requireCanRead: true } })
withResourceFetchingFromRoute(handler, "space")
);
Loading

0 comments on commit 9fc6c81

Please sign in to comment.