@mondaydotcomorg/monday-authorization 1.0.16 → 1.0.18

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,6 +32,7 @@ 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"));
@@ -45,33 +46,42 @@ class AuthorizationService {
45
46
  static isAuthorized(accountId, userId, resources, action) {
46
47
  return __awaiter(this, void 0, void 0, function* () {
47
48
  const internalAuthToken = monday_jwt_1.signAuthorizationHeader(INTERNAL_APP_NAME, accountId);
48
- const params = createAuthorizationParams(resources, action);
49
+ const { authorizationObjects } = createAuthorizationParams(resources, action);
50
+ const startTime = perf_hooks_1.performance.now();
49
51
  const response = yield monday_fetch_1.default(URL, {
50
52
  method: 'POST',
51
53
  headers: { Authorization: internalAuthToken, 'Content-Type': 'application/json' },
52
54
  timeout: REQUEST_TIMEOUT,
53
55
  body: JSON.stringify({
54
56
  user_id: userId,
55
- authorize_request_objects: params.authorizationObjects,
57
+ authorize_request_objects: authorizationObjects,
56
58
  }),
57
59
  }, { retries: 3, callback: logOnFetchFail });
60
+ const endTime = perf_hooks_1.performance.now();
61
+ const time = endTime - startTime;
62
+ const responseStatus = response.status;
63
+ prometheus_service_1.sendAuthorizationChecksPerRequestMetric(responseStatus, authorizationObjects.length);
58
64
  if (!response.ok) {
59
- logger.error({ status: response.status }, 'Authorization middleware: authorization request failed');
60
- return { isAuthorized: false };
65
+ logger.error({ status: response.status }, 'AuthorizationService: authorization request failed');
66
+ const isAuthorized = false;
67
+ authorizationObjects.forEach(function (authorizationObject) {
68
+ prometheus_service_1.sendAuthorizationCheckResponseTimeMetric(authorizationObject.resource_type, action, isAuthorized, responseStatus, time);
69
+ });
70
+ return { isAuthorized };
61
71
  }
62
72
  const responseBody = yield response.json();
63
73
  const unauthorizedObjects = [];
64
74
  responseBody.result.forEach(function (isAuthorized, index) {
65
- const authorizationObject = params.authorizationObjects[index];
75
+ const authorizationObject = authorizationObjects[index];
66
76
  if (!isAuthorized) {
67
77
  unauthorizedObjects.push(authorizationObject);
68
78
  }
69
- sendAuthorizationCheckMetric(authorizationObject.resource_type, isAuthorized);
79
+ prometheus_service_1.sendAuthorizationCheckResponseTimeMetric(authorizationObject.resource_type, action, isAuthorized, responseStatus, time);
70
80
  });
71
81
  if (unauthorizedObjects.length > 0) {
72
82
  logger.info({
73
83
  resources: JSON.stringify(unauthorizedObjects),
74
- }, 'Authorization middleware: resource is unauthorized');
84
+ }, 'AuthorizationService: resource is unauthorized');
75
85
  const unauthorizedIds = unauthorizedObjects.map(obj => obj.resource_id);
76
86
  return { isAuthorized: false, unauthorizedIds };
77
87
  }
@@ -99,12 +109,3 @@ function createAuthorizationParams(resources, action) {
99
109
  function logOnFetchFail(retries, error, response) {
100
110
  logger.error({ attempt: retries, error }, 'Authorization attempt failed due to network issues');
101
111
  }
102
- function sendAuthorizationCheckMetric(resourceType, isAuthorized) {
103
- const metricsManager = prometheus_service_1.getMetricsManager();
104
- if (metricsManager) {
105
- metricsManager.increaseCounter(prometheus_service_1.METRICS.AUTHORIZATION_CHECK, {
106
- resourceType,
107
- isAuthorized,
108
- });
109
- }
110
- }
@@ -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.16",
3
+ "version": "1.0.18",
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": "bc21495debb5e34772c3c1fd8dff26870081d103"
34
+ "gitHead": "003096557ca719ae941f421a0960c170e74d5cc1"
35
35
  }