Skip to content

Commit

Permalink
Move bootstrap to the policy engine top level module
Browse files Browse the repository at this point in the history
Check encryption configuration at the bootstrap.
  • Loading branch information
wcalderipe committed Mar 21, 2024
1 parent 094e414 commit aa96fd8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
23 changes: 22 additions & 1 deletion apps/policy-engine/src/engine/core/service/bootstrap.service.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<void> {
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,
Expand All @@ -36,6 +42,21 @@ export class BootstrapService {
await this.syncTenants()
}

private async checkEncryptionConfiguration(): Promise<void> {
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<void> {
const tenants = await this.tenantService.findAll()

Expand Down
12 changes: 3 additions & 9 deletions apps/policy-engine/src/engine/engine.module.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 {}
12 changes: 9 additions & 3 deletions apps/policy-engine/src/policy-engine.module.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -22,7 +23,6 @@ import { EncryptionModuleOptionFactory } from './shared/factory/encryption-modul

// Domain
EngineModule
// TenantModule
],
providers: [
{
Expand All @@ -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()
}
}

0 comments on commit aa96fd8

Please sign in to comment.