@mondaydotcomorg/monday-authorization 3.5.0-upgrade-observability-kit-2ebbd01 → 3.5.1-feat-shaime-support-entity-attributes-in-authorization-sdk-59162a3

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 (121) hide show
  1. package/dist/attributions-service.d.ts.map +1 -1
  2. package/dist/attributions-service.js +1 -0
  3. package/dist/authorization-attributes-ms-service.d.ts +67 -0
  4. package/dist/authorization-attributes-ms-service.d.ts.map +1 -0
  5. package/dist/authorization-attributes-ms-service.js +256 -0
  6. package/dist/authorization-attributes-sns-service.d.ts +85 -0
  7. package/dist/authorization-attributes-sns-service.d.ts.map +1 -0
  8. package/dist/authorization-attributes-sns-service.js +203 -0
  9. package/dist/base-attribute-assignment.d.ts +18 -0
  10. package/dist/base-attribute-assignment.d.ts.map +1 -0
  11. package/dist/base-attribute-assignment.js +43 -0
  12. package/dist/clients/graph-api.d.ts.map +1 -1
  13. package/dist/clients/graph-api.js +1 -0
  14. package/dist/constants/sns.d.ts +12 -2
  15. package/dist/constants/sns.d.ts.map +1 -1
  16. package/dist/constants/sns.js +22 -2
  17. package/dist/constants.d.ts +3 -0
  18. package/dist/constants.d.ts.map +1 -1
  19. package/dist/constants.js +4 -0
  20. package/dist/entity-attribute-assignment.d.ts +24 -0
  21. package/dist/entity-attribute-assignment.d.ts.map +1 -0
  22. package/dist/entity-attribute-assignment.js +36 -0
  23. package/dist/entity-attributes-constants.d.ts +7 -0
  24. package/dist/entity-attributes-constants.d.ts.map +1 -0
  25. package/dist/entity-attributes-constants.js +9 -0
  26. package/dist/errors/argument-error.d.ts +4 -0
  27. package/dist/errors/argument-error.d.ts.map +1 -0
  28. package/dist/errors/argument-error.js +11 -0
  29. package/dist/esm/attributions-service.d.ts.map +1 -1
  30. package/dist/esm/attributions-service.mjs +1 -0
  31. package/dist/esm/authorization-attributes-ms-service.d.ts +67 -0
  32. package/dist/esm/authorization-attributes-ms-service.d.ts.map +1 -0
  33. package/dist/esm/authorization-attributes-ms-service.mjs +254 -0
  34. package/dist/esm/authorization-attributes-sns-service.d.ts +85 -0
  35. package/dist/esm/authorization-attributes-sns-service.d.ts.map +1 -0
  36. package/dist/esm/authorization-attributes-sns-service.mjs +197 -0
  37. package/dist/esm/base-attribute-assignment.d.ts +18 -0
  38. package/dist/esm/base-attribute-assignment.d.ts.map +1 -0
  39. package/dist/esm/base-attribute-assignment.mjs +41 -0
  40. package/dist/esm/clients/graph-api.d.ts.map +1 -1
  41. package/dist/esm/clients/graph-api.mjs +2 -1
  42. package/dist/esm/constants/sns.d.ts +12 -2
  43. package/dist/esm/constants/sns.d.ts.map +1 -1
  44. package/dist/esm/constants/sns.mjs +17 -3
  45. package/dist/esm/constants.d.ts +3 -0
  46. package/dist/esm/constants.d.ts.map +1 -1
  47. package/dist/esm/constants.mjs +5 -1
  48. package/dist/esm/entity-attribute-assignment.d.ts +24 -0
  49. package/dist/esm/entity-attribute-assignment.d.ts.map +1 -0
  50. package/dist/esm/entity-attribute-assignment.mjs +34 -0
  51. package/dist/esm/entity-attributes-constants.d.ts +7 -0
  52. package/dist/esm/entity-attributes-constants.d.ts.map +1 -0
  53. package/dist/esm/entity-attributes-constants.mjs +7 -0
  54. package/dist/esm/errors/argument-error.d.ts +4 -0
  55. package/dist/esm/errors/argument-error.d.ts.map +1 -0
  56. package/dist/esm/errors/argument-error.mjs +9 -0
  57. package/dist/esm/index.d.ts +9 -1
  58. package/dist/esm/index.d.ts.map +1 -1
  59. package/dist/esm/index.mjs +7 -1
  60. package/dist/esm/resource-attribute-assignment.d.ts +24 -0
  61. package/dist/esm/resource-attribute-assignment.d.ts.map +1 -0
  62. package/dist/esm/resource-attribute-assignment.mjs +34 -0
  63. package/dist/esm/resource-attributes-constants.d.ts +25 -0
  64. package/dist/esm/resource-attributes-constants.d.ts.map +1 -0
  65. package/dist/esm/resource-attributes-constants.mjs +25 -0
  66. package/dist/esm/roles-service.d.ts +1 -1
  67. package/dist/esm/roles-service.mjs +1 -1
  68. package/dist/esm/types/authorization-attributes-contracts.d.ts +36 -4
  69. package/dist/esm/types/authorization-attributes-contracts.d.ts.map +1 -1
  70. package/dist/esm/types/authorization-attributes-contracts.mjs +6 -1
  71. package/dist/esm/utils/path-utils.d.ts +8 -0
  72. package/dist/esm/utils/path-utils.d.ts.map +1 -0
  73. package/dist/esm/utils/path-utils.mjs +15 -0
  74. package/dist/esm/utils/validation.d.ts +45 -0
  75. package/dist/esm/utils/validation.d.ts.map +1 -0
  76. package/dist/esm/utils/validation.mjs +75 -0
  77. package/dist/index.d.ts +9 -1
  78. package/dist/index.d.ts.map +1 -1
  79. package/dist/index.js +15 -2
  80. package/dist/resource-attribute-assignment.d.ts +24 -0
  81. package/dist/resource-attribute-assignment.d.ts.map +1 -0
  82. package/dist/resource-attribute-assignment.js +36 -0
  83. package/dist/resource-attributes-constants.d.ts +25 -0
  84. package/dist/resource-attributes-constants.d.ts.map +1 -0
  85. package/dist/resource-attributes-constants.js +28 -0
  86. package/dist/roles-service.d.ts +1 -1
  87. package/dist/roles-service.js +1 -1
  88. package/dist/types/authorization-attributes-contracts.d.ts +36 -4
  89. package/dist/types/authorization-attributes-contracts.d.ts.map +1 -1
  90. package/dist/types/authorization-attributes-contracts.js +5 -0
  91. package/dist/utils/path-utils.d.ts +8 -0
  92. package/dist/utils/path-utils.d.ts.map +1 -0
  93. package/dist/utils/path-utils.js +17 -0
  94. package/dist/utils/validation.d.ts +45 -0
  95. package/dist/utils/validation.d.ts.map +1 -0
  96. package/dist/utils/validation.js +77 -0
  97. package/package.json +2 -2
  98. package/src/attributions-service.ts +4 -0
  99. package/src/authorization-attributes-ms-service.ts +400 -0
  100. package/src/authorization-attributes-sns-service.ts +290 -0
  101. package/src/base-attribute-assignment.ts +56 -0
  102. package/src/clients/graph-api.ts +2 -1
  103. package/src/constants/sns.ts +19 -2
  104. package/src/constants.ts +4 -0
  105. package/src/entity-attribute-assignment.ts +43 -0
  106. package/src/entity-attributes-constants.ts +7 -0
  107. package/src/errors/argument-error.ts +7 -0
  108. package/src/index.ts +18 -1
  109. package/src/resource-attribute-assignment.ts +43 -0
  110. package/src/resource-attributes-constants.ts +26 -0
  111. package/src/roles-service.ts +1 -1
  112. package/src/types/authorization-attributes-contracts.ts +56 -3
  113. package/src/utils/path-utils.ts +14 -0
  114. package/src/utils/validation.ts +84 -0
  115. package/dist/authorization-attributes-service.d.ts +0 -54
  116. package/dist/authorization-attributes-service.d.ts.map +0 -1
  117. package/dist/authorization-attributes-service.js +0 -186
  118. package/dist/esm/authorization-attributes-service.d.ts +0 -54
  119. package/dist/esm/authorization-attributes-service.d.ts.map +0 -1
  120. package/dist/esm/authorization-attributes-service.mjs +0 -180
  121. package/src/authorization-attributes-service.ts +0 -234
@@ -1,234 +0,0 @@
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
- ResourceAttributesOperation,
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
-
22
- export class AuthorizationAttributesService {
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
- }
96
-
97
- /**
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.
103
- */
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: 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;
202
- }
203
- throw new Error('Unable to get sns topic arn from env variable');
204
- }
205
-
206
- /**
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
213
- */
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;
232
- }
233
- }
234
- }