@mondaydotcomorg/monday-authorization 3.5.1 → 3.5.2-feat-shaime-support-entity-attributes-4-ddec1d3
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-ms-service.d.ts +88 -0
- package/dist/authorization-attributes-ms-service.d.ts.map +1 -0
- package/dist/authorization-attributes-ms-service.js +290 -0
- package/dist/authorization-attributes-service.d.ts +25 -47
- package/dist/authorization-attributes-service.d.ts.map +1 -1
- package/dist/authorization-attributes-service.js +32 -171
- package/dist/authorization-attributes-sns-service.d.ts +91 -0
- package/dist/authorization-attributes-sns-service.d.ts.map +1 -0
- package/dist/authorization-attributes-sns-service.js +217 -0
- package/dist/base-attribute-assignment.d.ts +20 -0
- package/dist/base-attribute-assignment.d.ts.map +1 -0
- package/dist/base-attribute-assignment.js +36 -0
- package/dist/constants/sns.d.ts +12 -2
- package/dist/constants/sns.d.ts.map +1 -1
- package/dist/constants/sns.js +22 -2
- package/dist/entity-attribute-assignment.d.ts +20 -0
- package/dist/entity-attribute-assignment.d.ts.map +1 -0
- package/dist/entity-attribute-assignment.js +32 -0
- package/dist/entity-attributes-constants.d.ts +7 -0
- package/dist/entity-attributes-constants.d.ts.map +1 -0
- package/dist/entity-attributes-constants.js +11 -0
- package/dist/errors/argument-error.d.ts +4 -0
- package/dist/errors/argument-error.d.ts.map +1 -0
- package/dist/errors/argument-error.js +11 -0
- package/dist/esm/authorization-attributes-ms-service.d.ts +88 -0
- package/dist/esm/authorization-attributes-ms-service.d.ts.map +1 -0
- package/dist/esm/authorization-attributes-ms-service.mjs +288 -0
- package/dist/esm/authorization-attributes-service.d.ts +25 -47
- package/dist/esm/authorization-attributes-service.d.ts.map +1 -1
- package/dist/esm/authorization-attributes-service.mjs +32 -167
- package/dist/esm/authorization-attributes-sns-service.d.ts +91 -0
- package/dist/esm/authorization-attributes-sns-service.d.ts.map +1 -0
- package/dist/esm/authorization-attributes-sns-service.mjs +211 -0
- package/dist/esm/base-attribute-assignment.d.ts +20 -0
- package/dist/esm/base-attribute-assignment.d.ts.map +1 -0
- package/dist/esm/base-attribute-assignment.mjs +30 -0
- package/dist/esm/constants/sns.d.ts +12 -2
- package/dist/esm/constants/sns.d.ts.map +1 -1
- package/dist/esm/constants/sns.mjs +17 -3
- package/dist/esm/entity-attribute-assignment.d.ts +20 -0
- package/dist/esm/entity-attribute-assignment.d.ts.map +1 -0
- package/dist/esm/entity-attribute-assignment.mjs +30 -0
- package/dist/esm/entity-attributes-constants.d.ts +7 -0
- package/dist/esm/entity-attributes-constants.d.ts.map +1 -0
- package/dist/esm/entity-attributes-constants.mjs +9 -0
- package/dist/esm/errors/argument-error.d.ts +4 -0
- package/dist/esm/errors/argument-error.d.ts.map +1 -0
- package/dist/esm/errors/argument-error.mjs +9 -0
- package/dist/esm/resource-attribute-assignment.d.ts +20 -0
- package/dist/esm/resource-attribute-assignment.d.ts.map +1 -0
- package/dist/esm/resource-attribute-assignment.mjs +30 -0
- package/dist/esm/resource-attributes-constants.d.ts +25 -0
- package/dist/esm/resource-attributes-constants.d.ts.map +1 -0
- package/dist/esm/resource-attributes-constants.mjs +28 -0
- 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 +26 -11
- package/dist/esm/types/authorization-attributes-contracts.d.ts.map +1 -1
- package/dist/esm/types/authorization-attributes-contracts.mjs +6 -6
- package/dist/esm/types/authorization-attributes-service.interface.d.ts +57 -0
- package/dist/esm/types/authorization-attributes-service.interface.d.ts.map +1 -0
- package/dist/esm/types/authorization-attributes-service.interface.mjs +1 -0
- package/dist/esm/utils/validation.d.ts +45 -0
- package/dist/esm/utils/validation.d.ts.map +1 -0
- package/dist/esm/utils/validation.mjs +152 -0
- package/dist/resource-attribute-assignment.d.ts +20 -0
- package/dist/resource-attribute-assignment.d.ts.map +1 -0
- package/dist/resource-attribute-assignment.js +32 -0
- package/dist/resource-attributes-constants.d.ts +25 -0
- package/dist/resource-attributes-constants.d.ts.map +1 -0
- package/dist/resource-attributes-constants.js +31 -0
- 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 +26 -11
- package/dist/types/authorization-attributes-contracts.d.ts.map +1 -1
- package/dist/types/authorization-attributes-contracts.js +5 -5
- package/dist/types/authorization-attributes-service.interface.d.ts +57 -0
- package/dist/types/authorization-attributes-service.interface.d.ts.map +1 -0
- package/dist/types/authorization-attributes-service.interface.js +1 -0
- package/dist/utils/validation.d.ts +45 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +158 -0
- package/package.json +3 -1
- package/src/authorization-attributes-ms-service.ts +438 -0
- package/src/authorization-attributes-service.ts +34 -222
- package/src/authorization-attributes-sns-service.ts +312 -0
- package/src/base-attribute-assignment.ts +46 -0
- package/src/constants/sns.ts +19 -2
- package/src/entity-attribute-assignment.ts +30 -0
- package/src/entity-attributes-constants.ts +7 -0
- package/src/errors/argument-error.ts +7 -0
- package/src/resource-attribute-assignment.ts +38 -0
- package/src/resource-attributes-constants.ts +27 -0
- package/src/testKit/index.ts +5 -5
- package/src/types/authorization-attributes-contracts.ts +34 -11
- package/src/types/authorization-attributes-service.interface.ts +101 -0
- package/src/utils/validation.ts +171 -0
|
@@ -1,234 +1,46 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
import { APP_NAME, DEFAULT_FETCH_OPTIONS, ERROR_MESSAGES } from './constants';
|
|
20
|
-
import type { TopicAttributesMap } from 'aws-sdk/clients/sns';
|
|
21
|
-
|
|
1
|
+
import { AuthorizationAttributesMsService } from './authorization-attributes-ms-service';
|
|
2
|
+
import { AuthorizationAttributesSnsService } from './authorization-attributes-sns-service';
|
|
3
|
+
import { AuthorizationAttributesService as IAuthorizationAttributesService } from './types/authorization-attributes-service.interface';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Main service class for managing resource and entity attributes.
|
|
7
|
+
* Provides access to both direct (MS) and SNS operations.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const service = new AuthorizationAttributesService();
|
|
11
|
+
*
|
|
12
|
+
* // Use direct operations
|
|
13
|
+
* await service.direct().upsertResourceAttributes(accountId, assignments);
|
|
14
|
+
*
|
|
15
|
+
* // Use SNS operations
|
|
16
|
+
* await service.sns().upsertResourceAttributes(accountId, assignments, appName, actionId);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
22
19
|
export class AuthorizationAttributesService {
|
|
23
|
-
private
|
|
24
|
-
private
|
|
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
|
-
}
|
|
20
|
+
private _directService: AuthorizationAttributesMsService | null = null;
|
|
21
|
+
private _snsService: AuthorizationAttributesSnsService | null = null;
|
|
96
22
|
|
|
97
23
|
/**
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
* @
|
|
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.
|
|
24
|
+
* Gets the direct (MS) service instance.
|
|
25
|
+
* Initializes the service on first access (lazy initialization).
|
|
26
|
+
* @returns IAuthorizationAttributesService instance
|
|
103
27
|
*/
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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: ResourceAttributesOperation[]
|
|
155
|
-
): Promise<ResourceAttributesOperation[]> {
|
|
156
|
-
const topicArn: string = this.snsArn;
|
|
157
|
-
const sendToSnsPromises: Promise<ResourceAttributesOperation[]>[] = [];
|
|
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();
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
private async sendSingleSnsMessage(
|
|
168
|
-
topicArn: string,
|
|
169
|
-
accountId: number,
|
|
170
|
-
appName: string,
|
|
171
|
-
callerActionIdentifier: string,
|
|
172
|
-
operations: ResourceAttributesOperation[]
|
|
173
|
-
): Promise<ResourceAttributesOperation[]> {
|
|
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;
|
|
28
|
+
direct(): IAuthorizationAttributesService {
|
|
29
|
+
if (this._directService === null) {
|
|
30
|
+
this._directService = new AuthorizationAttributesMsService();
|
|
202
31
|
}
|
|
203
|
-
|
|
32
|
+
return this._directService;
|
|
204
33
|
}
|
|
205
34
|
|
|
206
35
|
/**
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
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
|
|
36
|
+
* Gets the SNS service instance.
|
|
37
|
+
* Initializes the service on first access (lazy initialization).
|
|
38
|
+
* @returns IAuthorizationAttributesService instance
|
|
213
39
|
*/
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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;
|
|
40
|
+
sns(): IAuthorizationAttributesService {
|
|
41
|
+
if (this._snsService === null) {
|
|
42
|
+
this._snsService = new AuthorizationAttributesSnsService();
|
|
232
43
|
}
|
|
44
|
+
return this._snsService;
|
|
233
45
|
}
|
|
234
46
|
}
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import chunk from 'lodash/chunk.js';
|
|
2
|
+
import { getTopicAttributes, sendToSns } from '@mondaydotcomorg/monday-sns';
|
|
3
|
+
import {
|
|
4
|
+
ResourceAttributeAssignment,
|
|
5
|
+
ResourceAttributeOperation,
|
|
6
|
+
EntityAttributeOperation,
|
|
7
|
+
AttributeOperation,
|
|
8
|
+
} from './types/authorization-attributes-contracts';
|
|
9
|
+
import { EntityAttributeAssignment } from './entity-attribute-assignment';
|
|
10
|
+
import { Resource } from './types/general';
|
|
11
|
+
import { logger } from './authorization-internal-service';
|
|
12
|
+
import {
|
|
13
|
+
ASYNC_RESOURCE_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE,
|
|
14
|
+
ASYNC_ENTITY_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE,
|
|
15
|
+
RESOURCE_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND,
|
|
16
|
+
ENTITY_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND,
|
|
17
|
+
RESOURCE_SNS_ARN_ENV_VAR_NAME,
|
|
18
|
+
ENTITY_SNS_ARN_ENV_VAR_NAME,
|
|
19
|
+
RESOURCE_SNS_DEV_TEST_NAME,
|
|
20
|
+
ENTITY_SNS_DEV_TEST_NAME,
|
|
21
|
+
SnsTopicType,
|
|
22
|
+
} from './constants/sns';
|
|
23
|
+
import type { TopicAttributesMap } from 'aws-sdk/clients/sns';
|
|
24
|
+
import { AuthorizationAttributesService } from './types/authorization-attributes-service.interface';
|
|
25
|
+
import { EntityType } from './entity-attributes-constants';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Service class for managing resource attributes asynchronously via SNS.
|
|
29
|
+
* Provides asynchronous operations to create/update and delete attributes on resources.
|
|
30
|
+
*/
|
|
31
|
+
export class AuthorizationAttributesSnsService implements AuthorizationAttributesService {
|
|
32
|
+
private static LOG_TAG = 'authorization_attributes';
|
|
33
|
+
private resourceSnsArn: string;
|
|
34
|
+
private entitySnsArn: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Public constructor to create the AuthorizationAttributesSnsService instance.
|
|
38
|
+
*/
|
|
39
|
+
constructor() {
|
|
40
|
+
this.resourceSnsArn = AuthorizationAttributesSnsService.getSnsTopicArn(SnsTopicType.RESOURCE);
|
|
41
|
+
this.entitySnsArn = AuthorizationAttributesSnsService.getSnsTopicArn(SnsTopicType.ENTITY);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Async function to upsert resource attributes using SNS.
|
|
46
|
+
* Sends the updates request to SNS and returns before the change actually took place.
|
|
47
|
+
* @param accountId The account ID
|
|
48
|
+
* @param appName App name of the calling app
|
|
49
|
+
* @param callerActionIdentifier Action identifier
|
|
50
|
+
* @param resourceAttributeAssignments Array of resource attribute assignments to upsert
|
|
51
|
+
* @return Promise with array of sent operations
|
|
52
|
+
*/
|
|
53
|
+
async upsertResourceAttributes(
|
|
54
|
+
accountId: number,
|
|
55
|
+
resourceAttributeAssignments: ResourceAttributeAssignment[],
|
|
56
|
+
appName?: string,
|
|
57
|
+
callerActionIdentifier?: string
|
|
58
|
+
): Promise<ResourceAttributeOperation[]> {
|
|
59
|
+
if (!appName || !callerActionIdentifier) {
|
|
60
|
+
throw new Error('appName and callerActionIdentifier are required for SNS service');
|
|
61
|
+
}
|
|
62
|
+
const operations: ResourceAttributeOperation[] = resourceAttributeAssignments.map(assignment => ({
|
|
63
|
+
...assignment,
|
|
64
|
+
operationType: AttributeOperation.UPSERT,
|
|
65
|
+
}));
|
|
66
|
+
return this.updateResourceAttributes(accountId, appName, callerActionIdentifier, operations);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Async function to delete resource attributes using SNS.
|
|
71
|
+
* Sends the updates request to SNS and returns before the change actually took place.
|
|
72
|
+
* @param accountId The account ID
|
|
73
|
+
* @param appName App name of the calling app
|
|
74
|
+
* @param callerActionIdentifier Action identifier
|
|
75
|
+
* @param resource The resource (resourceType, resourceId)
|
|
76
|
+
* @param attributeKeys Array of attribute keys to delete
|
|
77
|
+
* @return Promise with array of sent operations
|
|
78
|
+
*/
|
|
79
|
+
async deleteResourceAttributes(
|
|
80
|
+
accountId: number,
|
|
81
|
+
resource: Resource,
|
|
82
|
+
attributeKeys: string[],
|
|
83
|
+
appName?: string,
|
|
84
|
+
callerActionIdentifier?: string
|
|
85
|
+
): Promise<ResourceAttributeOperation[]> {
|
|
86
|
+
if (!appName || !callerActionIdentifier) {
|
|
87
|
+
throw new Error('appName and callerActionIdentifier are required for SNS service');
|
|
88
|
+
}
|
|
89
|
+
const operations: ResourceAttributeOperation[] = attributeKeys.map(
|
|
90
|
+
key =>
|
|
91
|
+
({
|
|
92
|
+
resourceType: resource.type,
|
|
93
|
+
resourceId: resource.id!,
|
|
94
|
+
key,
|
|
95
|
+
operationType: AttributeOperation.DELETE,
|
|
96
|
+
} as ResourceAttributeOperation)
|
|
97
|
+
);
|
|
98
|
+
return this.updateResourceAttributes(accountId, appName, callerActionIdentifier, operations);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Async function to upsert entity attributes using SNS.
|
|
103
|
+
* Sends the updates request to SNS and returns before the change actually took place.
|
|
104
|
+
* @param accountId The account ID
|
|
105
|
+
* @param appName App name of the calling app
|
|
106
|
+
* @param callerActionIdentifier Action identifier
|
|
107
|
+
* @param entityAttributeAssignments Array of entity attribute assignments to upsert
|
|
108
|
+
* @return Promise with array of sent operations
|
|
109
|
+
*/
|
|
110
|
+
async upsertEntityAttributes(
|
|
111
|
+
accountId: number,
|
|
112
|
+
entityAttributeAssignments: EntityAttributeAssignment[],
|
|
113
|
+
appName?: string,
|
|
114
|
+
callerActionIdentifier?: string
|
|
115
|
+
): Promise<EntityAttributeOperation[]> {
|
|
116
|
+
if (!appName || !callerActionIdentifier) {
|
|
117
|
+
throw new Error('appName and callerActionIdentifier are required for SNS service');
|
|
118
|
+
}
|
|
119
|
+
const operations: EntityAttributeOperation[] = entityAttributeAssignments.map(assignment => {
|
|
120
|
+
return {
|
|
121
|
+
entityId: assignment.entityId,
|
|
122
|
+
entityType: assignment.entityType,
|
|
123
|
+
key: assignment.attributeKey,
|
|
124
|
+
value: assignment.attributeValue,
|
|
125
|
+
operationType: AttributeOperation.UPSERT,
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
return this.updateEntityAttributes(accountId, appName, callerActionIdentifier, operations);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Async function to delete entity attributes using SNS.
|
|
133
|
+
* Sends the updates request to SNS and returns before the change actually took place.
|
|
134
|
+
* @param accountId The account ID
|
|
135
|
+
* @param appName App name of the calling app
|
|
136
|
+
* @param callerActionIdentifier Action identifier
|
|
137
|
+
* @param entityType The entity type
|
|
138
|
+
* @param entityId The entity ID
|
|
139
|
+
* @param attributeKeys Array of attribute keys to delete
|
|
140
|
+
* @return Promise with array of sent operations
|
|
141
|
+
*/
|
|
142
|
+
async deleteEntityAttributes(
|
|
143
|
+
accountId: number,
|
|
144
|
+
entityType: EntityType | string,
|
|
145
|
+
entityId: number,
|
|
146
|
+
attributeKeys: string[],
|
|
147
|
+
appName?: string,
|
|
148
|
+
callerActionIdentifier?: string
|
|
149
|
+
): Promise<EntityAttributeOperation[]> {
|
|
150
|
+
if (!appName || !callerActionIdentifier) {
|
|
151
|
+
throw new Error('appName and callerActionIdentifier are required for SNS service');
|
|
152
|
+
}
|
|
153
|
+
const operations: EntityAttributeOperation[] = attributeKeys.map(
|
|
154
|
+
key =>
|
|
155
|
+
({
|
|
156
|
+
entityType: entityType,
|
|
157
|
+
entityId,
|
|
158
|
+
key,
|
|
159
|
+
operationType: AttributeOperation.DELETE,
|
|
160
|
+
} as EntityAttributeOperation)
|
|
161
|
+
);
|
|
162
|
+
return this.updateEntityAttributes(accountId, appName, callerActionIdentifier, operations);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Async function, this function only send the updates request to SNS and return before the change actually took place
|
|
167
|
+
* @param accountId
|
|
168
|
+
* @param appName - App name of the calling app
|
|
169
|
+
* @param callerActionIdentifier - action identifier
|
|
170
|
+
* @param resourceAttributeOperations - Array of operations to do on resource attributes.
|
|
171
|
+
* @return {Promise<ResourceAttributesOperation[]>} Array of sent operations
|
|
172
|
+
* */
|
|
173
|
+
async updateResourceAttributes(
|
|
174
|
+
accountId: number,
|
|
175
|
+
appName: string,
|
|
176
|
+
callerActionIdentifier: string,
|
|
177
|
+
resourceAttributeOperations: ResourceAttributeOperation[]
|
|
178
|
+
): Promise<ResourceAttributeOperation[]> {
|
|
179
|
+
const topicArn: string = this.resourceSnsArn;
|
|
180
|
+
const sendToSnsPromises: Promise<ResourceAttributeOperation[]>[] = [];
|
|
181
|
+
const operationChucks = chunk(resourceAttributeOperations, ASYNC_RESOURCE_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE);
|
|
182
|
+
for (const operationsChunk of operationChucks) {
|
|
183
|
+
sendToSnsPromises.push(
|
|
184
|
+
this.sendSingleSnsMessage(
|
|
185
|
+
topicArn,
|
|
186
|
+
accountId,
|
|
187
|
+
appName,
|
|
188
|
+
callerActionIdentifier,
|
|
189
|
+
operationsChunk,
|
|
190
|
+
RESOURCE_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND,
|
|
191
|
+
'Authorization resource attributes async update: failed to send operations to SNS'
|
|
192
|
+
)
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
return (await Promise.all(sendToSnsPromises)).flat();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Async function, this function only send the updates request to SNS and return before the change actually took place
|
|
200
|
+
* @param accountId
|
|
201
|
+
* @param appName - App name of the calling app
|
|
202
|
+
* @param callerActionIdentifier - action identifier
|
|
203
|
+
* @param entityAttributeOperations - Array of operations to do on entity attributes.
|
|
204
|
+
* @return {Promise<EntityAttributeOperation[]>} Array of sent operations
|
|
205
|
+
* */
|
|
206
|
+
async updateEntityAttributes(
|
|
207
|
+
accountId: number,
|
|
208
|
+
appName: string,
|
|
209
|
+
callerActionIdentifier: string,
|
|
210
|
+
entityAttributeOperations: EntityAttributeOperation[]
|
|
211
|
+
): Promise<EntityAttributeOperation[]> {
|
|
212
|
+
const topicArn: string = this.entitySnsArn;
|
|
213
|
+
const sendToSnsPromises: Promise<EntityAttributeOperation[]>[] = [];
|
|
214
|
+
const operationChucks = chunk(entityAttributeOperations, ASYNC_ENTITY_ATTRIBUTES_MAX_OPERATIONS_PER_MESSAGE);
|
|
215
|
+
for (const operationsChunk of operationChucks) {
|
|
216
|
+
sendToSnsPromises.push(
|
|
217
|
+
this.sendSingleSnsMessage(
|
|
218
|
+
topicArn,
|
|
219
|
+
accountId,
|
|
220
|
+
appName,
|
|
221
|
+
callerActionIdentifier,
|
|
222
|
+
operationsChunk,
|
|
223
|
+
ENTITY_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND,
|
|
224
|
+
'Authorization entity attributes async update: failed to send operations to SNS'
|
|
225
|
+
)
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
return (await Promise.all(sendToSnsPromises)).flat();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
private async sendSingleSnsMessage<T extends ResourceAttributeOperation | EntityAttributeOperation>(
|
|
232
|
+
topicArn: string,
|
|
233
|
+
accountId: number,
|
|
234
|
+
appName: string,
|
|
235
|
+
callerActionIdentifier: string,
|
|
236
|
+
operations: T[],
|
|
237
|
+
kind: string,
|
|
238
|
+
errorLogMessage: string
|
|
239
|
+
): Promise<T[]> {
|
|
240
|
+
const payload = {
|
|
241
|
+
kind,
|
|
242
|
+
payload: {
|
|
243
|
+
accountId: accountId,
|
|
244
|
+
callerAppName: appName,
|
|
245
|
+
callerActionIdentifier: callerActionIdentifier,
|
|
246
|
+
operations: operations,
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
try {
|
|
250
|
+
await sendToSns(payload, topicArn);
|
|
251
|
+
return operations;
|
|
252
|
+
} catch (error) {
|
|
253
|
+
logger.error({ error, tag: AuthorizationAttributesSnsService.LOG_TAG }, errorLogMessage);
|
|
254
|
+
return [];
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
private static getSnsTopicArn(type: SnsTopicType): string {
|
|
259
|
+
let envVarName: string;
|
|
260
|
+
let devTestName: string;
|
|
261
|
+
|
|
262
|
+
switch (type) {
|
|
263
|
+
case SnsTopicType.ENTITY:
|
|
264
|
+
envVarName = ENTITY_SNS_ARN_ENV_VAR_NAME;
|
|
265
|
+
devTestName = ENTITY_SNS_DEV_TEST_NAME;
|
|
266
|
+
break;
|
|
267
|
+
default:
|
|
268
|
+
// Default to resource SNS constants
|
|
269
|
+
envVarName = RESOURCE_SNS_ARN_ENV_VAR_NAME;
|
|
270
|
+
devTestName = RESOURCE_SNS_DEV_TEST_NAME;
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const arnFromEnv: string | undefined = process.env[envVarName];
|
|
275
|
+
if (arnFromEnv) {
|
|
276
|
+
return arnFromEnv;
|
|
277
|
+
}
|
|
278
|
+
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
|
279
|
+
return devTestName;
|
|
280
|
+
}
|
|
281
|
+
throw new Error(`Unable to get ${type} sns topic arn from env variable`);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Checks we can contact the required SNS topic that used to send attribute updates to Authorization MS.
|
|
286
|
+
* This function can be used as health check for services that updating resource attributes in async is crucial.
|
|
287
|
+
* Note this function only verify the POD can contact AWS SDK and the topic exists, but the user still might get
|
|
288
|
+
* errors when pushing for the SNS (e.g: in case the AWS role of the POD don't have permissions to push messages).
|
|
289
|
+
* However, this is the best we can do without actually push dummy messages to the SNS.
|
|
290
|
+
* @return {Promise<boolean>} - true if succeeded
|
|
291
|
+
*/
|
|
292
|
+
async asyncResourceAttributesHealthCheck(): Promise<boolean> {
|
|
293
|
+
try {
|
|
294
|
+
const requestedTopicArn: string = this.resourceSnsArn;
|
|
295
|
+
const attributes: TopicAttributesMap = await getTopicAttributes(requestedTopicArn);
|
|
296
|
+
const isHealthy = !(!attributes || !('TopicArn' in attributes) || attributes.TopicArn !== requestedTopicArn);
|
|
297
|
+
if (!isHealthy) {
|
|
298
|
+
logger.error(
|
|
299
|
+
{ requestedTopicArn, snsReturnedAttributes: attributes, tag: AuthorizationAttributesSnsService.LOG_TAG },
|
|
300
|
+
'authorization-attributes-service failed in health check'
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
return isHealthy;
|
|
304
|
+
} catch (error) {
|
|
305
|
+
logger.error(
|
|
306
|
+
{ error, tag: AuthorizationAttributesSnsService.LOG_TAG },
|
|
307
|
+
'authorization-attributes-service got error during health check'
|
|
308
|
+
);
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ValidationUtils } from './utils/validation';
|
|
2
|
+
import isEqual from 'lodash/isEqual.js';
|
|
3
|
+
import { EntityAttributeAssignment, ResourceAttributeDelete } from './types/authorization-attributes-contracts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Base class for attribute assignments (Resource or Entity)
|
|
7
|
+
* Provides common validation and functionality
|
|
8
|
+
*/
|
|
9
|
+
export abstract class BaseAttributeAssignment<TId extends number, TType extends string> {
|
|
10
|
+
public readonly id: TId;
|
|
11
|
+
public readonly type: TType;
|
|
12
|
+
public readonly attributeKey: string;
|
|
13
|
+
public readonly attributeValue: string;
|
|
14
|
+
|
|
15
|
+
constructor(
|
|
16
|
+
id: TId,
|
|
17
|
+
type: string,
|
|
18
|
+
attributeKey: string,
|
|
19
|
+
attributeValue: string,
|
|
20
|
+
validTypes: readonly string[],
|
|
21
|
+
idFieldName: string,
|
|
22
|
+
typeFieldName: string
|
|
23
|
+
) {
|
|
24
|
+
const validated = ValidationUtils.validateAssignment<TType>(
|
|
25
|
+
{ id, type, attributeKey, attributeValue },
|
|
26
|
+
validTypes as readonly TType[],
|
|
27
|
+
{ id: idFieldName, type: typeFieldName }
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
this.id = validated.id as TId;
|
|
31
|
+
this.type = validated.type as TType;
|
|
32
|
+
this.attributeKey = validated.attributeKey;
|
|
33
|
+
this.attributeValue = validated.attributeValue;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Compares two assignments for equality
|
|
38
|
+
* @param other Another assignment instance
|
|
39
|
+
* @returns true if all properties are equal
|
|
40
|
+
*/
|
|
41
|
+
equals(other: BaseAttributeAssignment<TId, TType>): boolean {
|
|
42
|
+
return isEqual(this, other);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
abstract toDataTransferObject(): EntityAttributeAssignment | ResourceAttributeDelete;
|
|
46
|
+
}
|
package/src/constants/sns.ts
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
1
|
+
export enum SnsTopicType {
|
|
2
|
+
RESOURCE = 'resource',
|
|
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 =
|
|
3
9
|
'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';
|
|
4
15
|
export const RESOURCE_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND = 'resourceAttributeModification';
|
|
16
|
+
export const ENTITY_ATTRIBUTES_SNS_UPDATE_OPERATION_MESSAGE_KIND = 'entityAttributeModification';
|
|
5
17
|
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;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ENTITY_TYPES, EntityType } from './entity-attributes-constants';
|
|
2
|
+
import { BaseAttributeAssignment } from './base-attribute-assignment';
|
|
3
|
+
|
|
4
|
+
export class EntityAttributeAssignment extends BaseAttributeAssignment<number, EntityType> {
|
|
5
|
+
public readonly entityId: number;
|
|
6
|
+
public readonly entityType: EntityType;
|
|
7
|
+
|
|
8
|
+
constructor(entityId: number, entityType: string, attributeKey: string, attributeValue: string) {
|
|
9
|
+
super(entityId, entityType, attributeKey, attributeValue, ENTITY_TYPES, 'entityId', 'entityType');
|
|
10
|
+
this.entityId = entityId;
|
|
11
|
+
this.entityType = this.type;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
toDataTransferObject() {
|
|
15
|
+
return {
|
|
16
|
+
entityId: this.entityId,
|
|
17
|
+
entityType: this.entityType,
|
|
18
|
+
key: this.attributeKey,
|
|
19
|
+
value: this.attributeValue,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Compares two assignments for equality
|
|
24
|
+
* @param other Another EntityAttributeAssignment instance
|
|
25
|
+
* @returns true if all properties are equal
|
|
26
|
+
*/
|
|
27
|
+
equals(other: EntityAttributeAssignment): boolean {
|
|
28
|
+
return super.equals(other);
|
|
29
|
+
}
|
|
30
|
+
}
|