From aa96fd880eba256d9941b1383e0a6a4042260a60 Mon Sep 17 00:00:00 2001 From: William Calderipe Date: Thu, 21 Mar 2024 11:06:40 +0100 Subject: [PATCH] Move bootstrap to the policy engine top level module Check encryption configuration at the bootstrap. --- .../engine/core/service/bootstrap.service.ts | 23 ++++++++++++++++++- .../policy-engine/src/engine/engine.module.ts | 12 +++------- .../policy-engine/src/policy-engine.module.ts | 12 +++++++--- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/apps/policy-engine/src/engine/core/service/bootstrap.service.ts b/apps/policy-engine/src/engine/core/service/bootstrap.service.ts index 94bc03344..ae26bcb3e 100644 --- a/apps/policy-engine/src/engine/core/service/bootstrap.service.ts +++ b/apps/policy-engine/src/engine/core/service/bootstrap.service.ts @@ -1,3 +1,4 @@ +import { EncryptionService } from '@narval/encryption-module' import { FIXTURE } from '@narval/policy-engine-shared' import { Injectable, Logger } from '@nestjs/common' import { randomBytes } from 'crypto' @@ -7,11 +8,16 @@ import { TenantService } from './tenant.service' export class BootstrapService { private logger = new Logger(BootstrapService.name) - constructor(private tenantService: TenantService) {} + constructor( + private tenantService: TenantService, + private encryptionService: EncryptionService + ) {} async boot(): Promise { this.logger.log('Start engine bootstrap') + await this.checkEncryptionConfiguration() + if (!(await this.tenantService.findByClientId(FIXTURE.ORGANIZATION.id))) { await this.tenantService.onboard({ clientId: FIXTURE.ORGANIZATION.id, @@ -36,6 +42,21 @@ export class BootstrapService { await this.syncTenants() } + private async checkEncryptionConfiguration(): Promise { + this.logger.log('Check encryption configuration') + + try { + this.encryptionService.getKeyring() + this.logger.log('Encryption keyring configured') + } catch (error) { + this.logger.error( + 'Missing encryption keyring. Please provision the application with "make policy-engine/cli CMD=provision"' + ) + + throw error + } + } + private async syncTenants(): Promise { const tenants = await this.tenantService.findAll() diff --git a/apps/policy-engine/src/engine/engine.module.ts b/apps/policy-engine/src/engine/engine.module.ts index 59b436688..4553373eb 100644 --- a/apps/policy-engine/src/engine/engine.module.ts +++ b/apps/policy-engine/src/engine/engine.module.ts @@ -1,6 +1,6 @@ import { EncryptionModule } from '@narval/encryption-module' import { HttpModule } from '@nestjs/axios' -import { Module, OnApplicationBootstrap, ValidationPipe } from '@nestjs/common' +import { Module, ValidationPipe } from '@nestjs/common' import { ConfigModule, ConfigService } from '@nestjs/config' import { APP_PIPE } from '@nestjs/core' import { load } from '../policy-engine.config' @@ -56,12 +56,6 @@ import { TenantRepository } from './persistence/repository/tenant.repository' useClass: ValidationPipe } ], - exports: [EngineService, ProvisionService] + exports: [EngineService, ProvisionService, BootstrapService] }) -export class EngineModule implements OnApplicationBootstrap { - constructor(private bootstrapService: BootstrapService) {} - - async onApplicationBootstrap() { - await this.bootstrapService.boot() - } -} +export class EngineModule {} diff --git a/apps/policy-engine/src/policy-engine.module.ts b/apps/policy-engine/src/policy-engine.module.ts index 6f32e13cf..02f5e6e0d 100644 --- a/apps/policy-engine/src/policy-engine.module.ts +++ b/apps/policy-engine/src/policy-engine.module.ts @@ -1,7 +1,8 @@ import { EncryptionModule } from '@narval/encryption-module' -import { Module, ValidationPipe } from '@nestjs/common' +import { Module, OnApplicationBootstrap, ValidationPipe } from '@nestjs/common' import { ConfigModule, ConfigService } from '@nestjs/config' import { APP_PIPE } from '@nestjs/core' +import { BootstrapService } from './engine/core/service/bootstrap.service' import { EngineService } from './engine/core/service/engine.service' import { EngineModule } from './engine/engine.module' import { load } from './policy-engine.config' @@ -22,7 +23,6 @@ import { EncryptionModuleOptionFactory } from './shared/factory/encryption-modul // Domain EngineModule - // TenantModule ], providers: [ { @@ -31,4 +31,10 @@ import { EncryptionModuleOptionFactory } from './shared/factory/encryption-modul } ] }) -export class PolicyEngineModule {} +export class PolicyEngineModule implements OnApplicationBootstrap { + constructor(private bootstrapService: BootstrapService) {} + + async onApplicationBootstrap() { + await this.bootstrapService.boot() + } +}