Skip to content

Commit

Permalink
Prevent workflow version from bad update (#6848)
Browse files Browse the repository at this point in the history
Closes #6840

- Add query-hooks folder in common. Will be followed by hooks for
workflows and runs
- When updating a version, ensure the status is draft and that the
status is not manually updated
  • Loading branch information
thomtrp authored Sep 2, 2024
1 parent 7e03419 commit 329e736
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';

import { WorkflowVersionUpdateOnePreQueryHook } from 'src/modules/workflow/common/query-hooks/workflow-version/workflow-version-update-one.pre-query-hook';
import { WorkflowVersionValidationWorkspaceService } from 'src/modules/workflow/common/query-hooks/workflow-version/workflow-version-validation.workspace-service';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service';

@Module({
providers: [
WorkflowVersionUpdateOnePreQueryHook,
WorkflowVersionValidationWorkspaceService,
WorkflowCommonWorkspaceService,
],
})
export class WorkflowQueryHookModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { WorkspaceQueryHookInstance } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/interfaces/workspace-query-hook.interface';
import { UpdateOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';

import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/decorators/workspace-query-hook.decorator';
import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { WorkflowVersionValidationWorkspaceService } from 'src/modules/workflow/common/query-hooks/workflow-version/workflow-version-validation.workspace-service';
import { WorkflowVersionWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';

@WorkspaceQueryHook(`workflowVersion.updateOne`)
export class WorkflowVersionUpdateOnePreQueryHook
implements WorkspaceQueryHookInstance
{
constructor(
private readonly workflowVersionValidationWorkspaceService: WorkflowVersionValidationWorkspaceService,
) {}

async execute(
_authContext: AuthContext,
_objectName: string,
payload: UpdateOneResolverArgs<WorkflowVersionWorkspaceEntity>,
): Promise<UpdateOneResolverArgs<WorkflowVersionWorkspaceEntity>> {
await this.workflowVersionValidationWorkspaceService.validateWorkflowVersionForUpdateOne(
payload,
);

return payload;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CustomException } from 'src/utils/custom-exception';

export class WorkflowVersionValidationException extends CustomException {
constructor(message: string, code: WorkflowVersionValidationExceptionCode) {
super(message, code);
}
}

export enum WorkflowVersionValidationExceptionCode {
FORBIDDEN = 'FORBIDDEN',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Injectable } from '@nestjs/common';

import { UpdateOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';

import {
WorkflowVersionValidationException,
WorkflowVersionValidationExceptionCode,
} from 'src/modules/workflow/common/query-hooks/workflow-version/workflow-version-validation.exception';
import {
WorkflowVersionStatus,
WorkflowVersionWorkspaceEntity,
} from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service';

@Injectable()
export class WorkflowVersionValidationWorkspaceService {
constructor(
private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService,
) {}

async validateWorkflowVersionForUpdateOne(
payload: UpdateOneResolverArgs<WorkflowVersionWorkspaceEntity>,
) {
const workflowVersion =
await this.workflowCommonWorkspaceService.getWorkflowVersionOrFail(
payload.id,
);

if (workflowVersion.status !== WorkflowVersionStatus.DRAFT) {
throw new WorkflowVersionValidationException(
'Only draft workflow versions can be updated',
WorkflowVersionValidationExceptionCode.FORBIDDEN,
);
}

if (payload.data.status !== workflowVersion.status) {
throw new WorkflowVersionValidationException(
'Cannot update workflow version status manually',
WorkflowVersionValidationExceptionCode.FORBIDDEN,
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Module } from '@nestjs/common';

import { WorkflowQueryHookModule } from 'src/modules/workflow/common/query-hooks/workflow-query-hook.module';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service';

@Module({
imports: [WorkflowQueryHookModule],
providers: [WorkflowCommonWorkspaceService],
exports: [WorkflowCommonWorkspaceService],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export class WorkflowCommonWorkspaceService {
trigger: WorkflowTrigger;
}
> {
if (!workflowVersionId) {
throw new WorkflowTriggerException(
'Workflow version ID is required',
WorkflowTriggerExceptionCode.INVALID_INPUT,
);
}

const workflowVersionRepository =
await this.twentyORMManager.getRepository<WorkflowVersionWorkspaceEntity>(
'workflowVersion',
Expand Down

0 comments on commit 329e736

Please sign in to comment.