@alanszp/audit 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitignore +3 -0
- package/.npmignore +3 -0
- package/LICENSE +21 -0
- package/dist/audit.d.ts +10 -0
- package/dist/audit.js +31 -0
- package/dist/audit.js.map +1 -0
- package/dist/errors/AuditError.d.ts +3 -0
- package/dist/errors/AuditError.js +8 -0
- package/dist/errors/AuditError.js.map +1 -0
- package/dist/errors/MissingAuditFieldsError.d.ts +9 -0
- package/dist/errors/MissingAuditFieldsError.js +23 -0
- package/dist/errors/MissingAuditFieldsError.js.map +1 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.js +15 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/factory.d.ts +4 -0
- package/dist/factory.js +16 -0
- package/dist/factory.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces.d.ts +18 -0
- package/dist/interfaces.js +22 -0
- package/dist/interfaces.js.map +1 -0
- package/package.json +26 -0
- package/src/audit.ts +41 -0
- package/src/errors/AuditError.ts +3 -0
- package/src/errors/MissingAuditFieldsError.ts +28 -0
- package/src/errors/index.ts +2 -0
- package/src/factory.ts +16 -0
- package/src/index.ts +4 -0
- package/src/interfaces.ts +39 -0
- package/tsconfig.json +15 -0
package/.gitignore
ADDED
package/.npmignore
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Alan Szpigiel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/audit.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ILogger } from "@alanszp/logger";
|
|
2
|
+
import { AuditBody } from ".";
|
|
3
|
+
export declare class Audit {
|
|
4
|
+
logger: ILogger;
|
|
5
|
+
additions: Partial<AuditBody>;
|
|
6
|
+
constructor(logger: ILogger);
|
|
7
|
+
log(body: Partial<AuditBody>): void;
|
|
8
|
+
add(body: Partial<AuditBody>): void;
|
|
9
|
+
merge(body: Partial<AuditBody>): Partial<AuditBody>;
|
|
10
|
+
}
|
package/dist/audit.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Audit = void 0;
|
|
4
|
+
const logger_1 = require("@alanszp/logger");
|
|
5
|
+
const lodash_1 = require("lodash");
|
|
6
|
+
const _1 = require(".");
|
|
7
|
+
const MissingAuditFieldsError_1 = require("./errors/MissingAuditFieldsError");
|
|
8
|
+
class Audit {
|
|
9
|
+
constructor(logger) {
|
|
10
|
+
this.logger = logger;
|
|
11
|
+
this.additions = {};
|
|
12
|
+
}
|
|
13
|
+
log(body) {
|
|
14
|
+
const completeBody = this.merge(body);
|
|
15
|
+
const missingFields = (0, lodash_1.difference)(_1.REQUIRED_FIELDS, Object.keys(completeBody));
|
|
16
|
+
if (missingFields.length > 0) {
|
|
17
|
+
throw new MissingAuditFieldsError_1.MissingAuditFieldsError(missingFields);
|
|
18
|
+
}
|
|
19
|
+
const action = completeBody.action;
|
|
20
|
+
delete completeBody.action;
|
|
21
|
+
this.logger.info(action, Object.assign({ log_type: logger_1.LogType.AUDIT }, completeBody));
|
|
22
|
+
}
|
|
23
|
+
add(body) {
|
|
24
|
+
this.additions = this.merge(body);
|
|
25
|
+
}
|
|
26
|
+
merge(body) {
|
|
27
|
+
return (0, lodash_1.merge)(this.additions, body);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.Audit = Audit;
|
|
31
|
+
//# sourceMappingURL=audit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.js","sourceRoot":"","sources":["../src/audit.ts"],"names":[],"mappings":";;;AAAA,4CAAmD;AACnD,mCAA2C;AAC3C,wBAA+C;AAC/C,8EAA2E;AAE3E,MAAa,KAAK;IAKhB,YAAY,MAAe;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;IACtB,CAAC;IAEM,GAAG,CAAC,IAAwB;QACjC,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAEtC,MAAM,aAAa,GAAG,IAAA,mBAAU,EAC9B,kBAAe,EACf,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAC1B,CAAC;QAEF,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE;YAC5B,MAAM,IAAI,iDAAuB,CAAC,aAAa,CAAC,CAAC;SAClD;QAED,MAAM,MAAM,GAAG,YAAY,CAAC,MAAgB,CAAC;QAC7C,OAAO,YAAY,CAAC,MAAM,CAAC;QAE3B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,kBAAI,QAAQ,EAAE,gBAAO,CAAC,KAAK,IAAK,YAAY,EAAG,CAAC;IACzE,CAAC;IAEM,GAAG,CAAC,IAAwB;QACjC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;IAEM,KAAK,CAAC,IAAwB;QACnC,OAAO,IAAA,cAAK,EAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACrC,CAAC;CACF;AAnCD,sBAmCC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuditError = void 0;
|
|
4
|
+
const errors_1 = require("@alanszp/errors");
|
|
5
|
+
class AuditError extends errors_1.BaseError {
|
|
6
|
+
}
|
|
7
|
+
exports.AuditError = AuditError;
|
|
8
|
+
//# sourceMappingURL=AuditError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuditError.js","sourceRoot":"","sources":["../../src/errors/AuditError.ts"],"names":[],"mappings":";;;AAAA,4CAA4C;AAE5C,MAAa,UAAW,SAAQ,kBAAS;CAAG;AAA5C,gCAA4C"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RenderableContext, RenderableError } from "@alanszp/errors";
|
|
2
|
+
import { AuditError } from "./AuditError";
|
|
3
|
+
export declare class MissingAuditFieldsError extends AuditError implements RenderableError {
|
|
4
|
+
missingFields: string[];
|
|
5
|
+
constructor(missingFields: string[]);
|
|
6
|
+
code(): string;
|
|
7
|
+
renderMessage(): string;
|
|
8
|
+
context(): RenderableContext;
|
|
9
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MissingAuditFieldsError = void 0;
|
|
4
|
+
const AuditError_1 = require("./AuditError");
|
|
5
|
+
class MissingAuditFieldsError extends AuditError_1.AuditError {
|
|
6
|
+
constructor(missingFields) {
|
|
7
|
+
super("MissingAuditFieldsError");
|
|
8
|
+
this.missingFields = missingFields;
|
|
9
|
+
}
|
|
10
|
+
code() {
|
|
11
|
+
return "missing_audit_fields_error";
|
|
12
|
+
}
|
|
13
|
+
renderMessage() {
|
|
14
|
+
return "Missing properties on audit log.";
|
|
15
|
+
}
|
|
16
|
+
context() {
|
|
17
|
+
return {
|
|
18
|
+
missingFields: this.missingFields,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.MissingAuditFieldsError = MissingAuditFieldsError;
|
|
23
|
+
//# sourceMappingURL=MissingAuditFieldsError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MissingAuditFieldsError.js","sourceRoot":"","sources":["../../src/errors/MissingAuditFieldsError.ts"],"names":[],"mappings":";;;AACA,6CAA0C;AAE1C,MAAa,uBACX,SAAQ,uBAAU;IAKlB,YAAY,aAAuB;QACjC,KAAK,CAAC,yBAAyB,CAAC,CAAC;QACjC,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACrC,CAAC;IAED,IAAI;QACF,OAAO,4BAA4B,CAAC;IACtC,CAAC;IAED,aAAa;QACX,OAAO,kCAAkC,CAAC;IAC5C,CAAC;IAED,OAAO;QACL,OAAO;YACL,aAAa,EAAE,IAAI,CAAC,aAAa;SAClC,CAAC;IACJ,CAAC;CACF;AAxBD,0DAwBC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./AuditError"), exports);
|
|
14
|
+
__exportStar(require("./MissingAuditFieldsError"), exports);
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/errors/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,+CAA6B;AAC7B,4DAA0C"}
|
package/dist/factory.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAuditLogger = void 0;
|
|
4
|
+
const logger_1 = require("@alanszp/logger");
|
|
5
|
+
const audit_1 = require("./audit");
|
|
6
|
+
function createAuditLogger(appNameOrLogger) {
|
|
7
|
+
const logger = typeof appNameOrLogger === "string"
|
|
8
|
+
? (0, logger_1.createLogger)({
|
|
9
|
+
appName: appNameOrLogger,
|
|
10
|
+
console: { level: logger_1.LogLevel.INFO },
|
|
11
|
+
})
|
|
12
|
+
: appNameOrLogger;
|
|
13
|
+
return new audit_1.Audit(logger);
|
|
14
|
+
}
|
|
15
|
+
exports.createAuditLogger = createAuditLogger;
|
|
16
|
+
//# sourceMappingURL=factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../src/factory.ts"],"names":[],"mappings":";;;AAAA,4CAAkE;AAClE,mCAAgC;AAIhC,SAAgB,iBAAiB,CAAC,eAAiC;IACjE,MAAM,MAAM,GACV,OAAO,eAAe,KAAK,QAAQ;QACjC,CAAC,CAAC,IAAA,qBAAY,EAAC;YACX,OAAO,EAAE,eAAe;YACxB,OAAO,EAAE,EAAE,KAAK,EAAE,iBAAQ,CAAC,IAAI,EAAE;SAClC,CAAC;QACJ,CAAC,CAAC,eAAe,CAAC;IAEtB,OAAO,IAAI,aAAK,CAAC,MAAM,CAAC,CAAC;AAC3B,CAAC;AAVD,8CAUC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./factory"), exports);
|
|
14
|
+
__exportStar(require("./audit"), exports);
|
|
15
|
+
__exportStar(require("./interfaces"), exports);
|
|
16
|
+
__exportStar(require("./errors"), exports);
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,4CAA0B;AAC1B,0CAAwB;AACxB,+CAA6B;AAC7B,2CAAyB"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare enum CommonMetadataKeys {
|
|
2
|
+
EMPLOYEE_ID = "employee_id",
|
|
3
|
+
EMPLOYEE_IDS = "employee_ids",
|
|
4
|
+
CHANGES = "changes"
|
|
5
|
+
}
|
|
6
|
+
export interface Metadata {
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
export declare const REQUIRED_FIELDS: string[];
|
|
10
|
+
export interface AuditBody {
|
|
11
|
+
actorRef: string;
|
|
12
|
+
orgRef: string;
|
|
13
|
+
ip: string | string[];
|
|
14
|
+
action: string;
|
|
15
|
+
succeed: boolean;
|
|
16
|
+
targetRef?: string;
|
|
17
|
+
metadata?: Metadata;
|
|
18
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.REQUIRED_FIELDS = exports.CommonMetadataKeys = void 0;
|
|
4
|
+
var CommonMetadataKeys;
|
|
5
|
+
(function (CommonMetadataKeys) {
|
|
6
|
+
CommonMetadataKeys["EMPLOYEE_ID"] = "employee_id";
|
|
7
|
+
CommonMetadataKeys["EMPLOYEE_IDS"] = "employee_ids";
|
|
8
|
+
CommonMetadataKeys["CHANGES"] = "changes";
|
|
9
|
+
})(CommonMetadataKeys = exports.CommonMetadataKeys || (exports.CommonMetadataKeys = {}));
|
|
10
|
+
exports.REQUIRED_FIELDS = [
|
|
11
|
+
"actorRef",
|
|
12
|
+
"orgRef",
|
|
13
|
+
"ip",
|
|
14
|
+
"action",
|
|
15
|
+
"succeed",
|
|
16
|
+
];
|
|
17
|
+
// When will be set by logger (log time)
|
|
18
|
+
// Where will be set by logger (host name)
|
|
19
|
+
// Articles that we based to create this:
|
|
20
|
+
// https://workos.com/blog/the-developers-guide-to-audit-logs-siem
|
|
21
|
+
// https://www.strongdm.com/blog/audit-log-review-management
|
|
22
|
+
//# sourceMappingURL=interfaces.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../src/interfaces.ts"],"names":[],"mappings":";;;AAAA,IAAY,kBAIX;AAJD,WAAY,kBAAkB;IAC5B,iDAA2B,CAAA;IAC3B,mDAA6B,CAAA;IAC7B,yCAAmB,CAAA;AACrB,CAAC,EAJW,kBAAkB,GAAlB,0BAAkB,KAAlB,0BAAkB,QAI7B;AAMY,QAAA,eAAe,GAAG;IAC7B,UAAU;IACV,QAAQ;IACR,IAAI;IACJ,QAAQ;IACR,SAAS;CACV,CAAC;AAiBF,wCAAwC;AACxC,0CAA0C;AAE1C,yCAAyC;AACzC,kEAAkE;AAClE,4DAA4D"}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alanszp/audit",
|
|
3
|
+
"version": "4.0.2",
|
|
4
|
+
"description": "Alan's audit util.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "dist/index.d.ts",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"files": [
|
|
9
|
+
"**/*"
|
|
10
|
+
],
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"compile": "rm -rf ./dist && tsc --declaration",
|
|
16
|
+
"compile-watch": "tsc -w",
|
|
17
|
+
"build": "yarn run compile",
|
|
18
|
+
"prepack": "yarn run build"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@alanszp/errors": "^4.0.0",
|
|
22
|
+
"@alanszp/logger": "^4.0.2",
|
|
23
|
+
"lodash": "^4.17.21"
|
|
24
|
+
},
|
|
25
|
+
"gitHead": "c983be912daf7999c18052e7bcef490f3df9f863"
|
|
26
|
+
}
|
package/src/audit.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ILogger, LogType } from "@alanszp/logger";
|
|
2
|
+
import { difference, merge } from "lodash";
|
|
3
|
+
import { AuditBody, REQUIRED_FIELDS } from ".";
|
|
4
|
+
import { MissingAuditFieldsError } from "./errors/MissingAuditFieldsError";
|
|
5
|
+
|
|
6
|
+
export class Audit {
|
|
7
|
+
public logger: ILogger;
|
|
8
|
+
|
|
9
|
+
public additions: Partial<AuditBody>;
|
|
10
|
+
|
|
11
|
+
constructor(logger: ILogger) {
|
|
12
|
+
this.logger = logger;
|
|
13
|
+
this.additions = {};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public log(body: Partial<AuditBody>): void {
|
|
17
|
+
const completeBody = this.merge(body);
|
|
18
|
+
|
|
19
|
+
const missingFields = difference(
|
|
20
|
+
REQUIRED_FIELDS,
|
|
21
|
+
Object.keys(completeBody)
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
if (missingFields.length > 0) {
|
|
25
|
+
throw new MissingAuditFieldsError(missingFields);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const action = completeBody.action as string;
|
|
29
|
+
delete completeBody.action;
|
|
30
|
+
|
|
31
|
+
this.logger.info(action, { log_type: LogType.AUDIT, ...completeBody });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public add(body: Partial<AuditBody>): void {
|
|
35
|
+
this.additions = this.merge(body);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public merge(body: Partial<AuditBody>): Partial<AuditBody> {
|
|
39
|
+
return merge(this.additions, body);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { RenderableContext, RenderableError } from "@alanszp/errors";
|
|
2
|
+
import { AuditError } from "./AuditError";
|
|
3
|
+
|
|
4
|
+
export class MissingAuditFieldsError
|
|
5
|
+
extends AuditError
|
|
6
|
+
implements RenderableError
|
|
7
|
+
{
|
|
8
|
+
public missingFields: string[];
|
|
9
|
+
|
|
10
|
+
constructor(missingFields: string[]) {
|
|
11
|
+
super("MissingAuditFieldsError");
|
|
12
|
+
this.missingFields = missingFields;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
code(): string {
|
|
16
|
+
return "missing_audit_fields_error";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
renderMessage(): string {
|
|
20
|
+
return "Missing properties on audit log.";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
context(): RenderableContext {
|
|
24
|
+
return {
|
|
25
|
+
missingFields: this.missingFields,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/factory.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createLogger, ILogger, LogLevel } from "@alanszp/logger";
|
|
2
|
+
import { Audit } from "./audit";
|
|
3
|
+
|
|
4
|
+
export function createAuditLogger(appName: string): Audit;
|
|
5
|
+
export function createAuditLogger(logger: ILogger): Audit;
|
|
6
|
+
export function createAuditLogger(appNameOrLogger: string | ILogger): Audit {
|
|
7
|
+
const logger =
|
|
8
|
+
typeof appNameOrLogger === "string"
|
|
9
|
+
? createLogger({
|
|
10
|
+
appName: appNameOrLogger,
|
|
11
|
+
console: { level: LogLevel.INFO },
|
|
12
|
+
})
|
|
13
|
+
: appNameOrLogger;
|
|
14
|
+
|
|
15
|
+
return new Audit(logger);
|
|
16
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export enum CommonMetadataKeys {
|
|
2
|
+
EMPLOYEE_ID = "employee_id",
|
|
3
|
+
EMPLOYEE_IDS = "employee_ids",
|
|
4
|
+
CHANGES = "changes",
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface Metadata {
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const REQUIRED_FIELDS = [
|
|
12
|
+
"actorRef",
|
|
13
|
+
"orgRef",
|
|
14
|
+
"ip",
|
|
15
|
+
"action",
|
|
16
|
+
"succeed",
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
export interface AuditBody {
|
|
20
|
+
// Who
|
|
21
|
+
actorRef: string;
|
|
22
|
+
orgRef: string;
|
|
23
|
+
|
|
24
|
+
// Where (it can be an array since the request can jump between nodes, and http keeps record of their IPs)
|
|
25
|
+
ip: string | string[];
|
|
26
|
+
|
|
27
|
+
// What
|
|
28
|
+
action: string;
|
|
29
|
+
succeed: boolean;
|
|
30
|
+
targetRef?: string;
|
|
31
|
+
metadata?: Metadata;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// When will be set by logger (log time)
|
|
35
|
+
// Where will be set by logger (host name)
|
|
36
|
+
|
|
37
|
+
// Articles that we based to create this:
|
|
38
|
+
// https://workos.com/blog/the-developers-guide-to-audit-logs-siem
|
|
39
|
+
// https://www.strongdm.com/blog/audit-log-review-management
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"rootDir": "src",
|
|
4
|
+
"outDir": "dist",
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"target": "es6",
|
|
7
|
+
"types": ["node"],
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"sourceMap": true,
|
|
10
|
+
|
|
11
|
+
"alwaysStrict": true,
|
|
12
|
+
"strictNullChecks": true
|
|
13
|
+
},
|
|
14
|
+
"exclude": ["node_modules"]
|
|
15
|
+
}
|