Skip to content

Commit

Permalink
Merge branch 'develop' into feature/3408
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Kiryakov <[email protected]>

# Conflicts:
#	common/src/database-modules/database-server.ts
  • Loading branch information
Stepan-Kirjakov committed Jan 30, 2025
2 parents cb24d7a + a22531a commit 2e8d720
Show file tree
Hide file tree
Showing 21 changed files with 4,356 additions and 3,698 deletions.
174 changes: 171 additions & 3 deletions api-gateway/src/api/service/policy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Auth, AuthUser } from '#auth';
import { CACHE, POLICY_REQUIRED_PROPS, PREFIXES } from '#constants';
import { AnyFilesInterceptor, CacheService, EntityOwner, getCacheKey, InternalException, ONLY_SR, PolicyEngine, ProjectService, ServiceError, TaskManager, UploadedFiles, UseCache } from '#helpers';
import { BlockDTO, Examples, ExportMessageDTO, ImportMessageDTO, InternalServerErrorDTO, MigrationConfigDTO, pageHeader, PoliciesValidationDTO, PolicyCategoryDTO, PolicyDTO, PolicyPreviewDTO, PolicyTestDTO, PolicyValidationDTO, RunningDetailsDTO, ServiceUnavailableErrorDTO, TaskDTO } from '#middlewares';
import { IAuthUser, PinoLogger, RunFunctionAsync } from '@guardian/common';
import { DocumentType, Permissions, PolicyHelper, TaskAction, UserRole } from '@guardian/interfaces';
import { Body, Controller, Delete, Get, HttpCode, HttpException, HttpStatus, Param, Post, Put, Query, Req, Response, UseInterceptors, Version } from '@nestjs/common';
import { ApiAcceptedResponse, ApiBody, ApiConsumes, ApiExtraModels, ApiInternalServerErrorResponse, ApiOkResponse, ApiOperation, ApiParam, ApiQuery, ApiServiceUnavailableResponse, ApiTags } from '@nestjs/swagger';
import { BlockDTO, Examples, ExportMessageDTO, ImportMessageDTO, InternalServerErrorDTO, MigrationConfigDTO, pageHeader, PoliciesValidationDTO, PolicyCategoryDTO, PolicyDTO, PolicyPreviewDTO, PolicyTestDTO, PolicyValidationDTO, RunningDetailsDTO, ServiceUnavailableErrorDTO, TaskDTO } from '#middlewares';
import { AnyFilesInterceptor, CacheService, EntityOwner, getCacheKey, InternalException, ONLY_SR, PolicyEngine, ProjectService, ServiceError, TaskManager, UploadedFiles, UseCache } from '#helpers';
import { CACHE, POLICY_REQUIRED_PROPS, PREFIXES } from '#constants';

async function getOldResult(user: IAuthUser): Promise<PolicyDTO[]> {
const options: any = {};
Expand Down Expand Up @@ -2817,6 +2817,174 @@ export class PolicyApi {
}
}

/**
* Clear dry-run state.
*/
@Post('/:policyId/savepoint/create')
@Auth(
Permissions.POLICIES_POLICY_UPDATE
// UserRole.STANDARD_REGISTRY,
)
@ApiOperation({
summary: 'Create dru-run savepoint.',
description: 'Create dru-run savepoint.' + ONLY_SR
})
@ApiParam({
name: 'policyId',
type: String,
description: 'Policy Id',
required: true,
example: Examples.DB_ID
})
@ApiBody({
description: '.'
})
@ApiOkResponse({
description: '.'
})
@ApiInternalServerErrorResponse({
description: 'Internal server error.',
type: InternalServerErrorDTO
})
@ApiExtraModels(InternalServerErrorDTO)
@HttpCode(HttpStatus.OK)
async createSavepoint(
@AuthUser() user: IAuthUser,
@Param('policyId') policyId: string,
@Body() body: any,
@Req() req
) {
const engineService = new PolicyEngine();
const owner = new EntityOwner(user);
const policy = await engineService.accessPolicy(policyId, owner, 'read');
if (!PolicyHelper.isDryRunMode(policy)) {
throw new HttpException('Invalid status.', HttpStatus.FORBIDDEN);
}

console.log('Create savepoint');

const invalidedCacheTags = [`${PREFIXES.POLICIES}${policyId}/navigation`, `${PREFIXES.POLICIES}${policyId}/groups`];
await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheTags], user));

try {
return await engineService.createSavepoint(body, owner, policyId);
} catch (error) {
await InternalException(error, this.logger);
}
}

