@mondaydotcomorg/monday-authorization 1.0.17 → 1.0.19
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.
|
@@ -32,46 +32,53 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
32
32
|
};
|
|
33
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
34
|
exports.AuthorizationService = void 0;
|
|
35
|
+
const perf_hooks_1 = require("perf_hooks");
|
|
35
36
|
const monday_jwt_1 = require("@mondaydotcomorg/monday-jwt");
|
|
36
37
|
const MondayLogger = __importStar(require("@mondaydotcomorg/monday-logger"));
|
|
37
38
|
const monday_fetch_1 = __importDefault(require("@mondaydotcomorg/monday-fetch"));
|
|
38
39
|
const prometheus_service_1 = require("./prometheus-service");
|
|
39
40
|
const INTERNAL_APP_NAME = 'internal_ms';
|
|
40
|
-
const URL = `${process.env.MONDAY_INTERNAL_URL}/internal_ms/authorization/authorize`;
|
|
41
|
-
const IS_DEV_ENV = process.env.NODE_ENV === 'development';
|
|
42
|
-
const REQUEST_TIMEOUT = IS_DEV_ENV ? 60000 : 2000;
|
|
43
41
|
const logger = MondayLogger.getLogger();
|
|
44
42
|
class AuthorizationService {
|
|
45
43
|
static isAuthorized(accountId, userId, resources, action) {
|
|
46
44
|
return __awaiter(this, void 0, void 0, function* () {
|
|
47
45
|
const internalAuthToken = monday_jwt_1.signAuthorizationHeader(INTERNAL_APP_NAME, accountId);
|
|
48
|
-
const
|
|
49
|
-
const
|
|
46
|
+
const { authorizationObjects } = createAuthorizationParams(resources, action);
|
|
47
|
+
const startTime = perf_hooks_1.performance.now();
|
|
48
|
+
const response = yield monday_fetch_1.default(getUrl(), {
|
|
50
49
|
method: 'POST',
|
|
51
50
|
headers: { Authorization: internalAuthToken, 'Content-Type': 'application/json' },
|
|
52
|
-
timeout:
|
|
51
|
+
timeout: getRequestTimeout(),
|
|
53
52
|
body: JSON.stringify({
|
|
54
53
|
user_id: userId,
|
|
55
|
-
authorize_request_objects:
|
|
54
|
+
authorize_request_objects: authorizationObjects,
|
|
56
55
|
}),
|
|
57
56
|
}, { retries: 3, callback: logOnFetchFail });
|
|
57
|
+
const endTime = perf_hooks_1.performance.now();
|
|
58
|
+
const time = endTime - startTime;
|
|
59
|
+
const responseStatus = response.status;
|
|
60
|
+
prometheus_service_1.sendAuthorizationChecksPerRequestMetric(responseStatus, authorizationObjects.length);
|
|
58
61
|
if (!response.ok) {
|
|
59
|
-
logger.error({ status: response.status }, '
|
|
60
|
-
|
|
62
|
+
logger.error({ status: response.status }, 'AuthorizationService: authorization request failed');
|
|
63
|
+
const isAuthorized = false;
|
|
64
|
+
authorizationObjects.forEach(function (authorizationObject) {
|
|
65
|
+
prometheus_service_1.sendAuthorizationCheckResponseTimeMetric(authorizationObject.resource_type, action, isAuthorized, responseStatus, time);
|
|
66
|
+
});
|
|
67
|
+
return { isAuthorized };
|
|
61
68
|
}
|
|
62
69
|
const responseBody = yield response.json();
|
|
63
70
|
const unauthorizedObjects = [];
|
|
64
71
|
responseBody.result.forEach(function (isAuthorized, index) {
|
|
65
|
-
const authorizationObject =
|
|
72
|
+
const authorizationObject = authorizationObjects[index];
|
|
66
73
|
if (!isAuthorized) {
|
|
67
74
|
unauthorizedObjects.push(authorizationObject);
|
|
68
75
|
}
|
|
69
|
-
|
|
76
|
+
prometheus_service_1.sendAuthorizationCheckResponseTimeMetric(authorizationObject.resource_type, action, isAuthorized, responseStatus, time);
|
|
70
77
|
});
|
|
71
78
|
if (unauthorizedObjects.length > 0) {
|
|
72
79
|
logger.info({
|
|
73
80
|
resources: JSON.stringify(unauthorizedObjects),
|
|
74
|
-
}, '
|
|
81
|
+
}, 'AuthorizationService: resource is unauthorized');
|
|
75
82
|
const unauthorizedIds = unauthorizedObjects.map(obj => obj.resource_id);
|
|
76
83
|
return { isAuthorized: false, unauthorizedIds };
|
|
77
84
|
}
|
|
@@ -99,13 +106,10 @@ function createAuthorizationParams(resources, action) {
|
|
|
99
106
|
function logOnFetchFail(retries, error, response) {
|
|
100
107
|
logger.error({ attempt: retries, error }, 'Authorization attempt failed due to network issues');
|
|
101
108
|
}
|
|
102
|
-
function
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
isAuthorized,
|
|
109
|
-
});
|
|
110
|
-
}
|
|
109
|
+
function getUrl() {
|
|
110
|
+
return `${process.env.MONDAY_INTERNAL_URL}/internal_ms/authorization/authorize`;
|
|
111
|
+
}
|
|
112
|
+
function getRequestTimeout() {
|
|
113
|
+
const isDevEnv = process.env.NODE_ENV === 'development';
|
|
114
|
+
return isDevEnv ? 60000 : 2000;
|
|
111
115
|
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
import { Action } from './types/general';
|
|
1
2
|
export declare const METRICS: {
|
|
2
3
|
AUTHORIZATION_CHECK: string;
|
|
4
|
+
AUTHORIZATION_CHECKS_PER_REQUEST: string;
|
|
5
|
+
AUTHORIZATION_CHECK_RESPONSE_TIME: string;
|
|
3
6
|
};
|
|
4
7
|
export declare function setPrometheus(customPrometheus: any): void;
|
|
5
8
|
export declare function getMetricsManager(): any;
|
|
9
|
+
export declare function sendAuthorizationChecksPerRequestMetric(responseStatus: any, amountOfAuthorizationObjects: any): void;
|
|
10
|
+
export declare function sendAuthorizationCheckResponseTimeMetric(resourceType: string, action: Action, isAuthorized: boolean, responseStatus: number, time: number): void;
|
|
@@ -1,15 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMetricsManager = exports.setPrometheus = exports.METRICS = void 0;
|
|
3
|
+
exports.sendAuthorizationCheckResponseTimeMetric = exports.sendAuthorizationChecksPerRequestMetric = exports.getMetricsManager = exports.setPrometheus = exports.METRICS = void 0;
|
|
4
4
|
let prometheus = null;
|
|
5
|
+
let authorizationChecksPerRequestMetric = null;
|
|
6
|
+
let authorizationCheckResponseTimeMetric = null;
|
|
5
7
|
exports.METRICS = {
|
|
6
8
|
AUTHORIZATION_CHECK: 'authorization_check',
|
|
9
|
+
AUTHORIZATION_CHECKS_PER_REQUEST: 'authorization_checks_per_request',
|
|
10
|
+
AUTHORIZATION_CHECK_RESPONSE_TIME: 'authorization_check_response_time',
|
|
11
|
+
};
|
|
12
|
+
const authorizationChecksPerRequestMetricConfig = {
|
|
13
|
+
name: exports.METRICS.AUTHORIZATION_CHECKS_PER_REQUEST,
|
|
14
|
+
labels: ['responseStatus'],
|
|
15
|
+
description: 'Authorization checks per request summary',
|
|
16
|
+
};
|
|
17
|
+
const authorizationCheckResponseTimeMetricConfig = {
|
|
18
|
+
name: exports.METRICS.AUTHORIZATION_CHECK_RESPONSE_TIME,
|
|
19
|
+
labels: ['resourceType', 'action', 'isAuthorized', 'responseStatus'],
|
|
20
|
+
description: 'Authorization check response time summary',
|
|
7
21
|
};
|
|
8
22
|
function setPrometheus(customPrometheus) {
|
|
9
23
|
prometheus = customPrometheus;
|
|
24
|
+
const { METRICS_TYPES } = prometheus;
|
|
25
|
+
authorizationChecksPerRequestMetric = getMetricsManager().addMetric(METRICS_TYPES.SUMMARY, authorizationChecksPerRequestMetricConfig.name, authorizationChecksPerRequestMetricConfig.labels, authorizationChecksPerRequestMetricConfig.description);
|
|
26
|
+
authorizationCheckResponseTimeMetric = getMetricsManager().addMetric(METRICS_TYPES.SUMMARY, authorizationCheckResponseTimeMetricConfig.name, authorizationCheckResponseTimeMetricConfig.labels, authorizationCheckResponseTimeMetricConfig.description);
|
|
10
27
|
}
|
|
11
28
|
exports.setPrometheus = setPrometheus;
|
|
12
29
|
function getMetricsManager() {
|
|
13
30
|
return prometheus === null || prometheus === void 0 ? void 0 : prometheus.metricsManager;
|
|
14
31
|
}
|
|
15
32
|
exports.getMetricsManager = getMetricsManager;
|
|
33
|
+
function sendAuthorizationChecksPerRequestMetric(responseStatus, amountOfAuthorizationObjects) {
|
|
34
|
+
try {
|
|
35
|
+
if (authorizationChecksPerRequestMetric) {
|
|
36
|
+
authorizationChecksPerRequestMetric.labels(responseStatus).observe(amountOfAuthorizationObjects);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
catch (e) { }
|
|
40
|
+
}
|
|
41
|
+
exports.sendAuthorizationChecksPerRequestMetric = sendAuthorizationChecksPerRequestMetric;
|
|
42
|
+
function sendAuthorizationCheckResponseTimeMetric(resourceType, action, isAuthorized, responseStatus, time) {
|
|
43
|
+
try {
|
|
44
|
+
if (authorizationCheckResponseTimeMetric) {
|
|
45
|
+
authorizationCheckResponseTimeMetric.labels(resourceType, action, isAuthorized, responseStatus).observe(time);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch (e) { }
|
|
49
|
+
}
|
|
50
|
+
exports.sendAuthorizationCheckResponseTimeMetric = sendAuthorizationCheckResponseTimeMetric;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mondaydotcomorg/monday-authorization",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.19",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"license": "BSD-3-Clause",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"files": [
|
|
32
32
|
"dist/"
|
|
33
33
|
],
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "a10f09efb2cb77fabe017a3c661a165272a7d0a9"
|
|
35
35
|
}
|