@bigid/apps-infrastructure-node-js 0.1.0

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.
Files changed (68) hide show
  1. package/.dcignore +1547 -0
  2. package/.eslintrc.json +33 -0
  3. package/.idea/apps-infrastructure-node-js.iml +9 -0
  4. package/.idea/misc.xml +6 -0
  5. package/.idea/modules.xml +8 -0
  6. package/.idea/prettier.xml +7 -0
  7. package/.idea/runConfigurations.xml +10 -0
  8. package/.idea/snyk.project.settings.xml +6 -0
  9. package/.idea/vcs.xml +6 -0
  10. package/.prettierrc.json +8 -0
  11. package/README.md +11 -0
  12. package/lib/abstractProviders/executionProvider.d.ts +13 -0
  13. package/lib/abstractProviders/executionProvider.js +35 -0
  14. package/lib/abstractProviders/iconsProviders.d.ts +4 -0
  15. package/lib/abstractProviders/iconsProviders.js +6 -0
  16. package/lib/abstractProviders/index.d.ts +3 -0
  17. package/lib/abstractProviders/index.js +10 -0
  18. package/lib/abstractProviders/logsProvider.d.ts +2 -0
  19. package/lib/abstractProviders/logsProvider.js +7 -0
  20. package/lib/abstractProviders/manifestProvider.d.ts +3 -0
  21. package/lib/abstractProviders/manifestProvider.js +6 -0
  22. package/lib/appTypes/actionResponseDetails.d.ts +6 -0
  23. package/lib/appTypes/actionResponseDetails.js +2 -0
  24. package/lib/appTypes/executionContext.d.ts +10 -0
  25. package/lib/appTypes/executionContext.js +2 -0
  26. package/lib/appTypes/index.d.ts +2 -0
  27. package/lib/appTypes/index.js +2 -0
  28. package/lib/dto/actionResponseDetails.d.ts +8 -0
  29. package/lib/dto/actionResponseDetails.js +13 -0
  30. package/lib/dto/executionContext.d.ts +11 -0
  31. package/lib/dto/executionContext.js +16 -0
  32. package/lib/dto/index.d.ts +2 -0
  33. package/lib/dto/index.js +7 -0
  34. package/lib/dto/keysValuesToStore.d.ts +5 -0
  35. package/lib/dto/keysValuesToStore.js +19 -0
  36. package/lib/index.d.ts +5 -0
  37. package/lib/index.js +24 -0
  38. package/lib/routes/router.d.ts +1 -0
  39. package/lib/routes/router.js +15 -0
  40. package/lib/server.d.ts +10 -0
  41. package/lib/server.js +48 -0
  42. package/lib/services/bigidProxyService.d.ts +28 -0
  43. package/lib/services/bigidProxyService.js +135 -0
  44. package/lib/services/index.d.ts +1 -0
  45. package/lib/services/index.js +13 -0
  46. package/lib/utils/appLogger.d.ts +1 -0
  47. package/lib/utils/appLogger.js +22 -0
  48. package/lib/utils/constants.d.ts +1 -0
  49. package/lib/utils/constants.js +4 -0
  50. package/lib/utils/index.d.ts +1 -0
  51. package/lib/utils/index.js +5 -0
  52. package/package.json +33 -0
  53. package/src/abstractProviders/executionProvider.ts +27 -0
  54. package/src/abstractProviders/iconsProviders.ts +4 -0
  55. package/src/abstractProviders/index.ts +3 -0
  56. package/src/abstractProviders/logsProvider.ts +6 -0
  57. package/src/abstractProviders/manifestProvider.ts +3 -0
  58. package/src/dto/actionResponseDetails.ts +15 -0
  59. package/src/dto/executionContext.ts +30 -0
  60. package/src/dto/index.ts +2 -0
  61. package/src/index.ts +5 -0
  62. package/src/server.ts +52 -0
  63. package/src/services/bigidProxyService.ts +150 -0
  64. package/src/services/index.ts +1 -0
  65. package/src/utils/appLogger.ts +21 -0
  66. package/src/utils/constants.ts +1 -0
  67. package/src/utils/index.ts +1 -0
  68. package/tsconfig.json +19 -0