/**
* Clear dry-run state.
*/
@Post('/:policyId/savepoint/delete')
@Auth(
Permissions.POLICIES_POLICY_UPDATE
// UserRole.STANDARD_REGISTRY,
)
@ApiOperation({
summary: 'Delete dru-run savepoint.',
description: 'Delete dru-run savepoint.' + ONLY_SR
})
@ApiParam({
name: 'policyId',
type: String,
description: 'Policy Id',
required: true,
example: Examples.DB_ID
})
@ApiBody({
description: '.'
})
@ApiOkResponse({
description: '.'
})
@ApiInternalServerErrorResponse({
description: 'Internal server error.',
type: InternalServerErrorDTO
})
@ApiExtraModels(InternalServerErrorDTO)
@HttpCode(HttpStatus.OK)
async deleteSavepoint(
@AuthUser() user: IAuthUser,
@Param('policyId') policyId: string,
@Body() body: any,
@Req() req
) {
const engineService = new PolicyEngine();
const owner = new EntityOwner(user);
const policy = await engineService.accessPolicy(policyId, owner, 'read');
if (!PolicyHelper.isDryRunMode(policy)) {
throw new HttpException('Invalid status.', HttpStatus.FORBIDDEN);
}

console.log('Delete savepoint');

const invalidedCacheTags = [`${PREFIXES.POLICIES}${policyId}/navigation`, `${PREFIXES.POLICIES}${policyId}/groups`];
await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheTags], user));

try {
return await engineService.deleteSavepoint(body, owner, policyId);
} catch (error) {
await InternalException(error, this.logger);
}
}

/**
* Clear dry-run state.
*/
@Post('/:policyId/savepoint/restore')
@Auth(
Permissions.POLICIES_POLICY_UPDATE
// UserRole.STANDARD_REGISTRY,
)
@ApiOperation({
summary: 'Restore dru-run savepoint.',
description: 'Restore dru-run savepoint.' + ONLY_SR
})
@ApiParam({
name: 'policyId',
type: String,
description: 'Policy Id',
required: true,
example: Examples.DB_ID
})
@ApiBody({
description: '.'
})
@ApiOkResponse({
description: '.'
})
@ApiInternalServerErrorResponse({
description: 'Internal server error.',
type: InternalServerErrorDTO
})
@ApiExtraModels(InternalServerErrorDTO)
@HttpCode(HttpStatus.OK)
async restoreSavepoint(
@AuthUser() user: IAuthUser,
@Param('policyId') policyId: string,
@Body() body: any,
@Req() req
) {
const engineService = new PolicyEngine();
const owner = new EntityOwner(user);
const policy = await engineService.accessPolicy(policyId, owner, 'read');
if (!PolicyHelper.isDryRunMode(policy)) {
throw new HttpException('Invalid status.', HttpStatus.FORBIDDEN);
}

console.log('restore savepoint');

const invalidedCacheTags = [`${PREFIXES.POLICIES}${policyId}/navigation`, `${PREFIXES.POLICIES}${policyId}/groups`];
await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheTags], user));

try {
return await engineService.restoreSavepoint(body, owner, policyId);
} catch (error) {
await InternalException(error, this.logger);
}
}

/**
* Clear dry-run state.
*/
Expand Down
48 changes: 45 additions & 3 deletions api-gateway/src/helpers/policy-engine.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Singleton } from '../helpers/decorators/singleton.js';
import { DocumentType, GenerateUUIDv4, IOwner, MigrationConfig, PolicyEngineEvents, PolicyToolMetadata } from '@guardian/interfaces';
import { ExportMessageDTO, PoliciesValidationDTO, PolicyDTO, PolicyPreviewDTO, PolicyValidationDTO } from '#middlewares';
import { IAuthUser, NatsService } from '@guardian/common';
import { DocumentType, GenerateUUIDv4, IOwner, MigrationConfig, PolicyEngineEvents, PolicyToolMetadata } from '@guardian/interfaces';
import { Singleton } from '../helpers/decorators/singleton.js';
import { NewTask } from './task-manager.js';
import { ExportMessageDTO, PoliciesValidationDTO, PolicyDTO, PolicyPreviewDTO, PolicyValidationDTO } from '#middlewares';

/**
* Policy engine service
Expand Down Expand Up @@ -621,6 +621,48 @@ export class PolicyEngine extends NatsService {
return await this.sendMessage(PolicyEngineEvents.RESTART_DRY_RUN, { model, owner, policyId });
}

/**
* Create savepoint
* @param model
* @param owner
* @param policyId
*/
public async createSavepoint(
model: any,
owner: IOwner,
policyId: string
) {
return await this.sendMessage(PolicyEngineEvents.CREATE_SAVEPOINT, {model, owner, policyId});
}

/**
* Delete savepoint
* @param model
* @param owner
* @param policyId
*/
public async deleteSavepoint(
model: any,
owner: IOwner,
policyId: string
) {
return await this.sendMessage(PolicyEngineEvents.DELETE_SAVEPOINT, {model, owner, policyId});
}

/**
* Restore savepoint
* @param model
* @param owner
* @param policyId
*/
public async restoreSavepoint(
model: any,
owner: IOwner,
policyId: string
) {
return await this.sendMessage(PolicyEngineEvents.RESTORE_SAVEPOINT, {model, owner, policyId});
}

/**
* Get Virtual Documents
* @param policyId
Expand Down
Loading

0 comments on commit 2e8d720

Please sign in to comment.