Skip to content

Commit

Permalink
DPP-96 added event-logger plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Brummos committed Dec 8, 2023
1 parent 9e3fc6a commit e8ee09e
Show file tree
Hide file tree
Showing 35 changed files with 1,427 additions and 27 deletions.
82 changes: 82 additions & 0 deletions packages/data-store/src/__tests__/eventLogger.entities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { DataSource } from 'typeorm'
import { DataStoreEventLoggerEntities } from '../index'
import { DataStoreEventLoggerMigrations } from '../migrations/generic'
import {
ActionType,
AuditLoggingEvent,
InitiatorType,
LogLevel,
PartyCorrelationType,
SubSystem,
System,
SystemCorrelationIdType
} from '@sphereon/ssi-sdk.core'
import { auditEventEntityFrom, AuditEventEntity } from '../entities/eventLogger/AuditEventEntity'

describe('Database entities tests', (): void => {
let dbConnection: DataSource

beforeEach(async (): Promise<void> => {
dbConnection = await new DataSource({
type: 'sqlite',
database: ':memory:',
//logging: 'all',
migrationsRun: false,
migrations: DataStoreEventLoggerMigrations,
synchronize: false,
entities: [...DataStoreEventLoggerEntities],
}).initialize()
await dbConnection.runMigrations()
expect(await dbConnection.showMigrations()).toBeFalsy()
})

afterEach(async (): Promise<void> => {
await (await dbConnection).destroy()
})

it('should save audit event to database', async (): Promise<void> => {
const auditEvent: Omit<AuditLoggingEvent, 'id'> = {
timestamp: new Date(),
level: LogLevel.DEBUG,
correlationId: 'b40b8474-58a2-4b23-9fde-bd6ee1902cdb',
system: System.GENERAL,
subSystemType: SubSystem.DID_PROVIDER,
actionType: ActionType.CREATE,
actionSubType: 'Key generation',
initiatorType: InitiatorType.EXTERNAL,
systemCorrelationIdType: SystemCorrelationIdType.DID,
systemCorrelationId: '2dfa190f-b8f4-4532-831e-bc354f034775',
systemAlias: 'test_alias',
partyCorrelationType: PartyCorrelationType.DID,
partyCorrelationId: '75cfd84a-0f3b-4fb1-97a3-a1506c7ab850',
partyAlias: 'test_alias',
description: 'test_description',
data: 'test_data_string',
diagnosticData: { data: 'test_data_string'}
}

const auditEventEntity: AuditEventEntity = auditEventEntityFrom(auditEvent)
const fromDb: AuditEventEntity = await dbConnection.getRepository(AuditEventEntity).save(auditEventEntity)

expect(fromDb).toBeDefined()
expect(fromDb?.id).not.toBeNull()
expect(fromDb?.timestamp).toEqual(auditEvent.timestamp)
expect(fromDb?.level).toEqual(auditEvent.level)
expect(fromDb?.correlationId).toEqual(auditEvent.correlationId)
expect(fromDb?.system).toEqual(auditEvent.system)
expect(fromDb?.subSystemType).toEqual(auditEvent.subSystemType)
expect(fromDb?.actionType).toEqual(auditEvent.actionType)
expect(fromDb?.actionSubType).toEqual(auditEvent.actionSubType)
expect(fromDb?.initiatorType).toEqual(auditEvent.initiatorType)
expect(fromDb?.systemCorrelationIdType).toEqual(auditEvent.systemCorrelationIdType)
expect(fromDb?.systemCorrelationId).toEqual(auditEvent.systemCorrelationId)
expect(fromDb?.systemAlias).toEqual(auditEvent.systemAlias)
expect(fromDb?.partyCorrelationType).toEqual(auditEvent.partyCorrelationType)
expect(fromDb?.partyCorrelationId).toEqual(auditEvent.partyCorrelationId)
expect(fromDb?.partyAlias).toEqual(auditEvent.partyAlias)
expect(fromDb?.description).toEqual(auditEvent.description)
expect(fromDb?.data).toEqual(JSON.stringify(auditEvent.data))
expect(fromDb?.diagnosticData).toEqual(JSON.stringify(auditEvent.diagnosticData))
})

})
140 changes: 140 additions & 0 deletions packages/data-store/src/__tests__/eventLogger.store.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { DataSource } from 'typeorm'
import { DataStoreEventLoggerMigrations } from '../migrations/generic'
import {
DataStoreEventLoggerEntities,
EventLoggerStore,
GetAuditEventsArgs
} from '../index'
import {
ActionType,
AuditLoggingEvent,
InitiatorType,
LogLevel,
PartyCorrelationType,
SubSystem,
System,
SystemCorrelationIdType
} from '@sphereon/ssi-sdk.core'