@@ -0,0 +1,135 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.setValuesForBigIDProxy = exports.saveInStorage = exports.getValueFromAppStorage = exports.getAppStorage = exports.uploadAttachment = exports.executeHttpPut = exports.executeHttpPost = exports.executeHttpGet = exports.updateActionStatusToBigID = exports.initBigIDProxy = void 0;
16
+ const fs_1 = require("fs");
17
+ const https_1 = require("https");
18
+ const form_data_1 = __importDefault(require("form-data"));
19
+ const axios_1 = __importDefault(require("axios"));
20
+ const utils_1 = require("../utils");
21
+ const RequestMethod = {
22
+ POST: 'post',
23
+ GET: 'get',
24
+ PUT: 'put',
25
+ PATCH: 'patch',
26
+ DELETE: 'delete',
27
+ };
28
+ let bigidUrl;
29
+ let bigidUpdateStatusUrl;
30
+ let bigidToken;
31
+ let proxyTpaId;
32
+ const initBigIDProxy = (bigIdUrl, callBackUrl, bigIdToken, tpaId) => {
33
+ bigidUrl = bigIdUrl;
34
+ bigidUpdateStatusUrl = callBackUrl;
35
+ bigidToken = bigIdToken;
36
+ proxyTpaId = tpaId;
37
+ };
38
+ exports.initBigIDProxy = initBigIDProxy;
39
+ const doCallToUrl = (requestMethod, endpoint, bodyJson) => __awaiter(void 0, void 0, void 0, function* () {
40
+ utils_1.appLogger.info('--> bigid-proxy::callBigIdApi: [%s] %s', requestMethod, endpoint);
41
+ try {
42
+ const headers = {
43
+ 'Accept': 'application/json, text/plain, */*',
44
+ 'Content-Type': 'application/json',
45
+ 'Authorization': bigidToken,
46
+ };
47
+ const requestObj = {
48
+ method: requestMethod,
49
+ url: endpoint,
50
+ headers: headers,
51
+ httpsAgent: new https_1.Agent({
52
+ rejectUnauthorized: false
53
+ })
54
+ };
55
+ if (bodyJson && Object.keys(bodyJson).length > 0) {
56
+ requestObj.data = bodyJson;
57
+ }
58
+ const res = yield (0, axios_1.default)(requestObj);
59
+ utils_1.appLogger.info('<-- bigid-proxy::callBigIdApi: %s success', endpoint);
60
+ return res;
61
+ }
62
+ catch (error) {
63
+ utils_1.appLogger.info('<-- bigid-proxy::callBigIdApi: error calling bigID on %s %o', endpoint, error);
64
+ utils_1.appLogger.error(error);
65
+ return error === null || error === void 0 ? void 0 : error.message;
66
+ }
67
+ });
68
+ function callBigIdApi(requestMethod, endpoint, bodyJson, useEndpointWithoutBigIdBasePath) {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ const url = useEndpointWithoutBigIdBasePath ? endpoint : bigidUrl + endpoint;
71
+ return yield doCallToUrl(requestMethod, url, bodyJson);
72
+ });
73
+ }
74
+ /**
75
+ * This method receives a message object to update BigID regarding the current state of the action execution
76
+ * (should be used in case of an async actions)
77
+ * @param actionResponseDetailsParam
78
+ */
79
+ const updateActionStatusToBigID = (actionResponseDetails) => __awaiter(void 0, void 0, void 0, function* () {
80
+ return yield callBigIdApi(RequestMethod.PUT, `${bigidUpdateStatusUrl}`, actionResponseDetails, true);
81
+ });
82
+ exports.updateActionStatusToBigID = updateActionStatusToBigID;
83
+ /**
84
+ *
85
+ * @param endpoint - the endpoint in BigID, used for GET request. e.g - {BigIDBaseUrl}/ds_connections
86
+ * @return - String containing the response from BigID
87
+ */
88
+ const executeHttpGet = (endpoint) => __awaiter(void 0, void 0, void 0, function* () { return yield callBigIdApi(RequestMethod.GET, endpoint); });
89
+ exports.executeHttpGet = executeHttpGet;
90
+ /**
91
+ * the endpoint in BigID, used for POST requests. e.g - {BigIDBaseUrl}/scan
92
+ */
93
+ const executeHttpPost = (endpoint, actionResponseDetails) => __awaiter(void 0, void 0, void 0, function* () { return yield callBigIdApi(RequestMethod.POST, endpoint, actionResponseDetails); });
94
+ exports.executeHttpPost = executeHttpPost;
95
+ /**
96
+ * the endpoint in BigID, used for POST requests. e.g - {BigIDBaseUrl}/scan
97
+ */
98
+ const executeHttpPut = (endpoint, obj) => __awaiter(void 0, void 0, void 0, function* () { return yield callBigIdApi(RequestMethod.PUT, endpoint, obj); });
99
+ exports.executeHttpPut = executeHttpPut;
100
+ const uploadAttachment = (filePathToUpload) => {
101
+ const formData = new form_data_1.default();
102
+ formData.append('file', (0, fs_1.createReadStream)(filePathToUpload));
103
+ const headers = {
104
+ 'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`,
105
+ 'Authorization': bigidToken,
106
+ };
107
+ const requestObj = {
108
+ method: RequestMethod.POST,
109
+ url: `${bigidUpdateStatusUrl}/attachment`,
110
+ headers: headers,
111
+ httpsAgent: new https_1.Agent({
112
+ rejectUnauthorized: false
113
+ })
114
+ };
115
+ requestObj.data = formData;
116
+ return (0, axios_1.default)(requestObj);
117
+ };
118
+ exports.uploadAttachment = uploadAttachment;
119
+ const getAppStorage = () => (0, exports.executeHttpGet)(`tpa/${proxyTpaId}/storage`);
120
+ exports.getAppStorage = getAppStorage;
121
+ const getValueFromAppStorage = (key) => __awaiter(void 0, void 0, void 0, function* () {
122
+ const { data } = yield (0, exports.executeHttpGet)(`tpa/${proxyTpaId}/storage/key/${key}`);
123
+ return data === "Key not found" ? null : data.value;
124
+ });
125
+ exports.getValueFromAppStorage = getValueFromAppStorage;
126
+ const saveInStorage = (keyToStore, valueToStore) => __awaiter(void 0, void 0, void 0, function* () { return yield (0, exports.executeHttpPut)(`/tpa/${proxyTpaId}/storage`, { keysValues: [{ key: keyToStore, value: valueToStore }] }); });
127
+ exports.saveInStorage = saveInStorage;
128
+ const setValuesForBigIDProxy = (executionContext) => {
129
+ if (!executionContext.bigidBaseUrl || !executionContext.bigidToken) {
130
+ utils_1.appLogger.error(`Missing bigidUrl and/or bigidToken. bigidUrl=${executionContext.bigidBaseUrl}, bigidToken=${executionContext.bigidToken}`);
131
+ return;
132
+ }
133
+ (0, exports.initBigIDProxy)(executionContext.bigidBaseUrl, executionContext.updateResultCallback, executionContext.bigidToken, executionContext.tpaId);
134
+ };
135
+ exports.setValuesForBigIDProxy = setValuesForBigIDProxy;
@@ -0,0 +1 @@
1
+ export * from './bigidProxyService';
@@ -0,0 +1,13 @@
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("./bigidProxyService"), exports);
@@ -0,0 +1 @@
1
+ export declare const appLogger: import("log4js").Logger;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.appLogger = void 0;
4
+ const log4js_1 = require("log4js");
5
+ const constants_1 = require("./constants");
6
+ (0, log4js_1.configure)({
7
+ appenders: {
8
+ console: { type: 'stdout', layout: { type: 'colored' } },
9
+ dateFile: {
10
+ type: 'dateFile',
11
+ layout: { type: 'basic' },
12
+ filename: constants_1.LOGS_PATH,
13
+ compress: true,
14
+ daysToKeep: 14,
15
+ keepFileExt: true
16
+ }
17
+ },
18
+ categories: {
19
+ default: { appenders: ['console', 'dateFile'], level: process.env.LOG_LEVEL || 'info' }
20
+ }
21
+ });
22
+ exports.appLogger = (0, log4js_1.getLogger)();
@@ -0,0 +1 @@
1
+ export declare const LOGS_PATH = "log/app.log";
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LOGS_PATH = void 0;
4
+ exports.LOGS_PATH = 'log/app.log';
@@ -0,0 +1 @@
1
+ export { appLogger } from './appLogger';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.appLogger = void 0;
4
+ var appLogger_1 = require("./appLogger");
5
+ Object.defineProperty(exports, "appLogger", { enumerable: true, get: function () { return appLogger_1.appLogger; } });
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@bigid/apps-infrastructure-node-js",
3
+ "version": "0.1.0",
4
+ "description": "",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/bigexchange/apps-infrastructure-node-js.git"
10
+ },
11
+ "author": "Shay Sheli",
12
+ "homepage": "https://github.com/bigexchange/apps-infrastructure-node-js#readme",
13
+ "devDependencies": {
14
+ "@types/express": "^4.17.13",
15
+ "@types/http-errors": "^1.8.1",
16
+ "@types/node": "^16.11.10",
17
+ "@typescript-eslint/eslint-plugin": "4.6.1",
18
+ "@typescript-eslint/parser": "4.6.1",
19
+ "eslint": "7.12.1",
20
+ "eslint-config-prettier": "^6.15.0",
21
+ "eslint-plugin-no-only-tests": "2.4.0",
22
+ "eslint-plugin-prettier": "^3.1.4",
23
+ "prettier": "^2.4.1",
24
+ "typescript": "^4.5.2"
25
+ },
26
+ "dependencies": {
27
+ "axios": "^0.24.0",
28
+ "express": "^4.17.1",
29
+ "form-data": "^4.0.0",
30
+ "http-errors": "^1.8.1",
31
+ "log4js": "^6.3.0"
32
+ }
33
+ }
@@ -0,0 +1,27 @@
1
+ import { Request, Response } from 'express';
2
+ import { ActionResponseDetails } from '../dto/actionResponseDetails';
3
+ import { setValuesForBigIDProxy } from '../services';
4
+
5
+ export const StatusEnum = {
6
+ COMPLETED: 'COMPLETED',
7
+ IN_PROGRESS: 'IN_PROGRESS',
8
+ ERROR: 'ERROR',
9
+ };
10
+
11
+ export const handleExecution = async (req: Request, res: Response, executionProvider: ExecutionProvider) => {
12
+ setValuesForBigIDProxy(req.body);
13
+ await executionProvider.executeAction(req, res);
14
+ };
15
+
16
+ export abstract class ExecutionProvider {
17
+ public abstract executeAction(req: Request, res: Response): Promise<void>;
18
+
19
+ generateSyncSuccessMessage = (res: Response, executionId: string, message: string) =>
20
+ res.status(200).json(new ActionResponseDetails(executionId, StatusEnum.COMPLETED, 1, message));
21
+
22
+ generateAsyncSuccessMessage = (res: Response, executionId: string, message: string) =>
23
+ res.status(202).json(new ActionResponseDetails(executionId, StatusEnum.IN_PROGRESS, 0, message));
24
+
25
+ generateFailedResponse = (res: Response, executionId: string, e: Error) =>
26
+ res.status(400).json(new ActionResponseDetails(executionId, StatusEnum.ERROR, 0, e.message));
27
+ }
@@ -0,0 +1,4 @@
1
+ export abstract class IconsProviders {
2
+ public abstract getIconPath(): string;
3
+ public abstract getSideBarIconPath(): string;
4
+ }
@@ -0,0 +1,3 @@
1
+ export { ManifestProvider } from './manifestProvider';
2
+ export { IconsProviders } from './iconsProviders';
3
+ export { ExecutionProvider, StatusEnum } from './executionProvider';
@@ -0,0 +1,6 @@
1
+ import { createReadStream } from 'fs';
2
+ import { Request, Response } from 'express';
3
+ import { LOGS_PATH } from '../utils/constants';
4
+
5
+ export const fetchLogs = (req: Request, res: Response) =>
6
+ createReadStream(LOGS_PATH).pipe(res);
@@ -0,0 +1,3 @@
1
+ export abstract class ManifestProvider {
2
+ public abstract getManifest(req: any, res: any): string;
3
+ }
@@ -0,0 +1,15 @@
1
+ export class ActionResponseDetails {
2
+ executionId: string;
3
+ statusEnum: string;
4
+ progress: number;
5
+ message: string;
6
+ additionalData?: any;
7
+
8
+ constructor(executionId: string, statusEnum: string, progress: number, message: string, additionalData?: any) {
9
+ this.executionId = executionId;
10
+ this.statusEnum = statusEnum;
11
+ this.progress = progress;
12
+ this.message = message;
13
+ this.additionalData = additionalData;
14
+ }
15
+ }
@@ -0,0 +1,30 @@
1
+ export class ExecutionContext {
2
+ actionName: string;
3
+ executionId: string;
4
+ globalParams: Record<string, string>;
5
+ actionParams: Record<string, string>;
6
+ bigidBaseUrl: string;
7
+ bigidToken: string;
8
+ updateResultCallback: any;
9
+ tpaId: string;
10
+
11
+ constructor(
12
+ actionName: string,
13
+ executionId: string,
14
+ globalParams: Record<string, string>,
15
+ actionParams: Record<string, string>,
16
+ bigidBaseUrl: string,
17
+ bigidToken: string,
18
+ updateResultCallback: any,
19
+ tpaId: string,
20
+ ) {
21
+ this.actionName = actionName;
22
+ this.executionId = executionId;
23
+ this.globalParams = globalParams;
24
+ this.actionParams = actionParams;
25
+ this.bigidBaseUrl = bigidBaseUrl;
26
+ this.bigidToken = bigidToken;
27
+ this.updateResultCallback = updateResultCallback;
28
+ this.tpaId = tpaId;
29
+ }
30
+ }
@@ -0,0 +1,2 @@
1
+ export { ExecutionContext } from './executionContext';
2
+ export { ActionResponseDetails } from './actionResponseDetails';
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './abstractProviders';
2
+ export * from './dto';
3
+ export * from './utils';
4
+ export { updateActionStatusToBigID, uploadAttachment, getValueFromAppStorage, saveInStorage, executeHttpGet } from './services';
5
+ export { deployServer, ServerInit } from './server';
package/src/server.ts ADDED
@@ -0,0 +1,52 @@
1
+ import express, { NextFunction, Request, Response } from 'express';
2
+ import { ManifestProvider } from './abstractProviders/manifestProvider';
3
+ import { appLogger } from './utils/appLogger';
4
+ import { IconsProviders } from './abstractProviders/iconsProviders';
5
+ import { ExecutionProvider, handleExecution } from './abstractProviders/executionProvider';
6
+ import { fetchLogs } from './abstractProviders/logsProvider';
7
+ import createError from 'http-errors';
8
+
9
+ export interface ServerInit {
10
+ manifestController: ManifestProvider;
11
+ iconsController: IconsProviders;
12
+ executionController: ExecutionProvider;
13
+ serverPort?: number;
14
+ }
15
+
16
+ const app = express();
17
+
18
+ export const deployServer = (serverInit: ServerInit) => {
19
+ app.use(express.json());
20
+ app.use(express.urlencoded({ extended: false }));
21
+
22
+ app.get('/assets/icon', (req, res) => res.sendFile(serverInit.iconsController.getIconPath()));
23
+ app.get('/assets/sideBarIcon', (req, res) => res.sendFile(serverInit.iconsController.getSideBarIconPath()));
24
+ app.get('/manifest', serverInit.manifestController.getManifest);
25
+ app.post(
26
+ '/execute',
27
+ async (req: Request, res: Response) => await handleExecution(req, res, serverInit.executionController),
28
+ );
29
+ app.get('/logs', fetchLogs);
30
+
31
+ app.listen(process.env.PORT || serverInit.serverPort, () =>
32
+ appLogger.info(`Started server at port ${process.env.PORT || serverInit.serverPort}`),
33
+ );
34
+
35
+ // catch 404 and forward to error handler
36
+ app.use((req: Request, res: Response, next: NextFunction) => next(createError(404)));
37
+
38
+ // error handler
39
+ app.use((err: any, req: Request, res: Response, next: NextFunction) => {
40
+ const { message, status } = err;
41
+ appLogger.error(err);
42
+ res.locals.message = message;
43
+ res.locals.error = req.app.get('env') === 'development' ? err : {};
44
+
45
+ res.status(status || 500);
46
+ res.json({
47
+ status: 'error',
48
+ statusCode: status || 500,
49
+ message,
50
+ });
51
+ });
52
+ };
@@ -0,0 +1,150 @@
1
+ import { createReadStream } from 'fs';
2
+ import { Agent } from 'https';
3
+ import { ExecutionContext } from '../dto';
4
+ import FormData from 'form-data';
5
+
6
+ import axios from 'axios';
7
+ import { ActionResponseDetails } from '../dto/actionResponseDetails';
8
+ import { appLogger } from '../utils';
9
+
10
+ const RequestMethod = {
11
+ POST: 'post',
12
+ GET: 'get',
13
+ PUT: 'put',
14
+ PATCH: 'patch',
15
+ DELETE: 'delete',
16
+ };
17
+
18
+ let bigidUrl: string;
19
+ let bigidUpdateStatusUrl: string;
20
+ let bigidToken: string;
21
+ let proxyTpaId: string;
22
+
23
+ export const initBigIDProxy = (bigIdUrl: string, callBackUrl: string, bigIdToken: string, tpaId: string) => {
24
+ bigidUrl = bigIdUrl;
25
+ bigidUpdateStatusUrl = callBackUrl;
26
+ bigidToken = bigIdToken;
27
+ proxyTpaId = tpaId;
28
+ };
29
+
30
+ const doCallToUrl = async (requestMethod: string, endpoint: string, bodyJson?: ActionResponseDetails) => {
31
+ appLogger.info('--> bigid-proxy::callBigIdApi: [%s] %s', requestMethod, endpoint);
32
+ try {
33
+ const headers = {
34
+ Accept: 'application/json, text/plain, */*',
35
+ 'Content-Type': 'application/json',
36
+ Authorization: bigidToken,
37
+ };
38
+
39
+ const requestObj: Record<string, any> = {
40
+ method: requestMethod,
41
+ url: endpoint,
42
+ headers: headers,
43
+ httpsAgent: new Agent({
44
+ rejectUnauthorized: false,
45
+ }),
46
+ };
47
+
48
+ if (bodyJson && Object.keys(bodyJson).length > 0) {
49
+ requestObj.data = bodyJson;
50
+ }
51
+
52
+ const res = await axios(requestObj);
53
+
54
+ appLogger.info('<-- bigid-proxy::callBigIdApi: %s success', endpoint);
55
+ return res;
56
+ } catch (error: any) {
57
+ appLogger.info('<-- bigid-proxy::callBigIdApi: error calling bigID on %s %o', endpoint, error);
58
+ appLogger.error(error);
59
+ return error?.message;
60
+ }
61
+ };
62
+
63
+ async function callBigIdApi(
64
+ requestMethod: string,
65
+ endpoint: string,
66
+ bodyJson?: ActionResponseDetails,
67
+ useEndpointWithoutBigIdBasePath?: boolean,
68
+ ) {
69
+ const url = useEndpointWithoutBigIdBasePath ? endpoint : bigidUrl + endpoint;
70
+ return await doCallToUrl(requestMethod, url, bodyJson);
71
+ }
72
+
73
+ /**
74
+ * This method receives a message object to update BigID regarding the current state of the action execution
75
+ * (should be used in case of an async actions)
76
+ * @param ActionResponseDetails
77
+ */
78
+ export const updateActionStatusToBigID = async (actionResponseDetails: ActionResponseDetails) =>
79
+ await callBigIdApi(RequestMethod.PUT, `${bigidUpdateStatusUrl}`, actionResponseDetails, true);
80
+
81
+ /**
82
+ *
83
+ * @param endpoint - the endpoint in BigID, used for GET request. e.g - {BigIDBaseUrl}/ds_connections
84
+ * @return - String containing the response from BigID
85
+ */
86
+ export const executeHttpGet = async (endpoint: string) => await callBigIdApi(RequestMethod.GET, endpoint);
87
+
88
+ /**
89
+ * the endpoint in BigID, used for POST requests. e.g - {BigIDBaseUrl}/scan
90
+ */
91
+ export const executeHttpPost = async (endpoint: string, actionResponseDetails: ActionResponseDetails) =>
92
+ await callBigIdApi(RequestMethod.POST, endpoint, actionResponseDetails);
93
+
94
+ /**
95
+ * the endpoint in BigID, used for POST requests. e.g - {BigIDBaseUrl}/scan
96
+ */
97
+ export const executeHttpPut = async (endpoint: string, obj: any) =>
98
+ await callBigIdApi(RequestMethod.PUT, endpoint, obj);
99
+
100
+ export const uploadAttachment = (filePathToUpload: string) => {
101
+ const formData = new FormData();
102
+ formData.append('file', createReadStream(filePathToUpload));
103
+
104
+ const headers = {
105
+ 'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`,
106
+ Authorization: bigidToken,
107
+ };
108
+ const requestObj: Record<string, any> = {
109
+ method: RequestMethod.POST,
110
+ url: `${bigidUpdateStatusUrl}/attachment`,
111
+ headers: headers,
112
+ httpsAgent: new Agent({
113
+ rejectUnauthorized: false,
114
+ }),
115
+ };
116
+ requestObj.data = formData;
117
+ return axios(requestObj);
118
+ };
119
+
120
+ export const getAppStorage = () => executeHttpGet(`tpa/${proxyTpaId}/storage`);
121
+
122
+ export const getValueFromAppStorage = async (key: string) => {
123
+ const { data } = await executeHttpGet(`tpa/${proxyTpaId}/storage/key/${key}`);
124
+ return data === 'Key not found' ? null : data.value;
125
+ };
126
+
127
+ export const saveInStorage = async (keyToStore: any, valueToStore: any) =>
128
+ await executeHttpPut(`/tpa/${proxyTpaId}/storage`, {
129
+ keysValues: [
130
+ {
131
+ key: keyToStore,
132
+ value: valueToStore,
133
+ },
134
+ ],
135
+ });
136
+
137
+ export const setValuesForBigIDProxy = (executionContext: ExecutionContext) => {
138
+ if (!executionContext.bigidBaseUrl || !executionContext.bigidToken) {
139
+ appLogger.error(
140
+ `Missing bigidUrl and/or bigidToken. bigidUrl=${executionContext.bigidBaseUrl}, bigidToken=${executionContext.bigidToken}`,
141
+ );
142
+ return;
143
+ }
144
+ initBigIDProxy(
145
+ executionContext.bigidBaseUrl,
146
+ executionContext.updateResultCallback,
147
+ executionContext.bigidToken,
148
+ executionContext.tpaId,
149
+ );
150
+ };
@@ -0,0 +1 @@
1
+ export * from './bigidProxyService';
@@ -0,0 +1,21 @@
1
+ import { configure, getLogger } from 'log4js';
2
+ import { LOGS_PATH } from './constants';
3
+
4
+ configure({
5
+ appenders: {
6
+ console: { type: 'stdout', layout: { type: 'colored' } },
7
+ dateFile: {
8
+ type: 'dateFile',
9
+ layout: { type: 'basic' },
10
+ filename: LOGS_PATH,
11
+ compress: true,
12
+ daysToKeep: 14,
13
+ keepFileExt: true
14
+ }
15
+ },
16
+ categories: {
17
+ default: { appenders: ['console', 'dateFile'], level: process.env.LOG_LEVEL || 'info' }
18
+ }
19
+ });
20
+
21
+ export const appLogger = getLogger();
@@ -0,0 +1 @@
1
+ export const LOGS_PATH = 'log/app.log';
@@ -0,0 +1 @@
1
+ export { appLogger } from './appLogger'
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "exclude": [
3
+ "node_modules",
4
+ "lib",
5
+ "**/*.test*",
6
+ "**/*.spec*"
7
+ ],
8
+ "include": ["src"],
9
+ "compilerOptions": {
10
+ "target": "es2016",
11
+ "module": "commonjs",
12
+ "declaration": true,
13
+ "outDir": "./lib",
14
+ "esModuleInterop": true,
15
+ "forceConsistentCasingInFileNames": true,
16
+ "strict": true,
17
+ "skipLibCheck": true
18
+ }
19
+ }