-
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.
Check admin API key to create a tenant (#154)
* Check admin API key to create a tenant * Fix import path
- Loading branch information
1 parent
d93d654
commit 0473682
Showing
17 changed files
with
295 additions
and
39 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
11 changes: 11 additions & 0 deletions
11
apps/policy-engine/src/app/core/exception/engine-not-provisioned.exception.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,11 @@ | ||
import { HttpStatus } from '@nestjs/common' | ||
import { ApplicationException } from '../../../shared/exception/application.exception' | ||
|
||
export class EngineNotProvisionedException extends ApplicationException { | ||
constructor() { | ||
super({ | ||
message: 'The policy engine instance was not provisioned', | ||
suggestedHttpStatusCode: HttpStatus.INTERNAL_SERVER_ERROR | ||
}) | ||
} | ||
} |
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,32 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { ConfigService } from '@nestjs/config' | ||
import { Config } from '../../../policy-engine.config' | ||
import { Engine } from '../../../shared/types/domain.type' | ||
import { EngineRepository } from '../../persistence/repository/engine.repository' | ||
import { EngineNotProvisionedException } from '../exception/engine-not-provisioned.exception' | ||
|
||
@Injectable() | ||
export class EngineService { | ||
constructor( | ||
private configService: ConfigService<Config, true>, | ||
private engineRepository: EngineRepository | ||
) {} | ||
|
||
async getEngine(): Promise<Engine> { | ||
const engine = await this.engineRepository.findById(this.getId()) | ||
|
||
if (engine) { | ||
return engine | ||
} | ||
|
||
throw new EngineNotProvisionedException() | ||
} | ||
|
||
async create(engine: Engine): Promise<Engine> { | ||
return this.engineRepository.create(engine) | ||
} | ||
|
||
private getId(): string { | ||
return this.configService.get('engine.id', { infer: true }) | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
apps/policy-engine/src/app/http/rest/controller/tenant.controller.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
55 changes: 55 additions & 0 deletions
55
apps/policy-engine/src/app/persistence/repository/__test__/unit/engine.repository.spec.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,55 @@ | ||
import { Test } from '@nestjs/testing' | ||
import { EncryptionModule } from '../../../../../encryption/encryption.module' | ||
import { ApplicationException } from '../../../../../shared/exception/application.exception' | ||
import { KeyValueRepository } from '../../../../../shared/module/key-value/core/repository/key-value.repository' | ||
import { KeyValueService } from '../../../../../shared/module/key-value/core/service/key-value.service' | ||
import { InMemoryKeyValueRepository } from '../../../../../shared/module/key-value/persistence/repository/in-memory-key-value.repository' | ||
import { Engine } from '../../../../../shared/types/domain.type' | ||
import { EngineRepository } from '../../engine.repository' | ||
|
||
describe(EngineRepository.name, () => { | ||
let repository: EngineRepository | ||
let inMemoryKeyValueRepository: InMemoryKeyValueRepository | ||
|
||
beforeEach(async () => { | ||
inMemoryKeyValueRepository = new InMemoryKeyValueRepository() | ||
|
||
const module = await Test.createTestingModule({ | ||
imports: [EncryptionModule], | ||
providers: [ | ||
KeyValueService, | ||
EngineRepository, | ||
{ | ||
provide: KeyValueRepository, | ||
useValue: inMemoryKeyValueRepository | ||
} | ||
] | ||
}).compile() | ||
|
||
repository = module.get<EngineRepository>(EngineRepository) | ||
}) | ||
|
||
describe('create', () => { | ||
const engine: Engine = { | ||
id: 'test-engine-id', | ||
adminApiKey: 'unsafe-test-admin-api-key', | ||
masterKey: 'unsafe-test-master-key' | ||
} | ||
|
||
it('creates a new engine', async () => { | ||
await repository.create(engine) | ||
|
||
const value = await inMemoryKeyValueRepository.get(repository.getKey(engine.id)) | ||
const actualEngine = await repository.findById(engine.id) | ||
|
||
expect(value).not.toEqual(null) | ||
expect(engine).toEqual(actualEngine) | ||
}) | ||
|
||
it('throws an error when engine is duplicate', async () => { | ||
await repository.create(engine) | ||
|
||
await expect(repository.create(engine)).rejects.toThrow(ApplicationException) | ||
}) | ||
}) | ||
}) |
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
45 changes: 45 additions & 0 deletions
45
apps/policy-engine/src/app/persistence/repository/engine.repository.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,45 @@ | ||
import { HttpStatus, Injectable } from '@nestjs/common' | ||
import { ApplicationException } from '../../../shared/exception/application.exception' | ||
import { KeyValueService } from '../../../shared/module/key-value/core/service/key-value.service' | ||
import { engineSchema } from '../../../shared/schema/engine.schema' | ||
import { Engine } from '../../../shared/types/domain.type' | ||
|
||
@Injectable() | ||
export class EngineRepository { | ||
constructor(private keyValueService: KeyValueService) {} | ||
|
||
async findById(id: string): Promise<Engine | null> { | ||
const value = await this.keyValueService.get(this.getKey(id)) | ||
|
||
if (value) { | ||
return this.decode(value) | ||
} | ||
|
||
return null | ||
} | ||
|
||
async create(engine: Engine): Promise<Engine> { | ||
if (await this.keyValueService.get(this.getKey(engine.id))) { | ||
throw new ApplicationException({ | ||
message: 'Engine already exist', | ||
suggestedHttpStatusCode: HttpStatus.INTERNAL_SERVER_ERROR | ||
}) | ||
} | ||
|
||
await this.keyValueService.set(this.getKey(engine.id), this.encode(engine)) | ||
|
||
return engine | ||
} | ||
|
||
getKey(id: string): string { | ||
return `engine:${id}` | ||
} | ||
|
||
private encode(engine: Engine): string { | ||
return JSON.stringify(engine) | ||
} | ||
|
||
private decode(value: string): Engine { | ||
return engineSchema.parse(JSON.parse(value)) | ||
} | ||
} |
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
Oops, something went wrong.