@adobe/ccweb-add-on-ssl 0.1.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.
Files changed (90) hide show
  1. package/.c8rc.json +7 -0
  2. package/.mocharc.json +5 -0
  3. package/CODE_OF_CONDUCT.md +79 -0
  4. package/CONTRIBUTING.md +47 -0
  5. package/LICENSE +21 -0
  6. package/README.md +21 -0
  7. package/bin/run.cmd +3 -0
  8. package/bin/run.js +8 -0
  9. package/dist/AnalyticsMarkers.d.ts +35 -0
  10. package/dist/AnalyticsMarkers.d.ts.map +1 -0
  11. package/dist/AnalyticsMarkers.js +37 -0
  12. package/dist/app/CommandExecutor.d.ts +36 -0
  13. package/dist/app/CommandExecutor.d.ts.map +1 -0
  14. package/dist/app/CommandExecutor.js +25 -0
  15. package/dist/app/SSLReader.d.ts +48 -0
  16. package/dist/app/SSLReader.d.ts.map +1 -0
  17. package/dist/app/SSLReader.js +25 -0
  18. package/dist/app/SetupCommandExecutor.d.ts +64 -0
  19. package/dist/app/SetupCommandExecutor.d.ts.map +1 -0
  20. package/dist/app/SetupCommandExecutor.js +243 -0
  21. package/dist/app/WxpSSLReader.d.ts +60 -0
  22. package/dist/app/WxpSSLReader.d.ts.map +1 -0
  23. package/dist/app/WxpSSLReader.js +113 -0
  24. package/dist/app/index.d.ts +28 -0
  25. package/dist/app/index.d.ts.map +1 -0
  26. package/dist/app/index.js +28 -0
  27. package/dist/commands/setup.d.ts +54 -0
  28. package/dist/commands/setup.d.ts.map +1 -0
  29. package/dist/commands/setup.js +118 -0
  30. package/dist/config/index.d.ts +26 -0
  31. package/dist/config/index.d.ts.map +1 -0
  32. package/dist/config/index.js +26 -0
  33. package/dist/config/inversify.config.d.ts +27 -0
  34. package/dist/config/inversify.config.d.ts.map +1 -0
  35. package/dist/config/inversify.config.js +42 -0
  36. package/dist/config/inversify.types.d.ts +29 -0
  37. package/dist/config/inversify.types.d.ts.map +1 -0
  38. package/dist/config/inversify.types.js +29 -0
  39. package/dist/constants.d.ts +25 -0
  40. package/dist/constants.d.ts.map +1 -0
  41. package/dist/constants.js +25 -0
  42. package/dist/index.d.ts +28 -0
  43. package/dist/index.d.ts.map +1 -0
  44. package/dist/index.js +28 -0
  45. package/dist/models/SetupCommandOptions.d.ts +49 -0
  46. package/dist/models/SetupCommandOptions.d.ts.map +1 -0
  47. package/dist/models/SetupCommandOptions.js +53 -0
  48. package/dist/models/Types.d.ts +57 -0
  49. package/dist/models/Types.d.ts.map +1 -0
  50. package/dist/models/Types.js +40 -0
  51. package/dist/models/index.d.ts +26 -0
  52. package/dist/models/index.d.ts.map +1 -0
  53. package/dist/models/index.js +26 -0
  54. package/dist/tsconfig.tsbuildinfo +1 -0
  55. package/dist/validators/CommandValidator.d.ts +36 -0
  56. package/dist/validators/CommandValidator.d.ts.map +1 -0
  57. package/dist/validators/CommandValidator.js +25 -0
  58. package/dist/validators/SetupCommandValidator.d.ts +52 -0
  59. package/dist/validators/SetupCommandValidator.d.ts.map +1 -0
  60. package/dist/validators/SetupCommandValidator.js +94 -0
  61. package/dist/validators/index.d.ts +26 -0
  62. package/dist/validators/index.d.ts.map +1 -0
  63. package/dist/validators/index.js +26 -0
  64. package/package.json +65 -0
  65. package/src/AnalyticsMarkers.ts +36 -0
  66. package/src/app/CommandExecutor.ts +37 -0
  67. package/src/app/SSLReader.ts +51 -0
  68. package/src/app/SetupCommandExecutor.ts +310 -0
  69. package/src/app/WxpSSLReader.ts +121 -0
  70. package/src/app/index.ts +28 -0
  71. package/src/commands/setup.ts +146 -0
  72. package/src/config/index.ts +26 -0
  73. package/src/config/inversify.config.ts +49 -0
  74. package/src/config/inversify.types.ts +29 -0
  75. package/src/constants.ts +25 -0
  76. package/src/index.ts +28 -0
  77. package/src/models/SetupCommandOptions.ts +56 -0
  78. package/src/models/Types.ts +61 -0
  79. package/src/models/index.ts +26 -0
  80. package/src/test/app/SetupCommandExecutor.spec.ts +800 -0
  81. package/src/test/app/WxpSSLReader.spec.ts +244 -0
  82. package/src/test/commands/setup.spec.ts +161 -0
  83. package/src/test/data/cert/cert.pem +19 -0
  84. package/src/test/data/cert/key.pem +27 -0
  85. package/src/test/models/SetupCommandOptions.spec.ts +53 -0
  86. package/src/test/validators/SetupCommandValidator.spec.ts +168 -0
  87. package/src/validators/CommandValidator.ts +37 -0
  88. package/src/validators/SetupCommandValidator.ts +116 -0
  89. package/src/validators/index.ts +26 -0
  90. package/tsconfig.json +31 -0
