@beesolve/aws-accounts 1.0.0
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/LICENSE +21 -0
- package/README.md +189 -0
- package/dist/accountCreation.js +135 -0
- package/dist/applyLogic.js +1203 -0
- package/dist/awsClientConfig.js +26 -0
- package/dist/awsConfig.js +1365 -0
- package/dist/cli.js +201 -0
- package/dist/commands/graveyard.js +46 -0
- package/dist/commands/regenerate.js +17 -0
- package/dist/commands/remote.js +925 -0
- package/dist/diff.js +1012 -0
- package/dist/error.js +66 -0
- package/dist/helpers.js +21 -0
- package/dist/lambda/handler.js +375 -0
- package/dist/lambdaClient.js +220 -0
- package/dist/logger.js +26 -0
- package/dist/operations.js +218 -0
- package/dist/remoteStateCache.js +38 -0
- package/dist/reservedOuDeletion.js +46 -0
- package/dist/scanLogic.js +456 -0
- package/dist/state.js +618 -0
- package/dist/tags.js +14 -0
- package/dist-lambda/handler.mjs +3558 -0
- package/dist-lambda/lambda.zip +0 -0
- package/package.json +59 -0
package/dist/state.js
ADDED
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import * as v from "valibot";
|
|
3
|
+
import { toRecordByProperty } from "./helpers.js";
|
|
4
|
+
const nonEmptyString = v.pipe(v.string(), v.minLength(1));
|
|
5
|
+
const principalTypeSchema = v.picklist(["GROUP", "USER"]);
|
|
6
|
+
const organizationalUnitSchema = v.strictObject({
|
|
7
|
+
id: nonEmptyString,
|
|
8
|
+
parentId: nonEmptyString,
|
|
9
|
+
arn: nonEmptyString,
|
|
10
|
+
name: nonEmptyString
|
|
11
|
+
});
|
|
12
|
+
const accountTagSchema = v.strictObject({
|
|
13
|
+
key: nonEmptyString,
|
|
14
|
+
value: v.string()
|
|
15
|
+
});
|
|
16
|
+
const accountSchema = v.strictObject({
|
|
17
|
+
id: nonEmptyString,
|
|
18
|
+
arn: nonEmptyString,
|
|
19
|
+
name: nonEmptyString,
|
|
20
|
+
email: nonEmptyString,
|
|
21
|
+
status: nonEmptyString,
|
|
22
|
+
parentId: nonEmptyString,
|
|
23
|
+
tags: v.array(accountTagSchema)
|
|
24
|
+
});
|
|
25
|
+
const userSchema = v.strictObject({
|
|
26
|
+
userId: nonEmptyString,
|
|
27
|
+
userName: nonEmptyString,
|
|
28
|
+
displayName: v.string(),
|
|
29
|
+
email: v.string()
|
|
30
|
+
});
|
|
31
|
+
const groupSchema = v.strictObject({
|
|
32
|
+
groupId: nonEmptyString,
|
|
33
|
+
displayName: nonEmptyString,
|
|
34
|
+
description: v.optional(v.string())
|
|
35
|
+
});
|
|
36
|
+
const groupMembershipSchema = v.strictObject({
|
|
37
|
+
membershipId: nonEmptyString,
|
|
38
|
+
groupId: nonEmptyString,
|
|
39
|
+
userId: nonEmptyString
|
|
40
|
+
});
|
|
41
|
+
const customerManagedPolicyReferenceSchema = v.strictObject({
|
|
42
|
+
name: nonEmptyString,
|
|
43
|
+
path: nonEmptyString
|
|
44
|
+
});
|
|
45
|
+
const permissionSetSchema = v.strictObject({
|
|
46
|
+
permissionSetArn: nonEmptyString,
|
|
47
|
+
name: nonEmptyString,
|
|
48
|
+
description: v.string(),
|
|
49
|
+
inlinePolicy: v.nullable(nonEmptyString),
|
|
50
|
+
awsManagedPolicies: v.array(nonEmptyString),
|
|
51
|
+
customerManagedPolicies: v.array(customerManagedPolicyReferenceSchema)
|
|
52
|
+
});
|
|
53
|
+
const accountAssignmentSchema = v.strictObject({
|
|
54
|
+
accountId: nonEmptyString,
|
|
55
|
+
permissionSetArn: nonEmptyString,
|
|
56
|
+
principalId: nonEmptyString,
|
|
57
|
+
principalType: principalTypeSchema
|
|
58
|
+
});
|
|
59
|
+
const accessRoleSchema = v.strictObject({
|
|
60
|
+
accountId: nonEmptyString,
|
|
61
|
+
permissionSetArn: nonEmptyString,
|
|
62
|
+
principalId: nonEmptyString,
|
|
63
|
+
principalType: principalTypeSchema,
|
|
64
|
+
roleName: nonEmptyString
|
|
65
|
+
});
|
|
66
|
+
const stateSchema = v.strictObject({
|
|
67
|
+
version: nonEmptyString,
|
|
68
|
+
generatedAt: nonEmptyString,
|
|
69
|
+
organization: v.strictObject({
|
|
70
|
+
rootId: nonEmptyString,
|
|
71
|
+
organizationalUnits: v.array(organizationalUnitSchema),
|
|
72
|
+
accounts: v.array(accountSchema)
|
|
73
|
+
}),
|
|
74
|
+
identityCenter: v.strictObject({
|
|
75
|
+
instanceArn: nonEmptyString,
|
|
76
|
+
identityStoreId: nonEmptyString,
|
|
77
|
+
users: v.array(userSchema),
|
|
78
|
+
groups: v.array(groupSchema),
|
|
79
|
+
groupMemberships: v.array(groupMembershipSchema),
|
|
80
|
+
permissionSets: v.array(permissionSetSchema),
|
|
81
|
+
accountAssignments: v.array(accountAssignmentSchema),
|
|
82
|
+
accessRoles: v.array(accessRoleSchema)
|
|
83
|
+
})
|
|
84
|
+
});
|
|
85
|
+
function validateState(value) {
|
|
86
|
+
return v.parse(stateSchema, value);
|
|
87
|
+
}
|
|
88
|
+
function createWorkingState(props) {
|
|
89
|
+
return {
|
|
90
|
+
version: props.state.version,
|
|
91
|
+
generatedAt: props.state.generatedAt,
|
|
92
|
+
organization: {
|
|
93
|
+
rootId: props.state.organization.rootId,
|
|
94
|
+
organizationalUnitsById: toRecordByProperty(
|
|
95
|
+
props.state.organization.organizationalUnits,
|
|
96
|
+
"id"
|
|
97
|
+
),
|
|
98
|
+
accountsById: toRecordByProperty(props.state.organization.accounts, "id"),
|
|
99
|
+
accountsByName: toRecordByProperty(
|
|
100
|
+
props.state.organization.accounts,
|
|
101
|
+
"name"
|
|
102
|
+
)
|
|
103
|
+
},
|
|
104
|
+
identityCenter: createWorkingIdentityCenterState({
|
|
105
|
+
identityCenter: props.state.identityCenter
|
|
106
|
+
})
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function materializeWorkingState(props) {
|
|
110
|
+
return {
|
|
111
|
+
version: props.workingState.version,
|
|
112
|
+
generatedAt: props.workingState.generatedAt,
|
|
113
|
+
organization: {
|
|
114
|
+
rootId: props.workingState.organization.rootId,
|
|
115
|
+
organizationalUnits: Object.values(
|
|
116
|
+
props.workingState.organization.organizationalUnitsById
|
|
117
|
+
),
|
|
118
|
+
accounts: Object.values(props.workingState.organization.accountsById)
|
|
119
|
+
},
|
|
120
|
+
identityCenter: {
|
|
121
|
+
instanceArn: props.workingState.identityCenter.instanceArn,
|
|
122
|
+
identityStoreId: props.workingState.identityCenter.identityStoreId,
|
|
123
|
+
users: structuredClone(props.workingState.identityCenter.users),
|
|
124
|
+
groups: structuredClone(props.workingState.identityCenter.groups),
|
|
125
|
+
groupMemberships: structuredClone(
|
|
126
|
+
props.workingState.identityCenter.groupMemberships
|
|
127
|
+
),
|
|
128
|
+
permissionSets: structuredClone(
|
|
129
|
+
props.workingState.identityCenter.permissionSets
|
|
130
|
+
),
|
|
131
|
+
accountAssignments: structuredClone(
|
|
132
|
+
props.workingState.identityCenter.accountAssignments
|
|
133
|
+
),
|
|
134
|
+
accessRoles: structuredClone(
|
|
135
|
+
props.workingState.identityCenter.accessRoles
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function moveAccountInWorkingState(props) {
|
|
141
|
+
const currentAccount = props.workingState.organization.accountsById[props.accountId];
|
|
142
|
+
if (currentAccount == null || currentAccount.parentId === props.parentId) {
|
|
143
|
+
return props.workingState;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
...props.workingState,
|
|
147
|
+
organization: {
|
|
148
|
+
...props.workingState.organization,
|
|
149
|
+
accountsById: {
|
|
150
|
+
...props.workingState.organization.accountsById,
|
|
151
|
+
[props.accountId]: {
|
|
152
|
+
...currentAccount,
|
|
153
|
+
parentId: props.parentId
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
accountsByName: {
|
|
157
|
+
...props.workingState.organization.accountsByName,
|
|
158
|
+
[currentAccount.name]: {
|
|
159
|
+
...currentAccount,
|
|
160
|
+
parentId: props.parentId
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function upsertAccountInWorkingState(props) {
|
|
167
|
+
const currentAccount = props.workingState.organization.accountsById[props.account.id];
|
|
168
|
+
if (currentAccount != null && currentAccount.id === props.account.id && currentAccount.arn === props.account.arn && currentAccount.name === props.account.name && currentAccount.email === props.account.email && currentAccount.status === props.account.status && currentAccount.parentId === props.account.parentId && JSON.stringify(normalizeAccountTags(currentAccount.tags)) === JSON.stringify(normalizeAccountTags(props.account.tags))) {
|
|
169
|
+
return props.workingState;
|
|
170
|
+
}
|
|
171
|
+
let accountsByName = {
|
|
172
|
+
...props.workingState.organization.accountsByName
|
|
173
|
+
};
|
|
174
|
+
if (currentAccount != null && currentAccount.name !== props.account.name) {
|
|
175
|
+
const { [currentAccount.name]: _removed, ...rest } = accountsByName;
|
|
176
|
+
accountsByName = rest;
|
|
177
|
+
}
|
|
178
|
+
accountsByName = {
|
|
179
|
+
...accountsByName,
|
|
180
|
+
[props.account.name]: props.account
|
|
181
|
+
};
|
|
182
|
+
return {
|
|
183
|
+
...props.workingState,
|
|
184
|
+
organization: {
|
|
185
|
+
...props.workingState.organization,
|
|
186
|
+
accountsById: {
|
|
187
|
+
...props.workingState.organization.accountsById,
|
|
188
|
+
[props.account.id]: props.account
|
|
189
|
+
},
|
|
190
|
+
accountsByName
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function upsertOrganizationalUnitInWorkingState(props) {
|
|
195
|
+
const currentOrganizationalUnit = props.workingState.organization.organizationalUnitsById[props.organizationalUnit.id];
|
|
196
|
+
if (currentOrganizationalUnit != null && currentOrganizationalUnit.id === props.organizationalUnit.id && currentOrganizationalUnit.parentId === props.organizationalUnit.parentId && currentOrganizationalUnit.arn === props.organizationalUnit.arn && currentOrganizationalUnit.name === props.organizationalUnit.name) {
|
|
197
|
+
return props.workingState;
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
...props.workingState,
|
|
201
|
+
organization: {
|
|
202
|
+
...props.workingState.organization,
|
|
203
|
+
organizationalUnitsById: {
|
|
204
|
+
...props.workingState.organization.organizationalUnitsById,
|
|
205
|
+
[props.organizationalUnit.id]: props.organizationalUnit
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function renameOrganizationalUnitInWorkingState(props) {
|
|
211
|
+
const currentOrganizationalUnit = props.workingState.organization.organizationalUnitsById[props.organizationalUnitId];
|
|
212
|
+
if (currentOrganizationalUnit == null || currentOrganizationalUnit.name === props.name) {
|
|
213
|
+
return props.workingState;
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
...props.workingState,
|
|
217
|
+
organization: {
|
|
218
|
+
...props.workingState.organization,
|
|
219
|
+
organizationalUnitsById: {
|
|
220
|
+
...props.workingState.organization.organizationalUnitsById,
|
|
221
|
+
[props.organizationalUnitId]: {
|
|
222
|
+
...currentOrganizationalUnit,
|
|
223
|
+
name: props.name
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function removeOrganizationalUnitFromWorkingState(props) {
|
|
230
|
+
if (props.workingState.organization.organizationalUnitsById[props.organizationalUnitId] == null) {
|
|
231
|
+
return props.workingState;
|
|
232
|
+
}
|
|
233
|
+
const nextOrganizationalUnitsById = {
|
|
234
|
+
...props.workingState.organization.organizationalUnitsById
|
|
235
|
+
};
|
|
236
|
+
delete nextOrganizationalUnitsById[props.organizationalUnitId];
|
|
237
|
+
return {
|
|
238
|
+
...props.workingState,
|
|
239
|
+
organization: {
|
|
240
|
+
...props.workingState.organization,
|
|
241
|
+
organizationalUnitsById: nextOrganizationalUnitsById
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
function createAccountAssignmentKey(props) {
|
|
246
|
+
return [
|
|
247
|
+
props.accountId,
|
|
248
|
+
props.permissionSetArn,
|
|
249
|
+
props.principalId,
|
|
250
|
+
props.principalType
|
|
251
|
+
].join("|");
|
|
252
|
+
}
|
|
253
|
+
function createGroupMembershipKey(props) {
|
|
254
|
+
return [props.groupId, props.userId].join("|");
|
|
255
|
+
}
|
|
256
|
+
function upsertIdcUserInWorkingState(props) {
|
|
257
|
+
const currentUser = props.workingState.identityCenter.usersByUserName[props.user.userName];
|
|
258
|
+
if (currentUser != null && currentUser.userId === props.user.userId && currentUser.displayName === props.user.displayName && currentUser.userName === props.user.userName && currentUser.email === props.user.email) {
|
|
259
|
+
return props.workingState;
|
|
260
|
+
}
|
|
261
|
+
const remainingUsers = props.workingState.identityCenter.users.filter(
|
|
262
|
+
(user) => user.userName !== props.user.userName
|
|
263
|
+
);
|
|
264
|
+
return {
|
|
265
|
+
...props.workingState,
|
|
266
|
+
identityCenter: createWorkingIdentityCenterState({
|
|
267
|
+
identityCenter: {
|
|
268
|
+
...materializeWorkingIdentityCenterState({
|
|
269
|
+
identityCenter: props.workingState.identityCenter
|
|
270
|
+
}),
|
|
271
|
+
users: [...remainingUsers, props.user]
|
|
272
|
+
}
|
|
273
|
+
})
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
function removeIdcUserFromWorkingState(props) {
|
|
277
|
+
const user = props.workingState.identityCenter.usersByUserName[props.userName];
|
|
278
|
+
if (user == null) {
|
|
279
|
+
return props.workingState;
|
|
280
|
+
}
|
|
281
|
+
return {
|
|
282
|
+
...props.workingState,
|
|
283
|
+
identityCenter: createWorkingIdentityCenterState({
|
|
284
|
+
identityCenter: {
|
|
285
|
+
...materializeWorkingIdentityCenterState({
|
|
286
|
+
identityCenter: props.workingState.identityCenter
|
|
287
|
+
}),
|
|
288
|
+
users: props.workingState.identityCenter.users.filter(
|
|
289
|
+
(currentUser) => currentUser.userName !== props.userName
|
|
290
|
+
),
|
|
291
|
+
groupMemberships: props.workingState.identityCenter.groupMemberships.filter(
|
|
292
|
+
(groupMembership) => groupMembership.userId !== user.userId
|
|
293
|
+
),
|
|
294
|
+
accountAssignments: props.workingState.identityCenter.accountAssignments.filter(
|
|
295
|
+
(accountAssignment) => accountAssignment.principalType !== "USER" || accountAssignment.principalId !== user.userId
|
|
296
|
+
)
|
|
297
|
+
}
|
|
298
|
+
})
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
function upsertIdcGroupInWorkingState(props) {
|
|
302
|
+
const currentGroup = props.workingState.identityCenter.groupsByDisplayName[props.group.displayName];
|
|
303
|
+
if (currentGroup != null && currentGroup.groupId === props.group.groupId && currentGroup.displayName === props.group.displayName && (currentGroup.description ?? "") === (props.group.description ?? "")) {
|
|
304
|
+
return props.workingState;
|
|
305
|
+
}
|
|
306
|
+
const remainingGroups = props.workingState.identityCenter.groups.filter(
|
|
307
|
+
(group) => group.displayName !== props.group.displayName
|
|
308
|
+
);
|
|
309
|
+
return {
|
|
310
|
+
...props.workingState,
|
|
311
|
+
identityCenter: createWorkingIdentityCenterState({
|
|
312
|
+
identityCenter: {
|
|
313
|
+
...materializeWorkingIdentityCenterState({
|
|
314
|
+
identityCenter: props.workingState.identityCenter
|
|
315
|
+
}),
|
|
316
|
+
groups: [...remainingGroups, props.group]
|
|
317
|
+
}
|
|
318
|
+
})
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function removeIdcGroupFromWorkingState(props) {
|
|
322
|
+
const group = props.workingState.identityCenter.groupsByDisplayName[props.groupDisplayName];
|
|
323
|
+
if (group == null) {
|
|
324
|
+
return props.workingState;
|
|
325
|
+
}
|
|
326
|
+
return {
|
|
327
|
+
...props.workingState,
|
|
328
|
+
identityCenter: createWorkingIdentityCenterState({
|
|
329
|
+
identityCenter: {
|
|
330
|
+
...materializeWorkingIdentityCenterState({
|
|
331
|
+
identityCenter: props.workingState.identityCenter
|
|
332
|
+
}),
|
|
333
|
+
groups: props.workingState.identityCenter.groups.filter(
|
|
334
|
+
(currentGroup) => currentGroup.displayName !== props.groupDisplayName
|
|
335
|
+
),
|
|
336
|
+
groupMemberships: props.workingState.identityCenter.groupMemberships.filter(
|
|
337
|
+
(groupMembership) => groupMembership.groupId !== group.groupId
|
|
338
|
+
),
|
|
339
|
+
accountAssignments: props.workingState.identityCenter.accountAssignments.filter(
|
|
340
|
+
(accountAssignment) => accountAssignment.principalType !== "GROUP" || accountAssignment.principalId !== group.groupId
|
|
341
|
+
)
|
|
342
|
+
}
|
|
343
|
+
})
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
function upsertIdcPermissionSetInWorkingState(props) {
|
|
347
|
+
const currentPermissionSet = props.workingState.identityCenter.permissionSetsByName[props.permissionSet.name];
|
|
348
|
+
if (currentPermissionSet != null && currentPermissionSet.permissionSetArn === props.permissionSet.permissionSetArn && currentPermissionSet.name === props.permissionSet.name && currentPermissionSet.description === props.permissionSet.description && currentPermissionSet.inlinePolicy === props.permissionSet.inlinePolicy && JSON.stringify(currentPermissionSet.awsManagedPolicies) === JSON.stringify(props.permissionSet.awsManagedPolicies) && JSON.stringify(currentPermissionSet.customerManagedPolicies) === JSON.stringify(props.permissionSet.customerManagedPolicies)) {
|
|
349
|
+
return props.workingState;
|
|
350
|
+
}
|
|
351
|
+
const remainingPermissionSets = props.workingState.identityCenter.permissionSets.filter(
|
|
352
|
+
(permissionSet) => permissionSet.name !== props.permissionSet.name
|
|
353
|
+
);
|
|
354
|
+
return {
|
|
355
|
+
...props.workingState,
|
|
356
|
+
identityCenter: createWorkingIdentityCenterState({
|
|
357
|
+
identityCenter: {
|
|
358
|
+
...materializeWorkingIdentityCenterState({
|
|
359
|
+
identityCenter: props.workingState.identityCenter
|
|
360
|
+
}),
|
|
361
|
+
permissionSets: [...remainingPermissionSets, props.permissionSet]
|
|
362
|
+
}
|
|
363
|
+
})
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
function removeIdcPermissionSetFromWorkingState(props) {
|
|
367
|
+
const permissionSet = props.workingState.identityCenter.permissionSetsByName[props.permissionSetName];
|
|
368
|
+
if (permissionSet == null) {
|
|
369
|
+
return props.workingState;
|
|
370
|
+
}
|
|
371
|
+
return {
|
|
372
|
+
...props.workingState,
|
|
373
|
+
identityCenter: createWorkingIdentityCenterState({
|
|
374
|
+
identityCenter: {
|
|
375
|
+
...materializeWorkingIdentityCenterState({
|
|
376
|
+
identityCenter: props.workingState.identityCenter
|
|
377
|
+
}),
|
|
378
|
+
permissionSets: props.workingState.identityCenter.permissionSets.filter(
|
|
379
|
+
(currentPermissionSet) => currentPermissionSet.name !== props.permissionSetName
|
|
380
|
+
),
|
|
381
|
+
accountAssignments: props.workingState.identityCenter.accountAssignments.filter(
|
|
382
|
+
(accountAssignment) => accountAssignment.permissionSetArn !== permissionSet.permissionSetArn
|
|
383
|
+
)
|
|
384
|
+
}
|
|
385
|
+
})
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
function addAccountAssignmentToWorkingState(props) {
|
|
389
|
+
const assignmentKey = createAccountAssignmentKey({
|
|
390
|
+
accountId: props.accountAssignment.accountId,
|
|
391
|
+
permissionSetArn: props.accountAssignment.permissionSetArn,
|
|
392
|
+
principalId: props.accountAssignment.principalId,
|
|
393
|
+
principalType: props.accountAssignment.principalType
|
|
394
|
+
});
|
|
395
|
+
if (props.workingState.identityCenter.accountAssignmentsByKey[assignmentKey] != null) {
|
|
396
|
+
return props.workingState;
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
...props.workingState,
|
|
400
|
+
identityCenter: createWorkingIdentityCenterState({
|
|
401
|
+
identityCenter: {
|
|
402
|
+
...materializeWorkingIdentityCenterState({
|
|
403
|
+
identityCenter: props.workingState.identityCenter
|
|
404
|
+
}),
|
|
405
|
+
accountAssignments: [
|
|
406
|
+
...props.workingState.identityCenter.accountAssignments,
|
|
407
|
+
props.accountAssignment
|
|
408
|
+
]
|
|
409
|
+
}
|
|
410
|
+
})
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
function addGroupMembershipToWorkingState(props) {
|
|
414
|
+
const membershipKey = createGroupMembershipKey({
|
|
415
|
+
groupId: props.groupMembership.groupId,
|
|
416
|
+
userId: props.groupMembership.userId
|
|
417
|
+
});
|
|
418
|
+
if (props.workingState.identityCenter.groupMembershipsByKey[membershipKey] != null) {
|
|
419
|
+
return props.workingState;
|
|
420
|
+
}
|
|
421
|
+
return {
|
|
422
|
+
...props.workingState,
|
|
423
|
+
identityCenter: createWorkingIdentityCenterState({
|
|
424
|
+
identityCenter: {
|
|
425
|
+
...materializeWorkingIdentityCenterState({
|
|
426
|
+
identityCenter: props.workingState.identityCenter
|
|
427
|
+
}),
|
|
428
|
+
groupMemberships: [
|
|
429
|
+
...props.workingState.identityCenter.groupMemberships,
|
|
430
|
+
props.groupMembership
|
|
431
|
+
]
|
|
432
|
+
}
|
|
433
|
+
})
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
function removeGroupMembershipFromWorkingState(props) {
|
|
437
|
+
const membershipKey = createGroupMembershipKey({
|
|
438
|
+
groupId: props.groupMembership.groupId,
|
|
439
|
+
userId: props.groupMembership.userId
|
|
440
|
+
});
|
|
441
|
+
if (props.workingState.identityCenter.groupMembershipsByKey[membershipKey] == null) {
|
|
442
|
+
return props.workingState;
|
|
443
|
+
}
|
|
444
|
+
return {
|
|
445
|
+
...props.workingState,
|
|
446
|
+
identityCenter: createWorkingIdentityCenterState({
|
|
447
|
+
identityCenter: {
|
|
448
|
+
...materializeWorkingIdentityCenterState({
|
|
449
|
+
identityCenter: props.workingState.identityCenter
|
|
450
|
+
}),
|
|
451
|
+
groupMemberships: props.workingState.identityCenter.groupMemberships.filter(
|
|
452
|
+
(groupMembership) => createGroupMembershipKey({
|
|
453
|
+
groupId: groupMembership.groupId,
|
|
454
|
+
userId: groupMembership.userId
|
|
455
|
+
}) !== membershipKey
|
|
456
|
+
)
|
|
457
|
+
}
|
|
458
|
+
})
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
function removeAccountAssignmentFromWorkingState(props) {
|
|
462
|
+
const assignmentKey = createAccountAssignmentKey({
|
|
463
|
+
accountId: props.accountAssignment.accountId,
|
|
464
|
+
permissionSetArn: props.accountAssignment.permissionSetArn,
|
|
465
|
+
principalId: props.accountAssignment.principalId,
|
|
466
|
+
principalType: props.accountAssignment.principalType
|
|
467
|
+
});
|
|
468
|
+
if (props.workingState.identityCenter.accountAssignmentsByKey[assignmentKey] == null) {
|
|
469
|
+
return props.workingState;
|
|
470
|
+
}
|
|
471
|
+
return {
|
|
472
|
+
...props.workingState,
|
|
473
|
+
identityCenter: createWorkingIdentityCenterState({
|
|
474
|
+
identityCenter: {
|
|
475
|
+
...materializeWorkingIdentityCenterState({
|
|
476
|
+
identityCenter: props.workingState.identityCenter
|
|
477
|
+
}),
|
|
478
|
+
accountAssignments: props.workingState.identityCenter.accountAssignments.filter(
|
|
479
|
+
(accountAssignment) => createAccountAssignmentKey({
|
|
480
|
+
accountId: accountAssignment.accountId,
|
|
481
|
+
permissionSetArn: accountAssignment.permissionSetArn,
|
|
482
|
+
principalId: accountAssignment.principalId,
|
|
483
|
+
principalType: accountAssignment.principalType
|
|
484
|
+
}) !== assignmentKey
|
|
485
|
+
)
|
|
486
|
+
}
|
|
487
|
+
})
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
async function readStateFile(path) {
|
|
491
|
+
const content = await readFile(path, "utf8");
|
|
492
|
+
const parsed = JSON.parse(content);
|
|
493
|
+
return validateState(parsed);
|
|
494
|
+
}
|
|
495
|
+
function createAccessRoleName(assignment) {
|
|
496
|
+
return `AWSReservedSSO_${assignment.permissionSetArn.split("/").at(-1) ?? "PermissionSet"}_${assignment.accountId}`;
|
|
497
|
+
}
|
|
498
|
+
function createWorkingIdentityCenterState(props) {
|
|
499
|
+
const users = structuredClone(props.identityCenter.users);
|
|
500
|
+
const groups = structuredClone(props.identityCenter.groups);
|
|
501
|
+
const groupMemberships = structuredClone(
|
|
502
|
+
props.identityCenter.groupMemberships
|
|
503
|
+
);
|
|
504
|
+
const permissionSets = structuredClone(props.identityCenter.permissionSets);
|
|
505
|
+
const accountAssignments = structuredClone(
|
|
506
|
+
props.identityCenter.accountAssignments
|
|
507
|
+
);
|
|
508
|
+
return {
|
|
509
|
+
instanceArn: props.identityCenter.instanceArn,
|
|
510
|
+
identityStoreId: props.identityCenter.identityStoreId,
|
|
511
|
+
users,
|
|
512
|
+
usersByUserName: toRecordByProperty(users, "userName"),
|
|
513
|
+
groups,
|
|
514
|
+
groupsByDisplayName: toRecordByProperty(groups, "displayName"),
|
|
515
|
+
groupMemberships,
|
|
516
|
+
groupMembershipsByKey: toRecordByProperty(
|
|
517
|
+
groupMemberships,
|
|
518
|
+
createGroupMembershipKey
|
|
519
|
+
),
|
|
520
|
+
permissionSets,
|
|
521
|
+
permissionSetsByName: toRecordByProperty(permissionSets, "name"),
|
|
522
|
+
accountAssignments,
|
|
523
|
+
accountAssignmentsByKey: toRecordByProperty(
|
|
524
|
+
accountAssignments,
|
|
525
|
+
createAccountAssignmentKey
|
|
526
|
+
),
|
|
527
|
+
accessRoles: createAccessRoles({
|
|
528
|
+
accountAssignments
|
|
529
|
+
})
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
function materializeWorkingIdentityCenterState(props) {
|
|
533
|
+
return {
|
|
534
|
+
instanceArn: props.identityCenter.instanceArn,
|
|
535
|
+
identityStoreId: props.identityCenter.identityStoreId,
|
|
536
|
+
users: structuredClone(props.identityCenter.users),
|
|
537
|
+
groups: structuredClone(props.identityCenter.groups),
|
|
538
|
+
groupMemberships: structuredClone(props.identityCenter.groupMemberships),
|
|
539
|
+
permissionSets: structuredClone(props.identityCenter.permissionSets),
|
|
540
|
+
accountAssignments: structuredClone(
|
|
541
|
+
props.identityCenter.accountAssignments
|
|
542
|
+
),
|
|
543
|
+
accessRoles: structuredClone(props.identityCenter.accessRoles)
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
function createAccessRoles(props) {
|
|
547
|
+
return props.accountAssignments.map((accountAssignment) => ({
|
|
548
|
+
accountId: accountAssignment.accountId,
|
|
549
|
+
permissionSetArn: accountAssignment.permissionSetArn,
|
|
550
|
+
principalId: accountAssignment.principalId,
|
|
551
|
+
principalType: accountAssignment.principalType,
|
|
552
|
+
roleName: createAccessRoleName(accountAssignment)
|
|
553
|
+
}));
|
|
554
|
+
}
|
|
555
|
+
function compareByKeys(...values) {
|
|
556
|
+
for (let index = 0; index < values.length; index += 2) {
|
|
557
|
+
const left = values[index] ?? "";
|
|
558
|
+
const right = values[index + 1] ?? "";
|
|
559
|
+
const compared = left.localeCompare(right);
|
|
560
|
+
if (compared !== 0) {
|
|
561
|
+
return compared;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return 0;
|
|
565
|
+
}
|
|
566
|
+
function normalizeInlinePolicyString(value) {
|
|
567
|
+
if (value == null) {
|
|
568
|
+
return null;
|
|
569
|
+
}
|
|
570
|
+
try {
|
|
571
|
+
return JSON.stringify(sortJsonValue(JSON.parse(value)));
|
|
572
|
+
} catch {
|
|
573
|
+
return value;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
function normalizeAccountTags(tags) {
|
|
577
|
+
if (tags == null || tags.length === 0) {
|
|
578
|
+
return [];
|
|
579
|
+
}
|
|
580
|
+
return [...tags].sort(
|
|
581
|
+
(left, right) => compareByKeys(left.key, right.key, left.value, right.value)
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
function sortJsonValue(value) {
|
|
585
|
+
if (Array.isArray(value)) {
|
|
586
|
+
return value.map((entry) => sortJsonValue(entry));
|
|
587
|
+
}
|
|
588
|
+
if (value != null && typeof value === "object") {
|
|
589
|
+
return Object.fromEntries(
|
|
590
|
+
Object.entries(value).sort(([leftKey], [rightKey]) => leftKey.localeCompare(rightKey)).map(([key, nestedValue]) => [key, sortJsonValue(nestedValue)])
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
return value;
|
|
594
|
+
}
|
|
595
|
+
export {
|
|
596
|
+
addAccountAssignmentToWorkingState,
|
|
597
|
+
addGroupMembershipToWorkingState,
|
|
598
|
+
createAccessRoleName,
|
|
599
|
+
createGroupMembershipKey,
|
|
600
|
+
createWorkingState,
|
|
601
|
+
materializeWorkingState,
|
|
602
|
+
moveAccountInWorkingState,
|
|
603
|
+
readStateFile,
|
|
604
|
+
removeAccountAssignmentFromWorkingState,
|
|
605
|
+
removeGroupMembershipFromWorkingState,
|
|
606
|
+
removeIdcGroupFromWorkingState,
|
|
607
|
+
removeIdcPermissionSetFromWorkingState,
|
|
608
|
+
removeIdcUserFromWorkingState,
|
|
609
|
+
removeOrganizationalUnitFromWorkingState,
|
|
610
|
+
renameOrganizationalUnitInWorkingState,
|
|
611
|
+
stateSchema,
|
|
612
|
+
upsertAccountInWorkingState,
|
|
613
|
+
upsertIdcGroupInWorkingState,
|
|
614
|
+
upsertIdcPermissionSetInWorkingState,
|
|
615
|
+
upsertIdcUserInWorkingState,
|
|
616
|
+
upsertOrganizationalUnitInWorkingState,
|
|
617
|
+
validateState
|
|
618
|
+
};
|
package/dist/tags.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const MANAGED_BY_TAG_VALUE = "beesolve-aws-accounts";
|
|
2
|
+
function getStandardTags(purpose) {
|
|
3
|
+
if (purpose === "") {
|
|
4
|
+
throw new Error("A non-empty purpose is required");
|
|
5
|
+
}
|
|
6
|
+
return [
|
|
7
|
+
{ Key: "ManagedBy", Value: MANAGED_BY_TAG_VALUE },
|
|
8
|
+
{ Key: "Purpose", Value: purpose }
|
|
9
|
+
];
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
MANAGED_BY_TAG_VALUE,
|
|
13
|
+
getStandardTags
|
|
14
|
+
};
|