Skip to content
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

Jobs: rename statusUpdate to update #1460

Open
wants to merge 5 commits into
base: release-jobs
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions jobConfig.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
from: from
subject: "[SciCat] Your {{type}} job was submitted successfully"
bodyTemplateFile: src/common/email-templates/job-template-simplified.html
statusUpdate:
update:
auth: archivemanager
actions:
- actionType: rabbitmq
Expand All @@ -39,5 +39,5 @@ jobs:
required:
- pid
- files
statusUpdate:
update:
auth: "#all"
9 changes: 4 additions & 5 deletions src/casl/action.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export enum Action {
ReadOwn = "readown",
ReadAll = "readall",
Update = "update",
StatusUpdate = "statusUpdate",
Delete = "delete",
ListOwn = "listown",
ListAll = "listall",
Expand Down Expand Up @@ -182,17 +181,17 @@ export enum Action {
// endpoint authorization
JobCreate = "jobs_create",
JobRead = "jobs_read",
JobStatusUpdate = "job_update",
JobUpdate = "job_update",
JobDelete = "job_delete",
// data instance authorization
JobCreateConfiguration = "job_create_configuration",
JobCreateOwner = "job_create_owner",
JobCreateAny = "job_create_any",
JobReadAccess = "job_read_access",
JobReadAny = "job_read_any",
JobStatusUpdateConfiguration = "job_status_update_configuration",
JobStatusUpdateOwner = "job_status_update_owner",
JobStatusUpdateAny = "job_status_update_any",
JobUpdateConfiguration = "job_update_configuration",
JobUpdateOwner = "job_update_owner",
JobUpdateAny = "job_update_any",
//JobDeleteAny = "job_delete_any",

// -------------
Expand Down
56 changes: 26 additions & 30 deletions src/casl/casl-ability.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ import { User } from "src/users/schemas/user.schema";
import { Action } from "./action.enum";
import configuration from "src/config/configuration";
import { JobConfigService } from "src/config/job-config/jobconfig.service";
import {
CreateJobAuth,
StatusUpdateJobAuth,
} from "src/jobs/types/jobs-auth.enum";

import { CreateJobAuth, UpdateJobAuth } from "src/jobs/types/jobs-auth.enum";
import { JobConfig } from "src/config/job-config/jobconfig.interface";

type Subjects =
Expand Down Expand Up @@ -372,12 +368,12 @@ export class CaslAbilityFactory {
cannot(Action.JobRead, JobClass);
if (
Object.values(this.jobConfigService.allJobConfigs).some(
(j) => j.statusUpdate.auth == StatusUpdateJobAuth.All,
(j) => j.update.auth == UpdateJobAuth.All,
)
) {
can(Action.JobStatusUpdate, JobClass);
can(Action.JobUpdate, JobClass);
} else {
cannot(Action.JobStatusUpdate, JobClass);
cannot(Action.JobUpdate, JobClass);
}
cannot(Action.JobDelete, JobClass);
} else {
Expand All @@ -393,7 +389,7 @@ export class CaslAbilityFactory {
*/
can(Action.JobRead, JobClass);
can(Action.JobCreate, JobClass);
can(Action.JobStatusUpdate, JobClass);
can(Action.JobUpdate, JobClass);
} else {
const jobUserAuthorizationValues = [
...user.currentGroups.map((g) => "@" + g),
Expand Down Expand Up @@ -432,26 +428,26 @@ export class CaslAbilityFactory {
}
}
const jobUpdateEndPointAuthorizationValues = [
...Object.values(StatusUpdateJobAuth),
...Object.values(UpdateJobAuth),
...jobUserAuthorizationValues,
];
if (
user.currentGroups.some((g) =>
configuration().statusUpdateJobGroups.includes(g),
configuration().updateJobGroups.includes(g),
)
) {
can(Action.JobStatusUpdate, JobClass);
can(Action.JobUpdate, JobClass);
} else {
if (
Object.values(this.jobConfigService.allJobConfigs).some(
(j) =>
j.statusUpdate.auth &&
j.update.auth &&
jobUpdateEndPointAuthorizationValues.includes(
j.statusUpdate.auth as string,
j.update.auth as string,
),
)
) {
can(Action.JobStatusUpdate, JobClass);
can(Action.JobUpdate, JobClass);
}
}
}
Expand Down Expand Up @@ -1381,8 +1377,8 @@ export class CaslAbilityFactory {
datasetsValidation: true,
});
}
if (jobConfiguration.statusUpdate.auth === StatusUpdateJobAuth.All) {
can(Action.JobStatusUpdateConfiguration, JobClass, {
if (jobConfiguration.update.auth === UpdateJobAuth.All) {
can(Action.JobUpdateConfiguration, JobClass, {
ownerGroup: undefined,
});
}
Expand All @@ -1399,7 +1395,7 @@ export class CaslAbilityFactory {
*/
can(Action.JobReadAny, JobClass);
can(Action.JobCreateAny, JobClass);
can(Action.JobStatusUpdateAny, JobClass);
can(Action.JobUpdateAny, JobClass);
} else {
const jobUserAuthorizationValues = [
...user.currentGroups.map((g) => "@" + g),
Expand Down Expand Up @@ -1458,45 +1454,45 @@ export class CaslAbilityFactory {
}
}
const jobUpdateInstanceAuthorizationValues = [
...Object.values(StatusUpdateJobAuth).filter(
...Object.values(UpdateJobAuth).filter(
(v) => !String(v).includes("#job"),
),
...jobUserAuthorizationValues,
];

if (
user.currentGroups.some((g) =>
configuration().statusUpdateJobGroups.includes(g),
configuration().updateJobGroups.includes(g),
)
) {
if (
jobUpdateInstanceAuthorizationValues.some(
(a) => jobConfiguration.statusUpdate.auth === a,
(a) => jobConfiguration.update.auth === a,
)
) {
can(Action.JobStatusUpdateConfiguration, JobClass);
can(Action.JobUpdateConfiguration, JobClass);
}
can(Action.JobStatusUpdateOwner, JobClass, {
can(Action.JobUpdateOwner, JobClass, {
ownerUser: user.username,
});
can(Action.JobStatusUpdateOwner, JobClass, {
can(Action.JobUpdateOwner, JobClass, {
ownerGroup: { $in: user.currentGroups },
});
} else {
if (
jobUpdateInstanceAuthorizationValues.some(
(a) => jobConfiguration.statusUpdate.auth === a,
(a) => jobConfiguration.update.auth === a,
)
) {
can(Action.JobStatusUpdateConfiguration, JobClass);
can(Action.JobUpdateConfiguration, JobClass);
}
if (jobConfiguration.statusUpdate.auth === "#jobOwnerUser") {
can(Action.JobStatusUpdateConfiguration, JobClass, {
if (jobConfiguration.update.auth === "#jobOwnerUser") {
can(Action.JobUpdateConfiguration, JobClass, {
ownerUser: user.username,
});
}
if (jobConfiguration.statusUpdate.auth === "#jobOwnerGroup") {
can(Action.JobStatusUpdateConfiguration, JobClass, {
if (jobConfiguration.update.auth === "#jobOwnerGroup") {
can(Action.JobUpdateConfiguration, JobClass, {
ownerGroup: { $in: user.currentGroups },
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const configuration = () => {
process.env.DATASET_CREATION_VALIDATION_REGEX || ("" as string);

const createJobGroups = process.env.CREATE_JOB_GROUPS || ("" as string);
const statusUpdateJobGroups = process.env.UPDATE_JOB_GROUPS || ("" as string);
const updateJobGroups = process.env.UPDATE_JOB_GROUPS || ("" as string);
const deleteJobGroups = process.env.DELETE_JOB_GROUPS || ("" as string);

const proposalGroups = process.env.PROPOSAL_GROUPS || ("" as string);
Expand Down Expand Up @@ -95,7 +95,7 @@ const configuration = () => {
datasetCreationValidationEnabled: datasetCreationValidationEnabled,
datasetCreationValidationRegex: datasetCreationValidationRegex,
createJobGroups: createJobGroups,
statusUpdateJobGroups: statusUpdateJobGroups,
updateJobGroups: updateJobGroups,
deleteJobGroups: deleteJobGroups,
logoutURL: process.env.LOGOUT_URL ?? "", // Example: http://localhost:3000/
accessGroupsGraphQlConfig: {
Expand Down
10 changes: 5 additions & 5 deletions src/config/job-config/jobconfig.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CreateJobDto } from "../../jobs/dto/create-job.dto";
import { StatusUpdateJobDto } from "../../jobs/dto/status-update-job.dto";
import { UpdateJobDto } from "../../jobs/dto/update-job.dto";
import { JobsAuth } from "../../jobs/types/jobs-auth.enum";
import { JobClass } from "../../jobs/schemas/job.schema";

Expand All @@ -20,18 +20,18 @@ export interface JobConfig {
jobType: string;
configVersion: string;
create: JobOperation<CreateJobDto>;
statusUpdate: JobOperation<StatusUpdateJobDto>;
update: JobOperation<UpdateJobDto>;
}
export interface JobConfigOptions {
jobType: string;
configVersion: string;
create: JobOperationOptions;
statusUpdate: JobOperationOptions;
update: JobOperationOptions;
}

export type JobDto = CreateJobDto | StatusUpdateJobDto;
export type JobDto = CreateJobDto | UpdateJobDto;
/**
* Encapsulates all information for a particular job operation (eg "create", "statusUpdate")
* Encapsulates all information for a particular job operation (eg "create", "update")
*/
export interface JobOperation<DtoType extends JobDto> {
auth: JobsAuth | undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/config/job-config/jobconfig.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const JobConfigSchema = {
},
additionalProperties: true,
},
statusUpdate: {
update: {
type: "object",
properties: {
auth: { type: "string" },
Expand Down
11 changes: 4 additions & 7 deletions src/config/job-config/jobconfig.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { JobConfigSchema } from "./jobconfig.schema";
import { load } from "js-yaml";
import * as fs from "fs";
import { CreateJobDto } from "../../jobs/dto/create-job.dto";
import { StatusUpdateJobDto } from "../../jobs/dto/status-update-job.dto";
import { UpdateJobDto } from "../../jobs/dto/update-job.dto";
import { ConfigService } from "@nestjs/config";

/**
Expand All @@ -30,10 +30,7 @@ export class JobConfigService {
@Inject(CREATE_JOB_ACTION_CREATORS)
private create_creators: Record<string, JobActionCreator<CreateJobDto>>,
@Inject(UPDATE_JOB_ACTION_CREATORS)
private update_creators: Record<
string,
JobActionCreator<StatusUpdateJobDto>
>,
private update_creators: Record<string, JobActionCreator<UpdateJobDto>>,
configService: ConfigService,
) {
this.filePath = configService.get<string>("jobConfigurationFile") || "";
Expand Down Expand Up @@ -104,8 +101,8 @@ export class JobConfigService {
options.create,
this.create_creators,
),
statusUpdate: this.parseJobOperation<StatusUpdateJobDto>(
options.statusUpdate,
update: this.parseJobOperation<UpdateJobDto>(
options.update,
this.update_creators,
),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ApiProperty, ApiTags } from "@nestjs/swagger";
import { IsOptional, IsString, IsObject } from "class-validator";

@ApiTags("jobs")
export class StatusUpdateJobDto {
export class UpdateJobDto {
@ApiProperty({
type: String,
required: true,
Expand Down
37 changes: 17 additions & 20 deletions src/jobs/jobs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Request } from "express";
import { FilterQuery } from "mongoose";
import { JobsService } from "./jobs.service";
import { CreateJobDto } from "./dto/create-job.dto";
import { StatusUpdateJobDto } from "./dto/status-update-job.dto";
import { UpdateJobDto } from "./dto/update-job.dto";
import { DatasetListDto } from "./dto/dataset-list.dto";
import { PoliciesGuard } from "src/casl/guards/policies.guard";
import { CheckPolicies } from "src/casl/decorators/check-policies.decorator";
Expand Down Expand Up @@ -631,7 +631,7 @@ export class JobsController {
Job was created with configVersion ${jobInstance.configVersion}.
Current configVersion is ${jobConfig.configVersion}.
`,
"JobStatusUpdate",
"JobUpdate",
);
}
}
Expand Down Expand Up @@ -680,21 +680,21 @@ export class JobsController {
}

/**
* Update job status
* Update job
*/
@UseGuards(PoliciesGuard)
@CheckPolicies("jobs", (ability: AppAbility) =>
ability.can(Action.JobStatusUpdate, JobClass),
ability.can(Action.JobUpdate, JobClass),
)
@Patch(":id")
@ApiOperation({
summary: "It updates the status of an existing job.",
description: "It updates the status of an existing job.",
summary: "It updates an existing job.",
description: "It updates an existing job.",
})
@ApiBody({
description: "Status fields for the job to be updated",
description: "Fields for the job to be updated",
required: true,
type: StatusUpdateJobDto,
type: UpdateJobDto,
})
@ApiResponse({
status: HttpStatus.OK,
Expand All @@ -704,7 +704,7 @@ export class JobsController {
async update(
@Req() request: Request,
@Param("id") id: string,
@Body() statusUpdateJobDto: StatusUpdateJobDto,
@Body() updateJobDto: UpdateJobDto,
): Promise<JobClass | null> {
Logger.log("updating job ", id);
// Find existing job
Expand All @@ -726,26 +726,23 @@ export class JobsController {
jobConfig,
);
// check if the user can update this job
const canUpdateStatus =
ability.can(Action.JobStatusUpdateAny, JobClass) ||
ability.can(Action.JobStatusUpdateOwner, currentJobInstance) ||
ability.can(Action.JobStatusUpdateConfiguration, currentJobInstance);
if (!canUpdateStatus) {
const canUpdate =
ability.can(Action.JobUpdateAny, JobClass) ||
ability.can(Action.JobUpdateOwner, currentJobInstance) ||
ability.can(Action.JobUpdateConfiguration, currentJobInstance);
if (!canUpdate) {
throw new ForbiddenException("Unauthorized to update this job.");
}

// Allow actions to validate DTO
await this.validateDTO(jobConfig.statusUpdate.actions, statusUpdateJobDto);
await this.validateDTO(jobConfig.update.actions, updateJobDto);

// Update job in database
const updatedJob = await this.jobsService.statusUpdate(
id,
statusUpdateJobDto,
);
const updatedJob = await this.jobsService.update(id, updateJobDto);
// Perform the action that is specified in the update portion of the job configuration
if (updatedJob !== null) {
await this.checkConfigVersion(jobConfig, updatedJob);
await this.performActions(jobConfig.statusUpdate.actions, updatedJob);
await this.performActions(jobConfig.update.actions, updatedJob);
}
return updatedJob;
}
Expand Down
Loading
Loading