@@ -0,0 +1,800 @@
1
+ /********************************************************************************
2
+ * MIT License
3
+
4
+ * © Copyright 2023 Adobe. All rights reserved.
5
+
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ ********************************************************************************/
24
+
25
+ import type { AnalyticsService } from "@adobe/ccweb-add-on-analytics";
26
+ import type { Logger, Preferences } from "@adobe/ccweb-add-on-core";
27
+ import { ADD_ON_PREFERENCES_FILE, PreferenceJson } from "@adobe/ccweb-add-on-core";
28
+ import chai, { assert, expect } from "chai";
29
+ import chaiAsPromised from "chai-as-promised";
30
+ import chalk from "chalk";
31
+ import devcert from "@adobe/ccweb-add-on-devcert";
32
+ import type { Stats } from "fs-extra";
33
+ import fs from "fs-extra";
34
+ import "mocha";
35
+ import path from "path";
36
+ import process from "process";
37
+ import prompts from "prompts";
38
+ import type { SinonSandbox } from "sinon";
39
+ import sinon from "sinon";
40
+ import format from "string-template";
41
+ import type { StubbedInstance } from "ts-sinon";
42
+ import { stubInterface } from "ts-sinon";
43
+ import { AnalyticsErrorMarkers, AnalyticsSuccessMarkers } from "../../AnalyticsMarkers.js";
44
+ import type { CommandExecutor, SSLReader } from "../../app/index.js";
45
+ import { SetupCommandExecutor } from "../../app/index.js";
46
+ import { SSLRemoveOption, SSLSetupOption, SetupCommandOptions } from "../../models/index.js";
47
+
48
+ chai.use(chaiAsPromised);
49
+
50
+ describe("SSLSetupCommandExecutor", () => {
51
+ let sandbox: SinonSandbox;
52
+
53
+ let preferences: StubbedInstance<Preferences>;
54
+ let sslReader: StubbedInstance<SSLReader>;
55
+ let analyticsService: StubbedInstance<AnalyticsService>;
56
+ let logger: StubbedInstance<Logger>;
57
+
58
+ let commandExecutor: CommandExecutor;
59
+
60
+ beforeEach(() => {
61
+ sandbox = sinon.createSandbox();
62
+
63
+ sandbox.stub(console, "log");
64
+
65
+ preferences = stubInterface();
66
+ sslReader = stubInterface();
67
+ analyticsService = stubInterface();
68
+ logger = stubInterface();
69
+
70
+ commandExecutor = new SetupCommandExecutor(preferences, sslReader, analyticsService, logger);
71
+ });
72
+
73
+ afterEach(() => {
74
+ sandbox.restore();
75
+ });
76
+
77
+ describe("execute", () => {
78
+ const sslSetupTypePrompt = {
79
+ type: "select",
80
+ name: "sslSetupType",
81
+ message: promptMessage("How do you want to set up your SSL certificate"),
82
+ choices: [
83
+ {
84
+ title: promptMessageOption("Automatically, set it up for me"),
85
+ value: SSLSetupOption.Automatically
86
+ },
87
+ {
88
+ title: promptMessageOption("Manually, I already have an SSL certificate and key"),
89
+ value: SSLSetupOption.Manually
90
+ }
91
+ ],
92
+ initial: 0
93
+ };
94
+
95
+ const sslCertificatePathPrompt = {
96
+ type: "text",
97
+ name: "sslPath",
98
+ message: promptMessage("Please enter the path of your SSL certificate file")
99
+ };
100
+
101
+ const sslKeyPathPrompt = {
102
+ type: "text",
103
+ name: "sslPath",
104
+ message: promptMessage("Please enter the path of your SSL key file")
105
+ };
106
+
107
+ describe("when 'useExisting' flag is set to 'true'", () => {
108
+ it("should not prompt when SSL is set up manually for the provided hostname.", async () => {
109
+ const hostname = "localhost";
110
+ sslReader.isCustomSSL.withArgs(hostname).returns(true);
111
+
112
+ const promptStub = sandbox.stub(prompts, "prompt");
113
+
114
+ const options = new SetupCommandOptions(hostname, true, false);
115
+ await commandExecutor.execute(options);
116
+
117
+ assert.equal(promptStub.callCount, 0);
118
+ });
119
+
120
+ it("should not prompt when SSL is set up automatically for the provided hostname.", async () => {
121
+ const hostname = "localhost";
122
+ sslReader.isCustomSSL.withArgs(hostname).returns(false);
123
+ sslReader.isWxpSSL.withArgs(hostname).returns(true);
124
+
125
+ const promptStub = sandbox.stub(prompts, "prompt");
126
+
127
+ const options = new SetupCommandOptions(hostname, true, false);
128
+ await commandExecutor.execute(options);
129
+
130
+ assert.equal(promptStub.callCount, 0);
131
+ });
132
+
133
+ it("should log warning and exit when user does not select any option in the SSL setup prompt.", async () => {
134
+ const hostname = "localhost";
135
+ sslReader.isCustomSSL.withArgs(hostname).returns(false);
136
+ sslReader.isWxpSSL.withArgs(hostname).returns(false);
137
+
138
+ const promptStub = sandbox.stub(prompts, "prompt");
139
+ promptStub.withArgs(sslSetupTypePrompt).resolves({ sslSetupType: undefined });
140
+
141
+ analyticsService.postEvent.resolves();
142
+
143
+ logger.warning.returns();
144
+
145
+ const processExitStub = sandbox.stub(process, "exit");
146
+
147
+ const options = new SetupCommandOptions(hostname, true, false);
148
+ await commandExecutor.execute(options);
149
+
150
+ assert.equal(
151
+ logger.warning.calledOnceWith(
152
+ format("The Add-on requires a trusted SSL certificate in order to run on https://{hostname}", {
153
+ hostname: options.hostname
154
+ }),
155
+ {
156
+ prefix: "\n"
157
+ }
158
+ ),
159
+ true
160
+ );
161
+
162
+ assert.equal(promptStub.calledOnceWith(sslSetupTypePrompt), true);
163
+
164
+ assert.equal(
165
+ analyticsService.postEvent.calledOnceWith(
166
+ AnalyticsErrorMarkers.ERROR_SSL_SETUP,
167
+ "SSL setup option is not specified.",
168
+ false
169
+ ),
170
+ true
171
+ );
172
+
173
+ assert.equal(processExitStub.callCount, 1);
174
+ });
175
+
176
+ it("should log warning and exit when user provides an incorrect SSL certificate path.", async () => {
177
+ const hostname = "localhost";
178
+ sslReader.isCustomSSL.withArgs(hostname).returns(false);
179
+ sslReader.isWxpSSL.withArgs(hostname).returns(false);
180
+
181
+ const certificatePath = "/some-directory/localhost/ssl/";
182
+ const keyPath = "/some-directory/localhost/ssl/private-key.key";
183
+
184
+ const promptStub = sandbox.stub(prompts, "prompt");
185
+ promptStub.withArgs(sslSetupTypePrompt).resolves({
186
+ sslSetupType: SSLSetupOption.Manually
187
+ });
188
+
189
+ promptStub.withArgs(sslCertificatePathPrompt).resolves({ sslPath: certificatePath });
190
+ promptStub.withArgs(sslKeyPathPrompt).resolves({ sslPath: keyPath });
191
+
192
+ analyticsService.postEvent.resolves();
193
+
194
+ logger.warning.returns();
195
+ logger.error.returns();
196
+
197
+ const processExitStub = sandbox.stub(process, "exit");
198
+
199
+ const existsStub = sandbox.stub(fs, "existsSync");
200
+ existsStub.withArgs(certificatePath).returns(true);
201
+ existsStub.withArgs(keyPath).returns(true);
202
+
203
+ const certificateStats = <Stats>{ isFile: () => false };
204
+ const keyStats = <Stats>{ isFile: () => true };
205
+
206
+ const lstatStub = sandbox.stub(fs, "lstatSync");
207
+ lstatStub.withArgs(certificatePath).returns(certificateStats);
208
+ lstatStub.withArgs(keyPath).returns(keyStats);
209
+
210
+ const exitError = new Error("Process exit.");
211
+ processExitStub.callsFake(() => {
212
+ throw exitError;
213
+ });
214
+
215
+ const options = new SetupCommandOptions(hostname, true, false);
216
+
217
+ const execute = () => commandExecutor.execute(options);
218
+ await expect(execute()).to.be.rejectedWith(exitError);
219
+
220
+ assert.equal(
221
+ logger.warning.calledOnceWith(
222
+ format("The Add-on requires a trusted SSL certificate in order to run on https://{hostname}", {
223
+ hostname: options.hostname
224
+ }),
225
+ {
226
+ prefix: "\n"
227
+ }
228
+ ),
229
+ true
230
+ );
231
+
232
+ assert.equal(logger.error.callCount, 3);
233
+ assert.equal(
234
+ logger.error.calledWith(format("Invalid {asset} path specified.", { asset: "certificate" })),
235
+ true
236
+ );
237
+
238
+ assert.equal(
239
+ analyticsService.postEvent.calledOnceWith(
240
+ AnalyticsErrorMarkers.ERROR_SSL_SETUP,
241
+ format("Invalid {asset} path specified.", { asset: "certificate" }),
242
+ false
243
+ ),
244
+ true
245
+ );
246
+ });
247
+
248
+ it("should log warning and exit when user provides an incorrect SSL key path.", async () => {
249
+ const hostname = "localhost";
250
+ sslReader.isCustomSSL.withArgs(hostname).returns(false);
251
+ sslReader.isWxpSSL.withArgs(hostname).returns(false);
252
+
253
+ const certificatePath = "/some-directory/localhost/ssl/certificate.cert";
254
+ const keyPath = "/some-directory/localhost/ssl/";
255
+
256
+ const promptStub = sandbox.stub(prompts, "prompt");
257
+ promptStub
258
+ .withArgs({
259
+ type: "select",
260
+ name: "sslSetupType",
261
+ message: promptMessage("How do you want to set up your SSL certificate"),
262
+ choices: [
263
+ {
264
+ title: promptMessageOption("Automatically, set it up for me"),
265
+ value: SSLSetupOption.Automatically
266
+ },
267
+ {
268
+ title: promptMessageOption("Manually, I already have an SSL certificate and key"),
269
+ value: SSLSetupOption.Manually
270
+ }
271
+ ],
272
+ initial: 0
273
+ })
274
+ .resolves({
275
+ sslSetupType: SSLSetupOption.Manually
276
+ });
277
+
278
+ promptStub.withArgs(sslCertificatePathPrompt).resolves({ sslPath: certificatePath });
279
+ promptStub.withArgs(sslKeyPathPrompt).resolves({ sslPath: keyPath });
280
+
281
+ analyticsService.postEvent.resolves();
282
+
283
+ logger.warning.returns();
284
+ logger.error.returns();
285
+
286
+ const processExitStub = sandbox.stub(process, "exit");
287
+
288
+ const existsStub = sandbox.stub(fs, "existsSync");
289
+ existsStub.withArgs(certificatePath).returns(true);
290
+ existsStub.withArgs(keyPath).returns(true);
291
+
292
+ const certificateStats = <Stats>{ isFile: () => true };
293
+ const keyStats = <Stats>{ isFile: () => false };
294
+
295
+ const lstatStub = sandbox.stub(fs, "lstatSync");
296
+ lstatStub.withArgs(certificatePath).returns(certificateStats);
297
+ lstatStub.withArgs(keyPath).returns(keyStats);
298
+
299
+ const exitError = new Error("Process exit.");
300
+ processExitStub.callsFake(() => {
301
+ throw exitError;
302
+ });
303
+
304
+ const options = new SetupCommandOptions(hostname, true, false);
305
+
306
+ const execute = () => commandExecutor.execute(options);
307
+ await expect(execute()).to.be.rejectedWith(exitError);
308
+
309
+ assert.equal(
310
+ logger.warning.calledOnceWith(
311
+ format("The Add-on requires a trusted SSL certificate in order to run on https://{hostname}", {
312
+ hostname: options.hostname
313
+ }),
314
+ {
315
+ prefix: "\n"
316
+ }
317
+ ),
318
+ true
319
+ );
320
+
321
+ assert.equal(logger.error.callCount, 3);
322
+ assert.equal(
323
+ logger.error.calledWith(format("Invalid {asset} path specified.", { asset: "key" })),
324
+ true
325
+ );
326
+
327
+ assert.equal(
328
+ analyticsService.postEvent.calledOnceWith(
329
+ AnalyticsErrorMarkers.ERROR_SSL_SETUP,
330
+ format("Invalid {asset} path specified.", { asset: "key" }),
331
+ false
332
+ ),
333
+ true
334
+ );
335
+ });
336
+
337
+ it(`should set the 'ssl' property in '${ADD_ON_PREFERENCES_FILE}' for successful manual SSL setup.`, async () => {
338
+ const hostname = "localhost";
339
+ sslReader.isCustomSSL.withArgs(hostname).returns(false);
340
+ sslReader.isWxpSSL.withArgs(hostname).returns(false);
341
+
342
+ const certificatePath = "/some-directory/localhost/ssl/certificate.cert";
343
+ const keyPath = "/some-directory/localhost/ssl/private-key.key";
344
+
345
+ const promptStub = sandbox.stub(prompts, "prompt");
346
+ promptStub.withArgs(sslSetupTypePrompt).resolves({
347
+ sslSetupType: SSLSetupOption.Manually
348
+ });
349
+
350
+ promptStub.withArgs(sslCertificatePathPrompt).resolves({ sslPath: certificatePath });
351
+ promptStub.withArgs(sslKeyPathPrompt).resolves({ sslPath: keyPath });
352
+
353
+ preferences.get.returns(new PreferenceJson({}));
354
+ preferences.set.returns();
355
+
356
+ analyticsService.postEvent.resolves();
357
+
358
+ logger.warning.returns();
359
+ logger.success.returns();
360
+
361
+ const existsStub = sandbox.stub(fs, "existsSync");
362
+ existsStub.withArgs(certificatePath).returns(true);
363
+ existsStub.withArgs(keyPath).returns(true);
364
+
365
+ const certificateStats = <Stats>{ isFile: () => true };
366
+ const keyStats = <Stats>{ isFile: () => true };
367
+
368
+ const lstatStub = sandbox.stub(fs, "lstatSync");
369
+ lstatStub.withArgs(certificatePath).returns(certificateStats);
370
+ lstatStub.withArgs(keyPath).returns(keyStats);
371
+
372
+ const options = new SetupCommandOptions(hostname, true, false);
373
+ await commandExecutor.execute(options);
374
+
375
+ assert.equal(
376
+ logger.warning.calledOnceWith(
377
+ format("The Add-on requires a trusted SSL certificate in order to run on https://{hostname}", {
378
+ hostname: options.hostname
379
+ }),
380
+ {
381
+ prefix: "\n"
382
+ }
383
+ ),
384
+ true
385
+ );
386
+
387
+ assert.equal(
388
+ logger.success.calledOnceWith("Your preferences have been saved.", { prefix: "\n", postfix: "\n" }),
389
+ true
390
+ );
391
+
392
+ assert.equal(
393
+ analyticsService.postEvent.calledOnceWith(
394
+ AnalyticsSuccessMarkers.SUCCESSFUL_SSL_MANUAL_SETUP,
395
+ options.hostname,
396
+ true
397
+ ),
398
+ true
399
+ );
400
+ });
401
+
402
+ it("should create a devcert SSL certificate for automatic SSL setup.", async () => {
403
+ const hostname = "localhost";
404
+ sslReader.isCustomSSL.withArgs(hostname).returns(false);
405
+ sslReader.isWxpSSL.withArgs(hostname).returns(false);
406
+
407
+ const promptStub = sandbox.stub(prompts, "prompt");
408
+ promptStub.withArgs(sslSetupTypePrompt).resolves({
409
+ sslSetupType: SSLSetupOption.Automatically
410
+ });
411
+
412
+ const caPath = "/some-directory/certificate-authority/certificate.cert";
413
+ const sslDirectory = `/some-directory/domains/${hostname}`;
414
+
415
+ sandbox
416
+ .stub(devcert, "certificateFor")
417
+ .withArgs(hostname, { getCaPath: true })
418
+ // @ts-ignore -- IReturnData mock response
419
+ .resolves({ cert: <Buffer>{}, key: <Buffer>{}, caPath });
420
+
421
+ sandbox.stub(path, "resolve").withArgs(caPath, "..", "..", "domains", hostname).returns(sslDirectory);
422
+
423
+ analyticsService.postEvent.resolves();
424
+
425
+ logger.warning.returns();
426
+ logger.success.returns();
427
+ logger.message.returns();
428
+
429
+ const options = new SetupCommandOptions(hostname, true, false);
430
+ await commandExecutor.execute(options);
431
+
432
+ assert.equal(
433
+ logger.warning.calledOnceWith(
434
+ format("The Add-on requires a trusted SSL certificate in order to run on https://{hostname}", {
435
+ hostname: options.hostname
436
+ }),
437
+ {
438
+ prefix: "\n"
439
+ }
440
+ ),
441
+ true
442
+ );
443
+
444
+ assert.equal(
445
+ logger.message
446
+ .getCall(0)
447
+ .calledWith("This is only a one time step and may require you to enter your system's password"),
448
+ true
449
+ );
450
+ assert.equal(
451
+ logger.message
452
+ .getCall(1)
453
+ .calledWith(
454
+ "so that the SSL certificate can be added to your system's trusted certificate path."
455
+ ),
456
+ true
457
+ );
458
+ assert.equal(
459
+ logger.message
460
+ .getCall(2)
461
+ .calledWith("[If this takes longer than expected, please break and retry]"),
462
+ true
463
+ );
464
+
465
+ assert.equal(logger.success.calledOnceWith("SSL setup complete!", { prefix: "\n" }), true);
466
+
467
+ assert.equal(
468
+ logger.information
469
+ .getCall(0)
470
+ .calledWith("Setting up self-signed SSL certificate ...", { prefix: "\n" }),
471
+ true
472
+ );
473
+ assert.equal(
474
+ logger.information
475
+ .getCall(1)
476
+ .calledWith(format("You can find the SSL certificate in {sslDirectory}.", { sslDirectory }), {
477
+ postfix: "\n"
478
+ }),
479
+ true
480
+ );
481
+
482
+ assert.equal(
483
+ analyticsService.postEvent.calledOnceWith(
484
+ AnalyticsSuccessMarkers.SUCCESSFUL_SSL_AUTOMATIC_SETUP,
485
+ options.hostname,
486
+ true
487
+ ),
488
+ true
489
+ );
490
+ });
491
+ });
492
+
493
+ describe("when 'useExisting' flag is set to 'false'", () => {
494
+ const shouldRemovePrompt = {
495
+ type: "select",
496
+ name: "shouldRemove",
497
+ message: promptMessage("Do you want to remove your existing SSL certificate to set up a new one"),
498
+ choices: [
499
+ {
500
+ title: promptMessageOption("Yes, remove the existing SSL certificate"),
501
+ value: SSLRemoveOption.Remove
502
+ },
503
+ { title: promptMessageOption("No, keep my existing SSL certificate"), value: SSLRemoveOption.Keep }
504
+ ],
505
+ initial: 0
506
+ };
507
+
508
+ it("should prompt on whether to remove existing SSL and log warning and exit when user does not select any option.", async () => {
509
+ const hostname = "localhost";
510
+ sslReader.isCustomSSL.withArgs(hostname).returns(true);
511
+ sslReader.isWxpSSL.withArgs(hostname).returns(false);
512
+
513
+ const promptStub = sandbox.stub(prompts, "prompt");
514
+ promptStub.withArgs(shouldRemovePrompt).resolves({ shouldRemove: undefined });
515
+
516
+ analyticsService.postEvent.resolves();
517
+
518
+ logger.warning.returns();
519
+
520
+ const processExitStub = sandbox.stub(process, "exit");
521
+
522
+ const exitError = new Error("Process exit.");
523
+ processExitStub.callsFake(() => {
524
+ throw exitError;
525
+ });
526
+
527
+ const options = new SetupCommandOptions(hostname, false, false);
528
+
529
+ const execute = () => commandExecutor.execute(options);
530
+ await expect(execute()).to.be.rejectedWith(exitError);
531
+
532
+ assert.equal(
533
+ logger.warning.calledOnceWith(
534
+ format(
535
+ "A trusted SSL certificate is already configured for the Add-on to run on https://{hostname}",
536
+ { hostname: options.hostname }
537
+ ),
538
+ {
539
+ prefix: "\n"
540
+ }
541
+ ),
542
+ true
543
+ );
544
+
545
+ assert.equal(
546
+ analyticsService.postEvent.calledOnceWith(
547
+ AnalyticsErrorMarkers.ERROR_SSL_REMOVE,
548
+ "SSL remove option is not specified.",
549
+ false
550
+ ),
551
+ true
552
+ );
553
+ });
554
+
555
+ it("should prompt on whether to remove existing SSL and exit when user chooses to keep the existing.", async () => {
556
+ const hostname = "localhost";
557
+ sslReader.isCustomSSL.withArgs(hostname).returns(true);
558
+ sslReader.isWxpSSL.withArgs(hostname).returns(false);
559
+
560
+ const promptStub = sandbox.stub(prompts, "prompt");
561
+ promptStub.withArgs(shouldRemovePrompt).resolves({ shouldRemove: SSLRemoveOption.Keep });
562
+
563
+ analyticsService.postEvent.resolves();
564
+
565
+ logger.warning.returns();
566
+
567
+ const options = new SetupCommandOptions(hostname, false, false);
568
+ await commandExecutor.execute(options);
569
+
570
+ assert.equal(
571
+ logger.warning.calledOnceWith(
572
+ format(
573
+ "A trusted SSL certificate is already configured for the Add-on to run on https://{hostname}",
574
+ { hostname: options.hostname }
575
+ ),
576
+ {
577
+ prefix: "\n"
578
+ }
579
+ ),
580
+ true
581
+ );
582
+
583
+ assert.equal(promptStub.withArgs(sslSetupTypePrompt).callCount, 0);
584
+ });
585
+
586
+ it("should remove existing and create a new one when user chooses to remove a manually set up SSL.", async () => {
587
+ const hostname = "localhost";
588
+ const certificatePath = "/some-directory/localhost/ssl/certificate.cert";
589
+ const keyPath = "/some-directory/localhost/ssl/private-key.key";
590
+
591
+ // SSL remove.
592
+ sslReader.isCustomSSL.withArgs(hostname).returns(true);
593
+ sslReader.isWxpSSL.withArgs(hostname).returns(false);
594
+
595
+ const promptStub = sandbox.stub(prompts, "prompt");
596
+ promptStub.withArgs(shouldRemovePrompt).resolves({ shouldRemove: SSLRemoveOption.Remove });
597
+
598
+ analyticsService.postEvent.resolves();
599
+
600
+ logger.warning.returns();
601
+
602
+ preferences.get.onCall(0).returns(
603
+ new PreferenceJson({
604
+ ssl: {
605
+ [hostname]: { certificatePath, keyPath }
606
+ }
607
+ })
608
+ );
609
+
610
+ // SSL setup.
611
+ promptStub.withArgs(sslSetupTypePrompt).resolves({
612
+ sslSetupType: SSLSetupOption.Manually
613
+ });
614
+
615
+ promptStub.withArgs(sslCertificatePathPrompt).resolves({ sslPath: certificatePath });
616
+ promptStub.withArgs(sslKeyPathPrompt).resolves({ sslPath: keyPath });
617
+
618
+ preferences.get.onCall(1).returns(new PreferenceJson({}));
619
+ preferences.set.returns();
620
+
621
+ analyticsService.postEvent.resolves();
622
+
623
+ logger.warning.returns();
624
+ logger.success.returns();
625
+
626
+ const existsStub = sandbox.stub(fs, "existsSync");
627
+ existsStub.withArgs(certificatePath).returns(true);
628
+ existsStub.withArgs(keyPath).returns(true);
629
+
630
+ const certificateStats = <Stats>{ isFile: () => true };
631
+ const keyStats = <Stats>{ isFile: () => true };
632
+
633
+ const lstatStub = sandbox.stub(fs, "lstatSync");
634
+ lstatStub.withArgs(certificatePath).returns(certificateStats);
635
+ lstatStub.withArgs(keyPath).returns(keyStats);
636
+
637
+ const options = new SetupCommandOptions(hostname, false, false);
638
+ await commandExecutor.execute(options);
639
+
640
+ assert.equal(
641
+ logger.warning.calledOnceWith(
642
+ format(
643
+ "A trusted SSL certificate is already configured for the Add-on to run on https://{hostname}",
644
+ { hostname: options.hostname }
645
+ ),
646
+ {
647
+ prefix: "\n"
648
+ }
649
+ ),
650
+ true
651
+ );
652
+
653
+ assert.equal(
654
+ analyticsService.postEvent
655
+ .getCall(0)
656
+ .calledWith(AnalyticsSuccessMarkers.SUCCESSFUL_SSL_MANUAL_REMOVE, options.hostname, true),
657
+ true
658
+ );
659
+
660
+ assert.equal(logger.success.getCall(0).calledWith("Removed.", { postfix: "\n" }), true);
661
+
662
+ assert.equal(
663
+ logger.success
664
+ .getCall(1)
665
+ .calledWith("Your preferences have been saved.", { prefix: "\n", postfix: "\n" }),
666
+ true
667
+ );
668
+
669
+ assert.equal(
670
+ analyticsService.postEvent
671
+ .getCall(1)
672
+ .calledWith(AnalyticsSuccessMarkers.SUCCESSFUL_SSL_MANUAL_SETUP, options.hostname, true),
673
+ true
674
+ );
675
+ });
676
+
677
+ it("should remove existing and create a new one when user chooses to remove an automatically set up SSL.", async () => {
678
+ const hostname = "localhost";
679
+
680
+ // SSL remove.
681
+ sslReader.isCustomSSL.withArgs(hostname).returns(false);
682
+ sslReader.isWxpSSL.withArgs(hostname).returns(true);
683
+
684
+ const promptStub = sandbox.stub(prompts, "prompt");
685
+ promptStub.withArgs(shouldRemovePrompt).resolves({ shouldRemove: SSLRemoveOption.Remove });
686
+
687
+ analyticsService.postEvent.resolves();
688
+
689
+ logger.warning.returns();
690
+
691
+ // SSL setup.
692
+ promptStub.withArgs(sslSetupTypePrompt).resolves({
693
+ sslSetupType: SSLSetupOption.Automatically
694
+ });
695
+
696
+ preferences.get.onCall(1).returns(new PreferenceJson({}));
697
+ preferences.set.returns();
698
+
699
+ const caPath = "/some-directory/certificate-authority/certificate.cert";
700
+ const sslDirectory = `/some-directory/domains/${hostname}`;
701
+
702
+ sandbox
703
+ .stub(devcert, "certificateFor")
704
+ .withArgs(hostname, { getCaPath: true })
705
+ // @ts-ignore -- IReturnData mock response
706
+ .resolves({ cert: <Buffer>{}, key: <Buffer>{}, caPath });
707
+
708
+ const removeDomainStub = sandbox.stub(devcert, "removeDomain");
709
+ removeDomainStub.withArgs(hostname).resolves();
710
+
711
+ sandbox.stub(path, "resolve").withArgs(caPath, "..", "..", "domains", hostname).returns(sslDirectory);
712
+
713
+ analyticsService.postEvent.resolves();
714
+
715
+ logger.warning.returns();
716
+ logger.success.returns();
717
+ logger.message.returns();
718
+
719
+ const options = new SetupCommandOptions(hostname, false, false);
720
+ await commandExecutor.execute(options);
721
+
722
+ assert.equal(removeDomainStub.calledWith(hostname), true);
723
+
724
+ assert.equal(
725
+ logger.warning.calledOnceWith(
726
+ format(
727
+ "A trusted SSL certificate is already configured for the Add-on to run on https://{hostname}",
728
+ { hostname: options.hostname }
729
+ ),
730
+ {
731
+ prefix: "\n"
732
+ }
733
+ ),
734
+ true
735
+ );
736
+
737
+ assert.equal(
738
+ analyticsService.postEvent
739
+ .getCall(0)
740
+ .calledWith(AnalyticsSuccessMarkers.SUCCESSFUL_SSL_AUTOMATIC_REMOVE, options.hostname, true),
741
+ true
742
+ );
743
+
744
+ assert.equal(logger.success.getCall(0).calledWith("Removed.", { postfix: "\n" }), true);
745
+ assert.equal(logger.success.getCall(1).calledWith("SSL setup complete!", { prefix: "\n" }), true);
746
+
747
+ assert.equal(
748
+ logger.message
749
+ .getCall(0)
750
+ .calledWith("This is only a one time step and may require you to enter your system's password"),
751
+ true
752
+ );
753
+ assert.equal(
754
+ logger.message
755
+ .getCall(1)
756
+ .calledWith(
757
+ "so that the SSL certificate can be added to your system's trusted certificate path."
758
+ ),
759
+ true
760
+ );
761
+ assert.equal(
762
+ logger.message
763
+ .getCall(2)
764
+ .calledWith("[If this takes longer than expected, please break and retry]"),
765
+ true
766
+ );
767
+
768
+ assert.equal(
769
+ logger.information
770
+ .getCall(0)
771
+ .calledWith("Setting up self-signed SSL certificate ...", { prefix: "\n" }),
772
+ true
773
+ );
774
+ assert.equal(
775
+ logger.information
776
+ .getCall(1)
777
+ .calledWith(format("You can find the SSL certificate in {sslDirectory}.", { sslDirectory }), {
778
+ postfix: "\n"
779
+ }),
780
+ true
781
+ );
782
+
783
+ assert.equal(
784
+ analyticsService.postEvent
785
+ .getCall(1)
786
+ .calledWith(AnalyticsSuccessMarkers.SUCCESSFUL_SSL_AUTOMATIC_SETUP, options.hostname, true),
787
+ true
788
+ );
789
+ });
790
+ });
791
+ });
792
+ });
793
+
794
+ function promptMessage(message: string): string {
795
+ return chalk.hex("#E59400")(message);
796
+ }
797
+
798
+ function promptMessageOption(message: string): string {
799
+ return chalk.green.bold(message);
800
+ }