@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
@@ -0,0 +1,283 @@
1
+ const _ = require("../../../globals.js");
2
+ const inquirer = require("inquirer");
3
+ const aws = require("../services/aws/index.js");
4
+ const outputCredentials = require("../libs/output-credentials.js");
5
+ const { misc } = require("../libs/vars.js");
6
+ const {
7
+ setAWSClientDefaults,
8
+ } = require("../../../commands/create/services/aws/misc.js");
9
+ const parallelRunner = require("../libs/parallel-runner.js");
10
+ const resolveOperator = require("../libs/resolve-operator.js");
11
+
12
+ module.exports = [
13
+ "rekey",
14
+ _.config.preset === "devops"
15
+ ? "Rotates AWS access keys and credentials"
16
+ : false,
17
+ () => {},
18
+ async () => {
19
+ let operator = await resolveOperator();
20
+
21
+ let users = [];
22
+ let clients = [];
23
+ let sendEmail = false;
24
+
25
+ let answer = await inquirer.prompt([
26
+ {
27
+ type: "confirm",
28
+ name: "check",
29
+ message: `Rotate keys for all users`,
30
+ default: "Y",
31
+ },
32
+ ]);
33
+
34
+ if (answer.check) {
35
+ users = _.config.users.map((d) => d.username);
36
+ } else {
37
+ answer = await inquirer.prompt([
38
+ {
39
+ type: "checkbox",
40
+ name: "users",
41
+ message: "Select users",
42
+ choices: _.config.users.map((d) => d.username),
43
+ },
44
+ ]);
45
+
46
+ users = answer.users;
47
+ }
48
+
49
+ answer = await inquirer.prompt([
50
+ {
51
+ type: "confirm",
52
+ name: "check",
53
+ message: `Rotate keys for all clients`,
54
+ default: "Y",
55
+ },
56
+ ]);
57
+
58
+ if (answer.check) {
59
+ clients = aws.clients;
60
+ } else {
61
+ answer = await inquirer.prompt([
62
+ {
63
+ type: "checkbox",
64
+ name: "clients",
65
+ message: "Select clients",
66
+ choices: aws.clients,
67
+ },
68
+ ]);
69
+
70
+ clients = answer.clients;
71
+ }
72
+
73
+ answer = await inquirer.prompt([
74
+ {
75
+ type: "confirm",
76
+ name: "check",
77
+ message: `Would you like to email out the credentials after creation (requires office365 credentials in ~/targets/misc.json)?`,
78
+ default: "Y",
79
+ },
80
+ ]);
81
+
82
+ sendEmail = answer.check;
83
+
84
+ // Check for SES email credentials if user wants to send emails
85
+ if (sendEmail && !misc.nodemailer?.AWSSES?.username) {
86
+ answer = await inquirer.prompt([
87
+ {
88
+ type: "confirm",
89
+ name: "check",
90
+ message:
91
+ "No email credentials found, create them via AWS SES?",
92
+ default: "Y",
93
+ },
94
+ ]);
95
+
96
+ if (answer.check) {
97
+ try {
98
+ let creds = await aws.ses.ensureOperatorSESCredentials(
99
+ operator.username,
100
+ );
101
+
102
+ if (!creds) {
103
+ answer = await inquirer.prompt([
104
+ {
105
+ type: "confirm",
106
+ name: "check",
107
+ message:
108
+ "SES IAM user exists but secret is not stored locally. Rotate credentials to recapture?",
109
+ default: "Y",
110
+ },
111
+ ]);
112
+
113
+ if (answer.check) {
114
+ creds = await aws.ses.rotateOperatorSESCredentials(
115
+ operator.username,
116
+ );
117
+ }
118
+ }
119
+
120
+ if (creds) {
121
+ aws.ses.updateMiscNodemailer(creds);
122
+ console.log(
123
+ "\n \u2713 SES email credentials saved to misc.json\n",
124
+ );
125
+ }
126
+ } catch (err) {
127
+ console.log(
128
+ `\n \u26a0 Failed to create SES credentials: ${err.message}\n`,
129
+ );
130
+ }
131
+ }
132
+ }
133
+
134
+ // Separate users into: regular, runners, operator
135
+ // Processing order: regular users → runners → operator (last)
136
+ let runners = users.filter((u) => {
137
+ let config = _.config.users.find((d) => d.username === u);
138
+ return (
139
+ u !== operator.username &&
140
+ config &&
141
+ (config.runner || u === "aws-runner")
142
+ );
143
+ });
144
+ let nonOperatorUsers = users.filter(
145
+ (u) => u !== operator.username && !runners.includes(u),
146
+ );
147
+ let includeOperator = users.includes(operator.username);
148
+
149
+ let credentials = {};
150
+
151
+ // Process non-operator users in parallel
152
+ if (nonOperatorUsers.length) {
153
+ const batches = clients.map((client) => ({
154
+ users: nonOperatorUsers,
155
+ client,
156
+ fn: async (user) => {
157
+ setAWSClientDefaults(client);
158
+
159
+ await aws.iam.removeIAMUser(`fw-automation-${user}`);
160
+
161
+ return aws.iam.createFWIAMUser(
162
+ `fw-automation-${user}`,
163
+ _.config.users.find((d) => d.username === user)
164
+ .permissions,
165
+ );
166
+ },
167
+ }));
168
+
169
+ const resultsMap = await parallelRunner(batches, {
170
+ concurrency: 5,
171
+ verb: "Rotate credentials",
172
+ });
173
+
174
+ for (const [user, entries] of resultsMap) {
175
+ credentials[user] = {};
176
+
177
+ for (const { client, res } of entries) {
178
+ credentials[user][client] = {
179
+ key:
180
+ (res.AccessKey && res.AccessKey.AccessKeyId) ||
181
+ res.AccessKeyMetadata[0].AccessKeyId,
182
+ secret:
183
+ (res.AccessKey && res.AccessKey.SecretAccessKey) ||
184
+ "** secret **",
185
+ };
186
+ }
187
+ }
188
+ }
189
+
190
+ // Process runner users as a group (after regular, before operator)
191
+ if (runners.length) {
192
+ const runnerBatches = clients.map((client) => ({
193
+ users: runners,
194
+ client,
195
+ fn: async (user) => {
196
+ setAWSClientDefaults(client);
197
+
198
+ await aws.iam.removeIAMUser(`fw-automation-${user}`);
199
+
200
+ return aws.iam.createFWIAMUser(
201
+ `fw-automation-${user}`,
202
+ _.config.users.find((d) => d.username === user)
203
+ .permissions,
204
+ );
205
+ },
206
+ }));
207
+
208
+ const runnerResults = await parallelRunner(runnerBatches, {
209
+ concurrency: 5,
210
+ verb: "Rotate runner credentials",
211
+ });
212
+
213
+ for (const [user, entries] of runnerResults) {
214
+ credentials[user] = {};
215
+
216
+ for (const { client, res } of entries) {
217
+ credentials[user][client] = {
218
+ key:
219
+ (res.AccessKey && res.AccessKey.AccessKeyId) ||
220
+ res.AccessKeyMetadata[0].AccessKeyId,
221
+ secret:
222
+ (res.AccessKey && res.AccessKey.SecretAccessKey) ||
223
+ "** secret **",
224
+ };
225
+ }
226
+ }
227
+ }
228
+
229
+ // Rotate SES email credentials BEFORE rotating the operator's AWS key.
230
+ // The SES client uses fromIni({ profile: 'fishawack' }) which reads the
231
+ // operator's key from ~/.aws/credentials — once that key is rotated below
232
+ // the profile becomes stale and the SES call would fail.
233
+ if (misc.nodemailer?.AWSSES?.username) {
234
+ try {
235
+ const creds = await aws.ses.rotateOperatorSESCredentials(
236
+ operator.username,
237
+ );
238
+ aws.ses.updateMiscNodemailer(creds);
239
+ console.log(
240
+ "\n \u2713 SES email credentials rotated and saved to misc.json\n",
241
+ );
242
+ } catch (err) {
243
+ console.log(
244
+ `\n \u26a0 Failed to rotate SES credentials: ${err.message}\n`,
245
+ );
246
+ }
247
+ }
248
+
249
+ // Process operator sequentially — must be last per account
250
+ if (includeOperator) {
251
+ const operatorBatches = clients.map((client) => ({
252
+ users: [operator.username],
253
+ client,
254
+ fn: async (user) => {
255
+ setAWSClientDefaults(client);
256
+
257
+ return aws.iam.rotateFWIAMUser(
258
+ `fw-automation-${user}`,
259
+ operator.permissions,
260
+ );
261
+ },
262
+ }));
263
+
264
+ const operatorResults = await parallelRunner(operatorBatches, {
265
+ concurrency: 1,
266
+ verb: "Rotate operator credentials",
267
+ });
268
+
269
+ for (const [user, entries] of operatorResults) {
270
+ credentials[user] = {};
271
+
272
+ for (const { client, res } of entries) {
273
+ credentials[user][client] = {
274
+ key: res.AccessKey.AccessKeyId,
275
+ secret: res.AccessKey.SecretAccessKey,
276
+ };
277
+ }
278
+ }
279
+ }
280
+
281
+ await outputCredentials(credentials, sendEmail);
282
+ },
283
+ ];
@@ -0,0 +1,198 @@
1
+ const _ = require("../../../globals.js");
2
+ const inquirer = require("inquirer");
3
+ const aws = require("../services/aws/index.js");
4
+ const { setAWSClientDefaults } = require("../services/aws/misc.js");
5
+ const utilities = require("../libs/utilities");
6
+ const { client, region } = require("../libs/prompts.js");
7
+ const { secretGroups } = require("../libs/vars.js");
8
+
9
+ const SECRETS_NAMESPACE = "aws:elasticbeanstalk:application:environmentsecrets";
10
+
11
+ module.exports = [
12
+ ["sync-secrets", "syncs"],
13
+ _.config.preset === "devops"
14
+ ? "Sync Secrets Manager keys with EB environment secrets"
15
+ : false,
16
+ (yargs) => {
17
+ yargs.option("branch", {
18
+ alias: "b",
19
+ describe: "Branch to configure",
20
+ type: "string",
21
+ });
22
+ },
23
+ async (argv) => {
24
+ let branch = argv.branch || _.branch;
25
+
26
+ let answer = await inquirer.prompt([
27
+ {
28
+ type: "confirm",
29
+ name: "check",
30
+ message: `Syncing secrets for the ${utilities.colorize(branch, "success")} branch, is this correct?`,
31
+ default: "Y",
32
+ },
33
+ ]);
34
+
35
+ if (!answer.check) {
36
+ process.exit(1);
37
+ }
38
+
39
+ let answers = await inquirer.prompt([client, region]);
40
+
41
+ const slug = aws.slug(_.repoSafe, answers.client, branch, "eb");
42
+
43
+ setAWSClientDefaults(answers.client, answers.region);
44
+
45
+ // Verify EB environment exists
46
+ let appName;
47
+
48
+ try {
49
+ const { Environments } =
50
+ await aws.elasticbeanstalk.describeEnvironment(slug);
51
+ const active = Environments.filter(
52
+ (e) => e.Status !== "Terminated",
53
+ );
54
+ if (!active.length) {
55
+ console.log(
56
+ utilities.colorize(
57
+ `\n ✗ No active EB environment found for "${slug}"\n`,
58
+ "error",
59
+ ),
60
+ );
61
+ process.exit(1);
62
+ }
63
+ appName = active[0].ApplicationName;
64
+ } catch {
65
+ console.log(
66
+ utilities.colorize(
67
+ `\n ✗ No EB environment found for "${slug}"\n`,
68
+ "error",
69
+ ),
70
+ );
71
+ process.exit(1);
72
+ }
73
+
74
+ // Build expected environmentsecrets from Secrets Manager
75
+ const expectedSettings = [];
76
+ const groups = secretGroups;
77
+
78
+ for (const group of groups) {
79
+ const secretName = `${slug}-${group}`;
80
+
81
+ try {
82
+ const secret =
83
+ await aws.secretsmanager.describeSecret(secretName);
84
+ const secretValue =
85
+ await aws.secretsmanager.getSecretValue(secretName);
86
+ const keys = Object.keys(secretValue);
87
+
88
+ for (const key of keys) {
89
+ expectedSettings.push({
90
+ OptionName: key,
91
+ Value: `${secret.ARN}:${key}`,
92
+ Namespace: SECRETS_NAMESPACE,
93
+ });
94
+ }
95
+ } catch {
96
+ /* secret does not exist for this group */
97
+ }
98
+ }
99
+
100
+ // Get current EB configuration
101
+ const currentSettings =
102
+ await aws.elasticbeanstalk.describeConfigurationSettings(
103
+ appName,
104
+ slug,
105
+ );
106
+
107
+ const currentSecrets = currentSettings.filter(
108
+ (s) => s.Namespace === SECRETS_NAMESPACE,
109
+ );
110
+
111
+ // Diff: what to add and what to remove
112
+ const currentKeys = new Set(currentSecrets.map((s) => s.OptionName));
113
+ const expectedKeys = new Set(expectedSettings.map((s) => s.OptionName));
114
+
115
+ const toAdd = expectedSettings.filter(
116
+ (s) => !currentKeys.has(s.OptionName),
117
+ );
118
+ const toUpdate = expectedSettings.filter((s) => {
119
+ const current = currentSecrets.find(
120
+ (c) => c.OptionName === s.OptionName,
121
+ );
122
+ return current && current.Value !== s.Value;
123
+ });
124
+ const toRemove = currentSecrets
125
+ .filter((s) => !expectedKeys.has(s.OptionName))
126
+ .map((s) => ({
127
+ OptionName: s.OptionName,
128
+ Namespace: SECRETS_NAMESPACE,
129
+ }));
130
+
131
+ const optionSettings = [...toAdd, ...toUpdate];
132
+
133
+ if (!optionSettings.length && !toRemove.length) {
134
+ new utilities.Spinner(
135
+ "Environment secrets are in sync",
136
+ true,
137
+ ).ora.info();
138
+ return;
139
+ }
140
+
141
+ // Log summary
142
+ if (toAdd.length) {
143
+ console.log(
144
+ utilities.colorize(
145
+ `\n Adding ${toAdd.length} secret(s): ${toAdd.map((s) => s.OptionName).join(", ")}`,
146
+ "success",
147
+ ),
148
+ );
149
+ }
150
+
151
+ if (toUpdate.length) {
152
+ console.log(
153
+ utilities.colorize(
154
+ `\n Updating ${toUpdate.length} secret(s): ${toUpdate.map((s) => s.OptionName).join(", ")}`,
155
+ "info",
156
+ ),
157
+ );
158
+ }
159
+
160
+ if (toRemove.length) {
161
+ console.log(
162
+ utilities.colorize(
163
+ `\n Removing ${toRemove.length} secret(s): ${toRemove.map((s) => s.OptionName).join(", ")}`,
164
+ "warning",
165
+ ),
166
+ );
167
+ }
168
+
169
+ console.log("");
170
+
171
+ answer = await inquirer.prompt([
172
+ {
173
+ type: "confirm",
174
+ name: "proceed",
175
+ message: "Apply these changes?",
176
+ default: true,
177
+ },
178
+ ]);
179
+
180
+ if (!answer.proceed) {
181
+ process.exit(0);
182
+ }
183
+
184
+ await aws.elasticbeanstalk.updateEnvironmentSettings(
185
+ slug,
186
+ optionSettings,
187
+ toRemove,
188
+ );
189
+
190
+ console.log(
191
+ // eslint-disable-next-line prettier/prettier
192
+ utilities.colorize(
193
+ "\n ✔ Environment secrets synced\n",
194
+ "success",
195
+ ),
196
+ );
197
+ },
198
+ ];
@@ -0,0 +1,59 @@
1
+ const _ = require("../../../globals.js");
2
+ const utilities = require("../libs/utilities");
3
+ const email = require("../services/email.js");
4
+ const { readFileSync } = require("fs");
5
+ const htmlEmail = readFileSync(`${__dirname}/../templates/credentials.html`, {
6
+ encoding: "utf8",
7
+ });
8
+
9
+ module.exports = async function outputCredentials(credentials, sendEmail) {
10
+ let output = "";
11
+
12
+ for (var user in credentials) {
13
+ output += utilities.colorize(`\n${user}\n`, "title");
14
+
15
+ let outputUser = "";
16
+ for (var client in credentials[user]) {
17
+ outputUser += `\n[${client}]\n`;
18
+ outputUser += `aws_access_key_id = ${credentials[user][client].key}\n`;
19
+ outputUser += `aws_secret_access_key = ${credentials[user][client].secret}\n`;
20
+ }
21
+ output += outputUser;
22
+
23
+ if (sendEmail) {
24
+ await email.send(
25
+ _.config.users.find((d) => d.username === user).email,
26
+ "New AWS Keys",
27
+ `${htmlEmail}<pre><code>${outputUser.replace(/\n/g, "<br>")}</code></pre>`,
28
+ );
29
+ }
30
+ }
31
+
32
+ console.log(output);
33
+
34
+ // Output base64-encoded credentials for the CI runner user
35
+ let runner =
36
+ _.config.users.find((d) => d.runner) ||
37
+ _.config.users.find((d) => d.username === "aws-runner");
38
+
39
+ if (runner && credentials[runner.username]) {
40
+ let runnerCreds = "";
41
+
42
+ for (var c in credentials[runner.username]) {
43
+ runnerCreds += `[${c}]\n`;
44
+ runnerCreds += `aws_access_key_id = ${credentials[runner.username][c].key}\n`;
45
+ runnerCreds += `aws_secret_access_key = ${credentials[runner.username][c].secret}\n\n`;
46
+ }
47
+
48
+ let base64 = Buffer.from(runnerCreds.trim()).toString("base64");
49
+
50
+ console.log(
51
+ utilities.colorize("\n CI Runner Base64 Credentials\n", "title"),
52
+ );
53
+ console.log(
54
+ " Use this base64 string as the CI variable for pipeline AWS authentication.\n",
55
+ );
56
+ console.log(base64);
57
+ console.log("");
58
+ }
59
+ };