describe('Database entities tests', (): void => {
let dbConnection: DataSource
let eventLoggerStore: EventLoggerStore

beforeEach(async (): Promise<void> => {
dbConnection = await new DataSource({
type: 'sqlite',
database: ':memory:',
//logging: 'all',
migrationsRun: false,
migrations: DataStoreEventLoggerMigrations,
synchronize: false,
entities: DataStoreEventLoggerEntities,
}).initialize()
await dbConnection.runMigrations()
expect(await dbConnection.showMigrations()).toBeFalsy()
eventLoggerStore = new EventLoggerStore(dbConnection)
})

afterEach(async (): Promise<void> => {
await (await dbConnection).destroy()
})

it('should store audit event', async (): Promise<void> => {
const auditEvent: Omit<AuditLoggingEvent, 'id'> = {
timestamp: new Date(),
level: LogLevel.DEBUG,
correlationId: 'b40b8474-58a2-4b23-9fde-bd6ee1902cdb',
system: System.GENERAL,
subSystemType: SubSystem.DID_PROVIDER,
actionType: ActionType.CREATE,
actionSubType: 'Key generation',
initiatorType: InitiatorType.EXTERNAL,
systemCorrelationIdType: SystemCorrelationIdType.DID,
systemCorrelationId: '2dfa190f-b8f4-4532-831e-bc354f034775',
systemAlias: 'test_alias',
partyCorrelationType: PartyCorrelationType.DID,
partyCorrelationId: '75cfd84a-0f3b-4fb1-97a3-a1506c7ab850',
partyAlias: 'test_alias',
description: 'test_description',
data: 'test_data_string',
diagnosticData: { data: 'test_data_string'}
}

const savedAuditEvent: AuditLoggingEvent = await eventLoggerStore.storeAuditEvent({event: auditEvent})
expect(savedAuditEvent).toBeDefined()
})

it('should get all audit events', async (): Promise<void> => {
const auditEvent: Omit<AuditLoggingEvent, 'id'> = {
timestamp: new Date(),
level: LogLevel.DEBUG,
correlationId: 'b40b8474-58a2-4b23-9fde-bd6ee1902cdb',
system: System.GENERAL,
subSystemType: SubSystem.DID_PROVIDER,
actionType: ActionType.CREATE,
actionSubType: 'Key generation',
initiatorType: InitiatorType.EXTERNAL,
systemCorrelationIdType: SystemCorrelationIdType.DID,
systemCorrelationId: '2dfa190f-b8f4-4532-831e-bc354f034775',
systemAlias: 'test_alias',
partyCorrelationType: PartyCorrelationType.DID,
partyCorrelationId: '75cfd84a-0f3b-4fb1-97a3-a1506c7ab850',
partyAlias: 'test_alias',
description: 'test_description',
data: 'test_data_string',
diagnosticData: { data: 'test_data_string'}
}

const auditEvent1: AuditLoggingEvent = await eventLoggerStore.storeAuditEvent({event: auditEvent})
expect(auditEvent1).toBeDefined()

const auditEvent2: AuditLoggingEvent = await eventLoggerStore.storeAuditEvent({event: auditEvent})
expect(auditEvent2).toBeDefined()

const result: Array<AuditLoggingEvent> = await eventLoggerStore.getAuditEvents()
expect(result.length).toEqual(2)
})

it('should get audit events by filter', async (): Promise<void> => {
const auditEvent: Omit<AuditLoggingEvent, 'id'> = {
timestamp: new Date(),
level: LogLevel.DEBUG,
correlationId: 'b40b8474-58a2-4b23-9fde-bd6ee1902cdb',
system: System.GENERAL,
subSystemType: SubSystem.DID_PROVIDER,
actionType: ActionType.CREATE,
actionSubType: 'Key generation',
initiatorType: InitiatorType.EXTERNAL,
systemCorrelationIdType: SystemCorrelationIdType.DID,
systemCorrelationId: '2dfa190f-b8f4-4532-831e-bc354f034775',
systemAlias: 'test_alias',
partyCorrelationType: PartyCorrelationType.DID,
partyCorrelationId: '75cfd84a-0f3b-4fb1-97a3-a1506c7ab850',
partyAlias: 'test_alias',
description: 'test_description',
data: 'test_data_string',
diagnosticData: { data: 'test_data_string'}
}

const savedAuditEvent: AuditLoggingEvent = await eventLoggerStore.storeAuditEvent({event: auditEvent})
expect(savedAuditEvent).toBeDefined()

const args: GetAuditEventsArgs = {
filter: [{ correlationId: auditEvent.correlationId }],
}
const result: Array<AuditLoggingEvent> = await eventLoggerStore.getAuditEvents(args)

expect(result.length).toEqual(1)
})

it('should return no audit events if filter does not match', async (): Promise<void> => {
const args: GetAuditEventsArgs = {
filter: [{ correlationId: 'unknown_id'}],
}
const result: Array<AuditLoggingEvent> = await eventLoggerStore.getAuditEvents(args)

expect(result.length).toEqual(0)
})

})
106 changes: 106 additions & 0 deletions packages/data-store/src/entities/eventLogger/AuditEventEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {
BaseEntity,
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
UpdateDateColumn
} from 'typeorm'
import {
ActionType,
InitiatorType,
LogLevel,
PartyCorrelationType,
SubSystem,
System,
SystemCorrelationIdType,
AuditLoggingEvent,
ActionSubType
} from '@sphereon/ssi-sdk.core'

