-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add managed data store to armory app #233
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
eeef6c9
setup new admin module
a73cd29
Merge remote-tracking branch 'origin/main' into feature/nar-1581-crea…
ecc5deb
Merge remote-tracking branch 'origin/main' into feature/nar-1581-crea…
93c7393
Merge remote-tracking branch 'origin/main' into feature/nar-1581-crea…
ad783ef
add prisma schema
775cf16
update schema
8f776ac
add policy model
2c6d7f3
add comment
8c4eef8
add signature model
59d23ac
wip
af421fe
fix
5af09d4
revert
04281ad
revert
e16051a
CR
62ef776
Merge remote-tracking branch 'origin/main' into bootstrap-admin-service
37f8e6d
remove resourceId from data store action
316be9e
Add headers to data store config (#234)
62366f2
Add sending evaluation request
13dd168
fix
ce0e6d7
fix
46a4de7
fix
08bf013
Update apps/armory/src/admin/core/service/entity-data-store.service.ts
8125622
Update apps/armory/src/orchestration/core/service/cluster.service.ts
739ff94
fixes after CR
4d2d4fd
fix
78a1f20
fix circular dependency
fc120d6
fixes after CR
5ae2e11
Merge remote-tracking branch 'origin/main' into bootstrap-admin-service
b417f77
table fixes
a2a0383
Add signature unit test
23dd945
fix
0c4888e
add parser when setting data store
5719e53
fix devtool
3c4eee9
Merge remote-tracking branch 'origin/main' into bootstrap-admin-service
892c5f7
last CR
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 48 additions & 0 deletions
48
apps/armory/src/managed-data-store/core/__test__/unit/signature.service.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,48 @@ | ||
import { FIXTURE } from '@narval/policy-engine-shared' | ||
import { Payload, SigningAlg, buildSignerEip191, hash, secp256k1PrivateKeyToJwk, signJwt } from '@narval/signature' | ||
import { ApplicationException } from '../../../../shared/exception/application.exception' | ||
import { SignatureService } from '../../service/signature.service' | ||
|
||
describe(SignatureService.name, () => { | ||
const signatureService = new SignatureService() | ||
const DATA_STORE_PRIVATE_KEY = '7cfef3303797cbc7515d9ce22ffe849c701b0f2812f999b0847229c47951fca5' | ||
const jwk = secp256k1PrivateKeyToJwk(`0x${DATA_STORE_PRIVATE_KEY}`) | ||
|
||
it('throws an exception if the payload iat is older than the db createdAt date', async () => { | ||
const payload: Payload = { | ||
data: hash(FIXTURE.ENTITIES), | ||
sub: 'test-root-user-uid', | ||
iss: 'https://armory.narval.xyz', | ||
iat: Math.floor(new Date('2023-01-01').getTime() / 1000) // in seconds | ||
} | ||
|
||
const signature = await signJwt(payload, jwk, { alg: SigningAlg.EIP191 }, buildSignerEip191(DATA_STORE_PRIVATE_KEY)) | ||
|
||
await expect(() => | ||
signatureService.verifySignature({ | ||
pubKey: jwk, | ||
payload: { signature, data: FIXTURE.ENTITIES }, | ||
date: new Date('2024-01-01') | ||
}) | ||
).rejects.toThrow(ApplicationException) | ||
}) | ||
|
||
it('returns true if the payload iat is more recent than the db createdAt date', async () => { | ||
const payload: Payload = { | ||
data: hash(FIXTURE.ENTITIES), | ||
sub: 'test-root-user-uid', | ||
iss: 'https://armory.narval.xyz', | ||
iat: Math.floor(new Date('2024-01-01').getTime() / 1000) // in seconds | ||
} | ||
|
||
const signature = await signJwt(payload, jwk, { alg: SigningAlg.EIP191 }, buildSignerEip191(DATA_STORE_PRIVATE_KEY)) | ||
|
||
const result = await signatureService.verifySignature({ | ||
pubKey: jwk, | ||
payload: { signature, data: FIXTURE.ENTITIES }, | ||
date: new Date('2023-01-01') | ||
}) | ||
|
||
expect(result).toEqual(true) | ||
}) | ||
}) |
46 changes: 46 additions & 0 deletions
46
apps/armory/src/managed-data-store/core/service/entity-data-store.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,46 @@ | ||
import { Entities, EntityStore } from '@narval/policy-engine-shared' | ||
import { publicKeySchema } from '@narval/signature' | ||
import { HttpStatus, Injectable, NotFoundException } from '@nestjs/common' | ||
import { ClientRepository } from '../../persistence/repository/client.repository' | ||
import { EntityDataStoreRepository } from '../../persistence/repository/entity-data-store.repository' | ||
import { SignatureService } from './signature.service' | ||
|
||
@Injectable() | ||
export class EntityDataStoreService extends SignatureService<Entities> { | ||
constructor( | ||
private entitydataStoreRepository: EntityDataStoreRepository, | ||
private clientRepository: ClientRepository | ||
) { | ||
super() | ||
} | ||
|
||
async getEntities(orgId: string): Promise<EntityStore | null> { | ||
const entityStore = await this.entitydataStoreRepository.getLatestDataStore(orgId) | ||
|
||
return entityStore ? EntityStore.parse(entityStore.data) : null | ||
} | ||
|
||
async setEntities(orgId: string, payload: EntityStore) { | ||
const client = await this.clientRepository.getClient(orgId) | ||
|
||
if (!client) { | ||
throw new NotFoundException({ | ||
message: 'Client data not found', | ||
suggestedHttpStatusCode: HttpStatus.NOT_FOUND | ||
}) | ||
} | ||
|
||
const dataStore = await this.entitydataStoreRepository.getLatestDataStore(orgId) | ||
|
||
await this.verifySignature({ | ||
payload, | ||
pubKey: publicKeySchema.parse(client.entityPublicKey), | ||
date: dataStore?.createdAt | ||
}) | ||
|
||
return this.entitydataStoreRepository.setDataStore(orgId, { | ||
version: dataStore?.version ? dataStore.version + 1 : 1, | ||
data: EntityStore.parse(payload) | ||
}) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
apps/armory/src/managed-data-store/core/service/policy-data-store.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,46 @@ | ||
import { Policy, PolicyStore } from '@narval/policy-engine-shared' | ||
import { publicKeySchema } from '@narval/signature' | ||
import { HttpStatus, Injectable, NotFoundException } from '@nestjs/common' | ||
import { ClientRepository } from '../../persistence/repository/client.repository' | ||
import { PolicyDataStoreRepository } from '../../persistence/repository/policy-data-store.repository' | ||
import { SignatureService } from './signature.service' | ||
|
||
@Injectable() | ||
export class PolicyDataStoreService extends SignatureService<Policy[]> { | ||
constructor( | ||
private policyDataStoreRepository: PolicyDataStoreRepository, | ||
private clientRepository: ClientRepository | ||
) { | ||
super() | ||
} | ||
|
||
async getPolicies(orgId: string): Promise<PolicyStore | null> { | ||
const policyStore = await this.policyDataStoreRepository.getLatestDataStore(orgId) | ||
|
||
return policyStore ? PolicyStore.parse(policyStore.data) : null | ||
} | ||
|
||
async setPolicies(orgId: string, payload: PolicyStore) { | ||
const client = await this.clientRepository.getClient(orgId) | ||
|
||
if (!client) { | ||
throw new NotFoundException({ | ||
message: 'Client data not found', | ||
suggestedHttpStatusCode: HttpStatus.NOT_FOUND | ||
}) | ||
} | ||
|
||
const dataStore = await this.policyDataStoreRepository.getLatestDataStore(orgId) | ||
|
||
await this.verifySignature({ | ||
payload, | ||
pubKey: publicKeySchema.parse(client.policyPublicKey), | ||
date: dataStore?.createdAt | ||
}) | ||
|
||
return this.policyDataStoreRepository.setDataStore(orgId, { | ||
version: dataStore?.version ? dataStore.version + 1 : 1, | ||
data: PolicyStore.parse(payload) | ||
}) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
apps/armory/src/managed-data-store/core/service/signature.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,34 @@ | ||
import { Jwk, hash, verifyJwt } from '@narval/signature' | ||
import { HttpStatus, Injectable } from '@nestjs/common' | ||
import { ApplicationException } from '../../../shared/exception/application.exception' | ||
|
||
@Injectable() | ||
export class SignatureService<T> { | ||
async verifySignature({ | ||
pubKey, | ||
payload, | ||
date | ||
}: { | ||
pubKey: Jwk | ||
payload: { signature: string; data: T } | ||
date: Date | undefined | ||
}) { | ||
const validJwt = await verifyJwt(payload.signature, pubKey) | ||
|
||
if (validJwt.payload.data !== hash(payload.data)) { | ||
throw new ApplicationException({ | ||
message: 'Signature hash mismatch', | ||
suggestedHttpStatusCode: HttpStatus.FORBIDDEN | ||
}) | ||
} | ||
|
||
if (date && validJwt.payload.iat && validJwt.payload.iat < date.getTime() / 1000) { | ||
throw new ApplicationException({ | ||
message: 'Signature timestamp mismatch', | ||
suggestedHttpStatusCode: HttpStatus.FORBIDDEN | ||
}) | ||
} | ||
|
||
return true | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
apps/armory/src/managed-data-store/http/controller/data-store.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Entities, EntityStore, JwtString, Policy, PolicyStore } from '@narval/policy-engine-shared' | ||
import { Body, Controller, Get, Post } from '@nestjs/common' | ||
import { ApiTags } from '@nestjs/swagger' | ||
import { OrgId } from '../../../shared/decorator/org-id.decorator' | ||
import { EntityDataStoreService } from '../../core/service/entity-data-store.service' | ||
import { PolicyDataStoreService } from '../../core/service/policy-data-store.service' | ||
|
||
@Controller('/managed-data-store') | ||
@ApiTags('Managed Data Store') | ||
export class DataStoreController { | ||
constructor( | ||
private entityDataStoreService: EntityDataStoreService, | ||
private policyDataStoreService: PolicyDataStoreService | ||
) {} | ||
|
||
@Get('/entities') | ||
async getEntities(@OrgId() orgId: string): Promise<{ entity: EntityStore } | null> { | ||
const entity = await this.entityDataStoreService.getEntities(orgId) | ||
return entity ? { entity } : null | ||
} | ||
|
||
@Get('/policies') | ||
async getPolicies(@OrgId() orgId: string): Promise<{ policy: PolicyStore } | null> { | ||
const policy = await this.policyDataStoreService.getPolicies(orgId) | ||
return policy ? { policy } : null | ||
} | ||
|
||
@Post('/entities') | ||
setEntities(@OrgId() orgId: string, @Body() payload: { signature: JwtString; data: Entities }) { | ||
return this.entityDataStoreService.setEntities(orgId, payload) | ||
} | ||
|
||
@Post('/policies') | ||
setPolicies(@OrgId() orgId: string, @Body() payload: { signature: JwtString; data: Policy[] }) { | ||
return this.policyDataStoreService.setPolicies(orgId, payload) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
apps/armory/src/managed-data-store/managed-data-store.module.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,53 @@ | ||
import { HttpModule } from '@nestjs/axios' | ||
import { ClassSerializerInterceptor, Module } from '@nestjs/common' | ||
import { ConfigModule } from '@nestjs/config' | ||
import { APP_FILTER, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core' | ||
import { ZodValidationPipe } from 'nestjs-zod' | ||
import { load } from '../armory.config' | ||
import { OrchestrationModule } from '../orchestration/orchestration.module' | ||
import { ApplicationExceptionFilter } from '../shared/filter/application-exception.filter' | ||
import { ZodExceptionFilter } from '../shared/filter/zod-exception.filter' | ||
import { PersistenceModule } from '../shared/module/persistence/persistence.module' | ||
import { EntityDataStoreService } from './core/service/entity-data-store.service' | ||
import { PolicyDataStoreService } from './core/service/policy-data-store.service' | ||
import { DataStoreController } from './http/controller/data-store.controller' | ||
import { ClientRepository } from './persistence/repository/client.repository' | ||
import { EntityDataStoreRepository } from './persistence/repository/entity-data-store.repository' | ||
import { PolicyDataStoreRepository } from './persistence/repository/policy-data-store.repository' | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
load: [load] | ||
}), | ||
HttpModule, | ||
PersistenceModule, | ||
OrchestrationModule | ||
], | ||
controllers: [DataStoreController], | ||
providers: [ | ||
EntityDataStoreService, | ||
PolicyDataStoreService, | ||
EntityDataStoreRepository, | ||
PolicyDataStoreRepository, | ||
ClientRepository, | ||
{ | ||
provide: APP_FILTER, | ||
useClass: ApplicationExceptionFilter | ||
}, | ||
{ | ||
provide: APP_FILTER, | ||
useClass: ZodExceptionFilter | ||
}, | ||
{ | ||
provide: APP_INTERCEPTOR, | ||
useClass: ClassSerializerInterceptor | ||
}, | ||
{ | ||
provide: APP_PIPE, | ||
useClass: ZodValidationPipe | ||
} | ||
], | ||
exports: [] | ||
}) | ||
export class ManagedDataStoreModule {} |
12 changes: 12 additions & 0 deletions
12
apps/armory/src/managed-data-store/persistence/repository/client.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,12 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { Organization } from '@prisma/client/armory' | ||
import { PrismaService } from '../../../shared/module/persistence/service/prisma.service' | ||
|
||
@Injectable() | ||
export class ClientRepository { | ||
constructor(private prismaService: PrismaService) {} | ||
|
||
async getClient(id: string): Promise<Organization | null> { | ||
return this.prismaService.organization.findUnique({ where: { id } }) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
apps/armory/src/managed-data-store/persistence/repository/entity-data-store.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,38 @@ | ||
import { EntityStore } from '@narval/policy-engine-shared' | ||
import { Injectable } from '@nestjs/common' | ||
import { EntityDataStore } from '@prisma/client/armory' | ||
import { PrismaService } from '../../../shared/module/persistence/service/prisma.service' | ||
|
||
@Injectable() | ||
export class EntityDataStoreRepository { | ||
constructor(private prismaService: PrismaService) {} | ||
|
||
setDataStore(orgId: string, data: { version: number; data: EntityStore }) { | ||
return this.prismaService.entityDataStore.create({ data: { orgId, ...data } }) | ||
} | ||
|
||
async getLatestDataStore(orgId: string): Promise<EntityDataStore | null> { | ||
const version = await this.getLatestVersion(orgId) | ||
|
||
if (!version) return null | ||
|
||
const dataStore = await this.prismaService.entityDataStore.findFirst({ where: { orgId, version } }) | ||
|
||
if (!dataStore) return null | ||
|
||
return dataStore | ||
} | ||
|
||
private async getLatestVersion(orgId: string): Promise<number> { | ||
const data = await this.prismaService.entityDataStore.aggregate({ | ||
where: { | ||
orgId | ||
}, | ||
_max: { | ||
version: true | ||
} | ||
}) | ||
|
||
return data._max?.version || 0 | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure we always add the iat claim using seconds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from my research seconds are the standard. See this RFC.