@fishawack/lab-env 5.6.1 → 5.7.0-beta.10

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 (53) hide show
  1. package/CHANGELOG.md +136 -0
  2. package/_Test/boilerplate-adonis.js +1 -1
  3. package/_Test/boilerplate-craftcms.js +1 -1
  4. package/_Test/boilerplate-drupal.js +1 -1
  5. package/_Test/boilerplate-laravel.js +1 -1
  6. package/_Test/boilerplate-python.js +1 -1
  7. package/_Test/boilerplate-wordpress.js +1 -1
  8. package/_Test/boilerplate.js +1 -1
  9. package/_Test/provision.js +4 -9
  10. package/_Test/prune.js +8 -5
  11. package/bitbucket-pipelines.yml +2 -2
  12. package/cli.js +2 -0
  13. package/commands/create/cmds/dekey.js +13 -9
  14. package/commands/create/cmds/deprovision.js +66 -6
  15. package/commands/create/cmds/diagnose.js +10 -0
  16. package/commands/create/cmds/key.js +74 -41
  17. package/commands/create/cmds/provision.js +641 -22
  18. package/commands/create/cmds/rekey.js +283 -0
  19. package/commands/create/cmds/sync-secrets.js +198 -0
  20. package/commands/create/libs/output-credentials.js +59 -0
  21. package/commands/create/libs/parallel-runner.js +296 -0
  22. package/commands/create/libs/resolve-operator.js +59 -0
  23. package/commands/create/libs/utilities.js +75 -8
  24. package/commands/create/libs/vars.js +273 -377
  25. package/commands/create/services/aws/acm.js +61 -0
  26. package/commands/create/services/aws/cloudfront.js +51 -45
  27. package/commands/create/services/aws/ec2.js +781 -5
  28. package/commands/create/services/aws/elasticache.js +159 -0
  29. package/commands/create/services/aws/elasticbeanstalk.js +200 -48
  30. package/commands/create/services/aws/iam.js +430 -82
  31. package/commands/create/services/aws/index.js +1598 -100
  32. package/commands/create/services/aws/opensearch.js +155 -0
  33. package/commands/create/services/aws/rds.js +272 -0
  34. package/commands/create/services/aws/s3.js +52 -31
  35. package/commands/create/services/aws/secretsmanager.js +82 -0
  36. package/commands/create/services/aws/ses.js +203 -0
  37. package/commands/create/templates/elasticbeanstalk/.ebextensions/laravel/cron.config +45 -0
  38. package/commands/create/templates/elasticbeanstalk/.ebextensions/laravel/queue-worker.config +29 -0
  39. package/commands/create/templates/elasticbeanstalk/.ebextensions/laravel/rds-ssl.config +5 -0
  40. package/commands/create/templates/elasticbeanstalk/.ebextensions/laravel/software.config +12 -0
  41. package/commands/create/templates/elasticbeanstalk/.ebextensions/misc/setvars.config +1 -0
  42. package/commands/create/templates/elasticbeanstalk/.platform/confighooks/postdeploy/restart_queue.sh +4 -0
  43. package/commands/create/templates/elasticbeanstalk/.platform/confighooks/postdeploy/rewrite-flush.sh +1 -1
  44. package/commands/create/templates/elasticbeanstalk/.platform/confighooks/prebuild/setvars.sh +19 -0
  45. package/commands/create/templates/elasticbeanstalk/.platform/hooks/postdeploy/restart_queue.sh +4 -0
  46. package/commands/create/templates/elasticbeanstalk/.platform/hooks/prebuild/setvars.sh +19 -0
  47. package/commands/scan.js +1 -1
  48. package/core/1/CHANGELOG.md +41 -0
  49. package/core/1/Dockerfile +3 -3
  50. package/core/1/bitbucket-pipelines.yml +5 -5
  51. package/core/1/package.json +1 -1
  52. package/globals.js +4 -0
  53. package/package.json +9 -2