@Entity('AuditEvents')
export class AuditEventEntity extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id!: string

@Column({ name: 'timestamp', nullable: false, unique: false})
timestamp!: Date

@Column('simple-enum', { name: 'level', enum: LogLevel, nullable: false, unique: false })
level!: LogLevel

@Column({ name: 'correlationId', length: 255, nullable: false, unique: false})
correlationId!: string

@Column('simple-enum', { name: 'system', enum: System, nullable: false, unique: false })
system!: System

@Column('simple-enum', { name: 'subSystemType', enum: SubSystem, nullable: false, unique: false })
subSystemType!: SubSystem

@Column('simple-enum', { name: 'actionType', enum: ActionType, nullable: false, unique: false })
actionType!: ActionType

@Column({ name: 'actionSubType', length: 255, nullable: false, unique: false })
actionSubType!: ActionSubType

@Column('simple-enum', { name: 'initiatorType', enum: InitiatorType, nullable: false, unique: false })
initiatorType!: InitiatorType

@Column('simple-enum', { name: 'systemCorrelationIdType', enum: SystemCorrelationIdType, nullable: true, unique: false })
systemCorrelationIdType?: SystemCorrelationIdType

@Column({ name: 'systemCorrelationId', length: 255, nullable: true, unique: false})
systemCorrelationId?: string

@Column({ name: 'systemAlias', length: 255, nullable: false, unique: false})
systemAlias?: string

@Column('simple-enum', { name: 'partyCorrelationType', enum: PartyCorrelationType, nullable: true, unique: false })
partyCorrelationType?: PartyCorrelationType

@Column({ name: 'partyCorrelationId', length: 255, nullable: true, unique: false})
partyCorrelationId?: string

@Column({ name: 'partyAlias', length: 255, nullable: true, unique: false})
partyAlias?: string

@Column({ name: 'description', length: 255, nullable: false, unique: false})
description!: string

@Column({ name: 'data', length: 255, nullable: true, unique: false})
data?: string

@Column({ name: 'diagnosticData', length: 255, nullable: true, unique: false})
diagnosticData?: string

@CreateDateColumn({ name: 'created_at', nullable: false })
createdAt!: Date

@UpdateDateColumn({ name: 'last_updated_at', nullable: false })
lastUpdatedAt!: Date
}

export const auditEventEntityFrom = (args: Omit<AuditLoggingEvent, 'id'>): AuditEventEntity => {
const auditEventEntity: AuditEventEntity = new AuditEventEntity()
auditEventEntity.timestamp = args.timestamp
auditEventEntity.level = args.level
auditEventEntity.correlationId = args.correlationId
auditEventEntity.system = args.system
auditEventEntity.subSystemType = args.subSystemType
auditEventEntity.actionType = args.actionType
auditEventEntity.actionSubType = args.actionSubType
auditEventEntity.initiatorType = args.initiatorType
auditEventEntity.systemCorrelationIdType = args.systemCorrelationIdType
auditEventEntity.systemCorrelationId = args.systemCorrelationId
auditEventEntity.systemAlias = args.systemAlias
auditEventEntity.partyCorrelationType = args.partyCorrelationType
auditEventEntity.partyCorrelationId = args.partyCorrelationId
auditEventEntity.partyAlias = args.partyAlias
auditEventEntity.description = args.description
auditEventEntity.partyCorrelationType = args.partyCorrelationType
auditEventEntity.data = JSON.stringify(args.data)
auditEventEntity.diagnosticData = JSON.stringify(args.diagnosticData)

return auditEventEntity
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { GetAuditEventsArgs, StoreAuditEventArgs } from '../types'
import { AuditLoggingEvent } from '@sphereon/ssi-sdk.core'

export abstract class AbstractEventLoggerStore {
abstract getAuditEvents(args: GetAuditEventsArgs): Promise<Array<AuditLoggingEvent>>
abstract storeAuditEvent(args: StoreAuditEventArgs): Promise<AuditLoggingEvent>
}

Loading

0 comments on commit e8ee09e

Please sign in to comment.