mvc-common-toolkit 1.43.10 → 1.43.11
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/dist/src/constants.d.ts +42 -0
- package/dist/src/constants.js +50 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/gateways/alibaba-cloud-gateway.d.ts +30 -0
- package/dist/src/gateways/alibaba-cloud-gateway.js +120 -0
- package/dist/src/gateways/alibaba-cloud-gateway.js.map +1 -0
- package/dist/src/gateways/http-audit-gateway.d.ts +20 -0
- package/dist/src/gateways/http-audit-gateway.js +76 -0
- package/dist/src/gateways/http-audit-gateway.js.map +1 -0
- package/dist/src/gateways/index.d.ts +5 -0
- package/dist/src/gateways/index.js +22 -0
- package/dist/src/gateways/index.js.map +1 -0
- package/dist/src/gateways/internal-auth-gateway.d.ts +8 -0
- package/dist/src/gateways/internal-auth-gateway.js +40 -0
- package/dist/src/gateways/internal-auth-gateway.js.map +1 -0
- package/dist/src/gateways/stdout-audit-gateway.d.ts +7 -0
- package/dist/src/gateways/stdout-audit-gateway.js +25 -0
- package/dist/src/gateways/stdout-audit-gateway.js.map +1 -0
- package/dist/src/gateways/webhook-audit-gateway.d.ts +14 -0
- package/dist/src/gateways/webhook-audit-gateway.js +27 -0
- package/dist/src/gateways/webhook-audit-gateway.js.map +1 -0
- package/dist/src/interfaces.d.ts +218 -0
- package/dist/src/interfaces.js +3 -0
- package/dist/src/interfaces.js.map +1 -0
- package/dist/src/models/audit-log.d.ts +77 -0
- package/dist/src/models/audit-log.js +92 -0
- package/dist/src/models/audit-log.js.map +1 -0
- package/dist/src/models/index.d.ts +1 -0
- package/dist/src/models/index.js +18 -0
- package/dist/src/models/index.js.map +1 -0
- package/dist/src/pkg/array-helper.d.ts +1 -0
- package/dist/src/pkg/array-helper.js +12 -0
- package/dist/src/pkg/array-helper.js.map +1 -0
- package/dist/src/pkg/bcrypt-helper.d.ts +2 -0
- package/dist/src/pkg/bcrypt-helper.js +36 -0
- package/dist/src/pkg/bcrypt-helper.js.map +1 -0
- package/dist/src/pkg/crypto-helper.d.ts +2 -0
- package/dist/src/pkg/crypto-helper.js +16 -0
- package/dist/src/pkg/crypto-helper.js.map +1 -0
- package/dist/src/pkg/encryption-helper.d.ts +18 -0
- package/dist/src/pkg/encryption-helper.js +89 -0
- package/dist/src/pkg/encryption-helper.js.map +1 -0
- package/dist/src/pkg/encryption-helper.spec.d.ts +1 -0
- package/dist/src/pkg/encryption-helper.spec.js +238 -0
- package/dist/src/pkg/encryption-helper.spec.js.map +1 -0
- package/dist/src/pkg/filter-helper.d.ts +2 -0
- package/dist/src/pkg/filter-helper.js +102 -0
- package/dist/src/pkg/filter-helper.js.map +1 -0
- package/dist/src/pkg/filter-helper.spec.d.ts +1 -0
- package/dist/src/pkg/filter-helper.spec.js +94 -0
- package/dist/src/pkg/filter-helper.spec.js.map +1 -0
- package/dist/src/pkg/geoip-helper.d.ts +2 -0
- package/dist/src/pkg/geoip-helper.js +32 -0
- package/dist/src/pkg/geoip-helper.js.map +1 -0
- package/dist/src/pkg/hash-helper.d.ts +1 -0
- package/dist/src/pkg/hash-helper.js +37 -0
- package/dist/src/pkg/hash-helper.js.map +1 -0
- package/dist/src/pkg/http-request-utils.d.ts +4 -0
- package/dist/src/pkg/http-request-utils.js +55 -0
- package/dist/src/pkg/http-request-utils.js.map +1 -0
- package/dist/src/pkg/index.d.ts +18 -0
- package/dist/src/pkg/index.js +45 -0
- package/dist/src/pkg/index.js.map +1 -0
- package/dist/src/pkg/key-helper.d.ts +2 -0
- package/dist/src/pkg/key-helper.js +20 -0
- package/dist/src/pkg/key-helper.js.map +1 -0
- package/dist/src/pkg/logger.d.ts +9 -0
- package/dist/src/pkg/logger.js +23 -0
- package/dist/src/pkg/logger.js.map +1 -0
- package/dist/src/pkg/object-helper.d.ts +2 -0
- package/dist/src/pkg/object-helper.js +37 -0
- package/dist/src/pkg/object-helper.js.map +1 -0
- package/dist/src/pkg/paginated-cache-registry.d.ts +8 -0
- package/dist/src/pkg/paginated-cache-registry.js +23 -0
- package/dist/src/pkg/paginated-cache-registry.js.map +1 -0
- package/dist/src/pkg/query-helper.d.ts +3 -0
- package/dist/src/pkg/query-helper.js +60 -0
- package/dist/src/pkg/query-helper.js.map +1 -0
- package/dist/src/pkg/referral-tree-utils.d.ts +33 -0
- package/dist/src/pkg/referral-tree-utils.js +71 -0
- package/dist/src/pkg/referral-tree-utils.js.map +1 -0
- package/dist/src/pkg/scripts/index.d.ts +1 -0
- package/dist/src/pkg/scripts/index.js +28 -0
- package/dist/src/pkg/scripts/index.js.map +1 -0
- package/dist/src/pkg/scripts/lua.d.ts +10 -0
- package/dist/src/pkg/scripts/lua.js +109 -0
- package/dist/src/pkg/scripts/lua.js.map +1 -0
- package/dist/src/pkg/sort-helper.d.ts +3 -0
- package/dist/src/pkg/sort-helper.js +18 -0
- package/dist/src/pkg/sort-helper.js.map +1 -0
- package/dist/src/pkg/string-utils.d.ts +10 -0
- package/dist/src/pkg/string-utils.js +79 -0
- package/dist/src/pkg/string-utils.js.map +1 -0
- package/dist/src/pkg/task-helper.d.ts +2 -0
- package/dist/src/pkg/task-helper.js +30 -0
- package/dist/src/pkg/task-helper.js.map +1 -0
- package/dist/src/pkg/workflow/delayed-task-registry.d.ts +10 -0
- package/dist/src/pkg/workflow/delayed-task-registry.js +67 -0
- package/dist/src/pkg/workflow/delayed-task-registry.js.map +1 -0
- package/dist/src/pkg/workflow/delayed-task.d.ts +18 -0
- package/dist/src/pkg/workflow/delayed-task.js +95 -0
- package/dist/src/pkg/workflow/delayed-task.js.map +1 -0
- package/dist/src/pkg/workflow/index.d.ts +5 -0
- package/dist/src/pkg/workflow/index.js +22 -0
- package/dist/src/pkg/workflow/index.js.map +1 -0
- package/dist/src/pkg/workflow/processing-milestone.d.ts +18 -0
- package/dist/src/pkg/workflow/processing-milestone.js +39 -0
- package/dist/src/pkg/workflow/processing-milestone.js.map +1 -0
- package/dist/src/pkg/workflow/retry-task.d.ts +24 -0
- package/dist/src/pkg/workflow/retry-task.js +89 -0
- package/dist/src/pkg/workflow/retry-task.js.map +1 -0
- package/dist/src/pkg/workflow/retry-task.spec.d.ts +1 -0
- package/dist/src/pkg/workflow/retry-task.spec.js +145 -0
- package/dist/src/pkg/workflow/retry-task.spec.js.map +1 -0
- package/dist/src/pkg/workflow/sync-taskqueue.d.ts +32 -0
- package/dist/src/pkg/workflow/sync-taskqueue.js +108 -0
- package/dist/src/pkg/workflow/sync-taskqueue.js.map +1 -0
- package/dist/src/pkg/worksheet.utils.d.ts +27 -0
- package/dist/src/pkg/worksheet.utils.js +116 -0
- package/dist/src/pkg/worksheet.utils.js.map +1 -0
- package/dist/src/services/audit-service.d.ts +7 -0
- package/dist/src/services/audit-service.js +32 -0
- package/dist/src/services/audit-service.js.map +1 -0
- package/dist/src/services/excel.service.d.ts +25 -0
- package/dist/src/services/excel.service.js +95 -0
- package/dist/src/services/excel.service.js.map +1 -0
- package/dist/src/services/http-service.d.ts +7 -0
- package/dist/src/services/http-service.js +67 -0
- package/dist/src/services/http-service.js.map +1 -0
- package/dist/src/services/index.d.ts +8 -0
- package/dist/src/services/index.js +25 -0
- package/dist/src/services/index.js.map +1 -0
- package/dist/src/services/kafka-service.d.ts +15 -0
- package/dist/src/services/kafka-service.js +68 -0
- package/dist/src/services/kafka-service.js.map +1 -0
- package/dist/src/services/mailer-service.d.ts +15 -0
- package/dist/src/services/mailer-service.js +44 -0
- package/dist/src/services/mailer-service.js.map +1 -0
- package/dist/src/services/paginated-cache.d.ts +16 -0
- package/dist/src/services/paginated-cache.js +115 -0
- package/dist/src/services/paginated-cache.js.map +1 -0
- package/dist/src/services/paginated-cache.spec.d.ts +1 -0
- package/dist/src/services/paginated-cache.spec.js +284 -0
- package/dist/src/services/paginated-cache.spec.js.map +1 -0
- package/dist/src/services/redis-service.d.ts +33 -0
- package/dist/src/services/redis-service.js +230 -0
- package/dist/src/services/redis-service.js.map +1 -0
- package/dist/src/services/security-service.d.ts +11 -0
- package/dist/src/services/security-service.js +68 -0
- package/dist/src/services/security-service.js.map +1 -0
- package/package.json +1 -1
- package/src/constants.ts +47 -0
- package/src/gateways/alibaba-cloud-gateway.ts +127 -0
- package/src/gateways/http-audit-gateway.ts +104 -0
- package/src/gateways/index.ts +5 -0
- package/src/gateways/internal-auth-gateway.ts +42 -0
- package/src/gateways/stdout-audit-gateway.ts +23 -0
- package/src/gateways/webhook-audit-gateway.ts +33 -0
- package/src/interfaces.ts +304 -0
- package/src/models/audit-log.ts +126 -0
- package/src/models/index.ts +1 -0
- package/src/pkg/array-helper.ts +7 -0
- package/src/pkg/bcrypt-helper.ts +9 -0
- package/src/pkg/crypto-helper.ts +18 -0
- package/src/pkg/encryption-helper.spec.ts +423 -0
- package/src/pkg/encryption-helper.ts +155 -0
- package/src/pkg/filter-helper.spec.ts +105 -0
- package/src/pkg/filter-helper.ts +139 -0
- package/src/pkg/geoip-helper.ts +5 -0
- package/src/pkg/hash-helper.ts +12 -0
- package/src/pkg/http-request-utils.ts +75 -0
- package/src/pkg/index.ts +18 -0
- package/src/pkg/key-helper.ts +20 -0
- package/src/pkg/logger.ts +23 -0
- package/src/pkg/object-helper.ts +42 -0
- package/src/pkg/paginated-cache-registry.ts +25 -0
- package/src/pkg/query-helper.ts +79 -0
- package/src/pkg/referral-tree-utils.ts +165 -0
- package/src/pkg/scripts/index.ts +1 -0
- package/src/pkg/scripts/lua.ts +112 -0
- package/src/pkg/sort-helper.ts +19 -0
- package/src/pkg/string-utils.ts +104 -0
- package/src/pkg/task-helper.ts +25 -0
- package/src/pkg/workflow/delayed-task-registry.ts +54 -0
- package/src/pkg/workflow/delayed-task.ts +106 -0
- package/src/pkg/workflow/index.ts +5 -0
- package/src/pkg/workflow/processing-milestone.ts +54 -0
- package/src/pkg/workflow/retry-task.spec.ts +194 -0
- package/src/pkg/workflow/retry-task.ts +119 -0
- package/src/pkg/workflow/sync-taskqueue.ts +118 -0
- package/src/pkg/worksheet.utils.ts +178 -0
- package/src/services/audit-service.ts +22 -0
- package/src/services/excel.service.ts +103 -0
- package/src/services/http-service.ts +71 -0
- package/src/services/index.ts +8 -0
- package/src/services/kafka-service.ts +81 -0
- package/src/services/mailer-service.ts +43 -0
- package/src/services/paginated-cache.spec.ts +519 -0
- package/src/services/paginated-cache.ts +122 -0
- package/src/services/redis-service.ts +238 -0
- package/src/services/security-service.ts +80 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SecurityService = void 0;
|
|
13
|
+
class SecurityService {
|
|
14
|
+
constructor(cacheService, userService) {
|
|
15
|
+
this.cacheService = cacheService;
|
|
16
|
+
this.userService = userService;
|
|
17
|
+
}
|
|
18
|
+
static genLockUserCacheKey(userId) {
|
|
19
|
+
return `user:${userId}:lock`;
|
|
20
|
+
}
|
|
21
|
+
addFailureCount(userId, cacheKey) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const finalCacheKey = cacheKey || SecurityService.genLockUserCacheKey(userId);
|
|
24
|
+
yield this.cacheService.incrBy(finalCacheKey);
|
|
25
|
+
return this.cacheService.get(finalCacheKey);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
clearFailureCount(userId, cacheKey) {
|
|
29
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
const finalCacheKey = cacheKey || SecurityService.genLockUserCacheKey(userId);
|
|
31
|
+
yield this.cacheService.del(finalCacheKey);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
shouldLockUser(userId, cacheKey) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
try {
|
|
37
|
+
const incrementedAttemptsCount = yield this.addFailureCount(userId, cacheKey);
|
|
38
|
+
if (incrementedAttemptsCount >= SecurityService.LIMIT_TO_LOCK_USER) {
|
|
39
|
+
yield this.clearFailureCount(userId);
|
|
40
|
+
return {
|
|
41
|
+
success: true,
|
|
42
|
+
data: {
|
|
43
|
+
isLocked: true,
|
|
44
|
+
attemptsLeft: 0,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
success: true,
|
|
50
|
+
data: {
|
|
51
|
+
isLocked: false,
|
|
52
|
+
attemptsLeft: Math.max(0, SecurityService.LIMIT_TO_LOCK_USER - incrementedAttemptsCount),
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
return {
|
|
58
|
+
success: false,
|
|
59
|
+
message: error.message,
|
|
60
|
+
metadata: error.stack,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
SecurityService.LIMIT_TO_LOCK_USER = 5;
|
|
67
|
+
exports.SecurityService = SecurityService;
|
|
68
|
+
//# sourceMappingURL=security-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"security-service.js","sourceRoot":"","sources":["../../../src/services/security-service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,MAAa,eAAe;IAC1B,YACY,YAA0B,EAC1B,WAAwB;QADxB,iBAAY,GAAZ,YAAY,CAAc;QAC1B,gBAAW,GAAX,WAAW,CAAa;IACjC,CAAC;IAIG,MAAM,CAAC,mBAAmB,CAAC,MAAc;QAC9C,OAAO,QAAQ,MAAM,OAAO,CAAC;IAC/B,CAAC;IAEY,eAAe,CAC1B,MAAc,EACd,QAAiB;;YAEjB,MAAM,aAAa,GACjB,QAAQ,IAAI,eAAe,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC;YAE1D,MAAM,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;YAE9C,OAAO,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAC9C,CAAC;KAAA;IAEY,iBAAiB,CAAC,MAAc,EAAE,QAAiB;;YAC9D,MAAM,aAAa,GACjB,QAAQ,IAAI,eAAe,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC;YAE1D,MAAM,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAC7C,CAAC;KAAA;IAEK,cAAc,CAClB,MAAc,EACd,QAAiB;;YAEjB,IAAI;gBACF,MAAM,wBAAwB,GAAG,MAAM,IAAI,CAAC,eAAe,CACzD,MAAM,EACN,QAAQ,CACT,CAAC;gBAGF,IAAI,wBAAwB,IAAI,eAAe,CAAC,kBAAkB,EAAE;oBAClE,MAAM,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;oBAErC,OAAO;wBACL,OAAO,EAAE,IAAI;wBACb,IAAI,EAAE;4BACJ,QAAQ,EAAE,IAAI;4BACd,YAAY,EAAE,CAAC;yBAChB;qBACF,CAAC;iBACH;gBAED,OAAO;oBACL,OAAO,EAAE,IAAI;oBACb,IAAI,EAAE;wBACJ,QAAQ,EAAE,KAAK;wBACf,YAAY,EAAE,IAAI,CAAC,GAAG,CACpB,CAAC,EACD,eAAe,CAAC,kBAAkB,GAAG,wBAAwB,CAC9D;qBACF;iBACF,CAAC;aACH;YAAC,OAAO,KAAU,EAAE;gBACnB,OAAO;oBACL,OAAO,EAAE,KAAK;oBACd,OAAO,EAAE,KAAK,CAAC,OAAO;oBACtB,QAAQ,EAAE,KAAK,CAAC,KAAK;iBACtB,CAAC;aACH;QACH,CAAC;KAAA;;AAjEa,kCAAkB,GAAG,CAAC,CAAC;AAN1B,0CAAe"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mvc-common-toolkit",
|
|
3
|
-
"version": "1.43.
|
|
3
|
+
"version": "1.43.11",
|
|
4
4
|
"description": "This package contains common toolkits like query string parser, filter parser,... and other reusable patterns for any web projects",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export enum LOG_LEVEL {
|
|
2
|
+
INFO = "info",
|
|
3
|
+
ERROR = "error",
|
|
4
|
+
DEBUG = "debug",
|
|
5
|
+
FATAL = "fatal",
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type HttpMethod = "get" | "post" | "put" | "patch" | "delete";
|
|
9
|
+
|
|
10
|
+
export enum APP_ERROR {
|
|
11
|
+
HTTP_REQ_TIMEOUT = "ECONNABORTED",
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export enum SET_CACHE_POLICY {
|
|
15
|
+
KEEP_TTL,
|
|
16
|
+
WITH_TTL,
|
|
17
|
+
IF_EXISTS,
|
|
18
|
+
IF_NOT_EXISTS,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export enum APP_ENV {
|
|
22
|
+
DEVELOPMENT = "development",
|
|
23
|
+
STAGING = "staging",
|
|
24
|
+
PRODUCTION = "production",
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export enum FILTER_OPERATOR {
|
|
28
|
+
NOT = "not",
|
|
29
|
+
EQUAL = "eq",
|
|
30
|
+
LIKE = "like",
|
|
31
|
+
INS_LIKE = "ins_like", // case-insensitive
|
|
32
|
+
STARTS_WITH = "starts_with",
|
|
33
|
+
ENDS_WITH = "ends_with",
|
|
34
|
+
INS_STARTS_WITH = "ins_starts_with", // case-insensitive
|
|
35
|
+
INS_ENDS_WITH = "ins_ends_with", // case-insensitive
|
|
36
|
+
LESS_THAN = "lt",
|
|
37
|
+
LESS_THAN_OR_EQUAL = "lte",
|
|
38
|
+
GREATER_THAN = "gt",
|
|
39
|
+
GREATER_THAN_OR_EQUAL = "gte",
|
|
40
|
+
IN = "in",
|
|
41
|
+
NOT_IN = "nin",
|
|
42
|
+
OR = "or",
|
|
43
|
+
NOR = "nor",
|
|
44
|
+
AND = "and",
|
|
45
|
+
IS = "is",
|
|
46
|
+
NOT_EQUAL = "ne",
|
|
47
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import OSS from "ali-oss";
|
|
2
|
+
import axios, { HttpStatusCode } from "axios";
|
|
3
|
+
import Core from "@alicloud/pop-core";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
CloudStorageClient,
|
|
7
|
+
PutObjectOption,
|
|
8
|
+
STSResponse,
|
|
9
|
+
} from "../interfaces";
|
|
10
|
+
|
|
11
|
+
export interface AlibabaCloudGatewayConfig {
|
|
12
|
+
accessKey: string;
|
|
13
|
+
secret: string;
|
|
14
|
+
apiVersion?: string;
|
|
15
|
+
endpoint: string;
|
|
16
|
+
timeout?: number;
|
|
17
|
+
oss: {
|
|
18
|
+
bucketName: string;
|
|
19
|
+
region: string;
|
|
20
|
+
};
|
|
21
|
+
sts: {
|
|
22
|
+
roleArn: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class AlibabaCloudGateway implements CloudStorageClient {
|
|
27
|
+
protected coreClient: Core;
|
|
28
|
+
protected ossClient: OSS;
|
|
29
|
+
|
|
30
|
+
constructor(protected config: AlibabaCloudGatewayConfig) {
|
|
31
|
+
this.coreClient = new Core({
|
|
32
|
+
accessKeyId: config.accessKey,
|
|
33
|
+
accessKeySecret: config.secret,
|
|
34
|
+
apiVersion: config.apiVersion || "2015-04-01",
|
|
35
|
+
endpoint: config.endpoint,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
this.ossClient = new OSS({
|
|
39
|
+
accessKeyId: config.accessKey,
|
|
40
|
+
accessKeySecret: config.secret,
|
|
41
|
+
region: config.oss.region,
|
|
42
|
+
bucket: config.oss.bucketName,
|
|
43
|
+
timeout: config.timeout || 3000,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public async generateTmpCredentials(
|
|
48
|
+
sessionID: string
|
|
49
|
+
): Promise<STSResponse["Credentials"]> {
|
|
50
|
+
const requestResponse: STSResponse = await this.coreClient.request(
|
|
51
|
+
"AssumeRole",
|
|
52
|
+
{
|
|
53
|
+
RoleArn: this.config.sts.roleArn,
|
|
54
|
+
RoleSessionName: sessionID,
|
|
55
|
+
DurationSeconds: this.config.timeout || 900,
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
method: "POST",
|
|
59
|
+
contentType: "application/json",
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
return requestResponse.Credentials;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public async uploadLocalToBucket(
|
|
67
|
+
fileName: string,
|
|
68
|
+
fileData: Buffer,
|
|
69
|
+
options?: PutObjectOption
|
|
70
|
+
): Promise<string> {
|
|
71
|
+
const putResult = await this.ossClient.put(fileName, fileData, {
|
|
72
|
+
timeout: options?.timeout || 15000, // 15s
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
if (options?.domain) {
|
|
76
|
+
return `${options.domain}/${fileName}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (putResult?.url?.includes("http://")) {
|
|
80
|
+
return putResult.url.replace("http://", "https://");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return putResult.url;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public async deleteObject(filePath: string): Promise<void> {
|
|
87
|
+
await this.ossClient.delete(filePath);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public async uploadRemoteObjectToBucket(
|
|
91
|
+
fileName: string,
|
|
92
|
+
remoteUrl: string,
|
|
93
|
+
options?: PutObjectOption
|
|
94
|
+
): Promise<string> {
|
|
95
|
+
const response = await axios.get(remoteUrl, { responseType: "stream" });
|
|
96
|
+
if (response.status !== HttpStatusCode.Ok) {
|
|
97
|
+
throw new Error("failed to fetch from remote url");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const { data } = response;
|
|
101
|
+
|
|
102
|
+
const putResult = await this.ossClient.put(fileName, data, {
|
|
103
|
+
timeout: options.timeout || 15000, // 15s
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
if (options?.domain) {
|
|
107
|
+
return `${options.domain}/${fileName}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (putResult?.url?.includes("http://")) {
|
|
111
|
+
return putResult.url.replace("http://", "https://");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return putResult.url;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public getObjectReadStream(fileName: string): Promise<OSS.GetStreamResult> {
|
|
118
|
+
return this.ossClient.getStream(fileName);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public writeFileToDestination(
|
|
122
|
+
fileName: string,
|
|
123
|
+
destination: string | WritableStream
|
|
124
|
+
): Promise<OSS.GetObjectResult> {
|
|
125
|
+
return this.ossClient.get(fileName, destination);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
|
|
2
|
+
|
|
3
|
+
import { LOG_LEVEL } from "../constants";
|
|
4
|
+
import { AuditGateway } from "../interfaces";
|
|
5
|
+
import { maskFn } from "../pkg/string-utils";
|
|
6
|
+
|
|
7
|
+
export interface HttpAuditGatewayConfig {
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
appName?: string;
|
|
10
|
+
auth: {
|
|
11
|
+
username: string;
|
|
12
|
+
password: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class HttpAuditGateway implements AuditGateway {
|
|
17
|
+
protected baseURL: string;
|
|
18
|
+
protected access_token: string;
|
|
19
|
+
|
|
20
|
+
constructor(protected config: HttpAuditGatewayConfig) {
|
|
21
|
+
if (!config.baseUrl) {
|
|
22
|
+
throw new Error("Missing base url config");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
this.baseURL = config.baseUrl;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public async publish(
|
|
29
|
+
logId: string,
|
|
30
|
+
level: LOG_LEVEL,
|
|
31
|
+
content: Record<string, any>
|
|
32
|
+
) {
|
|
33
|
+
if (!this.access_token) {
|
|
34
|
+
await this.auth();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const response = await this.send(logId, {
|
|
38
|
+
method: "POST",
|
|
39
|
+
url: "/event/create",
|
|
40
|
+
data: {
|
|
41
|
+
...content,
|
|
42
|
+
level,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return response?.data;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private async auth(logId = "system", forceReAuth = false) {
|
|
50
|
+
if (this.access_token || forceReAuth) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const authResponse = await this.send<{ access_token: string }>(
|
|
55
|
+
logId,
|
|
56
|
+
{
|
|
57
|
+
method: "POST",
|
|
58
|
+
url: "/auth/login",
|
|
59
|
+
data: this.config.auth,
|
|
60
|
+
},
|
|
61
|
+
false,
|
|
62
|
+
false
|
|
63
|
+
); // not retry, not use token
|
|
64
|
+
|
|
65
|
+
this.access_token = authResponse?.data?.access_token;
|
|
66
|
+
|
|
67
|
+
return !!this.access_token;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected async send<T>(
|
|
71
|
+
logId: string,
|
|
72
|
+
options: AxiosRequestConfig,
|
|
73
|
+
is_retry = false,
|
|
74
|
+
use_access_token = true
|
|
75
|
+
): Promise<AxiosResponse<T>> {
|
|
76
|
+
console.log(
|
|
77
|
+
`[${logId}] ${
|
|
78
|
+
is_retry ? "Resend" : "Send"
|
|
79
|
+
} to Audit Service: ${JSON.stringify(options, maskFn)}`
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
options.baseURL = this.baseURL;
|
|
83
|
+
if (use_access_token) {
|
|
84
|
+
options.headers = {
|
|
85
|
+
Authorization: `Bearer ${this.access_token}`,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const response = await axios.request(options);
|
|
90
|
+
|
|
91
|
+
// use token, status 401, not retry => auth & retry
|
|
92
|
+
if (response?.status == 401 && use_access_token && !is_retry) {
|
|
93
|
+
console.log(`[${logId}] Retrying...`);
|
|
94
|
+
|
|
95
|
+
const isAuthSuccess = await this.auth(logId, true);
|
|
96
|
+
|
|
97
|
+
if (isAuthSuccess) {
|
|
98
|
+
return this.send(logId, options, true);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return response;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HttpService,
|
|
3
|
+
InternalAuthGatewayOptions,
|
|
4
|
+
InternalAuthLoginResult,
|
|
5
|
+
InternalServiceAuthResult,
|
|
6
|
+
OperationResult,
|
|
7
|
+
} from "../interfaces";
|
|
8
|
+
|
|
9
|
+
export class InternalAuthGateway {
|
|
10
|
+
constructor(
|
|
11
|
+
protected httpService: HttpService,
|
|
12
|
+
protected options: InternalAuthGatewayOptions
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
public async login(): Promise<OperationResult<InternalAuthLoginResult>> {
|
|
16
|
+
return this.httpService.send(
|
|
17
|
+
"post",
|
|
18
|
+
`${this.options.endpointURL}/actors/login`,
|
|
19
|
+
{
|
|
20
|
+
body: {
|
|
21
|
+
secret: this.options.secret,
|
|
22
|
+
},
|
|
23
|
+
serviceName: this.options.serviceName,
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public async verify(
|
|
29
|
+
accessToken: string
|
|
30
|
+
): Promise<OperationResult<InternalServiceAuthResult>> {
|
|
31
|
+
return this.httpService.send(
|
|
32
|
+
"post",
|
|
33
|
+
`${this.options.endpointURL}/actors/verify`,
|
|
34
|
+
{
|
|
35
|
+
body: {
|
|
36
|
+
accessToken,
|
|
37
|
+
},
|
|
38
|
+
serviceName: this.options.serviceName,
|
|
39
|
+
}
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { LOG_LEVEL } from "../constants";
|
|
2
|
+
import { loggers } from "../pkg";
|
|
3
|
+
import { AuditGateway, Logger } from "../interfaces";
|
|
4
|
+
|
|
5
|
+
export class StdOutAuditGateway implements AuditGateway {
|
|
6
|
+
protected logger: Logger;
|
|
7
|
+
|
|
8
|
+
constructor() {
|
|
9
|
+
this.logger = new loggers.PinoLogger();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public async publish(
|
|
13
|
+
logId: string,
|
|
14
|
+
level: LOG_LEVEL,
|
|
15
|
+
content: any
|
|
16
|
+
): Promise<any> {
|
|
17
|
+
return this.logger.info(
|
|
18
|
+
`[LogId ${logId}]`,
|
|
19
|
+
`[${level.toUpperCase}]`,
|
|
20
|
+
content
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { LOG_LEVEL } from "../constants";
|
|
2
|
+
import { AuditGateway, HttpService } from "../interfaces";
|
|
3
|
+
|
|
4
|
+
interface WebhookAuditGatewayConfig {
|
|
5
|
+
projectName: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class WebhookAuditGateway implements AuditGateway {
|
|
9
|
+
public static contentField = "content";
|
|
10
|
+
|
|
11
|
+
constructor(
|
|
12
|
+
protected webhookURL: string,
|
|
13
|
+
protected httpService: HttpService,
|
|
14
|
+
protected config: WebhookAuditGatewayConfig
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
public publish(logId: string, level: LOG_LEVEL, content: any): Promise<any> {
|
|
18
|
+
const logContent =
|
|
19
|
+
content && typeof content === "object"
|
|
20
|
+
? JSON.stringify(content)
|
|
21
|
+
: content;
|
|
22
|
+
const msg = `[${this.config.projectName}] | [${level}] | [${logId}]: ${logContent}`;
|
|
23
|
+
|
|
24
|
+
return this.httpService.send("post", this.webhookURL, {
|
|
25
|
+
headers: {
|
|
26
|
+
"Content-Type": "application/json",
|
|
27
|
+
},
|
|
28
|
+
body: {
|
|
29
|
+
[WebhookAuditGateway.contentField]: msg,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|