-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Engine provision and encryption module
- Loading branch information
1 parent
af961c4
commit cae1222
Showing
55 changed files
with
1,060 additions
and
572 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
apps/policy-engine/src/app/core/service/provision.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { generateKeyEncryptionKey, generateMasterKey } from '@narval/encryption-module' | ||
import { Injectable, Logger } from '@nestjs/common' | ||
import { ConfigService } from '@nestjs/config' | ||
import { randomBytes } from 'crypto' | ||
import { Config } from '../../../policy-engine.config' | ||
import { EngineService } from './engine.service' | ||
|
||
@Injectable() | ||
export class ProvisionService { | ||
private logger = new Logger(ProvisionService.name) | ||
|
||
constructor( | ||
private configService: ConfigService<Config, true>, | ||
private engineService: EngineService | ||
) {} | ||
|
||
async provision(): Promise<void> { | ||
this.logger.log('Start engine provision') | ||
|
||
const engine = await this.engineService.getEngine() | ||
|
||
const isFirstTime = engine === null | ||
|
||
// IMPORTANT: The order of internal methods call matters. | ||
|
||
if (isFirstTime) { | ||
await this.createEngine() | ||
await this.maybeSetupEncryption() | ||
} | ||
} | ||
|
||
private async createEngine(): Promise<void> { | ||
this.logger.log('Generate admin API key and save engine') | ||
|
||
await this.engineService.save({ | ||
id: this.getEngineId(), | ||
adminApiKey: randomBytes(20).toString('hex') | ||
}) | ||
} | ||
|
||
private async maybeSetupEncryption(): Promise<void> { | ||
// Get the engine's latest state. | ||
const engine = await this.engineService.getEngineOrThrow() | ||
|
||
if (engine.masterKey) { | ||
return this.logger.log('Skip master key set up because it already exists') | ||
} | ||
|
||
const keyring = this.configService.get('keyring', { infer: true }) | ||
|
||
if (keyring.type === 'raw') { | ||
this.logger.log('Generate and save engine master key') | ||
|
||
const { masterPassword } = keyring | ||
const kek = generateKeyEncryptionKey(masterPassword, this.getEngineId()) | ||
const masterKey = await generateMasterKey(kek) | ||
|
||
await this.engineService.save({ ...engine, masterKey }) | ||
} | ||
} | ||
|
||
private getEngineId(): string { | ||
return this.configService.get('engine.id', { infer: true }) | ||
} | ||
} |
Oops, something went wrong.