-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAudit.ts
64 lines (53 loc) · 1.45 KB
/
Audit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { values } from 'lodash';
import { format } from 'util';
export type AuditEntry = {
action: AuditAction,
type: string,
id: string | number,
};
type InternalAuditEntry = {
time: string,
} & AuditEntry;
export enum AuditAction {
Read = 'read',
Write = 'write',
Remove = 'remove',
Create = 'create',
}
export class Audit {
entries: { [key: string]: InternalAuditEntry } = {};
constructor(
private requestId?: string,
private principal?: string,
private deviceId?: string,
) {
}
public async clear() {
this.entries = {};
}
public async add(entry: AuditEntry) {
const type = entry.type.replace(/ /ig, '_').toLocaleLowerCase();
const key = `${type}:${entry.id}`;
if (this.entries[key] != null) {
// we don't override the time if it has been access more than once
return;
}
this.entries[key] = {
...entry,
type,
time: new Date().toISOString(),
};
}
/**
* We don't want lambda to play with our log messages
*/
public async dump() {
values(this.entries).forEach((e) => {
process.stdout.write(
// tslint:disable-next-line: prefer-template
format('AUDIT', e.time, this.requestId, this.deviceId, this.principal, e.action, e.type, e.id) + '\n',
);
});
this.clear();
}
}