@fishawack/lab-env 5.7.0-beta.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 (52) hide show
  1. package/CHANGELOG.md +117 -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 +1 -0
  13. package/commands/create/cmds/dekey.js +13 -9
  14. package/commands/create/cmds/deprovision.js +36 -0
  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 +573 -69
  18. package/commands/create/cmds/rekey.js +283 -0
  19. package/commands/create/cmds/sync-secrets.js +2 -1
  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 +124 -143
  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 +675 -48
  28. package/commands/create/services/aws/elasticache.js +159 -0
  29. package/commands/create/services/aws/elasticbeanstalk.js +155 -57
  30. package/commands/create/services/aws/iam.js +420 -86
  31. package/commands/create/services/aws/index.js +1401 -260
  32. package/commands/create/services/aws/opensearch.js +155 -0
  33. package/commands/create/services/aws/rds.js +57 -31
  34. package/commands/create/services/aws/s3.js +52 -31
  35. package/commands/create/services/aws/secretsmanager.js +21 -12
  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/software.config +12 -0
  40. package/commands/create/templates/elasticbeanstalk/.ebextensions/misc/setvars.config +2 -16
  41. package/commands/create/templates/elasticbeanstalk/.platform/confighooks/postdeploy/restart_queue.sh +4 -0
  42. package/commands/create/templates/elasticbeanstalk/.platform/confighooks/postdeploy/rewrite-flush.sh +1 -1
  43. package/commands/create/templates/elasticbeanstalk/.platform/confighooks/prebuild/setvars.sh +19 -0
  44. package/commands/create/templates/elasticbeanstalk/.platform/hooks/postdeploy/restart_queue.sh +4 -0
  45. package/commands/create/templates/elasticbeanstalk/.platform/hooks/prebuild/setvars.sh +19 -0
  46. package/commands/scan.js +1 -1
  47. package/core/1/CHANGELOG.md +41 -0
  48. package/core/1/Dockerfile +3 -3
  49. package/core/1/bitbucket-pipelines.yml +5 -5
  50. package/core/1/package.json +1 -1
  51. package/globals.js +4 -0
  52. package/package.json +7 -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,68 +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",
149
- );
150
-
151
- await module.exports.attachIAMPolicy(
152
- UserName,
153
172
  "arn:aws:iam::aws:policy/SecretsManagerReadWrite",
173
+ "arn:aws:iam::aws:policy/AmazonSESFullAccess",
154
174
  );
155
175
  }
156
176
 
157
177
  if (permissions.includes("manage-users")) {
158
- await module.exports.attachIAMPolicy(
159
- UserName,
160
- "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
+ ),
161
196
  );
162
197
  }
163
198
  };
164
199
 
