Skip to content

Commit

Permalink
feat: event-logger improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Brummos committed Feb 2, 2024
1 parent 0076160 commit a3fdcd2
Show file tree
Hide file tree
Showing 16 changed files with 3,886 additions and 3,813 deletions.
2 changes: 1 addition & 1 deletion packages/contact-manager-rest-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1 align="center">
<br>
<a href="https://www.sphereon.com"><img src="https://sphereon.com/content/themes/sphereon/assets/img/logo.svg" alt="Sphereon" width="400"></a>
<br>Contact Manager Rest API(Typescript)
<br>Contact Manager Rest API (Typescript)
<br>
</h1>

Expand Down
2 changes: 0 additions & 2 deletions packages/contact-manager-rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@
"typeorm": "^0.3.12"
},
"files": [
".yalc/**/*",
"dist/**/*",
"src/**/*",
"README.md",
"plugin.schema.json",
"LICENSE"
],
"private": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class AuditEventEntity extends BaseEntity {
@Column({ name: 'systemCorrelationId', nullable: true, unique: false })
systemCorrelationId?: string

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

@Column('simple-enum', { name: 'partyCorrelationType', enum: PartyCorrelationType, nullable: true, unique: false })
Expand Down
8 changes: 7 additions & 1 deletion packages/data-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ export { StatusListStore } from './statusList/StatusListStore'
import { AuditEventEntity, auditEventEntityFrom } from './entities/eventLogger/AuditEventEntity'
export { AbstractEventLoggerStore } from './eventLogger/AbstractEventLoggerStore'
export { EventLoggerStore } from './eventLogger/EventLoggerStore'
export { DataStoreMigrations } from './migrations'
export {
DataStoreMigrations,
DataStoreEventLoggerMigrations,
DataStoreContactMigrations,
DataStoreIssuanceBrandingMigrations,
DataStoreStatusListMigrations
} from './migrations'
export * from './types'
export * from './utils/contact/MappingUtils'

Expand Down
8 changes: 7 additions & 1 deletion packages/data-store/src/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export { DataStoreMigrations } from './generic'
export {
DataStoreMigrations,
DataStoreEventLoggerMigrations,
DataStoreContactMigrations,
DataStoreIssuanceBrandingMigrations,
DataStoreStatusListMigrations
} from './generic'
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class CreateAuditEvents1701634812183 implements MigrationInterface {
await queryRunner.query(`CREATE TYPE "public"."System_correlation_id_type_enum" AS ENUM('did', 'email', 'hostname', 'phone', 'user')`)
await queryRunner.query(`CREATE TYPE "public"."Party_correlation_type_enum" AS ENUM('did', 'email', 'hostname', 'phone')`)
await queryRunner.query(
`CREATE TABLE "AuditEvents" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "timestamp" TIMESTAMP NOT NULL, "level" "public"."Level_enum" NOT NULL, "correlationId" TEXT NOT NULL, "system" "public"."System_enum" NOT NULL, "subSystemType" "public"."Subsystem_type_enum" NOT NULL, "actionType" "public"."Action_type_enum" NOT NULL, "actionSubType" TEXT NOT NULL, "initiatorType" "public"."Initiator_type_enum" NOT NULL, "systemCorrelationIdType" "public"."System_correlation_id_type_enum", "systemCorrelationId" TEXT, "systemAlias" TEXT NOT NULL, "partyCorrelationType" "public"."Party_correlation_type_enum", "partyCorrelationId" TEXT, "partyAlias" TEXT, "description" TEXT NOT NULL, "data" TEXT, "diagnosticData" TEXT, "created_at" TIMESTAMP NOT NULL DEFAULT now(), "last_updated_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_AuditEvents_id" PRIMARY KEY ("id"))`
`CREATE TABLE "AuditEvents" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "timestamp" TIMESTAMP NOT NULL, "level" "public"."Level_enum" NOT NULL, "correlationId" TEXT NOT NULL, "system" "public"."System_enum" NOT NULL, "subSystemType" "public"."Subsystem_type_enum" NOT NULL, "actionType" "public"."Action_type_enum" NOT NULL, "actionSubType" TEXT NOT NULL, "initiatorType" "public"."Initiator_type_enum" NOT NULL, "systemCorrelationIdType" "public"."System_correlation_id_type_enum", "systemCorrelationId" TEXT, "systemAlias" TEXT, "partyCorrelationType" "public"."Party_correlation_type_enum", "partyCorrelationId" TEXT, "partyAlias" TEXT, "description" TEXT NOT NULL, "data" TEXT, "diagnosticData" TEXT, "created_at" TIMESTAMP NOT NULL DEFAULT now(), "last_updated_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_AuditEvents_id" PRIMARY KEY ("id"))`
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class CreateAuditEvents1701634819487 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "AuditEvents" ("id" varchar PRIMARY KEY NOT NULL, "timestamp" datetime NOT NULL, "level" varchar CHECK( "level" IN ('0','1','2','3') ) NOT NULL, "correlationId" varchar NOT NULL, "system" varchar CHECK( "system" IN ('general','kms','identity','oid4vci','credentials','web3','profile','contact') ) NOT NULL, "subSystemType" varchar CHECK( "subSystemType" IN ('key','did_provider','did_resolver','oid4vp_op','oid4vci_client','siopv2_op','contact_manager','vc_issuer','vc_verifier','vc_persistence','transport','profile') ) NOT NULL, "actionType" varchar CHECK( "actionType" IN ('create','read','update','delete','execute') ) NOT NULL, "actionSubType" varchar NOT NULL, "initiatorType" varchar CHECK( "initiatorType" IN ('user','system','external') ) NOT NULL, "systemCorrelationIdType" varchar CHECK( "systemCorrelationIdType" IN ('did','email','hostname','phone','user') ), "systemCorrelationId" varchar, "systemAlias" varchar NOT NULL, "partyCorrelationType" varchar CHECK( "partyCorrelationType" IN ('did','email','hostname','phone') ), "partyCorrelationId" varchar, "partyAlias" varchar, "description" varchar NOT NULL, "data" varchar, "diagnosticData" varchar, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "last_updated_at" datetime NOT NULL DEFAULT (datetime('now')))`
`CREATE TABLE "AuditEvents" ("id" varchar PRIMARY KEY NOT NULL, "timestamp" datetime NOT NULL, "level" varchar CHECK( "level" IN ('0','1','2','3') ) NOT NULL, "correlationId" varchar NOT NULL, "system" varchar CHECK( "system" IN ('general','kms','identity','oid4vci','credentials','web3','profile','contact') ) NOT NULL, "subSystemType" varchar CHECK( "subSystemType" IN ('key','did_provider','did_resolver','oid4vp_op','oid4vci_client','siopv2_op','contact_manager','vc_issuer','vc_verifier','vc_persistence','transport','profile') ) NOT NULL, "actionType" varchar CHECK( "actionType" IN ('create','read','update','delete','execute') ) NOT NULL, "actionSubType" varchar NOT NULL, "initiatorType" varchar CHECK( "initiatorType" IN ('user','system','external') ) NOT NULL, "systemCorrelationIdType" varchar CHECK( "systemCorrelationIdType" IN ('did','email','hostname','phone','user') ), "systemCorrelationId" varchar, "systemAlias" varchar, "partyCorrelationType" varchar CHECK( "partyCorrelationType" IN ('did','email','hostname','phone') ), "partyCorrelationId" varchar, "partyAlias" varchar, "description" varchar NOT NULL, "data" varchar, "diagnosticData" varchar, "created_at" datetime NOT NULL DEFAULT (datetime('now')), "last_updated_at" datetime NOT NULL DEFAULT (datetime('now')))`
)
}

Expand Down
3 changes: 2 additions & 1 deletion packages/event-logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ This is mainly as a fallback for when no listener is present within the agent.
```typescript
import { migrations, Entities } from '@veramo/data-store'
import { EventLogger, IEventLogger } from '@sphereon/ssi-sdk.event-logger'
import { EventLoggerStore, DataStoreMigrations, DataStoreEventLoggerEntities, LoggingEventType } from '@sphereon/ssi-sdk.data-store'
import { EventLoggerStore, DataStoreMigrations, DataStoreEventLoggerEntities } from '@sphereon/ssi-sdk.data-store'
import { LoggingEventType } from '@sphereon/ssi-sdk.core'

const dbConnection = createConnection({
type: 'react-native',
Expand Down
19 changes: 16 additions & 3 deletions packages/event-logger/src/agent/EventLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { EventLoggerOptions, GetAuditEventsArgs, IEventLogger, RequiredContext,
* {@inheritDoc IEventLogger}
*/

// Exposing the methods here for any REST implementation
export const eventLoggerAuditMethods: Array<string> = ['loggerGetAuditEvents', 'loggerLogAuditEvent']
export const eventLoggerMethods: Array<string> = [...eventLoggerAuditMethods]

export class EventLogger implements IAgentPlugin {
readonly schema = schema.IEventLogger
readonly eventTypes: Array<LoggingEventType> = []
Expand All @@ -18,7 +22,7 @@ export class EventLogger implements IAgentPlugin {
loggerLogAuditEvent: this.loggerLogAuditEvent.bind(this),
}

private readonly store: AbstractEventLoggerStore
private readonly store?: AbstractEventLoggerStore

constructor(options: EventLoggerOptions) {
const { store, eventTypes } = options
Expand All @@ -29,7 +33,7 @@ export class EventLogger implements IAgentPlugin {
public async onEvent(event: LoggingEvent, context: RequiredContext): Promise<void> {
switch (event.type) {
case LoggingEventType.AUDIT:
await this.loggerLogAuditEvent({ event: event.data }, context)
await context.agent.loggerLogAuditEvent({ event: event.data })
break
default:
return Promise.reject(Error('Event type not supported'))
Expand All @@ -39,17 +43,26 @@ export class EventLogger implements IAgentPlugin {
private async loggerGetAuditEvents(args?: GetAuditEventsArgs): Promise<Array<AuditLoggingEvent>> {
const { filter } = args ?? {}

if (!this.store) {
return Promise.reject(Error('No store available in options'))
}

return this.store.getAuditEvents({ filter })
}

private async loggerLogAuditEvent(args: LogAuditEventArgs, context: RequiredContext): Promise<AuditLoggingEvent> {
private async loggerLogAuditEvent(args: LogAuditEventArgs): Promise<AuditLoggingEvent> {
const { event } = args

if (!this.store) {
return Promise.reject(Error('No store available in options'))
}

return this.store.storeAuditEvent({
event: {
...event,
system: event.system,
subSystemType: event.subSystemType,
initiatorType: event.initiatorType,
level: event.level ?? LogLevel.INFO,
correlationId: event.correlationId ?? uuidv4(),
timestamp: new Date(),
Expand Down
2 changes: 1 addition & 1 deletion packages/event-logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
*/
const schema = require('../plugin.schema.json')
export { schema }
export { EventLogger } from './agent/EventLogger'
export { EventLogger, eventLoggerMethods, eventLoggerAuditMethods } from './agent/EventLogger'
export * from './types/IEventLogger'
9 changes: 5 additions & 4 deletions packages/event-logger/src/types/IEventLogger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IAgentContext, IPluginMethodMap } from '@veramo/core'
import { AuditLoggingEvent, NonPersistedAuditLoggingEvent as NPAuditLoggingEvent, LoggingEventType, SubSystem, System } from '@sphereon/ssi-sdk.core'
import { AuditLoggingEvent, NonPersistedAuditLoggingEvent as NPAuditLoggingEvent, LoggingEventType, SubSystem, System, InitiatorType } from '@sphereon/ssi-sdk.core'
import { AbstractEventLoggerStore, FindAuditLoggingEventArgs } from '@sphereon/ssi-sdk.data-store'

export interface IEventLogger extends IPluginMethodMap {
Expand All @@ -8,7 +8,7 @@ export interface IEventLogger extends IPluginMethodMap {
}

export type EventLoggerOptions = {
store: AbstractEventLoggerStore
store?: AbstractEventLoggerStore
eventTypes: Array<LoggingEventType>
}

Expand All @@ -20,14 +20,15 @@ export type LogAuditEventArgs = {
event: NonPersistedAuditLoggingEvent
}

export type NonPersistedAuditLoggingEvent = Omit<NPAuditLoggingEvent, 'system' | 'subSystemType'> & {
export type NonPersistedAuditLoggingEvent = Omit<NPAuditLoggingEvent, 'system' | 'subSystemType' | 'initiatorType'> & {
system: System
subSystemType: SubSystem
initiatorType: InitiatorType
}

export type LoggingEvent = {
type: LoggingEventType
data: NonPersistedAuditLoggingEvent
}

export type RequiredContext = IAgentContext<never>
export type RequiredContext = IAgentContext<IEventLogger>
23 changes: 20 additions & 3 deletions packages/ssi-sdk-core/src/loggers/eventLogger/EventLogger.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
import { IAgentContext } from '@veramo/core'
import { NonPersistedAuditLoggingEvent, EventLoggerArgs, LoggingEvent, LogLevel, SubSystem, System } from '../../types'
import Debug, { Debugger } from 'debug'
import {
NonPersistedAuditLoggingEvent,
EventLoggerArgs,
LoggingEvent,
LogLevel,
SubSystem,
System,
InitiatorType
} from '../../types'

class EventLogger {
private readonly context?: IAgentContext<any>
private readonly namespace?: string
private readonly system?: System
private readonly subSystemType?: SubSystem
private readonly logLevel: LogLevel
private readonly initiatorType?: InitiatorType
private readonly debug: Debugger

constructor(args: EventLoggerArgs) {
const { context, namespace = 'sphereon:ssi-sdk:EventLogger', system, subSystem, logLevel = LogLevel.INFO } = args
const {
context,
namespace = 'sphereon:ssi-sdk:EventLogger',
system,
subSystem,
logLevel = LogLevel.INFO,
initiatorType
} = args

this.context = context
this.namespace = namespace
this.system = system
this.subSystemType = subSystem
this.logLevel = logLevel
this.initiatorType = initiatorType
this.debug = Debug(this.namespace)
}

Expand All @@ -35,6 +51,7 @@ class EventLogger {
...(!event.data.level && { level: this.logLevel }),
...(!event.data.system && { system: this.system }),
...(!event.data.subSystemType && { subSystemType: this.subSystemType }),
...(!event.data.initiatorType && { initiatorType: this.initiatorType }),
}

// TODO make default behaviour more configurable once we have a logger registry
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { IAgentContext } from '@veramo/core'
import EventLogger from './EventLogger'
import { LogLevel, SubSystem, System } from '../../types'
import { InitiatorType, LogLevel, SubSystem, System } from '../../types'

class EventLoggerBuilder {
private context?: IAgentContext<any>
private namespace?: string
private system?: System
private subSystem?: SubSystem
private logLevel?: LogLevel
private initiatorType?: InitiatorType

withContext(context: IAgentContext<any>): this {
this.context = context
Expand All @@ -34,13 +35,19 @@ class EventLoggerBuilder {
return this
}

withInitiatorType(initiatorType: InitiatorType): this {
this.initiatorType = initiatorType
return this
}

public build(): EventLogger {
return new EventLogger({
context: this.context,
namespace: this.namespace,
system: this.system,
subSystem: this.subSystem,
logLevel: this.logLevel,
initiatorType: this.initiatorType
})
}
}
Expand Down
7 changes: 5 additions & 2 deletions packages/ssi-sdk-core/src/types/events.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { IAgentContext } from '@veramo/core'

export enum LogLevel {
DEBUG = 0,
TRACE = 0,
DEBUG,
INFO,
WARNING,
ERROR,
Expand Down Expand Up @@ -102,11 +103,12 @@ export type AuditLoggingEvent = {
}
export type PartialAuditLoggingEvent = Partial<AuditLoggingEvent>

export type NonPersistedAuditLoggingEvent = Omit<AuditLoggingEvent, 'id' | 'timestamp' | 'level' | 'correlationId' | 'system' | 'subSystemType'> & {
export type NonPersistedAuditLoggingEvent = Omit<AuditLoggingEvent, 'id' | 'timestamp' | 'level' | 'correlationId' | 'system' | 'subSystemType' | 'initiatorType'> & {
level?: LogLevel
correlationId?: string
system?: System
subSystemType?: SubSystem
initiatorType?: InitiatorType
}

export type LoggingEvent = {
Expand All @@ -120,4 +122,5 @@ export type EventLoggerArgs = {
system?: System
subSystem?: SubSystem
logLevel?: LogLevel
initiatorType?: InitiatorType
}
4 changes: 3 additions & 1 deletion packages/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
{ "path": "siopv2-oid4vp-rp-rest-client" },
{ "path": "qr-code-generator" },
{ "path": "contact-manager" },
{ "path": "contact-manager-rest-api" },
{ "path": "data-store" },
{ "path": "wellknown-did-issuer" },
{ "path": "wellknown-did-verifier" },
{ "path": "kv-store" },
{ "path": "issuance-branding" },
{ "path": "headless-web3-provider" },
{ "path": "event-logger" }
{ "path": "event-logger" },
{ "path": "remote-server-rest-api" }
]
}
Loading

0 comments on commit a3fdcd2

Please sign in to comment.