@mondaydotcomorg/monday-authorization 3.5.3-feat-shaime-support-entity-attributes-in-authorization-sdk-a77c130 → 3.6.0-feat-shaime-support-entity-attributes-1-f58d933
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/authorization-attributes-service.d.ts +47 -26
- package/dist/authorization-attributes-service.d.ts.map +1 -1
- package/dist/authorization-attributes-service.js +171 -33
- package/dist/authorization-middleware.d.ts +2 -2
- package/dist/authorization-middleware.d.ts.map +1 -1
- package/dist/authorization-service.d.ts +3 -3
- package/dist/authorization-service.d.ts.map +1 -1
- package/dist/base-attribute-assignment.d.ts +6 -5
- package/dist/base-attribute-assignment.d.ts.map +1 -1
- package/dist/base-attribute-assignment.js +8 -17
- package/dist/constants/sns.d.ts +2 -12
- package/dist/constants/sns.d.ts.map +1 -1
- package/dist/constants/sns.js +2 -22
- package/dist/entity-attribute-assignment.d.ts +4 -8
- package/dist/entity-attribute-assignment.d.ts.map +1 -1
- package/dist/entity-attribute-assignment.js +5 -4
- package/dist/entity-attributes-constants.d.ts +6 -6
- package/dist/entity-attributes-constants.d.ts.map +1 -1
- package/dist/entity-attributes-constants.js +7 -5
- package/dist/errors/argument-error.d.ts.map +1 -1
- package/dist/errors/argument-error.js +0 -1
- package/dist/esm/authorization-attributes-service.d.ts +47 -26
- package/dist/esm/authorization-attributes-service.d.ts.map +1 -1
- package/dist/esm/authorization-attributes-service.mjs +167 -33
- package/dist/esm/authorization-middleware.d.ts +2 -2
- package/dist/esm/authorization-middleware.d.ts.map +1 -1
- package/dist/esm/authorization-service.d.ts +3 -3
- package/dist/esm/authorization-service.d.ts.map +1 -1
- package/dist/esm/base-attribute-assignment.d.ts +6 -5
- package/dist/esm/base-attribute-assignment.d.ts.map +1 -1
- package/dist/esm/base-attribute-assignment.mjs +4 -17
- package/dist/esm/constants/sns.d.ts +2 -12
- package/dist/esm/constants/sns.d.ts.map +1 -1
- package/dist/esm/constants/sns.mjs +3 -17
- package/dist/esm/entity-attribute-assignment.d.ts +4 -8
- package/dist/esm/entity-attribute-assignment.d.ts.map +1 -1
- package/dist/esm/entity-attribute-assignment.mjs +5 -4
- package/dist/esm/entity-attributes-constants.d.ts +6 -6
- package/dist/esm/entity-attributes-constants.d.ts.map +1 -1
- package/dist/esm/entity-attributes-constants.mjs +8 -6
- package/dist/esm/errors/argument-error.d.ts.map +1 -1
- package/dist/esm/errors/argument-error.mjs +0 -1
- package/dist/esm/index.d.ts +0 -9
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.mjs +0 -7
- package/dist/esm/prometheus-service.d.ts +2 -1
- package/dist/esm/prometheus-service.d.ts.map +1 -1
- package/dist/esm/resource-attribute-assignment.d.ts +4 -8
- package/dist/esm/resource-attribute-assignment.d.ts.map +1 -1
- package/dist/esm/resource-attribute-assignment.mjs +3 -2
- package/dist/esm/resource-attributes-constants.d.ts +21 -11
- package/dist/esm/resource-attributes-constants.d.ts.map +1 -1
- package/dist/esm/resource-attributes-constants.mjs +17 -14
- package/dist/esm/testKit/index.d.ts +4 -4
- package/dist/esm/testKit/index.d.ts.map +1 -1
- package/dist/esm/types/authorization-attributes-contracts.d.ts +5 -4
- package/dist/esm/types/authorization-attributes-contracts.d.ts.map +1 -1
- package/dist/esm/types/authorization-attributes-service.interface.d.ts +7 -20
- package/dist/esm/types/authorization-attributes-service.interface.d.ts.map +1 -1
- package/dist/esm/types/general.d.ts +2 -1
- package/dist/esm/types/general.d.ts.map +1 -1
- package/dist/esm/utils/assigment-schema.d.ts +29 -0
- package/dist/esm/utils/assigment-schema.d.ts.map +1 -0
- package/dist/esm/utils/assigment-schema.mjs +29 -0
- package/dist/esm/utils/validation.d.ts +13 -38
- package/dist/esm/utils/validation.d.ts.map +1 -1
- package/dist/esm/utils/validation.mjs +38 -97
- package/dist/index.d.ts +0 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -15
- package/dist/prometheus-service.d.ts +2 -1
- package/dist/prometheus-service.d.ts.map +1 -1
- package/dist/resource-attribute-assignment.d.ts +4 -8
- package/dist/resource-attribute-assignment.d.ts.map +1 -1
- package/dist/resource-attribute-assignment.js +3 -2
- package/dist/resource-attributes-constants.d.ts +21 -11
- package/dist/resource-attributes-constants.d.ts.map +1 -1
- package/dist/resource-attributes-constants.js +16 -13
- package/dist/testKit/index.d.ts +4 -4
- package/dist/testKit/index.d.ts.map +1 -1
- package/dist/types/authorization-attributes-contracts.d.ts +5 -4
- package/dist/types/authorization-attributes-contracts.d.ts.map +1 -1
- package/dist/types/authorization-attributes-service.interface.d.ts +7 -20
- package/dist/types/authorization-attributes-service.interface.d.ts.map +1 -1
- package/dist/types/general.d.ts +2 -1
- package/dist/types/general.d.ts.map +1 -1
- package/dist/utils/assigment-schema.d.ts +29 -0
- package/dist/utils/assigment-schema.d.ts.map +1 -0
- package/dist/utils/assigment-schema.js +36 -0
- package/dist/utils/validation.d.ts +13 -38
- package/dist/utils/validation.d.ts.map +1 -1
- package/dist/utils/validation.js +42 -97
- package/package.json +3 -3
- package/src/authorization-attributes-service.ts +222 -35
- package/src/authorization-middleware.ts +2 -2
- package/src/authorization-service.ts +4 -4
- package/src/base-attribute-assignment.ts +10 -35
- package/src/constants/sns.ts +2 -19
- package/src/entity-attribute-assignment.ts +9 -6
- package/src/entity-attributes-constants.ts +6 -6
- package/src/errors/argument-error.ts +0 -1
- package/src/index.ts +0 -9
- package/src/prometheus-service.ts +3 -1
- package/src/resource-attribute-assignment.ts +11 -13
- package/src/resource-attributes-constants.ts +15 -23
- package/src/testKit/index.ts +5 -5
- package/src/types/authorization-attributes-contracts.ts +5 -5
- package/src/types/authorization-attributes-service.interface.ts +12 -38
- package/src/types/general.ts +2 -3
- package/src/utils/assigment-schema.ts +29 -0
- package/src/utils/validation.ts +41 -92
- package/dist/authorization-attributes-ms-service.d.ts +0 -90
- package/dist/authorization-attributes-ms-service.d.ts.map +0 -1
- package/dist/authorization-attributes-ms-service.js +0 -352
- package/dist/authorization-attributes-sns-service.d.ts +0 -90
- package/dist/authorization-attributes-sns-service.d.ts.map +0 -1
- package/dist/authorization-attributes-sns-service.js +0 -217
- package/dist/esm/authorization-attributes-ms-service.d.ts +0 -90
- package/dist/esm/authorization-attributes-ms-service.d.ts.map +0 -1
- package/dist/esm/authorization-attributes-ms-service.mjs +0 -350
- package/dist/esm/authorization-attributes-sns-service.d.ts +0 -90
- package/dist/esm/authorization-attributes-sns-service.d.ts.map +0 -1
- package/dist/esm/authorization-attributes-sns-service.mjs +0 -211
- package/src/authorization-attributes-ms-service.ts +0 -514
- package/src/authorization-attributes-sns-service.ts +0 -312
package/dist/utils/validation.js
CHANGED
|
@@ -1,117 +1,62 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const Ajv = require('ajv');
|
|
4
4
|
const errors_argumentError = require('../errors/argument-error.js');
|
|
5
|
+
const utils_assigmentSchema = require('./assigment-schema.js');
|
|
6
|
+
|
|
7
|
+
const _interopDefault = e => e && e.__esModule ? e : { default: e };
|
|
8
|
+
|
|
9
|
+
const Ajv__default = /*#__PURE__*/_interopDefault(Ajv);
|
|
5
10
|
|
|
6
11
|
/**
|
|
7
|
-
* Utility class for common validation operations using
|
|
12
|
+
* Utility class for common validation operations using AJV
|
|
8
13
|
*/
|
|
9
14
|
class ValidationUtils {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
catch (error) {
|
|
22
|
-
if (error instanceof zod.z.ZodError) {
|
|
23
|
-
throw new errors_argumentError.ArgumentError(`${fieldName} must be an integer, got: ${value}`);
|
|
24
|
-
}
|
|
25
|
-
throw error;
|
|
15
|
+
static ajv = new Ajv__default.default({ allErrors: true });
|
|
16
|
+
static integerValidator = this.ajv.compile({ type: 'number', multipleOf: 1 });
|
|
17
|
+
static stringValidator = this.ajv.compile({ type: 'string', minLength: 1 });
|
|
18
|
+
static stringArrayValidator = this.ajv.compile({
|
|
19
|
+
type: 'array',
|
|
20
|
+
items: { type: 'string' },
|
|
21
|
+
});
|
|
22
|
+
static validateInteger(value) {
|
|
23
|
+
const valid = this.integerValidator(value);
|
|
24
|
+
if (!valid) {
|
|
25
|
+
throw new errors_argumentError.ArgumentError('Invalid integer');
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
* @throws ArgumentError if value is not a string or is empty
|
|
33
|
-
*/
|
|
34
|
-
static validateString(value, fieldName) {
|
|
35
|
-
const schema = zod.z.string().min(1);
|
|
36
|
-
try {
|
|
37
|
-
schema.parse(value);
|
|
38
|
-
}
|
|
39
|
-
catch (error) {
|
|
40
|
-
if (error instanceof zod.z.ZodError) {
|
|
41
|
-
if (typeof value !== 'string') {
|
|
42
|
-
throw new errors_argumentError.ArgumentError(`${fieldName} must be a string, got: ${typeof value}`);
|
|
43
|
-
}
|
|
44
|
-
throw new errors_argumentError.ArgumentError(`${fieldName} must be a non-empty string`);
|
|
45
|
-
}
|
|
46
|
-
throw error;
|
|
28
|
+
static validateString(value) {
|
|
29
|
+
const valid = this.stringValidator(value);
|
|
30
|
+
if (!valid) {
|
|
31
|
+
throw new errors_argumentError.ArgumentError('Invalid string');
|
|
47
32
|
}
|
|
48
33
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
* @param minLength Minimum required length (default: 0)
|
|
54
|
-
* @returns The validated array
|
|
55
|
-
* @throws ArgumentError if value is not an array or doesn't meet minimum length
|
|
56
|
-
*/
|
|
57
|
-
static validateArray(value, fieldName, minLength = 0) {
|
|
58
|
-
const schema = zod.z.array(zod.z.any()).min(minLength);
|
|
59
|
-
try {
|
|
60
|
-
return schema.parse(value);
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
if (error instanceof zod.z.ZodError) {
|
|
64
|
-
if (!Array.isArray(value)) {
|
|
65
|
-
throw new errors_argumentError.ArgumentError(`${fieldName} must be an array`);
|
|
66
|
-
}
|
|
67
|
-
throw new errors_argumentError.ArgumentError(`${fieldName} must have at least ${minLength} items`);
|
|
68
|
-
}
|
|
69
|
-
throw error;
|
|
34
|
+
static validateStringArray(value) {
|
|
35
|
+
const valid = this.stringArrayValidator(value);
|
|
36
|
+
if (!valid) {
|
|
37
|
+
throw new errors_argumentError.ArgumentError('Invalid string array');
|
|
70
38
|
}
|
|
71
39
|
}
|
|
72
40
|
/**
|
|
73
|
-
* Validates
|
|
74
|
-
*
|
|
75
|
-
* @param validValues Array of valid values
|
|
76
|
-
* @param fieldName The name of the field for error messages
|
|
77
|
-
* @returns The validated value as the enum type
|
|
78
|
-
* @throws ArgumentError if value is not in validValues
|
|
41
|
+
* Validates an attribute assignment object using a single AJV schema.
|
|
42
|
+
* Preserves legacy error messages for each field.
|
|
79
43
|
*/
|
|
80
|
-
static
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
throw new errors_argumentError.ArgumentError(`${fieldName} must be one of [${validValues.join(', ')}], got: ${value}`);
|
|
88
|
-
}
|
|
89
|
-
throw error;
|
|
44
|
+
static validateResourceAssignment(resource) {
|
|
45
|
+
const valid = utils_assigmentSchema.resourceAssignmentSchema(resource);
|
|
46
|
+
if (!valid) {
|
|
47
|
+
const errorMessages = (utils_assigmentSchema.resourceAssignmentSchema.errors || [])
|
|
48
|
+
.map(err => err.message || 'validation failed')
|
|
49
|
+
.join(', ');
|
|
50
|
+
throw new errors_argumentError.ArgumentError(`Invalid resource attribute assignment: ${errorMessages}`);
|
|
90
51
|
}
|
|
91
52
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const schema = zod.z.array(zod.z.string());
|
|
100
|
-
try {
|
|
101
|
-
schema.parse(value);
|
|
102
|
-
}
|
|
103
|
-
catch (error) {
|
|
104
|
-
if (error instanceof zod.z.ZodError) {
|
|
105
|
-
const zodError = error;
|
|
106
|
-
const firstError = zodError.issues[0];
|
|
107
|
-
// Check if it's an array item validation error
|
|
108
|
-
if (firstError.path.length > 0 && typeof firstError.path[0] === 'number') {
|
|
109
|
-
const index = firstError.path[0];
|
|
110
|
-
throw new errors_argumentError.ArgumentError(`All ${fieldName} must be strings, but item at index ${index} is not`);
|
|
111
|
-
}
|
|
112
|
-
throw new errors_argumentError.ArgumentError(`${fieldName} must be an array`);
|
|
113
|
-
}
|
|
114
|
-
throw error;
|
|
53
|
+
static validateEntityAssignment(entity) {
|
|
54
|
+
const valid = utils_assigmentSchema.entityAssignmentSchema(entity);
|
|
55
|
+
if (!valid) {
|
|
56
|
+
const errorMessages = (utils_assigmentSchema.entityAssignmentSchema.errors || [])
|
|
57
|
+
.map(err => err.message || 'validation failed')
|
|
58
|
+
.join(', ');
|
|
59
|
+
throw new errors_argumentError.ArgumentError(`Invalid entity attribute assignment: ${errorMessages}`);
|
|
115
60
|
}
|
|
116
61
|
}
|
|
117
62
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mondaydotcomorg/monday-authorization",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0-feat-shaime-support-entity-attributes-1-f58d933",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"license": "BSD-3-Clause",
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
"@mondaydotcomorg/monday-observability-kit": "^1.5.3",
|
|
29
29
|
"@mondaydotcomorg/monday-sns": "^1.2.1",
|
|
30
30
|
"@mondaydotcomorg/trident-backend-api": "^0.24.3",
|
|
31
|
+
"ajv": "^8.12.0",
|
|
31
32
|
"lodash": "^4.17.21",
|
|
32
33
|
"node-fetch": "^2.6.7",
|
|
33
34
|
"on-headers": "^1.0.2",
|
|
34
|
-
"ts-node": "^10.0.0"
|
|
35
|
-
"zod": "^4.1.13"
|
|
35
|
+
"ts-node": "^10.0.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@mondaydotcomorg/trident-library": "^1.1.44",
|
|
@@ -1,47 +1,234 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
import chunk from 'lodash/chunk.js';
|
|
2
|
+
import { Api, FetcherConfig, HttpClient } from '@mondaydotcomorg/trident-backend-api';
|
|
3
|
+
import { getTopicAttributes, sendToSns } from '@mondaydotcomorg/monday-sns';
|
|
4
|
+
import { HttpFetcherError, RecursivePartial } from '@mondaydotcomorg/monday-fetch-api';
|
|
5
|
+
import {
|
|
6
|
+
ResourceAttributeAssignment,
|
|
7
|
+
ResourceAttributeResponse,
|
|
8
|
+
ResourceAttributeUpsertOperation,
|
|
9
|
+
} from './types/authorization-attributes-contracts';
|
|
10
|
+
import { Resource } from './types/general';
|
|
11
|
+
import { logger } from './authorization-internal-service';
|
|
12
|
+
import { getAttributionsFromApi } from './attributions-service';
|
|
13
|
+
import {
|
|
14
|
+
ASYNC_RESOURCE_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE,
|
|
15
|
+
RESOURCE_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND,
|
|
16
|
+
SNS_ARN_ENV_VAR_NAME,
|
|
17
|
+
SNS_DEV_TEST_NAME,
|
|
18
|
+
} from './constants/sns';
|
|
19
|
+
import { APP_NAME, DEFAULT_FETCH_OPTIONS, ERROR_MESSAGES } from './constants';
|
|
20
|
+
import type { TopicAttributesMap } from 'aws-sdk/clients/sns';
|
|
21
|
+
|
|
20
22
|
export class AuthorizationAttributesService {
|
|
21
|
-
private
|
|
22
|
-
private
|
|
23
|
+
private static LOG_TAG = 'authorization_attributes';
|
|
24
|
+
private static API_PATHS = {
|
|
25
|
+
UPSERT_RESOURCE_ATTRIBUTES: '/attributes/{accountId}/resource',
|
|
26
|
+
DELETE_RESOURCE_ATTRIBUTES: '/attributes/{accountId}/resource/{resourceType}/{resourceId}',
|
|
27
|
+
} as const;
|
|
28
|
+
private httpClient: HttpClient;
|
|
29
|
+
private fetchOptions: RecursivePartial<FetcherConfig>;
|
|
30
|
+
private snsArn: string;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Public constructor to create the AuthorizationAttributesService instance.
|
|
34
|
+
* @param httpClient The HTTP client to use for API requests, if not provided, the default HTTP client from Api will be used.
|
|
35
|
+
* @param fetchOptions The fetch options to use for API requests, if not provided, the default fetch options will be used.
|
|
36
|
+
*/
|
|
37
|
+
constructor(httpClient?: HttpClient, fetchOptions?: RecursivePartial<FetcherConfig>) {
|
|
38
|
+
if (!httpClient) {
|
|
39
|
+
httpClient = Api.getPart('httpClient');
|
|
40
|
+
if (!httpClient) {
|
|
41
|
+
throw new Error(ERROR_MESSAGES.HTTP_CLIENT_NOT_INITIALIZED);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!fetchOptions) {
|
|
46
|
+
fetchOptions = DEFAULT_FETCH_OPTIONS;
|
|
47
|
+
} else {
|
|
48
|
+
fetchOptions = {
|
|
49
|
+
...DEFAULT_FETCH_OPTIONS,
|
|
50
|
+
...fetchOptions,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
this.httpClient = httpClient;
|
|
54
|
+
this.fetchOptions = fetchOptions;
|
|
55
|
+
this.snsArn = AuthorizationAttributesService.getSnsTopicArn();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Upsert resource attributes synchronously, performing http call to the authorization MS to assign the given attributes to the given resource.
|
|
60
|
+
* @param accountId
|
|
61
|
+
* @param resourceAttributeAssignments - Array of resource (resourceType, resourceId) and attribute (key, value) pairs to upsert in the authorization MS.
|
|
62
|
+
* e.g. [{ resourceType: 'board', resourceId: 123, key: 'board_kind', value: 'private' }]
|
|
63
|
+
* @returns ResourceAttributeResponse - The affected (created and updated_ resource attributes assignments in the `attributes` field.
|
|
64
|
+
*/
|
|
65
|
+
async upsertResourceAttributes(
|
|
66
|
+
accountId: number,
|
|
67
|
+
resourceAttributeAssignments: ResourceAttributeAssignment[]
|
|
68
|
+
): Promise<ResourceAttributeResponse> {
|
|
69
|
+
const attributionHeaders = getAttributionsFromApi();
|
|
70
|
+
try {
|
|
71
|
+
return await this.httpClient.fetch<ResourceAttributeResponse>(
|
|
72
|
+
{
|
|
73
|
+
url: {
|
|
74
|
+
appName: APP_NAME,
|
|
75
|
+
path: AuthorizationAttributesService.API_PATHS.UPSERT_RESOURCE_ATTRIBUTES.replace(
|
|
76
|
+
'{accountId}',
|
|
77
|
+
accountId.toString()
|
|
78
|
+
),
|
|
79
|
+
},
|
|
80
|
+
method: 'POST',
|
|
81
|
+
headers: {
|
|
82
|
+
'Content-Type': 'application/json',
|
|
83
|
+
...attributionHeaders,
|
|
84
|
+
},
|
|
85
|
+
body: JSON.stringify({ resourceAttributeAssignments }),
|
|
86
|
+
},
|
|
87
|
+
this.fetchOptions
|
|
88
|
+
);
|
|
89
|
+
} catch (err) {
|
|
90
|
+
if (err instanceof HttpFetcherError) {
|
|
91
|
+
throw new Error(ERROR_MESSAGES.REQUEST_FAILED('upsertResourceAttributes', err.status, err.message));
|
|
92
|
+
}
|
|
93
|
+
throw err;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
23
96
|
|
|
24
97
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* @
|
|
98
|
+
* Delete resource attributes assignments synchronously, performing http call to the authorization MS to delete the given attributes from the given singular resource.
|
|
99
|
+
* @param accountId
|
|
100
|
+
* @param resource - The resource (resourceType, resourceId) to delete the attributes for.
|
|
101
|
+
* @param attributeKeys - Array of attribute keys to delete for the resource.
|
|
102
|
+
* @returns ResourceAttributeResponse - The affected (deleted) resource attributes assignments in the `attributes` field.
|
|
28
103
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
104
|
+
async deleteResourceAttributes(
|
|
105
|
+
accountId: number,
|
|
106
|
+
resource: Resource,
|
|
107
|
+
attributeKeys: string[]
|
|
108
|
+
): Promise<ResourceAttributeResponse> {
|
|
109
|
+
const attributionHeaders = getAttributionsFromApi();
|
|
110
|
+
if (!resource.id) {
|
|
111
|
+
throw new Error('Resource ID is required');
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
return await this.httpClient.fetch<ResourceAttributeResponse>(
|
|
115
|
+
{
|
|
116
|
+
url: {
|
|
117
|
+
appName: APP_NAME,
|
|
118
|
+
path: AuthorizationAttributesService.API_PATHS.DELETE_RESOURCE_ATTRIBUTES.replace(
|
|
119
|
+
'{accountId}',
|
|
120
|
+
accountId.toString()
|
|
121
|
+
)
|
|
122
|
+
.replace('{resourceType}', resource.type)
|
|
123
|
+
.replace('{resourceId}', resource.id.toString()),
|
|
124
|
+
},
|
|
125
|
+
method: 'DELETE',
|
|
126
|
+
headers: {
|
|
127
|
+
'Content-Type': 'application/json',
|
|
128
|
+
...attributionHeaders,
|
|
129
|
+
},
|
|
130
|
+
body: JSON.stringify({ keys: attributeKeys }),
|
|
131
|
+
},
|
|
132
|
+
this.fetchOptions
|
|
133
|
+
);
|
|
134
|
+
} catch (err) {
|
|
135
|
+
if (err instanceof HttpFetcherError) {
|
|
136
|
+
throw new Error(ERROR_MESSAGES.REQUEST_FAILED('deleteResourceAttributes', err.status, err.message));
|
|
137
|
+
}
|
|
138
|
+
throw err;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Async function, this function only send the updates request to SNS and return before the change actually took place
|
|
144
|
+
* @param accountId
|
|
145
|
+
* @param appName - App name of the calling app
|
|
146
|
+
* @param callerActionIdentifier - action identifier
|
|
147
|
+
* @param resourceAttributeOperations - Array of operations to do on resource attributes.
|
|
148
|
+
* @return {Promise<ResourceAttributesOperation[]>} Array of sent operations
|
|
149
|
+
* */
|
|
150
|
+
async updateResourceAttributesAsync(
|
|
151
|
+
accountId: number,
|
|
152
|
+
appName: string,
|
|
153
|
+
callerActionIdentifier: string,
|
|
154
|
+
resourceAttributeOperations: ResourceAttributeUpsertOperation[]
|
|
155
|
+
): Promise<ResourceAttributeUpsertOperation[]> {
|
|
156
|
+
const topicArn: string = this.snsArn;
|
|
157
|
+
const sendToSnsPromises: Promise<ResourceAttributeAssignment[]>[] = [];
|
|
158
|
+
const operationChucks = chunk(resourceAttributeOperations, ASYNC_RESOURCE_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE);
|
|
159
|
+
for (const operationsChunk of operationChucks) {
|
|
160
|
+
sendToSnsPromises.push(
|
|
161
|
+
this.sendSingleSnsMessage(topicArn, accountId, appName, callerActionIdentifier, operationsChunk)
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
return (await Promise.all(sendToSnsPromises)).flat() as ResourceAttributeUpsertOperation[];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private async sendSingleSnsMessage(
|
|
168
|
+
topicArn: string,
|
|
169
|
+
accountId: number,
|
|
170
|
+
appName: string,
|
|
171
|
+
callerActionIdentifier: string,
|
|
172
|
+
operations: ResourceAttributeAssignment[]
|
|
173
|
+
): Promise<ResourceAttributeAssignment[]> {
|
|
174
|
+
const payload = {
|
|
175
|
+
kind: RESOURCE_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND,
|
|
176
|
+
payload: {
|
|
177
|
+
accountId: accountId,
|
|
178
|
+
callerAppName: appName,
|
|
179
|
+
callerActionIdentifier: callerActionIdentifier,
|
|
180
|
+
operations: operations,
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
try {
|
|
184
|
+
await sendToSns(payload, topicArn);
|
|
185
|
+
return operations;
|
|
186
|
+
} catch (error) {
|
|
187
|
+
logger.error(
|
|
188
|
+
{ error, tag: AuthorizationAttributesService.LOG_TAG },
|
|
189
|
+
'Authorization resource attributes async update: failed to send operations to SNS'
|
|
190
|
+
);
|
|
191
|
+
return [];
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private static getSnsTopicArn(): string {
|
|
196
|
+
const arnFromEnv: string | undefined = process.env[SNS_ARN_ENV_VAR_NAME];
|
|
197
|
+
if (arnFromEnv) {
|
|
198
|
+
return arnFromEnv;
|
|
199
|
+
}
|
|
200
|
+
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
|
201
|
+
return SNS_DEV_TEST_NAME;
|
|
32
202
|
}
|
|
33
|
-
|
|
203
|
+
throw new Error('Unable to get sns topic arn from env variable');
|
|
34
204
|
}
|
|
35
205
|
|
|
36
206
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
207
|
+
* Checks we can contact the required SNS topic that used to send attribute updates to Authorization MS.
|
|
208
|
+
* This function can be used as health check for services that updating resource attributes in async is crucial.
|
|
209
|
+
* Note this function only verify the POD can contact AWS SDK and the topic exists, but the user still might get
|
|
210
|
+
* errors when pushing for the SNS (e.g: in case the AWS role of the POD don't have permissions to push messages).
|
|
211
|
+
* However, this is the best we can do without actually push dummy messages to the SNS.
|
|
212
|
+
* @return {Promise<boolean>} - true if succeeded
|
|
40
213
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
214
|
+
async asyncResourceAttributesHealthCheck(): Promise<boolean> {
|
|
215
|
+
try {
|
|
216
|
+
const requestedTopicArn: string = this.snsArn;
|
|
217
|
+
const attributes: TopicAttributesMap = await getTopicAttributes(requestedTopicArn);
|
|
218
|
+
const isHealthy = !(!attributes || !('TopicArn' in attributes) || attributes.TopicArn !== requestedTopicArn);
|
|
219
|
+
if (!isHealthy) {
|
|
220
|
+
logger.error(
|
|
221
|
+
{ requestedTopicArn, snsReturnedAttributes: attributes, tag: AuthorizationAttributesService.LOG_TAG },
|
|
222
|
+
'authorization-attributes-service failed in health check'
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
return isHealthy;
|
|
226
|
+
} catch (error) {
|
|
227
|
+
logger.error(
|
|
228
|
+
{ error, tag: AuthorizationAttributesService.LOG_TAG },
|
|
229
|
+
'authorization-attributes-service got error during health check'
|
|
230
|
+
);
|
|
231
|
+
return false;
|
|
44
232
|
}
|
|
45
|
-
return this._snsService;
|
|
46
233
|
}
|
|
47
234
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import onHeaders from 'on-headers';
|
|
2
2
|
import { AuthorizationInternalService } from './authorization-internal-service';
|
|
3
3
|
import { AuthorizationService, createAuthorizationParams } from './authorization-service';
|
|
4
|
-
import { BaseRequest, BaseResponse, Context, ContextGetter, ResourceGetter } from './types/general';
|
|
4
|
+
import { Action, BaseRequest, BaseResponse, Context, ContextGetter, ResourceGetter } from './types/general';
|
|
5
5
|
import type { NextFunction } from 'express';
|
|
6
6
|
|
|
7
7
|
// getAuthorizationMiddleware is duplicated in testKit/index.ts
|
|
8
8
|
// If you are making changes to this function, please make sure to update the other file as well
|
|
9
9
|
export function getAuthorizationMiddleware(
|
|
10
|
-
action:
|
|
10
|
+
action: Action,
|
|
11
11
|
resourceGetter: ResourceGetter,
|
|
12
12
|
contextGetter?: ContextGetter
|
|
13
13
|
) {
|
|
@@ -3,7 +3,7 @@ import { MondayFetchOptions } from '@mondaydotcomorg/monday-fetch';
|
|
|
3
3
|
import { Api } from '@mondaydotcomorg/trident-backend-api';
|
|
4
4
|
import { HttpFetcherError } from '@mondaydotcomorg/monday-fetch-api';
|
|
5
5
|
import { getIgniteClient, IgniteClient } from '@mondaydotcomorg/ignite-sdk';
|
|
6
|
-
import { AuthorizationObject, AuthorizationParams, Resource } from './types/general';
|
|
6
|
+
import { Action, AuthorizationObject, AuthorizationParams, Resource } from './types/general';
|
|
7
7
|
import { sendAuthorizationCheckResponseTimeMetric } from './prometheus-service';
|
|
8
8
|
import { recordAuthorizationTiming } from './metrics-service';
|
|
9
9
|
import {
|
|
@@ -74,7 +74,7 @@ export class AuthorizationService {
|
|
|
74
74
|
accountId: number,
|
|
75
75
|
userId: number,
|
|
76
76
|
resources: Resource[],
|
|
77
|
-
action:
|
|
77
|
+
action: Action
|
|
78
78
|
): Promise<AuthorizeResponse>;
|
|
79
79
|
|
|
80
80
|
static async isAuthorized(
|
|
@@ -223,7 +223,7 @@ export class AuthorizationService {
|
|
|
223
223
|
accountId: number,
|
|
224
224
|
userId: number,
|
|
225
225
|
resources: Resource[],
|
|
226
|
-
action:
|
|
226
|
+
action: Action
|
|
227
227
|
): Promise<AuthorizeResponse> {
|
|
228
228
|
const { authorizationObjects } = createAuthorizationParams(resources, action);
|
|
229
229
|
return this.isAuthorizedMultiple(accountId, userId, authorizationObjects);
|
|
@@ -338,7 +338,7 @@ export async function setIgniteClient() {
|
|
|
338
338
|
AuthorizationInternalService.setIgniteClient(igniteClient);
|
|
339
339
|
}
|
|
340
340
|
|
|
341
|
-
export function createAuthorizationParams(resources: Resource[], action:
|
|
341
|
+
export function createAuthorizationParams(resources: Resource[], action: Action): AuthorizationParams {
|
|
342
342
|
const params = {
|
|
343
343
|
authorizationObjects: resources.map((resource: Resource) => {
|
|
344
344
|
const authorizationObject: AuthorizationObject = {
|
|
@@ -1,37 +1,18 @@
|
|
|
1
|
-
import
|
|
1
|
+
import isEqual from 'lodash/isEqual.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Base class for attribute assignments (Resource or Entity)
|
|
5
5
|
* Provides common validation and functionality
|
|
6
6
|
*/
|
|
7
|
-
export abstract class BaseAttributeAssignment<
|
|
8
|
-
public readonly id:
|
|
9
|
-
public readonly type:
|
|
7
|
+
export abstract class BaseAttributeAssignment<T, R> {
|
|
8
|
+
public readonly id: number;
|
|
9
|
+
public readonly type: T;
|
|
10
10
|
public readonly attributeKey: string;
|
|
11
11
|
public readonly attributeValue: string;
|
|
12
12
|
|
|
13
|
-
constructor(
|
|
14
|
-
id: TId,
|
|
15
|
-
type: string,
|
|
16
|
-
attributeKey: string,
|
|
17
|
-
attributeValue: string,
|
|
18
|
-
validTypes: readonly string[],
|
|
19
|
-
idFieldName: string,
|
|
20
|
-
typeFieldName: string
|
|
21
|
-
) {
|
|
22
|
-
// Validate id
|
|
23
|
-
ValidationUtils.validateInteger(id, idFieldName);
|
|
24
|
-
|
|
25
|
-
// Validate type
|
|
26
|
-
this.type = ValidationUtils.validateEnum(type, validTypes as readonly TType[], typeFieldName) as TType;
|
|
27
|
-
|
|
28
|
-
// Validate attributeKey
|
|
29
|
-
ValidationUtils.validateString(attributeKey, 'attributeKey');
|
|
30
|
-
|
|
31
|
-
// Validate attributeValue
|
|
32
|
-
ValidationUtils.validateString(attributeValue, 'attributeValue');
|
|
33
|
-
|
|
13
|
+
constructor(id: number, type: T, attributeKey: string, attributeValue: string) {
|
|
34
14
|
this.id = id;
|
|
15
|
+
this.type = type;
|
|
35
16
|
this.attributeKey = attributeKey;
|
|
36
17
|
this.attributeValue = attributeValue;
|
|
37
18
|
}
|
|
@@ -41,15 +22,9 @@ export abstract class BaseAttributeAssignment<TId extends number, TType extends
|
|
|
41
22
|
* @param other Another assignment instance
|
|
42
23
|
* @returns true if all properties are equal
|
|
43
24
|
*/
|
|
44
|
-
equals(other: BaseAttributeAssignment<
|
|
45
|
-
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
return (
|
|
49
|
-
this.id === other.id &&
|
|
50
|
-
this.type === other.type &&
|
|
51
|
-
this.attributeKey === other.attributeKey &&
|
|
52
|
-
this.attributeValue === other.attributeValue
|
|
53
|
-
);
|
|
25
|
+
equals(other: BaseAttributeAssignment<T, R>): boolean {
|
|
26
|
+
return isEqual(this, other);
|
|
54
27
|
}
|
|
28
|
+
|
|
29
|
+
abstract toDataTransferObject(): R;
|
|
55
30
|
}
|
package/src/constants/sns.ts
CHANGED
|
@@ -1,22 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
ENTITY = 'entity',
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
// Resource SNS constants
|
|
7
|
-
export const RESOURCE_SNS_ARN_ENV_VAR_NAME = 'SHARED_AUTHORIZATION_SNS_ENDPOINT_RESOURCE_ATTRIBUTES';
|
|
8
|
-
export const RESOURCE_SNS_DEV_TEST_NAME =
|
|
1
|
+
export const SNS_ARN_ENV_VAR_NAME = 'SHARED_AUTHORIZATION_SNS_ENDPOINT_RESOURCE_ATTRIBUTES';
|
|
2
|
+
export const SNS_DEV_TEST_NAME =
|
|
9
3
|
'arn:aws:sns:us-east-1:000000000000:monday-authorization-resource-attributes-sns-local';
|
|
10
|
-
|
|
11
|
-
// Entity SNS constants
|
|
12
|
-
export const ENTITY_SNS_ARN_ENV_VAR_NAME = 'SHARED_AUTHORIZATION_SNS_ENDPOINT_ENTITY_ATTRIBUTES';
|
|
13
|
-
export const ENTITY_SNS_DEV_TEST_NAME =
|
|
14
|
-
'arn:aws:sns:us-east-1:000000000000:monday-authorization-entity-attributes-sns-local';
|
|
15
4
|
export const RESOURCE_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND = 'resourceAttributeModification';
|
|
16
|
-
export const ENTITY_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND = 'entityAttributeModification';
|
|
17
5
|
export const ASYNC_RESOURCE_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE = 100;
|
|
18
|
-
export const ASYNC_ENTITY_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE = 100;
|
|
19
|
-
|
|
20
|
-
// Legacy exports for backward compatibility
|
|
21
|
-
export const SNS_ARN_ENV_VAR_NAME = RESOURCE_SNS_ARN_ENV_VAR_NAME;
|
|
22
|
-
export const SNS_DEV_TEST_NAME = RESOURCE_SNS_DEV_TEST_NAME;
|
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EntityType } from './entity-attributes-constants';
|
|
2
2
|
import { BaseAttributeAssignment } from './base-attribute-assignment';
|
|
3
|
+
import { EntityAttributeAssignment as EntityAttributeAssignmentContract } from './types/authorization-attributes-contracts';
|
|
4
|
+
import { ValidationUtils } from './utils/validation';
|
|
3
5
|
|
|
4
|
-
export class EntityAttributeAssignment extends BaseAttributeAssignment<
|
|
6
|
+
export class EntityAttributeAssignment extends BaseAttributeAssignment<EntityType, EntityAttributeAssignmentContract> {
|
|
5
7
|
public readonly entityId: number;
|
|
6
8
|
public readonly entityType: EntityType;
|
|
7
9
|
|
|
8
|
-
constructor(entityId: number, entityType:
|
|
9
|
-
|
|
10
|
+
constructor(entityId: number, entityType: EntityType, key: string, value: string) {
|
|
11
|
+
ValidationUtils.validateEntityAssignment({ entityId, entityType, key, value });
|
|
12
|
+
super(entityId, entityType, key, value);
|
|
10
13
|
this.entityId = entityId;
|
|
11
|
-
this.entityType =
|
|
14
|
+
this.entityType = entityType;
|
|
12
15
|
}
|
|
13
16
|
|
|
14
|
-
toDataTransferObject() {
|
|
17
|
+
toDataTransferObject(): EntityAttributeAssignmentContract {
|
|
15
18
|
return {
|
|
16
19
|
entityId: this.entityId,
|
|
17
20
|
entityType: this.entityType,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
1
|
+
export enum EntityType {
|
|
2
|
+
User = 'user',
|
|
3
|
+
Team = 'team',
|
|
4
|
+
Account = 'account',
|
|
5
|
+
}
|
|
6
6
|
|
|
7
|
-
export
|
|
7
|
+
export const ENTITY_TYPES = Object.freeze(Object.values(EntityType));
|