From 80b8d28de99b6f44ab990824b395eaf4a34c13f4 Mon Sep 17 00:00:00 2001 From: artembuslaev Date: Wed, 3 Apr 2024 17:16:26 +0400 Subject: [PATCH] refactoring Signed-off-by: artembuslaev --- api-gateway/src/api/service/artifact.ts | 2 +- api-gateway/src/api/service/policy.ts | 104 ++++++++++++------ api-gateway/src/api/service/tool.ts | 32 ++++-- api-gateway/src/helpers/guardians.ts | 9 +- api-gateway/src/helpers/policy-engine.ts | 15 ++- .../preview-policy-dialog.component.html | 6 +- .../preview-policy-dialog.component.ts | 1 - .../src/app/services/policy-engine.service.ts | 50 ++++++--- frontend/src/app/services/tools.service.ts | 26 +++-- .../helpers/schema-import-export-helper.ts | 6 +- .../api/helpers/tool-import-export-helper.ts | 55 +++++---- .../helpers/policy-import-export-helper.ts | 61 ++++++---- .../policy-engine/policy-engine.service.ts | 18 ++- .../src/policy-engine/policy-engine.ts | 13 ++- interfaces/src/interface/index.ts | 1 + .../policy-tool-metadata.interface.ts | 9 ++ 16 files changed, 278 insertions(+), 130 deletions(-) create mode 100644 interfaces/src/interface/policy-tool-metadata.interface.ts diff --git a/api-gateway/src/api/service/artifact.ts b/api-gateway/src/api/service/artifact.ts index 1f2ce58c27..19419209b5 100644 --- a/api-gateway/src/api/service/artifact.ts +++ b/api-gateway/src/api/service/artifact.ts @@ -144,7 +144,7 @@ export class ArtifactApi { }) @ApiConsumes('multipart/form-data') @ApiBody({ - description: 'Form data with artifacts', + description: 'Form data with artifacts.', required: true, schema: { type: 'array', diff --git a/api-gateway/src/api/service/policy.ts b/api-gateway/src/api/service/policy.ts index 15c9a2354e..94b1f5725a 100644 --- a/api-gateway/src/api/service/policy.ts +++ b/api-gateway/src/api/service/policy.ts @@ -1213,11 +1213,19 @@ export class PolicyApi { const engineService = new PolicyEngine(); const versionOfTopicId = req.query ? req.query.versionOfTopicId : null; try { - const policies = await engineService.importMessage(req.user, req.body.messageId, versionOfTopicId, req.body.metadata); + const policies = await engineService.importMessage( + req.user, + req.body.messageId, + versionOfTopicId, + req.body.metadata + ); return res.status(201).send(policies); } catch (error) { new Logger().error(error, ['API_GATEWAY']); - throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR); + throw new HttpException( + error.message, + HttpStatus.INTERNAL_SERVER_ERROR + ); } } @@ -1247,14 +1255,29 @@ export class PolicyApi { const messageId = req.body.messageId; const versionOfTopicId = req.query ? req.query.versionOfTopicId : null; const taskManager = new TaskManager(); - const task = taskManager.start(TaskAction.IMPORT_POLICY_MESSAGE, user.id); - RunFunctionAsync(async () => { - const engineService = new PolicyEngine(); - await engineService.importMessageAsync(user, messageId, versionOfTopicId, task, req.body.metadata); - }, async (error) => { - new Logger().error(error, ['API_GATEWAY']); - taskManager.addError(task.taskId, { code: 500, message: 'Unknown error: ' + error.message }); - }); + const task = taskManager.start( + TaskAction.IMPORT_POLICY_MESSAGE, + user.id + ); + RunFunctionAsync( + async () => { + const engineService = new PolicyEngine(); + await engineService.importMessageAsync( + user, + messageId, + versionOfTopicId, + task, + req.body.metadata + ); + }, + async (error) => { + new Logger().error(error, ['API_GATEWAY']); + taskManager.addError(task.taskId, { + code: 500, + message: 'Unknown error: ' + error.message, + }); + } + ); return res.status(202).send(task); } @@ -1402,7 +1425,7 @@ export class PolicyApi { }) @ApiConsumes('multipart/form-data') @ApiBody({ - description: 'Form data with policy file and metadata', + description: 'Form data with policy file and metadata.', required: true, schema: { type: 'object', @@ -1442,11 +1465,15 @@ export class PolicyApi { @Query('versionOfTopicId') versionOfTopicId, ): Promise { try { - const policyFile = files.find(item => item.fieldname === 'policyFile'); + const policyFile = files.find( + (item) => item.fieldname === 'policyFile' + ); if (!policyFile) { throw new Error('There is no policy file'); } - const metadata = files.find(item => item.fieldname === 'metadata'); + const metadata = files.find( + (item) => item.fieldname === 'metadata' + ); const engineService = new PolicyEngine(); return await engineService.importFile( user, @@ -1456,7 +1483,10 @@ export class PolicyApi { ); } catch (error) { new Logger().error(error, ['API_GATEWAY']); - throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR); + throw new HttpException( + error.message, + HttpStatus.INTERNAL_SERVER_ERROR + ); } } @@ -1538,7 +1568,7 @@ export class PolicyApi { }) @ApiConsumes('multipart/form-data') @ApiBody({ - description: 'Form data with policy file and metadata', + description: 'Form data with policy file and metadata.', required: true, schema: { type: 'object', @@ -1579,24 +1609,34 @@ export class PolicyApi { ): Promise { const taskManager = new TaskManager(); const task = taskManager.start(TaskAction.IMPORT_POLICY_FILE, user.id); - RunFunctionAsync(async () => { - const policyFile = files.find(item => item.fieldname === 'policyFile'); - if (!policyFile) { - throw new Error('There is no policy file'); + RunFunctionAsync( + async () => { + const policyFile = files.find( + (item) => item.fieldname === 'policyFile' + ); + if (!policyFile) { + throw new Error('There is no policy file'); + } + const metadata = files.find( + (item) => item.fieldname === 'metadata' + ); + const engineService = new PolicyEngine(); + await engineService.importFileAsync( + user, + policyFile.buffer, + versionOfTopicId, + task, + metadata?.buffer && JSON.parse(metadata.buffer.toString()) + ); + }, + async (error) => { + new Logger().error(error, ['API_GATEWAY']); + taskManager.addError(task.taskId, { + code: 500, + message: 'Unknown error: ' + error.message, + }); } - const metadata = files.find(item => item.fieldname === 'metadata'); - const engineService = new PolicyEngine(); - await engineService.importFileAsync( - user, - policyFile.buffer, - versionOfTopicId, - task, - metadata?.buffer && JSON.parse(metadata.buffer.toString()) - ); - }, async (error) => { - new Logger().error(error, ['API_GATEWAY']); - taskManager.addError(task.taskId, { code: 500, message: 'Unknown error: ' + error.message }); - }); + ); return task; } diff --git a/api-gateway/src/api/service/tool.ts b/api-gateway/src/api/service/tool.ts index 7fda5d807e..1e2922626b 100644 --- a/api-gateway/src/api/service/tool.ts +++ b/api-gateway/src/api/service/tool.ts @@ -716,7 +716,7 @@ export class ToolsApi { } /** - * Import tool from IPFS + * Import tool from file with metadata */ @Post('/import/file-metadata') @ApiSecurity('bearerAuth') @@ -732,7 +732,7 @@ export class ToolsApi { }) @ApiConsumes('multipart/form-data') @ApiBody({ - description: 'Form data with policy file and metadata', + description: 'Form data with tool file and metadata.', required: true, schema: { type: 'object', @@ -762,20 +762,32 @@ export class ToolsApi { }) @UseInterceptors(AnyFilesInterceptor()) @HttpCode(HttpStatus.CREATED) - async toolImportFileWithMetadata(@Req() req, @UploadedFiles() files: any): Promise { + async toolImportFileWithMetadata( + @Req() req, + @UploadedFiles() files: any + ): Promise { await checkPermission(UserRole.STANDARD_REGISTRY)(req.user); const guardian = new Guardians(); try { - const file = files.find(item => item.fieldname === 'file'); + const file = files.find((item) => item.fieldname === 'file'); if (!file) { throw new Error('There is no tool file'); } - const metadata = files.find(item => item.fieldname === 'metadata'); - const tool = await guardian.importToolFile(file.buffer, req.user.did, JSON.parse(metadata.buffer.toString())); + const metadata = files.find( + (item) => item.fieldname === 'metadata' + ); + const tool = await guardian.importToolFile( + file.buffer, + req.user.did, + metadata?.buffer && JSON.parse(metadata.buffer.toString()) + ); return tool; } catch (error) { new Logger().error(error, ['API_GATEWAY']); - throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR); + throw new HttpException( + error.message, + HttpStatus.INTERNAL_SERVER_ERROR + ); } } @@ -830,7 +842,7 @@ export class ToolsApi { } /** - * Import tool from IPFS (Async) + * Import tool from file with metadata (Async) */ @Post('/push/import/file-metadata') @ApiSecurity('bearerAuth') @@ -842,12 +854,12 @@ export class ToolsApi { }) @ApiConsumes('multipart/form-data') @ApiBody({ - description: 'Form data with policy file and metadata', + description: 'Form data with tool file and metadata.', required: true, schema: { type: 'object', properties: { - 'policyFile': { + 'file': { type: 'string', format: 'binary', }, diff --git a/api-gateway/src/helpers/guardians.ts b/api-gateway/src/helpers/guardians.ts index a6f0aae0ca..294951b5b8 100644 --- a/api-gateway/src/helpers/guardians.ts +++ b/api-gateway/src/helpers/guardians.ts @@ -1,5 +1,5 @@ import { Singleton } from '@helpers/decorators/singleton'; -import { ApplicationStates, CommonSettings, ContractAPI, ContractType, GenerateUUIDv4, IArtifact, IChainItem, IContract, IDidObject, IRetirePool, IRetireRequest, ISchema, IToken, ITokenInfo, IUser, IVCDocument, IVPDocument, MessageAPI, RetireTokenPool, RetireTokenRequest, SchemaNode, SuggestionsOrderPriority } from '@guardian/interfaces'; +import { ApplicationStates, CommonSettings, ContractAPI, ContractType, GenerateUUIDv4, IArtifact, IChainItem, IContract, IDidObject, IRetirePool, IRetireRequest, ISchema, IToken, ITokenInfo, IUser, IVCDocument, IVPDocument, MessageAPI, PolicyToolMetadata, RetireTokenPool, RetireTokenRequest, SchemaNode, SuggestionsOrderPriority } from '@guardian/interfaces'; import { IAuthUser, NatsService } from '@guardian/common'; import { NewTask } from './task-manager'; @@ -173,7 +173,6 @@ export class Guardians extends NatsService { /** * Update token * @param token - * @param task */ public async updateToken(token: IToken | any): Promise { return await this.sendMessage(MessageAPI.UPDATE_TOKEN, { token }); @@ -1951,8 +1950,9 @@ export class Guardians extends NatsService { * Load tool file for import * @param zip * @param owner + * @param metadata */ - public async importToolFile(zip: any, owner: string, metadata?: any) { + public async importToolFile(zip: any, owner: string, metadata?: PolicyToolMetadata) { return await this.sendMessage(MessageAPI.TOOL_IMPORT_FILE, { zip, owner, metadata }); } @@ -1988,8 +1988,9 @@ export class Guardians extends NatsService { * @param zip * @param owner * @param task + * @param metadata */ - public async importToolFileAsync(zip: any, owner: string, task: NewTask, metadata?: any) { + public async importToolFileAsync(zip: any, owner: string, task: NewTask, metadata?: PolicyToolMetadata) { return await this.sendMessage(MessageAPI.TOOL_IMPORT_FILE_ASYNC, { zip, owner, task, metadata }); } diff --git a/api-gateway/src/helpers/policy-engine.ts b/api-gateway/src/helpers/policy-engine.ts index 4ce44bcd45..299218683e 100644 --- a/api-gateway/src/helpers/policy-engine.ts +++ b/api-gateway/src/helpers/policy-engine.ts @@ -1,5 +1,5 @@ import { Singleton } from '@helpers/decorators/singleton'; -import { DocumentType, GenerateUUIDv4, MigrationConfig, PolicyEngineEvents } from '@guardian/interfaces'; +import { DocumentType, GenerateUUIDv4, MigrationConfig, PolicyEngineEvents, PolicyToolMetadata } from '@guardian/interfaces'; import { IAuthUser, NatsService } from '@guardian/common'; import { NewTask } from './task-manager'; @@ -269,12 +269,13 @@ export class PolicyEngine extends NatsService { * @param user * @param zip * @param versionOfTopicId + * @param metadata */ public async importFile( user: IAuthUser, zip: Buffer, versionOfTopicId?: string, - metadata?: { tools: { [key: string]: string } } + metadata?: PolicyToolMetadata ) { return await this.sendMessage(PolicyEngineEvents.POLICY_IMPORT_FILE, { zip, @@ -290,13 +291,14 @@ export class PolicyEngine extends NatsService { * @param zip * @param versionOfTopicId * @param task + * @param metadata */ public async importFileAsync( user: IAuthUser, zip: Buffer, versionOfTopicId: string, task: NewTask, - metadata?: { tools: { [key: string]: string } } + metadata?: PolicyToolMetadata ) { return await this.sendMessage( PolicyEngineEvents.POLICY_IMPORT_FILE_ASYNC, @@ -308,12 +310,14 @@ export class PolicyEngine extends NatsService { * Import policy from message * @param user * @param messageId + * @param versionOfTopicId + * @param metadata */ public async importMessage( user: IAuthUser, messageId: string, versionOfTopicId: string, - metadata?: { tools: { [key: string]: string } } + metadata?: PolicyToolMetadata ) { return await this.sendMessage( PolicyEngineEvents.POLICY_IMPORT_MESSAGE, @@ -327,13 +331,14 @@ export class PolicyEngine extends NatsService { * @param messageId * @param versionOfTopicId * @param task + * @param metadata */ public async importMessageAsync( user: IAuthUser, messageId: string, versionOfTopicId: string, task: NewTask, - metadata?: { tools: { [key: string]: string } } + metadata?: PolicyToolMetadata ) { return await this.sendMessage( PolicyEngineEvents.POLICY_IMPORT_MESSAGE_ASYNC, diff --git a/frontend/src/app/modules/policy-engine/helpers/preview-policy-dialog/preview-policy-dialog.component.html b/frontend/src/app/modules/policy-engine/helpers/preview-policy-dialog/preview-policy-dialog.component.html index e49e4285aa..a301e93d83 100644 --- a/frontend/src/app/modules/policy-engine/helpers/preview-policy-dialog/preview-policy-dialog.component.html +++ b/frontend/src/app/modules/policy-engine/helpers/preview-policy-dialog/preview-policy-dialog.component.html @@ -109,8 +109,8 @@
-
Description
-
{{ tool.description }}
+
Tools
+
{{ tools }}
@@ -159,7 +159,7 @@
- +
diff --git a/frontend/src/app/modules/policy-engine/helpers/preview-policy-dialog/preview-policy-dialog.component.ts b/frontend/src/app/modules/policy-engine/helpers/preview-policy-dialog/preview-policy-dialog.component.ts index e41bfc0f1e..1c59c7c1ff 100644 --- a/frontend/src/app/modules/policy-engine/helpers/preview-policy-dialog/preview-policy-dialog.component.ts +++ b/frontend/src/app/modules/policy-engine/helpers/preview-policy-dialog/preview-policy-dialog.component.ts @@ -78,7 +78,6 @@ export class PreviewPolicyDialog { ]) ); } - this.tools = this.toolConfigs.map((tool) => tool.name).join(', '); } if (this.config.data.module) { diff --git a/frontend/src/app/services/policy-engine.service.ts b/frontend/src/app/services/policy-engine.service.ts index d2ad5febf8..a0fd4ea6f9 100644 --- a/frontend/src/app/services/policy-engine.service.ts +++ b/frontend/src/app/services/policy-engine.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core'; import { HttpClient, HttpResponse } from '@angular/common/http'; import { Observable } from 'rxjs'; import { API_BASE_URL } from './api'; -import { MigrationConfig } from '@guardian/interfaces'; +import { MigrationConfig, PolicyToolMetadata } from '@guardian/interfaces'; /** * Services for working from policy and separate blocks. @@ -116,21 +116,45 @@ export class PolicyEngineService { return this.http.get(`${this.url}/${policyId}/export/message`); } - public pushImportByMessage(messageId: string, versionOfTopicId?: string, metadata?: { tools: { [key: string]: string }}): Observable<{ taskId: string, expectation: number }> { - var query = versionOfTopicId ? `?versionOfTopicId=${versionOfTopicId}` : ''; - return this.http.post<{ taskId: string, expectation: number }>(`${this.url}/push/import/message${query}`, { messageId, metadata }); - } - - public pushImportByFile(policyFile: any, versionOfTopicId?: string, metadata?: { tools: { [key: string]: string }}): Observable<{ taskId: string, expectation: number }> { - var query = versionOfTopicId ? `?versionOfTopicId=${versionOfTopicId}` : ''; + public pushImportByMessage( + messageId: string, + versionOfTopicId?: string, + metadata?: PolicyToolMetadata + ): Observable<{ taskId: string; expectation: number }> { + var query = versionOfTopicId + ? `?versionOfTopicId=${versionOfTopicId}` + : ''; + return this.http.post<{ taskId: string; expectation: number }>( + `${this.url}/push/import/message${query}`, + { messageId, metadata } + ); + } + + public pushImportByFile( + policyFile: any, + versionOfTopicId?: string, + metadata?: PolicyToolMetadata + ): Observable<{ taskId: string; expectation: number }> { + var query = versionOfTopicId + ? `?versionOfTopicId=${versionOfTopicId}` + : ''; const formData = new FormData(); - formData.append('policyFile', new Blob([policyFile], { type: "application/octet-stream" })); + formData.append( + 'policyFile', + new Blob([policyFile], { type: 'application/octet-stream' }) + ); if (metadata) { - formData.append('metadata', new Blob([JSON.stringify(metadata)], { - type: "application/json", - })); + formData.append( + 'metadata', + new Blob([JSON.stringify(metadata)], { + type: 'application/json', + }) + ); } - return this.http.post<{ taskId: string, expectation: number }>(`${this.url}/push/import/file-metadata${query}`, formData); + return this.http.post<{ taskId: string; expectation: number }>( + `${this.url}/push/import/file-metadata${query}`, + formData + ); } public previewByMessage(messageId: string): Observable { diff --git a/frontend/src/app/services/tools.service.ts b/frontend/src/app/services/tools.service.ts index 9e32bc0df3..1f8e9236f1 100644 --- a/frontend/src/app/services/tools.service.ts +++ b/frontend/src/app/services/tools.service.ts @@ -2,7 +2,7 @@ import { HttpClient, HttpResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { API_BASE_URL } from './api'; -import { ISchema } from '@guardian/interfaces'; +import { PolicyToolMetadata } from '@guardian/interfaces'; /** * Services for working from tools. @@ -94,15 +94,27 @@ export class ToolsService { return this.http.post<{ taskId: string, expectation: number }>(`${this.url}/push/import/message`, { messageId }); } - public pushImportByFile(file: any, metadata?: { tools: { [key: string]: string }}): Observable<{ taskId: string, expectation: number }> { + public pushImportByFile( + file: any, + metadata?: PolicyToolMetadata + ): Observable<{ taskId: string; expectation: number }> { const formData = new FormData(); - formData.append('file', new Blob([file], { type: "application/octet-stream" })); + formData.append( + 'file', + new Blob([file], { type: 'application/octet-stream' }) + ); if (metadata) { - formData.append('metadata', new Blob([JSON.stringify(metadata)], { - type: "application/json", - })); + formData.append( + 'metadata', + new Blob([JSON.stringify(metadata)], { + type: 'application/json', + }) + ); } - return this.http.post<{ taskId: string, expectation: number }>(`${this.url}/push/import/file-metadata`, formData); + return this.http.post<{ taskId: string; expectation: number }>( + `${this.url}/push/import/file-metadata`, + formData + ); } public validate(policy: any): Observable { diff --git a/guardian-service/src/api/helpers/schema-import-export-helper.ts b/guardian-service/src/api/helpers/schema-import-export-helper.ts index ea60408be2..ee3e982fd8 100644 --- a/guardian-service/src/api/helpers/schema-import-export-helper.ts +++ b/guardian-service/src/api/helpers/schema-import-export-helper.ts @@ -181,7 +181,7 @@ export async function importSchemaByFiles( topicId: string, notifier: INotifier, skipGenerateId = false, - subSchemasMapping: { name: string, iri: string }[] = [] + outerSchemasMapping?: { name: string, iri: string }[] ): Promise { notifier.start('Import schemas'); @@ -213,12 +213,12 @@ export async function importSchemaByFiles( file.owner = owner; file.topicId = topicId || 'draft'; file.status = SchemaStatus.DRAFT; - if (file.document?.$defs) { + if (file.document?.$defs && outerSchemasMapping) { for (const def of Object.values(file.document.$defs)) { if (!def || uuidMap.has(def.$id)) { continue; } - const subSchemaMapping = subSchemasMapping.find( + const subSchemaMapping = outerSchemasMapping.find( (item) => item.name === def.title ); if (subSchemaMapping) { diff --git a/guardian-service/src/api/helpers/tool-import-export-helper.ts b/guardian-service/src/api/helpers/tool-import-export-helper.ts index ba9d767223..1f7ffa995c 100644 --- a/guardian-service/src/api/helpers/tool-import-export-helper.ts +++ b/guardian-service/src/api/helpers/tool-import-export-helper.ts @@ -20,6 +20,7 @@ import { GenerateUUIDv4, IRootConfig, ModuleStatus, + PolicyToolMetadata, SchemaCategory, SchemaStatus, TagType, @@ -77,6 +78,9 @@ export async function replaceConfig( } for (const item of tools) { + if (!item.newHash || !item.messageId) { + continue; + } replaceAllEntities(tool.config, ['messageId'], item.oldMessageId, item.messageId); replaceAllEntities(tool.config, ['hash'], item.oldHash, item.newHash); } @@ -336,7 +340,7 @@ export async function importToolByFile( owner: string, components: IToolComponents, notifier: INotifier, - metadata?: { tools: any[] } + metadata?: PolicyToolMetadata ): Promise { notifier.start('Import tool'); @@ -351,16 +355,23 @@ export async function importToolByFile( const users = new Users(); const root = await users.getHederaAccount(owner); - const toolsMapping: { oldMessageId: string, messageId: string, oldHash: string, newHash?: string }[] = [] - + const toolsMapping: { + oldMessageId: string; + messageId: string; + oldHash: string; + newHash?: string; + }[] = []; if (metadata?.tools) { // tslint:disable-next-line:no-shadowed-variable for (const tool of tools) { - if (tool.messageId !== metadata.tools[tool.messageId]) { + if ( + metadata.tools[tool.messageId] && + tool.messageId !== metadata.tools[tool.messageId] + ) { toolsMapping.push({ oldMessageId: tool.messageId, messageId: metadata.tools[tool.messageId], - oldHash: tool.hash + oldHash: tool.hash, }); tool.messageId = metadata.tools[tool.messageId]; } @@ -423,25 +434,23 @@ export async function importToolByFile( notifier.sub(true); for (const toolMapping of toolsMapping) { - // tslint:disable-next-line:no-shadowed-variable - const toolByMessageId = toolsResult.tools.find(tool => tool.messageId === toolMapping.messageId); - toolMapping.newHash = toolByMessageId.hash; + const toolByMessageId = toolsResult.tools.find( + // tslint:disable-next-line:no-shadowed-variable + (tool) => tool.messageId === toolMapping.messageId + ); + toolMapping.newHash = toolByMessageId?.hash; } - const toolsSchemas = ( - await Promise.all( - toolsResult.tools.map(async (toolResult) => { - const toolSchemas = await DatabaseServer.getSchemas({ - topicId: toolResult.topicId, - category: SchemaCategory.TOOL, - }); - return toolSchemas.map((schema) => ({ - name: schema.name, - iri: schema.iri, - })); - }) - ) - ).flat(); + const toolsSchemas = (await DatabaseServer.getSchemas( + { + category: SchemaCategory.TOOL, + // tslint:disable-next-line:no-shadowed-variable + topicId: { $in: toolsResult.tools.map((tool) => tool.topicId) }, + }, + { + fields: ['name', 'iri'], + } + )) as { name: string; iri: string }[]; // Import Schemas const schemasResult = await importSchemaByFiles( @@ -548,4 +557,4 @@ export async function updateToolConfig(tool: PolicyTool): Promise { tool.tools = list; return tool; -} \ No newline at end of file +} diff --git a/guardian-service/src/policy-engine/helpers/policy-import-export-helper.ts b/guardian-service/src/policy-engine/helpers/policy-import-export-helper.ts index 881c0419a1..57ba8f4cdf 100644 --- a/guardian-service/src/policy-engine/helpers/policy-import-export-helper.ts +++ b/guardian-service/src/policy-engine/helpers/policy-import-export-helper.ts @@ -4,6 +4,7 @@ import { ConfigType, GenerateUUIDv4, ModuleStatus, + PolicyToolMetadata, PolicyType, SchemaCategory, SchemaEntity, @@ -89,7 +90,7 @@ export class PolicyImportExportHelper { versionOfTopicId: string, notifier: INotifier, additionalPolicyConfig?: Partial, - metadata?: { tools: {[key: string]: string} } + metadata?: PolicyToolMetadata, ): Promise<{ /** * New Policy @@ -113,15 +114,22 @@ export class PolicyImportExportHelper { notifier.start('Resolve Hedera account'); const root = await users.getHederaAccount(policyOwner); - const toolsMapping: { oldMessageId: string, messageId: string, oldHash: string, newHash?: string }[] = [] - + const toolsMapping: { + oldMessageId: string; + messageId: string; + oldHash: string; + newHash?: string; + }[] = []; if (metadata?.tools) { for (const tool of tools) { - if (tool.messageId !== metadata.tools[tool.messageId]) { + if ( + metadata.tools[tool.messageId] && + tool.messageId !== metadata.tools[tool.messageId] + ) { toolsMapping.push({ oldMessageId: tool.messageId, messageId: metadata.tools[tool.messageId], - oldHash: tool.hash + oldHash: tool.hash, }); tool.messageId = metadata.tools[tool.messageId]; } @@ -204,28 +212,26 @@ export class PolicyImportExportHelper { notifier.sub(false); for (const toolMapping of toolsMapping) { - const toolByMessageId = toolsResult.tools.find(tool => tool.messageId === toolMapping.messageId); - toolMapping.newHash = toolByMessageId.hash; + const toolByMessageId = toolsResult.tools.find( + // tslint:disable-next-line:no-shadowed-variable + (tool) => tool.messageId === toolMapping.messageId + ); + toolMapping.newHash = toolByMessageId?.hash; } // Import Tokens const tokensResult = await importTokensByFiles(policyOwner, tokens, notifier); const tokenMap = tokensResult.tokenMap; - const toolsSchemas = ( - await Promise.all( - toolsResult.tools.map(async (toolResult) => { - const toolSchemas = await DatabaseServer.getSchemas({ - topicId: toolResult.topicId, - category: SchemaCategory.TOOL, - }); - return toolSchemas.map((schema) => ({ - name: schema.name, - iri: schema.iri, - })); - }) - ) - ).flat(); + const toolsSchemas = (await DatabaseServer.getSchemas( + { + category: SchemaCategory.TOOL, + topicId: { $in: toolsResult.tools.map((tool) => tool.topicId) }, + }, + { + fields: ['name', 'iri'], + } + )) as { name: string; iri: string }[]; // Import Schemas const schemasResult = await importSchemaByFiles( @@ -246,7 +252,13 @@ export class PolicyImportExportHelper { notifier.completedAndStart('Saving in DB'); // Replace id - await PolicyImportExportHelper.replaceConfig(policy, schemasMap, artifactsMap, tokenMap, toolsMapping as any); + await PolicyImportExportHelper.replaceConfig( + policy, + schemasMap, + artifactsMap, + tokenMap, + toolsMapping + ); // Save const model = new DataBaseHelper(Policy).create(policy as Policy); @@ -328,7 +340,7 @@ export class PolicyImportExportHelper { schemasMap: SchemaImportResult[], artifactsMap: Map, tokenMap: any[], - tools: { oldMessageId: string, messageId: string, oldHash: string, newHash: string }[] + tools: { oldMessageId: string, messageId: string, oldHash: string, newHash?: string }[] ) { if (await new DataBaseHelper(Policy).findOne({ name: policy.name })) { policy.name = policy.name + '_' + Date.now(); @@ -349,6 +361,9 @@ export class PolicyImportExportHelper { } for (const item of tools) { + if (!item.newHash || !item.messageId) { + continue; + } replaceAllEntities(policy.config, ['messageId'], item.oldMessageId, item.messageId); replaceAllEntities(policy.config, ['hash'], item.oldHash, item.newHash); } diff --git a/guardian-service/src/policy-engine/policy-engine.service.ts b/guardian-service/src/policy-engine/policy-engine.service.ts index 42cce8cbeb..aa93b92050 100644 --- a/guardian-service/src/policy-engine/policy-engine.service.ts +++ b/guardian-service/src/policy-engine/policy-engine.service.ts @@ -933,7 +933,14 @@ export class PolicyEngineService { new Logger().info(`Import policy by file`, ['GUARDIAN_SERVICE']); const did = await this.getUserDid(user.username); const policyToImport = await PolicyImportExport.parseZipFile(Buffer.from(zip.data), true); - const result = await PolicyImportExportHelper.importPolicy(policyToImport, did, versionOfTopicId, emptyNotifier(), undefined, metadata); + const result = await PolicyImportExportHelper.importPolicy( + policyToImport, + did, + versionOfTopicId, + emptyNotifier(), + undefined, + metadata + ); if (result?.errors?.length) { const message = PolicyImportExportHelper.errorsMessage(result.errors); new Logger().warn(message, ['GUARDIAN_SERVICE']); @@ -960,7 +967,14 @@ export class PolicyEngineService { notifier.start('File parsing'); const policyToImport = await PolicyImportExport.parseZipFile(Buffer.from(zip.data), true); notifier.completed(); - const result = await PolicyImportExportHelper.importPolicy(policyToImport, did, versionOfTopicId, notifier, undefined, metadata); + const result = await PolicyImportExportHelper.importPolicy( + policyToImport, + did, + versionOfTopicId, + notifier, + undefined, + metadata + ); if (result?.errors?.length) { const message = PolicyImportExportHelper.errorsMessage(result.errors); notifier.error(message); diff --git a/guardian-service/src/policy-engine/policy-engine.ts b/guardian-service/src/policy-engine/policy-engine.ts index 5cd9146168..ff29e51535 100644 --- a/guardian-service/src/policy-engine/policy-engine.ts +++ b/guardian-service/src/policy-engine/policy-engine.ts @@ -1,4 +1,4 @@ -import { GenerateUUIDv4, IRootConfig, ModelHelper, NotificationAction, PolicyEvents, PolicyType, Schema, SchemaEntity, SchemaHelper, SchemaStatus, TagType, TopicType } from '@guardian/interfaces'; +import { GenerateUUIDv4, IRootConfig, ModelHelper, NotificationAction, PolicyEvents, PolicyToolMetadata, PolicyType, Schema, SchemaEntity, SchemaHelper, SchemaStatus, TagType, TopicType } from '@guardian/interfaces'; import { Artifact, DataBaseHelper, @@ -974,7 +974,7 @@ export class PolicyEngine extends NatsService { hederaAccount: IRootConfig, versionOfTopicId: string, notifier: INotifier, - metadata: any + metadata?: PolicyToolMetadata ): Promise<{ /** * New Policy @@ -1024,7 +1024,14 @@ export class PolicyEngine extends NatsService { } as any); } notifier.completed(); - return await PolicyImportExportHelper.importPolicy(policyToImport, owner, versionOfTopicId, notifier, undefined, metadata); + return await PolicyImportExportHelper.importPolicy( + policyToImport, + owner, + versionOfTopicId, + notifier, + undefined, + metadata + ); } /** diff --git a/interfaces/src/interface/index.ts b/interfaces/src/interface/index.ts index 5c8c427ddb..688520e3a0 100644 --- a/interfaces/src/interface/index.ts +++ b/interfaces/src/interface/index.ts @@ -38,3 +38,4 @@ export * from './wizard-config.interface'; export * from './schema-node.interface'; export * from './policy-category-export.interface'; export * from './migration-config.interface'; +export * from './policy-tool-metadata.interface'; diff --git a/interfaces/src/interface/policy-tool-metadata.interface.ts b/interfaces/src/interface/policy-tool-metadata.interface.ts new file mode 100644 index 0000000000..2deeaf3e35 --- /dev/null +++ b/interfaces/src/interface/policy-tool-metadata.interface.ts @@ -0,0 +1,9 @@ +/** + * Policy tool metadata + */ +export interface PolicyToolMetadata { + /** + * Tools mapping + */ + tools?: { [key: string]: string }; +}