-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
278 additions
and
214 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,131 @@ | ||
import { createEvent } from './lib/createEvent'; | ||
import { CredentialBase, LibraryConfiguration, TargetBase, EventBase, DataSchema, UserBase, EventsPerTarget, SchemasPerTarget, AvailableEventsDefinition, TargetDefinition, RuntimeData } from './types'; | ||
import { z } from "zod" | ||
import { createEvent } from "./lib/createEvent"; | ||
import { | ||
CredentialBase, | ||
LibraryConfiguration, | ||
TargetBase, | ||
UserBase, | ||
EventsPerTarget, | ||
SchemasPerTarget, | ||
AvailableEventsDefinition, | ||
RuntimeData, | ||
} from "./types"; | ||
import { z } from "zod"; | ||
|
||
export async function handleAuditLogTrigger< | ||
T extends TargetBase, | ||
E extends EventsPerTarget<T>, | ||
S extends SchemasPerTarget<T>, | ||
S extends SchemasPerTarget<T>, | ||
U extends UserBase, | ||
C extends CredentialBase, | ||
K extends LibraryConfiguration<T,E,S,U,C>, | ||
K extends LibraryConfiguration<T, E, S, U, C>, | ||
L extends keyof K["events"], | ||
M extends keyof K["events"][L]["events"] | ||
M extends keyof K["events"][L]["events"], | ||
>({ | ||
configuration, | ||
target, | ||
event, | ||
data, | ||
user | ||
} : { | ||
configuration: LibraryConfiguration<T,E,S,U,C>, | ||
target: L, | ||
event: M, | ||
data: z.infer<K["events"][L]["dataSchema"]["schema"]>, | ||
user: U, | ||
} | ||
) { | ||
const triggerStaticMetadata = ( | ||
configuration.events as unknown as AvailableEventsDefinition<L,EventsPerTarget<L>,SchemasPerTarget<L>> | ||
)[target]["events"][event] | ||
configuration, | ||
target, | ||
event, | ||
data, | ||
user, | ||
}: { | ||
configuration: LibraryConfiguration<T, E, S, U, C>; | ||
target: L; | ||
event: M; | ||
data: z.infer<K["events"][L]["dataSchema"]["schema"]>; | ||
user: U; | ||
}) { | ||
const triggerStaticMetadata = ( | ||
configuration.events as unknown as AvailableEventsDefinition< | ||
L, | ||
EventsPerTarget<L>, | ||
SchemasPerTarget<L> | ||
> | ||
)[target]["events"][event]; | ||
|
||
const schema = ( | ||
configuration.events as unknown as AvailableEventsDefinition<L,EventsPerTarget<L>,SchemasPerTarget<L>> | ||
)[target].dataSchema | ||
const schema = ( | ||
configuration.events as unknown as AvailableEventsDefinition< | ||
L, | ||
EventsPerTarget<L>, | ||
SchemasPerTarget<L> | ||
> | ||
)[target].dataSchema; | ||
|
||
const eventData = createEvent( | ||
triggerStaticMetadata, | ||
data, | ||
user, | ||
schema, | ||
) | ||
const eventData = createEvent(triggerStaticMetadata, data, user, schema); | ||
|
||
const credentials = await ( | ||
configuration.getRelevantCredentials as unknown as (params: Omit<RuntimeData<L,EventsPerTarget<L>,SchemasPerTarget<L>,U,C>, 'credential'>) => Promise<Iterable<C>> | ||
)({ | ||
configuration: configuration as unknown as LibraryConfiguration<L,EventsPerTarget<L>,SchemasPerTarget<L>,U,C>, | ||
target, | ||
data, | ||
user, | ||
event | ||
}) | ||
const credentials = await ( | ||
configuration.getRelevantCredentials as unknown as ( | ||
params: Omit< | ||
RuntimeData<L, EventsPerTarget<L>, SchemasPerTarget<L>, U, C>, | ||
"credential" | ||
>, | ||
) => Promise<Iterable<C>> | ||
)({ | ||
configuration: configuration as unknown as LibraryConfiguration< | ||
L, | ||
EventsPerTarget<L>, | ||
SchemasPerTarget<L>, | ||
U, | ||
C | ||
>, | ||
target, | ||
data, | ||
user, | ||
event, | ||
}); | ||
|
||
try { | ||
for (const credential of credentials) { | ||
const innerEvent = { ...eventData }; | ||
|
||
(configuration.interceptEvent as unknown as (params: RuntimeData<L,EventsPerTarget<L>,SchemasPerTarget<L>,U,C>) => void)({ | ||
configuration: configuration as unknown as LibraryConfiguration<L,EventsPerTarget<L>,SchemasPerTarget<L>,U,C>, | ||
target, | ||
data, | ||
user, | ||
event, | ||
credential | ||
}) | ||
try { | ||
for (const credential of credentials) { | ||
const innerEvent = { ...eventData }; | ||
|
||
if ( | ||
credential.disabledEvents && | ||
credential.disabledEvents.includes(event as string) && | ||
target !== "SYSTEM" | ||
) { | ||
continue; | ||
} | ||
( | ||
configuration.interceptEvent as unknown as ( | ||
params: RuntimeData<L, EventsPerTarget<L>, SchemasPerTarget<L>, U, C>, | ||
) => void | ||
)({ | ||
configuration: configuration as unknown as LibraryConfiguration< | ||
L, | ||
EventsPerTarget<L>, | ||
SchemasPerTarget<L>, | ||
U, | ||
C | ||
>, | ||
target, | ||
data, | ||
user, | ||
event, | ||
credential, | ||
}); | ||
|
||
await (configuration.reportEvent as unknown as (params: Omit<RuntimeData<L,EventsPerTarget<L>,SchemasPerTarget<L>,U,C>, 'event'> & { event: typeof innerEvent}) => Promise<void>)({ | ||
configuration: configuration as unknown as LibraryConfiguration<L,EventsPerTarget<L>,SchemasPerTarget<L>,U,C>, | ||
target, | ||
data, | ||
user, | ||
event: innerEvent, | ||
credential | ||
}) | ||
} | ||
} catch (error) { | ||
throw Error() | ||
} | ||
if ( | ||
credential.disabledEvents && | ||
credential.disabledEvents.includes(event as string) && | ||
target !== "SYSTEM" | ||
) { | ||
continue; | ||
} | ||
|
||
await ( | ||
configuration.reportEvent as unknown as ( | ||
params: Omit< | ||
RuntimeData<L, EventsPerTarget<L>, SchemasPerTarget<L>, U, C>, | ||
"event" | ||
> & { event: typeof innerEvent }, | ||
) => Promise<void> | ||
)({ | ||
configuration: configuration as unknown as LibraryConfiguration< | ||
L, | ||
EventsPerTarget<L>, | ||
SchemasPerTarget<L>, | ||
U, | ||
C | ||
>, | ||
target, | ||
data, | ||
user, | ||
event: innerEvent, | ||
credential, | ||
}); | ||
} | ||
} catch (error) { | ||
throw 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 |
---|---|---|
@@ -1,63 +1,68 @@ | ||
import { DataSchema, EventBase, EventDefinition, TargetBase, UserBase } from "../types"; | ||
import { | ||
DataSchema, | ||
EventBase, | ||
EventDefinition, | ||
TargetBase, | ||
UserBase, | ||
} from "../types"; | ||
import { flattenObject } from "../utils/index"; | ||
import { z, ZodSchema} from "zod" | ||
import { z, ZodSchema } from "zod"; | ||
|
||
export function createEvent< | ||
T extends TargetBase, | ||
E extends EventBase, | ||
S extends ZodSchema, | ||
M extends DataSchema<S>, | ||
T extends TargetBase, | ||
E extends EventBase, | ||
S extends ZodSchema, | ||
M extends DataSchema<S>, | ||
>( | ||
eventStaticMetadata: EventDefinition<E, T>, | ||
data: z.infer<M["schema"]>, | ||
user: UserBase, | ||
schema?: M | ||
eventStaticMetadata: EventDefinition<E, T>, | ||
data: z.infer<M["schema"]>, | ||
user: UserBase, | ||
schema?: M, | ||
) { | ||
let dynamicSection; | ||
if (schema && schema.id && schema.name) { | ||
dynamicSection = { | ||
...eventStaticMetadata, | ||
target: { | ||
id: data[schema.id], | ||
name: data[schema.name], | ||
type: eventStaticMetadata.target, | ||
}, | ||
}; | ||
} | ||
// if (typeof AuditLogTriggerTargets[triggerStaticMetadata.target] !== "string") { | ||
// dynamicSection = { | ||
// ...eventStaticMetadata, | ||
// target: { | ||
// id: data[(AuditLogTriggerTargets[triggerStaticMetadata.target] as { id: string }).id], | ||
// name: data[(AuditLogTriggerTargets[triggerStaticMetadata.target] as { name: string }).name], | ||
// type: (AuditLogTriggerTargets[triggerStaticMetadata.target]as { value: string }).value | ||
// } | ||
// } | ||
// } else { | ||
// dynamicSection = { | ||
// ...triggerStaticMetadata, | ||
// target: { | ||
// id: data.id, | ||
// name: data.name, | ||
// type: AuditLogTriggerTargets[triggerStaticMetadata.target] | ||
// } | ||
// } | ||
// } | ||
|
||
let dynamicSection | ||
if (schema && schema.id && schema.name) { | ||
dynamicSection = { | ||
...eventStaticMetadata, | ||
target: { | ||
id: data[schema.id], | ||
name: data[schema.name], | ||
type: eventStaticMetadata.target | ||
} | ||
} | ||
} | ||
// if (typeof AuditLogTriggerTargets[triggerStaticMetadata.target] !== "string") { | ||
// dynamicSection = { | ||
// ...eventStaticMetadata, | ||
// target: { | ||
// id: data[(AuditLogTriggerTargets[triggerStaticMetadata.target] as { id: string }).id], | ||
// name: data[(AuditLogTriggerTargets[triggerStaticMetadata.target] as { name: string }).name], | ||
// type: (AuditLogTriggerTargets[triggerStaticMetadata.target]as { value: string }).value | ||
// } | ||
// } | ||
// } else { | ||
// dynamicSection = { | ||
// ...triggerStaticMetadata, | ||
// target: { | ||
// id: data.id, | ||
// name: data.name, | ||
// type: AuditLogTriggerTargets[triggerStaticMetadata.target] | ||
// } | ||
// } | ||
// } | ||
|
||
return { | ||
actor: { | ||
id: user.id, | ||
name: user.name, | ||
}, | ||
...dynamicSection, | ||
is_anonymous: user.id === "-1" ? true : false, | ||
is_failure: false, | ||
group: { | ||
id: "default", | ||
name: "default", | ||
}, | ||
fields: flattenObject(data), | ||
// source_ip, | ||
}; | ||
} | ||
return { | ||
actor: { | ||
id: user.id, | ||
name: user.name, | ||
}, | ||
...dynamicSection, | ||
is_anonymous: user.id === "-1" ? true : false, | ||
is_failure: false, | ||
group: { | ||
id: "default", | ||
name: "default", | ||
}, | ||
fields: flattenObject(data), | ||
// source_ip, | ||
}; | ||
} |
Oops, something went wrong.