165
200
  module.exports.removeIAMPolicy = async (UserName, policy) => {
166
- const client = new IAMClient({});
167
-
168
201
  let res = await Spinner.prototype.simple(
169
202
  `Detaching IAM policy ${policy}`,
170
203
  () => {
171
- return client.send(
204
+ return getClient().send(
172
205
  new DetachUserPolicyCommand({ UserName, PolicyArn: policy }),
173
206
  );
174
207
  },
@@ -178,10 +211,10 @@ module.exports.removeIAMPolicy = async (UserName, policy) => {
178
211
  };
179
212
 
180
213
  module.exports.listIAMPolicies = async (UserName) => {
181
- const client = new IAMClient({});
182
-
183
214
  let res = await Spinner.prototype.simple(`Listing IAM policies`, () => {
184
- return client.send(new ListAttachedUserPoliciesCommand({ UserName }));
215
+ return getClient().send(
216
+ new ListAttachedUserPoliciesCommand({ UserName }),
217
+ );
185
218
  });
186
219
 
187
220
  return res;
@@ -190,10 +223,20 @@ module.exports.listIAMPolicies = async (UserName) => {
190
223
  module.exports.removeAllIAMPolicies = async (UserName) => {
191
224
  let res = await module.exports.listIAMPolicies(UserName);
192
225
 
193
- for (let i = 0; i < res.AttachedPolicies.length; i++) {
194
- await module.exports.removeIAMPolicy(
195
- UserName,
196
- 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
+ ),
197
240
  );
198
241
  }
199
242
 
@@ -201,22 +244,18 @@ module.exports.removeAllIAMPolicies = async (UserName) => {
201
244
  };
202
245
 
203
246
  module.exports.createAccessKey = async (UserName) => {
204
- const client = new IAMClient({});
205
-
206
247
  let res = await Spinner.prototype.simple(`Creating access key`, () => {
207
- return client.send(new CreateAccessKeyCommand({ UserName }));
248
+ return getClient().send(new CreateAccessKeyCommand({ UserName }));
208
249
  });
209
250
 
210
251
  return res;
211
252
  };
212
253
 
213
254
  module.exports.removeAccessKey = async (UserName, AccessKeyId) => {
214
- const client = new IAMClient({});
215
-
216
255
  let res = await Spinner.prototype.simple(
217
256
  `Removing access key ${AccessKeyId}`,
218
257
  () => {
219
- return client.send(
258
+ return getClient().send(
220
259
  new DeleteAccessKeyCommand({ AccessKeyId, UserName }),
221
260
  );
222
261
  },
@@ -226,10 +265,8 @@ module.exports.removeAccessKey = async (UserName, AccessKeyId) => {
226
265
  };
227
266
 
228
267
  module.exports.listAccessKeys = async (UserName) => {
229
- const client = new IAMClient({});
230
-
231
268
  let res = await Spinner.prototype.simple(`Listing access keys`, () => {
232
- return client.send(new ListAccessKeysCommand({ UserName }));
269
+ return getClient().send(new ListAccessKeysCommand({ UserName }));
233
270
  });
234
271
 
235
272
  return res;
@@ -238,10 +275,20 @@ module.exports.listAccessKeys = async (UserName) => {
238
275
  module.exports.removeAllAccessKeys = async (UserName) => {
239
276
  let res = await module.exports.listAccessKeys(UserName);
240
277
 
241
- for (let i = 0; i < res.AccessKeyMetadata.length; i++) {
242
- await module.exports.removeAccessKey(
243
- UserName,
244
- 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
+ ),
245
292
  );
246
293
  }
247
294
 
@@ -258,27 +305,72 @@ module.exports.createAccessKeySafe = async (UserName) => {
258
305
  return res;
259
306
  };
260
307
 
261
- module.exports.getRole = async (RoleName) => {
262
- 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);
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
+ };
263
348
 
349
+ module.exports.getRole = async (RoleName) => {
264
350
  let res = await Spinner.prototype.simple(
265
351
  `Retrieving the role ${RoleName}`,
266
352
  () => {
267
- return client.send(new GetRoleCommand({ RoleName }));
353
+ return getClient().send(new GetRoleCommand({ RoleName }));
268
354
  },
269
355
  );
270
356
 
271
357
  return res;
272
358
  };
273
359
 
274
- module.exports.createRole = async (RoleName, AssumeRolePolicyDocument) => {
275
- const client = new IAMClient({});
276
-
360
+ module.exports.createRole = async (
361
+ RoleName,
362
+ AssumeRolePolicyDocument,
363
+ Tags,
364
+ ) => {
277
365
  let res = await Spinner.prototype.simple(
278
366
  `Creating the role ${RoleName}`,
279
367
  () => {
280
- return client.send(
281
- new CreateRoleCommand({ RoleName, AssumeRolePolicyDocument }),
368
+ return getClient().send(
369
+ new CreateRoleCommand({
370
+ RoleName,
371
+ AssumeRolePolicyDocument,
372
+ ...(Tags && Tags.length ? { Tags } : {}),
373
+ }),
282
374
  );
283
375
  },
284
376
  );
@@ -286,14 +378,15 @@ module.exports.createRole = async (RoleName, AssumeRolePolicyDocument) => {
286
378
  return res;
287
379
  };
288
380
 
289
- module.exports.createInstanceProfile = async (InstanceProfileName) => {
290
- const client = new IAMClient({});
291
-
381
+ module.exports.createInstanceProfile = async (InstanceProfileName, Tags) => {
292
382
  let res = await Spinner.prototype.simple(
293
383
  `Creating the instance profile ${InstanceProfileName}`,
294
384
  () => {
295
- return client.send(
296
- new CreateInstanceProfileCommand({ InstanceProfileName }),
385
+ return getClient().send(
386
+ new CreateInstanceProfileCommand({
387
+ InstanceProfileName,
388
+ ...(Tags && Tags.length ? { Tags } : {}),
389
+ }),
297
390
  );
298
391
  },
299
392
  );
@@ -305,12 +398,10 @@ module.exports.attachRoleToInstanceProfile = async (
305
398
  RoleName,
306
399
  InstanceProfileName,
307
400
  ) => {
308
- const client = new IAMClient({});
309
-
310
401
  let res = await Spinner.prototype.simple(
311
402
  `Attaching role ${RoleName} to the instance profile ${InstanceProfileName}`,
312
403
  () => {
313
- return client.send(
404
+ return getClient().send(
314
405
  new AddRoleToInstanceProfileCommand({
315
406
  RoleName,
316
407
  InstanceProfileName,
@@ -323,12 +414,10 @@ module.exports.attachRoleToInstanceProfile = async (
323
414
  };
324
415
 
325
416
  module.exports.attachRolePolicy = async (RoleName, PolicyArn) => {
326
- const client = new IAMClient({});
327
-
328
417
  let res = await Spinner.prototype.simple(
329
418
  `Attaching Role policy ${PolicyArn}`,
330
419
  () => {
331
- return client.send(
420
+ return getClient().send(
332
421
  new AttachRolePolicyCommand({ RoleName, PolicyArn }),
333
422
  );
334
423
  },
@@ -342,12 +431,10 @@ module.exports.attachInlineRolePolicy = async (
342
431
  PolicyName,
343
432
  PolicyDocument,
344
433
  ) => {
345
- const client = new IAMClient({});
346
-
347
434
  let res = await Spinner.prototype.simple(
348
435
  `Attaching Inline Role policy ${PolicyName}`,
349
436
  () => {
350
- return client.send(
437
+ return getClient().send(
351
438
  new PutRolePolicyCommand({
352
439
  RoleName,
353
440
  PolicyName,
@@ -402,6 +489,10 @@ module.exports.ensureEBInstanceProfileExists = async () => {
402
489
  role,
403
490
  "arn:aws:iam::aws:policy/AWSSecretsManagerClientReadOnlyAccess",
404
491
  );
492
+ await module.exports.attachRolePolicy(
493
+ role,
494
+ "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore",
495
+ );
405
496
  await module.exports.attachInlineRolePolicy(
406
497
  role,
407
498
  "lab-env-elasticbeanstalk-describe-env",
@@ -504,3 +595,246 @@ module.exports.ensureEBManagedUpdateProfileExists = async () => {
504
595
  }),
505
596
  );
506
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
+ };