@@ -9,25 +9,35 @@ const {
9
9
  CreateAccessKeyCommand,
10
10
  DeleteAccessKeyCommand,
11
11
  ListAccessKeysCommand,
12
+ AddUserToGroupCommand,
12
13
  GetRoleCommand,
13
14
  CreateRoleCommand,
15
+ DeleteRoleCommand,
14
16
  AttachRolePolicyCommand,
17
+ DetachRolePolicyCommand,
18
+ ListAttachedRolePoliciesCommand,
15
19
  CreateInstanceProfileCommand,
20
+ DeleteInstanceProfileCommand,
16
21
  AddRoleToInstanceProfileCommand,
22
+ RemoveRoleFromInstanceProfileCommand,
17
23
  PutRolePolicyCommand,
24
+ DeleteRolePolicyCommand,
25
+ ListRolePoliciesCommand,
26
+ CreateServiceLinkedRoleCommand,
18
27
  } = require("@aws-sdk/client-iam");
19
28
  const { Spinner } = require("../../libs/utilities");
29
+ const { awsClientConfig } = require("../../libs/vars");
20
30
 
21
- module.exports.createIAMUser = async (UserName, tags = []) => {
22
- const client = new IAMClient({});
31
+ const getClient = () => new IAMClient(awsClientConfig);
23
32
 
33
+ module.exports.createIAMUser = async (UserName, tags = []) => {
24
34
  let res;
25
35
 
26
36
  try {
27
37
  res = await Spinner.prototype.simple(
28
38
  `Creating IAM user ${UserName}`,
29
39
  () => {
30
- return client.send(
40
+ return getClient().send(
31
41
  new CreateUserCommand({
32
42
  UserName,
33
43
  Tags: [
@@ -41,7 +51,7 @@ module.exports.createIAMUser = async (UserName, tags = []) => {
41
51
  res = await Spinner.prototype.simple(
42
52
  `Retrieving the already existing IAM user ${UserName}`,
43
53
  () => {
44
- return client.send(new GetUserCommand({ UserName }));
54
+ return getClient().send(new GetUserCommand({ UserName }));
45
55
  },
46
56
  );
47
57
  }
@@ -59,16 +69,25 @@ module.exports.createFWIAMUser = async (UserName, permissions) => {
59
69
  return res;
60
70
  };
61
71
 
62
- module.exports.removeIAMUser = async (UserName) => {
63
- const client = new IAMClient({});
72
+ module.exports.addUserToGroup = async (UserName, GroupName, iamClient) => {
73
+ const c = iamClient || getClient();
74
+
75
+ await Spinner.prototype.simple(
76
+ `Adding IAM user ${UserName} to group ${GroupName}`,
77
+ () => {
78
+ return c.send(new AddUserToGroupCommand({ UserName, GroupName }));
79
+ },
80
+ );
81
+ };
64
82
 
83
+ module.exports.removeIAMUser = async (UserName) => {
65
84
  let res;
66
85
 
67
86
  try {
68
87
  await Spinner.prototype.simple(
69
88
  `Checking if IAM user ${UserName} exists`,
70
89
  () => {
71
- return client.send(new GetUserCommand({ UserName }));
90
+ return getClient().send(new GetUserCommand({ UserName }));
72
91
  },
73
92
  );
74
93
 
@@ -79,7 +98,7 @@ module.exports.removeIAMUser = async (UserName) => {
79
98
  res = await Spinner.prototype.simple(
80
99
  `Removing IAM user ${UserName}`,
81
100
  () => {
82
- return client.send(new DeleteUserCommand({ UserName }));
101
+ return getClient().send(new DeleteUserCommand({ UserName }));
83
102
  },
84
103
  );
85
104
  } catch {
@@ -90,12 +109,10 @@ module.exports.removeIAMUser = async (UserName) => {
90
109
  };
91
110
 
92
111
  module.exports.attachIAMPolicy = async (UserName, policy) => {
93
- const client = new IAMClient({});
94
-
95
112
  let res = await Spinner.prototype.simple(
96
113
  `Attaching IAM policy ${policy}`,
97
114
  () => {
98
- return client.send(
115
+ return getClient().send(
99
116
  new AttachUserPolicyCommand({ UserName, PolicyArn: policy }),
100
117
  );
101
118
  },
@@ -107,63 +124,84 @@ module.exports.attachIAMPolicy = async (UserName, policy) => {
107
124
  module.exports.syncFWIAMPolicies = async (
108
125
  UserName,
109
126
  permissions = ["auto-content", "deploy-static"],
127
+ preserve = [],
110
128
  ) => {
111
- await module.exports.removeAllIAMPolicies(UserName);
129
+ let res = await module.exports.listIAMPolicies(UserName);
130
+
131
+ // Detach existing policies in parallel
132
+ const toDetach = res.AttachedPolicies.filter(
133
+ (p) => !preserve.includes(p.PolicyArn),
134
+ );
135
+ if (toDetach.length) {
136
+ await Spinner.prototype.simple(
137
+ `Detaching ${toDetach.length} IAM policies`,
138
+ () =>
139
+ Promise.all(
140
+ toDetach.map((p) =>
141
+ getClient().send(
142
+ new DetachUserPolicyCommand({
143
+ UserName,
144
+ PolicyArn: p.PolicyArn,
145
+ }),
146
+ ),
147
+ ),
148
+ ),
149
+ );
150
+ }
151
+
152
+ // Collect policies to attach
153
+ const policies = [];
112
154
 
113
155
  if (
114
156
  permissions.includes("auto-content") ||
115
157
  permissions.includes("deploy-static")
116
158
  ) {
117
- await module.exports.attachIAMPolicy(
118
- UserName,
119
- "arn:aws:iam::aws:policy/AmazonS3FullAccess",
120
- );
159
+ policies.push("arn:aws:iam::aws:policy/AmazonS3FullAccess");
121
160
  }
122
161
 
123
162
  if (permissions.includes("deploy-static")) {
124
- await module.exports.attachIAMPolicy(
125
- UserName,
126
- "arn:aws:iam::aws:policy/CloudFrontFullAccess",
127
- );
163
+ policies.push("arn:aws:iam::aws:policy/CloudFrontFullAccess");
128
164
  }
129
165
 
130
166
  if (permissions.includes("deploy-fullstack")) {
131
- await module.exports.attachIAMPolicy(
132
- UserName,
167
+ policies.push(
133
168
  "arn:aws:iam::aws:policy/AdministratorAccess-AWSElasticBeanstalk",
134
- );
135
-
136
- await module.exports.attachIAMPolicy(
137
- UserName,
138
169
  "arn:aws:iam::aws:policy/AmazonOpenSearchServiceFullAccess",
139
- );
140
-
141
- await module.exports.attachIAMPolicy(
142
- UserName,
143
170
  "arn:aws:iam::aws:policy/AmazonRDSFullAccess",
144
- );
145
-
146
- await module.exports.attachIAMPolicy(
147
- UserName,
148
171
  "arn:aws:iam::aws:policy/AmazonElastiCacheFullAccess",
172
+ "arn:aws:iam::aws:policy/SecretsManagerReadWrite",
173
+ "arn:aws:iam::aws:policy/AmazonSESFullAccess",
149
174
  );
150
175
  }
151
176
 
152
177
  if (permissions.includes("manage-users")) {
153
- await module.exports.attachIAMPolicy(
154
- UserName,
155
- "arn:aws:iam::aws:policy/IAMFullAccess",
178
+ policies.push("arn:aws:iam::aws:policy/IAMFullAccess");
179
+ }
180
+
181
+ // Attach all policies in parallel
182
+ if (policies.length) {
183
+ await Spinner.prototype.simple(
184
+ `Attaching ${policies.length} IAM policies`,
185
+ () =>
186
+ Promise.all(
187
+ policies.map((p) =>
188
+ getClient().send(
189
+ new AttachUserPolicyCommand({
190
+ UserName,
191
+ PolicyArn: p,
192
+ }),
193
+ ),
194
+ ),
195
+ ),
156
196
  );
157
197
  }
158
198
  };
159
199
 
160
200
  module.exports.removeIAMPolicy = async (UserName, policy) => {
161
- const client = new IAMClient({});
162
-
163
201
  let res = await Spinner.prototype.simple(
164
202
  `Detaching IAM policy ${policy}`,
165
203
  () => {
166
- return client.send(
204
+ return getClient().send(
167
205
  new DetachUserPolicyCommand({ UserName, PolicyArn: policy }),
168
206
  );
169
207
  },
@@ -173,10 +211,10 @@ module.exports.removeIAMPolicy = async (UserName, policy) => {
173
211
  };
174
212
 
175
213
  module.exports.listIAMPolicies = async (UserName) => {
176
- const client = new IAMClient({});
177
-
178
214
  let res = await Spinner.prototype.simple(`Listing IAM policies`, () => {
179
- return client.send(new ListAttachedUserPoliciesCommand({ UserName }));
215
+ return getClient().send(
216
+ new ListAttachedUserPoliciesCommand({ UserName }),
217
+ );
180
218
  });
181
219
 
182
220
  return res;
@@ -185,10 +223,20 @@ module.exports.listIAMPolicies = async (UserName) => {
185
223
  module.exports.removeAllIAMPolicies = async (UserName) => {
186
224
  let res = await module.exports.listIAMPolicies(UserName);
187
225
 
188
- for (let i = 0; i < res.AttachedPolicies.length; i++) {
189
- await module.exports.removeIAMPolicy(
190
- UserName,
191
- res.AttachedPolicies[i].PolicyArn,
226
+ if (res.AttachedPolicies.length) {
227
+ await Spinner.prototype.simple(
228
+ `Detaching ${res.AttachedPolicies.length} IAM policies`,
229
+ () =>
230
+ Promise.all(
231
+ res.AttachedPolicies.map((p) =>
232
+ getClient().send(
233
+ new DetachUserPolicyCommand({
234
+ UserName,
235
+ PolicyArn: p.PolicyArn,
236
+ }),
237
+ ),
238
+ ),
239
+ ),
192
240
  );
193
241
  }
194
242
 
@@ -196,22 +244,18 @@ module.exports.removeAllIAMPolicies = async (UserName) => {
196
244
  };
197
245
 
198
246
  module.exports.createAccessKey = async (UserName) => {
199
- const client = new IAMClient({});
200
-
201
247
  let res = await Spinner.prototype.simple(`Creating access key`, () => {
202
- return client.send(new CreateAccessKeyCommand({ UserName }));
248
+ return getClient().send(new CreateAccessKeyCommand({ UserName }));
203
249
  });
204
250
 
205
251
  return res;
206
252
  };
207
253
 
208
254
  module.exports.removeAccessKey = async (UserName, AccessKeyId) => {
209
- const client = new IAMClient({});
210
-
211
255
  let res = await Spinner.prototype.simple(
212
256
  `Removing access key ${AccessKeyId}`,
213
257
  () => {
214
- return client.send(
258
+ return getClient().send(
215
259
  new DeleteAccessKeyCommand({ AccessKeyId, UserName }),
216
260
  );
217
261
  },
@@ -221,10 +265,8 @@ module.exports.removeAccessKey = async (UserName, AccessKeyId) => {
221
265
  };
222
266
 
223
267
  module.exports.listAccessKeys = async (UserName) => {
224
- const client = new IAMClient({});
225
-
226
268
  let res = await Spinner.prototype.simple(`Listing access keys`, () => {
227
- return client.send(new ListAccessKeysCommand({ UserName }));
269
+ return getClient().send(new ListAccessKeysCommand({ UserName }));
228
270
  });
229
271
 
230
272
  return res;
@@ -233,10 +275,20 @@ module.exports.listAccessKeys = async (UserName) => {
233
275
  module.exports.removeAllAccessKeys = async (UserName) => {
234
276
  let res = await module.exports.listAccessKeys(UserName);
235
277
 
236
- for (let i = 0; i < res.AccessKeyMetadata.length; i++) {
237
- await module.exports.removeAccessKey(
238
- UserName,
239
- res.AccessKeyMetadata[i].AccessKeyId,
278
+ if (res.AccessKeyMetadata.length) {
279
+ await Spinner.prototype.simple(
280
+ `Removing ${res.AccessKeyMetadata.length} access keys`,
281
+ () =>
282
+ Promise.all(
283
+ res.AccessKeyMetadata.map((k) =>
284
+ getClient().send(
285
+ new DeleteAccessKeyCommand({
286
+ UserName,
287
+ AccessKeyId: k.AccessKeyId,
288
+ }),
289
+ ),
290
+ ),
291
+ ),
240
292
  );
241
293
  }
242
294
 
@@ -253,27 +305,72 @@ module.exports.createAccessKeySafe = async (UserName) => {
253
305
  return res;
254
306
  };
255
307
 
256
- module.exports.getRole = async (RoleName) => {
257
- const client = new IAMClient({});
308
+ module.exports.rotateAccessKey = async (UserName) => {
309
+ let res = await module.exports.listAccessKeys(UserName);
310
+ let keys = res.AccessKeyMetadata;
311
+
312
+ if (keys.length === 2) {
313
+ console.log(
314
+ `\n⚠ ${UserName} has 2 existing keys (likely from a failed previous rotation). Removing the older key to make room.\n`,
315
+ );
316
+
317
+ keys.sort((a, b) => new Date(a.CreateDate) - new Date(b.CreateDate));
318
+
319
+ await module.exports.removeAccessKey(UserName, keys[0].AccessKeyId);
320
+
321
+ keys = [keys[1]];
322
+ }
323
+
324
+ let newKey = await module.exports.createAccessKey(UserName);
258
325
 
326
+ if (keys.length === 1) {
327
+ await module.exports.removeAccessKey(UserName, keys[0].AccessKeyId);
328
+ }
329
+
330
+ return newKey;
331
+ };
332
+
333
+ module.exports.rotateFWIAMUser = async (UserName, permissions) => {
334
+ await module.exports.createIAMUser(UserName);
335
+
336
+ let preserve = [];
337
+
338
+ if (permissions && permissions.includes("manage-users")) {
339
+ preserve.push("arn:aws:iam::aws:policy/IAMFullAccess");
340
+ }
341
+
342
+ await module.exports.syncFWIAMPolicies(UserName, permissions, preserve);
343
+
344
+ let res = await module.exports.rotateAccessKey(UserName);
345
+
346
+ return res;
347
+ };
348
+
349
+ module.exports.getRole = async (RoleName) => {
259
350
  let res = await Spinner.prototype.simple(
260
351
  `Retrieving the role ${RoleName}`,
261
352
  () => {
262
- return client.send(new GetRoleCommand({ RoleName }));
353
+ return getClient().send(new GetRoleCommand({ RoleName }));
263
354
  },
264
355
  );
265
356
 
266
357
  return res;
267
358
  };
268
359
 
269
- module.exports.createRole = async (RoleName, AssumeRolePolicyDocument) => {
270
- const client = new IAMClient({});
271
-
360
+ module.exports.createRole = async (
361
+ RoleName,
362
+ AssumeRolePolicyDocument,
363
+ Tags,
364
+ ) => {
272
365
  let res = await Spinner.prototype.simple(
273
366
  `Creating the role ${RoleName}`,
274
367
  () => {
275
- return client.send(
276
- new CreateRoleCommand({ RoleName, AssumeRolePolicyDocument }),
368
+ return getClient().send(
369
+ new CreateRoleCommand({
370
+ RoleName,
371
+ AssumeRolePolicyDocument,
372
+ ...(Tags && Tags.length ? { Tags } : {}),
373
+ }),
277
374
  );
278
375
  },
279
376
  );
@@ -281,14 +378,15 @@ module.exports.createRole = async (RoleName, AssumeRolePolicyDocument) => {
281
378
  return res;
282
379
  };
283
380
 
284
- module.exports.createInstanceProfile = async (InstanceProfileName) => {
285
- const client = new IAMClient({});
286
-
381
+ module.exports.createInstanceProfile = async (InstanceProfileName, Tags) => {
287
382
  let res = await Spinner.prototype.simple(
288
383
  `Creating the instance profile ${InstanceProfileName}`,
289
384
  () => {
290
- return client.send(
291
- new CreateInstanceProfileCommand({ InstanceProfileName }),
385
+ return getClient().send(
386
+ new CreateInstanceProfileCommand({
387
+ InstanceProfileName,
388
+ ...(Tags && Tags.length ? { Tags } : {}),
389
+ }),
292
390
  );
293
391
  },
294
392
  );
@@ -300,12 +398,10 @@ module.exports.attachRoleToInstanceProfile = async (
300
398
  RoleName,
301
399
  InstanceProfileName,
302
400
  ) => {
303
- const client = new IAMClient({});
304
-
305
401
  let res = await Spinner.prototype.simple(
306
402
  `Attaching role ${RoleName} to the instance profile ${InstanceProfileName}`,
307
403
  () => {
308
- return client.send(
404
+ return getClient().send(
309
405
  new AddRoleToInstanceProfileCommand({
310
406
  RoleName,
311
407
  InstanceProfileName,
@@ -318,12 +414,10 @@ module.exports.attachRoleToInstanceProfile = async (
318
414
  };
319
415
 
320
416
  module.exports.attachRolePolicy = async (RoleName, PolicyArn) => {
321
- const client = new IAMClient({});
322
-
323
417
  let res = await Spinner.prototype.simple(
324
418
  `Attaching Role policy ${PolicyArn}`,
325
419
  () => {
326
- return client.send(
420
+ return getClient().send(
327
421
  new AttachRolePolicyCommand({ RoleName, PolicyArn }),
328
422
  );
329
423
  },
@@ -337,12 +431,10 @@ module.exports.attachInlineRolePolicy = async (
337
431
  PolicyName,
338
432
  PolicyDocument,
339
433
  ) => {
340
- const client = new IAMClient({});
341
-
342
434
  let res = await Spinner.prototype.simple(
343
435
  `Attaching Inline Role policy ${PolicyName}`,
344
436
  () => {
345
- return client.send(
437
+ return getClient().send(
346
438
  new PutRolePolicyCommand({
347
439
  RoleName,
348
440
  PolicyName,
@@ -393,6 +485,14 @@ module.exports.ensureEBInstanceProfileExists = async () => {
393
485
  role,
394
486
  "arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker",
395
487
  );
488
+ await module.exports.attachRolePolicy(
489
+ role,
490
+ "arn:aws:iam::aws:policy/AWSSecretsManagerClientReadOnlyAccess",
491
+ );
492
+ await module.exports.attachRolePolicy(
493
+ role,
494
+ "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore",
495
+ );
396
496
  await module.exports.attachInlineRolePolicy(
397
497
  role,
398
498
  "lab-env-elasticbeanstalk-describe-env",
@@ -465,6 +565,11 @@ module.exports.ensureEBManagedUpdateProfileExists = async () => {
465
565
  "arn:aws:iam::aws:policy/AWSElasticBeanstalkManagedUpdatesCustomerRolePolicy",
466
566
  );
467
567
 
568
+ await module.exports.attachRolePolicy(
569
+ role,
570
+ "arn:aws:iam::aws:policy/AWSSecretsManagerClientReadOnlyAccess",
571
+ );
572
+
468
573
  await module.exports.attachInlineRolePolicy(
469
574
  role,
470
575
  "lab-env-elasticbeanstalk-custom-managed-updates-permissions",
@@ -490,3 +595,246 @@ module.exports.ensureEBManagedUpdateProfileExists = async () => {
490
595
  }),
491
596
  );
492
597
  };
598
+
599
+ module.exports.ensureEnvironmentInstanceProfile = async (
600
+ ebSlug,
601
+ s3BucketName,
602
+ opensearchDomainArn,
603
+ tags,
604
+ ) => {
605
+ const role = `${ebSlug}-role`;
606
+
607
+ try {
608
+ await module.exports.getRole(role);
609
+ } catch {
610
+ await module.exports.createRole(
611
+ role,
612
+ JSON.stringify({
613
+ Version: "2012-10-17",
614
+ Statement: [
615
+ {
616
+ Effect: "Allow",
617
+ Action: ["sts:AssumeRole"],
618
+ Principal: {
619
+ Service: ["ec2.amazonaws.com"],
620
+ },
621
+ },
622
+ ],
623
+ }),
624
+ tags,
625
+ );
626
+
627
+ await module.exports.createInstanceProfile(role, tags);
628
+ await module.exports.attachRoleToInstanceProfile(role, role);
629
+ }
630
+
631
+ await module.exports.attachRolePolicy(
632
+ role,
633
+ "arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier",
634
+ );
635
+ await module.exports.attachRolePolicy(
636
+ role,
637
+ "arn:aws:iam::aws:policy/AWSElasticBeanstalkWorkerTier",
638
+ );
639
+ await module.exports.attachRolePolicy(
640
+ role,
641
+ "arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker",
642
+ );
643
+ await module.exports.attachRolePolicy(
644
+ role,
645
+ "arn:aws:iam::aws:policy/AWSSecretsManagerClientReadOnlyAccess",
646
+ );
647
+ await module.exports.attachRolePolicy(
648
+ role,
649
+ "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore",
650
+ );
651
+ await module.exports.attachInlineRolePolicy(
652
+ role,
653
+ "lab-env-elasticbeanstalk-describe-env",
654
+ JSON.stringify({
655
+ Version: "2012-10-17",
656
+ Statement: [
657
+ {
658
+ Sid: "VisualEditor0",
659
+ Effect: "Allow",
660
+ Action: [
661
+ "ec2:DescribeTags",
662
+ "autoscaling:DescribeAutoScalingGroups",
663
+ "elasticbeanstalk:DescribeEvents",
664
+ ],
665
+ Resource: "*",
666
+ },
667
+ ],
668
+ }),
669
+ );
670
+
671
+ if (s3BucketName) {
672
+ await module.exports.attachInlineRolePolicy(
673
+ role,
674
+ "lab-env-s3-bucket-access",
675
+ JSON.stringify({
676
+ Version: "2012-10-17",
677
+ Statement: [
678
+ {
679
+ Sid: "S3BucketAccess",
680
+ Effect: "Allow",
681
+ Action: [
682
+ "s3:GetObject",
683
+ "s3:PutObject",
684
+ "s3:DeleteObject",
685
+ ],
686
+ Resource: `arn:aws:s3:::${s3BucketName}/*`,
687
+ },
688
+ {
689
+ Sid: "S3BucketList",
690
+ Effect: "Allow",
691
+ Action: ["s3:ListBucket", "s3:GetBucketLocation"],
692
+ Resource: `arn:aws:s3:::${s3BucketName}`,
693
+ },
694
+ ],
695
+ }),
696
+ );
697
+ }
698
+
699
+ if (opensearchDomainArn) {
700
+ await module.exports.attachInlineRolePolicy(
701
+ role,
702
+ "lab-env-opensearch-access",
703
+ JSON.stringify({
704
+ Version: "2012-10-17",
705
+ Statement: [
706
+ {
707
+ Sid: "OpenSearchAccess",
708
+ Effect: "Allow",
709
+ Action: [
710
+ "es:ESHttpGet",
711
+ "es:ESHttpPost",
712
+ "es:ESHttpPut",
713
+ "es:ESHttpDelete",
714
+ "es:ESHttpHead",
715
+ "es:ESHttpPatch",
716
+ ],
717
+ Resource: `${opensearchDomainArn}/*`,
718
+ },
719
+ ],
720
+ }),
721
+ );
722
+ }
723
+
724
+ return role;
725
+ };
726
+
727
+ module.exports.ensureOpenSearchServiceRole = async () => {
728
+ try {
729
+ await Spinner.prototype.simple(
730
+ "Ensuring OpenSearch service-linked role exists",
731
+ () => {
732
+ return getClient().send(
733
+ new CreateServiceLinkedRoleCommand({
734
+ AWSServiceName: "opensearchservice.amazonaws.com",
735
+ }),
736
+ );
737
+ },
738
+ );
739
+ } catch (e) {
740
+ if (
741
+ e.Code !== "InvalidInput" &&
742
+ e.name !== "InvalidInputException" &&
743
+ e.name !== "InvalidInput"
744
+ ) {
745
+ throw e;
746
+ }
747
+ // Role already exists — safe to continue
748
+ }
749
+ };
750
+
751
+ module.exports.removeEnvironmentInstanceProfile = async (ebSlug) => {
752
+ const role = `${ebSlug}-role`;
753
+
754
+ try {
755
+ await module.exports.getRole(role);
756
+ } catch {
757
+ return;
758
+ }
759
+
760
+ const attachedPolicies = await Spinner.prototype.simple(
761
+ `Listing attached policies for ${role}`,
762
+ () => {
763
+ return getClient().send(
764
+ new ListAttachedRolePoliciesCommand({ RoleName: role }),
765
+ );
766
+ },
767
+ );
768
+
769
+ for (const policy of attachedPolicies.AttachedPolicies || []) {
770
+ await Spinner.prototype.simple(
771
+ `Detaching policy ${policy.PolicyName}`,
772
+ () => {
773
+ return getClient().send(
774
+ new DetachRolePolicyCommand({
775
+ RoleName: role,
776
+ PolicyArn: policy.PolicyArn,
777
+ }),
778
+ );
779
+ },
780
+ );
781
+ }
782
+
783
+ const inlinePolicies = await Spinner.prototype.simple(
784
+ `Listing inline policies for ${role}`,
785
+ () => {
786
+ return getClient().send(
787
+ new ListRolePoliciesCommand({ RoleName: role }),
788
+ );
789
+ },
790
+ );
791
+
792
+ for (const policyName of inlinePolicies.PolicyNames || []) {
793
+ await Spinner.prototype.simple(
794
+ `Deleting inline policy ${policyName}`,
795
+ () => {
796
+ return getClient().send(
797
+ new DeleteRolePolicyCommand({
798
+ RoleName: role,
799
+ PolicyName: policyName,
800
+ }),
801
+ );
802
+ },
803
+ );
804
+ }
805
+
806
+ try {
807
+ await Spinner.prototype.simple(
808
+ `Removing role from instance profile ${role}`,
809
+ () => {
810
+ return getClient().send(
811
+ new RemoveRoleFromInstanceProfileCommand({
812
+ RoleName: role,
813
+ InstanceProfileName: role,
814
+ }),
815
+ );
816
+ },
817
+ );
818
+ } catch {
819
+ /* empty */
820
+ }
821
+
822
+ try {
823
+ await Spinner.prototype.simple(
824
+ `Deleting instance profile ${role}`,
825
+ () => {
826
+ return getClient().send(
827
+ new DeleteInstanceProfileCommand({
828
+ InstanceProfileName: role,
829
+ }),
830
+ );
831
+ },
832
+ );
833
+ } catch {
834
+ /* empty */
835
+ }
836
+
837
+ await Spinner.prototype.simple(`Deleting role ${role}`, () => {
838
+ return getClient().send(new DeleteRoleCommand({ RoleName: role }));
839
+ });
840
+ };