Skip to content

Commit

Permalink
feat: refactor http controller validation (#880)
Browse files Browse the repository at this point in the history
* feat: initial replacement of domain validation

* fix: node route handlers

* fix: some validator usage in route handlers

* fix: validation in analysis route handlers

* fix: set missing optional attributes for mounting validators

* fix: minor cleanup of dependencies and unused code

* refactor: http handler operation constant usage
  • Loading branch information
tada5hi authored Nov 6, 2024
1 parent 207fcd4 commit 6e11074
Show file tree
Hide file tree
Showing 63 changed files with 1,249 additions and 1,307 deletions.
84 changes: 60 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/server-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
"@routup/rate-limit": "^2.4.0",
"@routup/swagger": "^2.4.1",
"@socket.io/redis-emitter": "^5.1.0",
"@validup/adapter-routup": "^0.1.8",
"@validup/adapter-validator": "^0.1.7",
"amqp-extension": "^4.0.0-beta.3",
"busboy": "^1.6.0",
"cors": "^2.8.5",
"docker-scan": "^1.1.0",
"dotenv": "^16.4.5",
"envix": "^1.3.0",
"express-validator": "^7.2.0",
"hapic": "^2.5.1",
"locter": "^2.1.4",
"mysql2": "^3.11.0",
Expand All @@ -49,7 +50,8 @@
"routup": "^4.0.1",
"singa": "^1.0.0",
"typeorm": "^0.3.20",
"typeorm-extension": "^3.6.2",
"typeorm-extension": "^3.6.3",
"validup": "^0.1.7",
"yargs": "^17.7.1",
"zod": "^3.23.7"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,62 @@
*/
import { BadRequestError } from '@ebec/http';
import {
AnalysisBucketType, AnalysisBuildStatus, AnalysisRunStatus,
AnalysisBucketType,
} from '@privateaim/core-kit';
import { useRequestIdentityOrFail } from '@privateaim/server-http-kit';
import { HTTPHandlerOperation, useRequestIdentityOrFail } from '@privateaim/server-http-kit';
import type { Request, Response } from 'routup';
import { sendCreated } from 'routup';
import { useDataSource } from 'typeorm-extension';
import { AnalysisBucketFileEntity } from '../../../../domains';
import { runAnalysisFileValidation } from '../utils';
import { useDataSource, validateEntityJoinColumns } from 'typeorm-extension';
import { RoutupContainerAdapter } from '@validup/adapter-routup';
import { AnalysisBucketFileEntity, AnalysisEntity } from '../../../../domains';
import { AnalysisBucketFileValidator } from '../utils';

export async function createAnalysisBucketFileRouteHandler(req: Request, res: Response) : Promise<any> {
const result = await runAnalysisFileValidation(req, 'create');
result.data.analysis_id = result.relation.bucket.analysis_id;
const validator = new AnalysisBucketFileValidator();
const validatorAdapter = new RoutupContainerAdapter(validator);
const data = await validatorAdapter.run(req, {
group: HTTPHandlerOperation.CREATE,
});

const dataSource = await useDataSource();
await validateEntityJoinColumns(data, {
dataSource,
entityTarget: AnalysisBucketFileEntity,
});

data.analysis_id = data.bucket.analysis_id;

const repository = dataSource.getRepository(AnalysisBucketFileEntity);

const identity = useRequestIdentityOrFail(req);
result.data.realm_id = identity.realmId;
data.realm_id = identity.realmId;

switch (identity.type) {
case 'user': {
result.data.user_id = identity.id;
data.user_id = identity.id;
break;
}
case 'robot': {
result.data.robot_id = identity.id;
data.robot_id = identity.id;
break;
}
default: {
throw new BadRequestError('Only user-/robot-accounts are permitted.');
}
}

let entity = repository.create(result.data);
let entity = repository.create(data);

if (
result.relation.bucket.type === AnalysisBucketType.CODE &&
result.relation.analysis
) {
if (
result.relation.analysis.build_status &&
result.relation.analysis.build_status !== AnalysisBuildStatus.FAILED
) {
throw new BadRequestError('The analysis has already been built and can no longer be modified.');
}
if (data.bucket.type === AnalysisBucketType.CODE) {
const analysisRepository = dataSource.getRepository(AnalysisEntity);
const analysis = await analysisRepository.findOne({
where: {
id: data.analysis_id,
},
});

if (
result.relation.analysis.run_status &&
result.relation.analysis.run_status !== AnalysisRunStatus.FAILED
) {
throw new BadRequestError('The analysis has already been started and can no longer be modified.');
if (analysis.configuration_locked) {
throw new BadRequestError('The analysis has already been locked and can therefore no longer be modified.');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import { isRealmResourceWritable } from '@authup/core-kit';
import type { Request, Response } from 'routup';
import { sendAccepted, useRequestParam } from 'routup';
import { useDataSource } from 'typeorm-extension';
import { useRequestIdentityRealm } from '@privateaim/server-http-kit';
import { HTTPHandlerOperation, useRequestIdentityRealm } from '@privateaim/server-http-kit';
import { RoutupContainerAdapter } from '@validup/adapter-routup';
import { AnalysisBucketFileEntity } from '../../../../domains';
import { runAnalysisFileValidation } from '../utils';
import { AnalysisBucketFileValidator } from '../utils';

export async function updateAnalysisBucketFileRouteHandler(req: Request, res: Response) : Promise<any> {
const id = useRequestParam(req, 'id');

const validator = new AnalysisBucketFileValidator();
const validatorAdapter = new RoutupContainerAdapter(validator);
const data = await validatorAdapter.run(req, {
group: HTTPHandlerOperation.UPDATE,
});

const dataSource = await useDataSource();
const repository = dataSource.getRepository(AnalysisBucketFileEntity);
let entity = await repository.findOneBy({ id });
Expand All @@ -29,9 +36,7 @@ export async function updateAnalysisBucketFileRouteHandler(req: Request, res: Re
throw new ForbiddenError();
}

const result = await runAnalysisFileValidation(req, 'update');

entity = repository.merge(entity, result.data);
entity = repository.merge(entity, data);

entity = await repository.save(entity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* view the LICENSE file that was distributed with this source code.
*/

export * from './validation';
export * from './validator';
Loading

0 comments on commit 6e11074

Please sign in to comment.