@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,51 @@
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 { SSLData } from "../models/index.js";
26
+
27
+ /**
28
+ * Contracts for reading the SSL artifacts.
29
+ */
30
+ export interface SSLReader {
31
+ /**
32
+ * Is SSL set up manually by the user.
33
+ * @param hostname - Hostname in the SSL certificate.
34
+ * @returns Boolean value representing whether SSL is set up manually.
35
+ */
36
+ isCustomSSL(hostname: string): boolean;
37
+
38
+ /**
39
+ * Is SSL set up automatically by the tool.
40
+ * @param hostname - Hostname in the SSL certificate.
41
+ * @returns Boolean value representing whether SSL is set up automatically.
42
+ */
43
+ isWxpSSL(hostname: string): boolean;
44
+
45
+ /**
46
+ * Read the SSL artifacts.
47
+ * @param hostname - Hostname in the SSL certificate.
48
+ * @returns Promise of {@link SSLData}.
49
+ */
50
+ read(hostname: string): Promise<SSLData>;
51
+ }
@@ -0,0 +1,310 @@
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 { ITypes as IAnalyticsTypes } from "@adobe/ccweb-add-on-analytics";
27
+ import type { Logger, Preferences } from "@adobe/ccweb-add-on-core";
28
+ import { ITypes as ICoreTypes, isFile } from "@adobe/ccweb-add-on-core";
29
+ import chalk from "chalk";
30
+ import devcert from "@adobe/ccweb-add-on-devcert";
31
+ import { inject, injectable } from "inversify";
32
+ import path from "path";
33
+ import process from "process";
34
+ import prompts from "prompts";
35
+ import "reflect-metadata";
36
+ import format from "string-template";
37
+ import { AnalyticsErrorMarkers, AnalyticsSuccessMarkers } from "../AnalyticsMarkers.js";
38
+ import { ITypes } from "../config/inversify.types.js";
39
+ import type { SetupCommandOptions } from "../models/SetupCommandOptions.js";
40
+ import { SSLRemoveOption, SSLSetupOption } from "../models/Types.js";
41
+ import type { CommandExecutor } from "./CommandExecutor.js";
42
+ import type { SSLReader } from "./SSLReader.js";
43
+
44
+ const MAX_PROMPT_RETRIES = 3;
45
+
46
+ /**
47
+ * Setup command execution implementation class.
48
+ */
49
+ @injectable()
50
+ export class SetupCommandExecutor implements CommandExecutor {
51
+ private readonly _preferences: Preferences;
52
+ private readonly _sslReader: SSLReader;
53
+ private readonly _analyticsService: AnalyticsService;
54
+ private readonly _logger: Logger;
55
+
56
+ /**
57
+ * Instantiate {@link SetupCommandExecutor}.
58
+ * @param preferences - {@link Preferences} reference.
59
+ * @param sslReader - {@link SSLReader} reference.
60
+ * @param analyticsService - {@link AnalyticsService} reference.
61
+ * @param logger - {@link Logger} reference.
62
+ * @returns Reference to a new {@link SetupCommandExecutor} instance.
63
+ */
64
+ constructor(
65
+ @inject(ICoreTypes.Preferences) preferences: Preferences,
66
+ @inject(ITypes.SSLReader) sslReader: SSLReader,
67
+ @inject(IAnalyticsTypes.AnalyticsService) analyticsService: AnalyticsService,
68
+ @inject(ICoreTypes.Logger) logger: Logger
69
+ ) {
70
+ this._preferences = preferences;
71
+ this._sslReader = sslReader;
72
+ this._analyticsService = analyticsService;
73
+ this._logger = logger;
74
+ }
75
+
76
+ /**
77
+ * Setup self-signed SSL certificate and key by checking if user defined preferences
78
+ * have SSL config data or if the user has already setup the certificates last time.
79
+ * Otherwise prompt the user to setup the SSL themselves by providing paths as the input or
80
+ * by allowing the CLI to setup the certificates on user's behalf.
81
+ * @param options - Command arguments provided by user represented as {@link SetupCommandOptions}.
82
+ * @returns Promise.
83
+ */
84
+ async execute(options: SetupCommandOptions): Promise<void> {
85
+ if (!(await this._shouldSetupNewSSL(options))) {
86
+ return;
87
+ }
88
+
89
+ const response = await prompts.prompt({
90
+ type: "select",
91
+ name: "sslSetupType",
92
+ message: this._promptMessage(LOGS.setupSSL),
93
+ choices: [
94
+ { title: this._promptMessageOption(LOGS.automatically), value: SSLSetupOption.Automatically },
95
+ { title: this._promptMessageOption(LOGS.manually), value: SSLSetupOption.Manually }
96
+ ],
97
+ initial: 0
98
+ });
99
+
100
+ if (!response || !response.sslSetupType) {
101
+ this._analyticsService.postEvent(
102
+ AnalyticsErrorMarkers.ERROR_SSL_SETUP,
103
+ LOGS.sslSetupOptionNotSpecified,
104
+ false
105
+ );
106
+
107
+ return process.exit(1);
108
+ }
109
+
110
+ if (response.sslSetupType === SSLSetupOption.Manually) {
111
+ await this._setupSSLManually(options.hostname);
112
+ this._analyticsService.postEvent(
113
+ AnalyticsSuccessMarkers.SUCCESSFUL_SSL_MANUAL_SETUP,
114
+ options.hostname,
115
+ true
116
+ );
117
+ } else {
118
+ await this._setupSSLAutomatically(options.hostname);
119
+ this._analyticsService.postEvent(
120
+ AnalyticsSuccessMarkers.SUCCESSFUL_SSL_AUTOMATIC_SETUP,
121
+ options.hostname,
122
+ true
123
+ );
124
+ }
125
+ }
126
+
127
+ private async _shouldSetupNewSSL(options: SetupCommandOptions): Promise<boolean> {
128
+ const isCustomSSL = this._sslReader.isCustomSSL(options.hostname);
129
+ const isWxpSSL = this._sslReader.isWxpSSL(options.hostname);
130
+ const doesSSLExist = isCustomSSL || isWxpSSL;
131
+
132
+ // When SSL is not configured, set up a new SSL.
133
+ if (!doesSSLExist) {
134
+ this._logger.warning(format(LOGS.sslRequired, { hostname: options.hostname }), { prefix: LOGS.newLine });
135
+ return true;
136
+ }
137
+
138
+ // When SSL is configured and the option to use existing is passed, do not set up a new SSL.
139
+ if (options.useExisting) {
140
+ console.log();
141
+ return false;
142
+ }
143
+
144
+ // Prompt the user about the existing SSL and
145
+ // check if the user wants to remove the existing to create a new one.
146
+ this._logger.warning(format(LOGS.sslAlreadyConfigured, { hostname: options.hostname }), {
147
+ prefix: LOGS.newLine
148
+ });
149
+
150
+ // Consider updating the signature of `_removeExistingSSL` to accept only `options`
151
+ // when we introduce the @oclf command to `remove` an existing SSL.
152
+ return await this._removeExistingSSL(options, isCustomSSL, isWxpSSL);
153
+ }
154
+
155
+ private async _setupSSLManually(hostname: string) {
156
+ const certificatePath = await this._promptUserForPath("certificate");
157
+ const keyPath = await this._promptUserForPath("key");
158
+
159
+ const userPreference = this._preferences.get();
160
+ if (userPreference.ssl === undefined) {
161
+ userPreference.ssl = new Map();
162
+ }
163
+
164
+ userPreference.ssl.set(hostname, { certificatePath, keyPath });
165
+ this._preferences.set(userPreference);
166
+
167
+ this._logger.success(LOGS.preferencesSaved, { prefix: LOGS.newLine, postfix: LOGS.newLine });
168
+ }
169
+
170
+ private async _setupSSLAutomatically(hostname: string) {
171
+ this._logger.information(LOGS.settingUpSSL, { prefix: LOGS.newLine });
172
+ this._logger.message(LOGS.requireSystemPassword);
173
+ this._logger.message(LOGS.requireSystemPasswordReason);
174
+ this._logger.message(LOGS.retryRunningIfTakingLonger);
175
+
176
+ const sslData = await devcert.certificateFor(hostname, { getCaPath: true });
177
+
178
+ this._logger.success(LOGS.sslSetupComplete, { prefix: LOGS.newLine });
179
+
180
+ if (sslData?.caPath) {
181
+ const sslDirectory = path.resolve(sslData.caPath, "..", "..", "domains", hostname);
182
+ this._logger.information(format(LOGS.findSSLCertificate, { sslDirectory }), { postfix: LOGS.newLine });
183
+ }
184
+ }
185
+
186
+ private async _removeExistingSSL(
187
+ options: SetupCommandOptions,
188
+ isCustomSSL: boolean,
189
+ isWxpSSL: boolean
190
+ ): Promise<boolean> {
191
+ const response = await prompts.prompt({
192
+ type: "select",
193
+ name: "shouldRemove",
194
+ message: this._promptMessage(LOGS.removeExistingSSL),
195
+ choices: [
196
+ { title: this._promptMessageOption(LOGS.removeSSL), value: SSLRemoveOption.Remove },
197
+ { title: this._promptMessageOption(LOGS.keepSSL), value: SSLRemoveOption.Keep }
198
+ ],
199
+ initial: 0
200
+ });
201
+
202
+ // If no response is obtained, which indicates something has gone wrong, then exit.
203
+ if (!response || !response.shouldRemove) {
204
+ this._analyticsService.postEvent(
205
+ AnalyticsErrorMarkers.ERROR_SSL_REMOVE,
206
+ LOGS.sslRemoveOptionNotSpecified,
207
+ false
208
+ );
209
+ return process.exit(1);
210
+ }
211
+
212
+ // If the user chooses to keep the existing SSL, do not set up a new SSL.
213
+ if (response.shouldRemove === SSLRemoveOption.Keep) {
214
+ console.log();
215
+ return false;
216
+ }
217
+
218
+ // Remove the existing SSL based on how it was set up earlier.
219
+ if (isCustomSSL) {
220
+ const userPreference = this._preferences.get();
221
+ userPreference.ssl!.delete(options.hostname);
222
+ this._preferences.set(userPreference);
223
+
224
+ this._analyticsService.postEvent(
225
+ AnalyticsSuccessMarkers.SUCCESSFUL_SSL_MANUAL_REMOVE,
226
+ options.hostname,
227
+ true
228
+ );
229
+ }
230
+
231
+ if (isWxpSSL) {
232
+ await devcert.removeDomain(options.hostname);
233
+ this._analyticsService.postEvent(
234
+ AnalyticsSuccessMarkers.SUCCESSFUL_SSL_AUTOMATIC_REMOVE,
235
+ options.hostname,
236
+ true
237
+ );
238
+ }
239
+
240
+ this._logger.success(LOGS.removed, { postfix: LOGS.newLine });
241
+ return true;
242
+ }
243
+
244
+ private async _promptUserForPath(asset: "certificate" | "key") {
245
+ const promptData = {
246
+ certificate: {
247
+ type: "text",
248
+ name: "sslPath",
249
+ message: this._promptMessage(LOGS.specifyCertificatePath)
250
+ },
251
+ key: {
252
+ type: "text",
253
+ name: "sslPath",
254
+ message: this._promptMessage(LOGS.specifyKeyPath)
255
+ }
256
+ };
257
+
258
+ let maxTries = MAX_PROMPT_RETRIES;
259
+ /* eslint-disable no-await-in-loop -- Retry logic */
260
+ while (maxTries > 0) {
261
+ const { sslPath } = await prompts.prompt(promptData[asset]);
262
+ if (isFile(sslPath)) {
263
+ return sslPath;
264
+ }
265
+
266
+ maxTries--;
267
+ this._logger.error(format(LOGS.invalidPathSpecified, { asset }));
268
+ }
269
+
270
+ this._analyticsService.postEvent(
271
+ AnalyticsErrorMarkers.ERROR_SSL_SETUP,
272
+ format(LOGS.invalidPathSpecified, { asset }),
273
+ false
274
+ );
275
+ return process.exit(1);
276
+ }
277
+
278
+ private _promptMessage(message: string): string {
279
+ return chalk.hex("#E59400")(message);
280
+ }
281
+
282
+ private _promptMessageOption(message: string): string {
283
+ return chalk.green.bold(message);
284
+ }
285
+ }
286
+
287
+ const LOGS = {
288
+ newLine: "\n",
289
+ sslRequired: "The Add-on requires a trusted SSL certificate in order to run on https://{hostname}",
290
+ sslAlreadyConfigured: "A trusted SSL certificate is already configured for the Add-on to run on https://{hostname}",
291
+ removeExistingSSL: "Do you want to remove your existing SSL certificate to set up a new one",
292
+ removeSSL: "Yes, remove the existing SSL certificate",
293
+ keepSSL: "No, keep my existing SSL certificate",
294
+ setupSSL: "How do you want to set up your SSL certificate",
295
+ automatically: "Automatically, set it up for me",
296
+ manually: "Manually, I already have an SSL certificate and key",
297
+ settingUpSSL: "Setting up self-signed SSL certificate ...",
298
+ requireSystemPassword: "This is only a one time step and may require you to enter your system's password",
299
+ requireSystemPasswordReason: "so that the SSL certificate can be added to your system's trusted certificate path.",
300
+ retryRunningIfTakingLonger: "[If this takes longer than expected, please break and retry]",
301
+ sslSetupComplete: "SSL setup complete!",
302
+ preferencesSaved: "Your preferences have been saved.",
303
+ findSSLCertificate: "You can find the SSL certificate in {sslDirectory}.",
304
+ specifyCertificatePath: "Please enter the path of your SSL certificate file",
305
+ specifyKeyPath: "Please enter the path of your SSL key file",
306
+ invalidPathSpecified: "Invalid {asset} path specified.",
307
+ sslSetupOptionNotSpecified: "SSL setup option is not specified.",
308
+ sslRemoveOptionNotSpecified: "SSL remove option is not specified.",
309
+ removed: "Removed."
310
+ };
@@ -0,0 +1,121 @@
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 { Logger, Preferences, SSLSettings } from "@adobe/ccweb-add-on-core";
26
+ import { ITypes as ICoreTypes, isFile } from "@adobe/ccweb-add-on-core";
27
+ import devcert from "@adobe/ccweb-add-on-devcert";
28
+ import fs from "fs-extra";
29
+ import { inject, injectable } from "inversify";
30
+ import "reflect-metadata";
31
+ import type { SSLData } from "../models/index.js";
32
+
33
+ /**
34
+ * Implementation class for reading the SSL artifacts.
35
+ */
36
+ @injectable()
37
+ export class WxpSSLReader {
38
+ private readonly _preferences: Preferences;
39
+ private readonly _logger: Logger;
40
+
41
+ /**
42
+ * Instantiate {@link WxpSSLReader}.
43
+ * @param Preferences - {@link Preferences} reference.
44
+ * @param logger - {@link Logger} reference.
45
+ * @returns Reference to a new {@link WxpSSLReader} instance.
46
+ */
47
+ constructor(@inject(ICoreTypes.Preferences) preferences: Preferences, @inject(ICoreTypes.Logger) logger: Logger) {
48
+ this._preferences = preferences;
49
+ this._logger = logger;
50
+ }
51
+
52
+ /**
53
+ * Is SSL set up manually by the user.
54
+ * @param hostname - Hostname in the SSL certificate.
55
+ * @returns Boolean value representing whether SSL is set up manually.
56
+ */
57
+ isCustomSSL(hostname: string): boolean {
58
+ return this._getUserDefinedSSL(hostname) !== undefined;
59
+ }
60
+
61
+ /**
62
+ * Is SSL set up automatically by the tool.
63
+ * @param hostname - Hostname in the SSL certificate.
64
+ * @returns Boolean value representing whether SSL is set up automatically.
65
+ */
66
+ isWxpSSL(hostname: string): boolean {
67
+ return devcert.hasCertificateFor(hostname);
68
+ }
69
+
70
+ /**
71
+ * Read the SSL artifacts.
72
+ * @param hostname - Hostname in the SSL certificate.
73
+ * @returns Promise of {@link SSLData}.
74
+ */
75
+ async read(hostname: string): Promise<SSLData> {
76
+ const sslSettings = this._getUserDefinedSSL(hostname);
77
+
78
+ // When SSL is set up manuually by the `user`.
79
+ if (sslSettings !== undefined) {
80
+ const { certificatePath, keyPath } = sslSettings;
81
+ if (!certificatePath || !isFile(certificatePath)) {
82
+ this._logger.error(LOGS.invalidCertificatePath);
83
+ return process.exit(1);
84
+ }
85
+
86
+ if (!keyPath || !isFile(keyPath)) {
87
+ this._logger.error(LOGS.invalidKeyPath);
88
+ return process.exit(1);
89
+ }
90
+
91
+ return {
92
+ cert: fs.readFileSync(certificatePath),
93
+ key: fs.readFileSync(keyPath)
94
+ };
95
+ }
96
+
97
+ // When SSL is set up automatically by `devcert`.
98
+ if (this.isWxpSSL(hostname)) {
99
+ return await devcert.certificateFor(hostname);
100
+ }
101
+
102
+ this._logger.error(LOGS.noSSLDataFound, { postfix: LOGS.newLine });
103
+ return process.exit(1);
104
+ }
105
+
106
+ private _getUserDefinedSSL(hostname: string): SSLSettings | undefined {
107
+ const { ssl } = this._preferences.get();
108
+ if (ssl === undefined) {
109
+ return undefined;
110
+ }
111
+
112
+ return ssl.get(hostname);
113
+ }
114
+ }
115
+
116
+ const LOGS = {
117
+ newLine: "\n",
118
+ invalidCertificatePath: "Invalid SSL certificate file path.",
119
+ invalidKeyPath: "Invalid SSL key file path.",
120
+ noSSLDataFound: "No SSL related certificate or key files were found. Please retry after setting them up."
121
+ };
@@ -0,0 +1,28 @@
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
+ export * from "./CommandExecutor.js";
26
+ export * from "./SSLReader.js";
27
+ export * from "./SetupCommandExecutor.js";
28
+ export * from "./WxpSSLReader.js";
@@ -0,0 +1,146 @@
1
+ #!/usr/bin/env node
2
+
3
+ /********************************************************************************
4
+ * MIT License
5
+
6
+ * © Copyright 2023 Adobe. All rights reserved.
7
+
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the "Software"), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ ********************************************************************************/
26
+
27
+ import type { AnalyticsConsent, AnalyticsService } from "@adobe/ccweb-add-on-analytics";
28
+ import { CLIProgram, ITypes as IAnalyticsTypes } from "@adobe/ccweb-add-on-analytics";
29
+ import { UncaughtExceptionHandler } from "@adobe/ccweb-add-on-core";
30
+ import type { AccountService } from "@adobe/ccweb-add-on-developer-terms";
31
+ import { ITypes as IDeveloperTermsTypes } from "@adobe/ccweb-add-on-developer-terms";
32
+ import type { Config } from "@oclif/core";
33
+ import { Command, Flags } from "@oclif/core";
34
+ import process from "process";
35
+ import "reflect-metadata";
36
+ import { AnalyticsErrorMarkers } from "../AnalyticsMarkers.js";
37
+ import type { CommandExecutor } from "../app/CommandExecutor.js";
38
+ import { IContainer, ITypes } from "../config/index.js";
39
+ import { PROGRAM_NAME } from "../constants.js";
40
+ import { SetupCommandOptions } from "../models/SetupCommandOptions.js";
41
+ import type { CommandValidator } from "../validators/CommandValidator.js";
42
+
43
+ /**
44
+ * SSL Setup command.
45
+ */
46
+ export class Setup extends Command {
47
+ private readonly _accountService: AccountService;
48
+
49
+ private readonly _analyticsConsent: AnalyticsConsent;
50
+ private readonly _analyticsService: AnalyticsService;
51
+
52
+ private readonly _commandValidator: CommandValidator;
53
+ private readonly _commandExecutor: CommandExecutor;
54
+
55
+ static description = "Setup a trusted SSL certificate for hosting an add-on.";
56
+
57
+ static args = {};
58
+
59
+ static flags = {
60
+ hostname: Flags.string({
61
+ char: "h",
62
+ description: "Hostname in the SSL certificate.",
63
+ required: true
64
+ }),
65
+ useExisting: Flags.boolean({
66
+ char: "e",
67
+ description: "Use existing SSL if present.",
68
+ required: false,
69
+ default: false,
70
+ hidden: true
71
+ }),
72
+ login: Flags.boolean({
73
+ char: "l",
74
+ description: "Force login.",
75
+ required: false,
76
+ default: false
77
+ }),
78
+ analytics: Flags.string({
79
+ char: "a",
80
+ description: "Turn on/off sending analytics to Adobe.",
81
+ options: ["on", "off"],
82
+ required: false
83
+ }),
84
+ verbose: Flags.boolean({
85
+ char: "v",
86
+ description: "Print detailed logs.",
87
+ required: false,
88
+ default: false
89
+ })
90
+ };
91
+
92
+ constructor(argv: string[], config: Config) {
93
+ super(argv, config);
94
+
95
+ this._accountService = IContainer.get<AccountService>(IDeveloperTermsTypes.AccountService);
96
+
97
+ this._analyticsConsent = IContainer.get<AnalyticsConsent>(IAnalyticsTypes.AnalyticsConsent);
98
+
99
+ this._analyticsService = IContainer.get<AnalyticsService>(IAnalyticsTypes.AnalyticsService);
100
+ this._analyticsService.program = new CLIProgram(PROGRAM_NAME, this.config.name + "@" + this.config.version);
101
+ this._analyticsService.startTime = Date.now();
102
+
103
+ this._commandValidator = IContainer.getNamed<CommandValidator>(ITypes.CommandValidator, "setup");
104
+ this._commandExecutor = IContainer.getNamed<CommandExecutor>(ITypes.CommandExecutor, "setup");
105
+ }
106
+
107
+ async run() {
108
+ UncaughtExceptionHandler.registerExceptionHandler(PROGRAM_NAME);
109
+
110
+ const rootDirectory = process.cwd();
111
+ process.chdir(rootDirectory);
112
+
113
+ const {
114
+ flags: { hostname, useExisting, login, analytics, verbose }
115
+ } = await this.parse(Setup);
116
+
117
+ await this._seekTermsOfUseConsent(login, verbose);
118
+
119
+ await this._seekAnalyticsConsent(analytics);
120
+
121
+ const options = new SetupCommandOptions(hostname, useExisting, verbose);
122
+ await this._commandValidator.validate(options);
123
+ await this._commandExecutor.execute(options);
124
+ }
125
+
126
+ async catch(error: { message: string }) {
127
+ this._analyticsService.postEvent(AnalyticsErrorMarkers.ERROR_SSL_SETUP, error.message, false);
128
+ throw error;
129
+ }
130
+
131
+ private async _seekTermsOfUseConsent(login: boolean, verbose: boolean): Promise<void> {
132
+ if (login) {
133
+ await this._accountService.invalidateToken(verbose);
134
+ }
135
+
136
+ await this._accountService.seekTermsOfUseConsent();
137
+ }
138
+
139
+ private async _seekAnalyticsConsent(analytics: string | undefined): Promise<void> {
140
+ if (analytics === undefined) {
141
+ await this._analyticsConsent.get();
142
+ } else {
143
+ await this._analyticsConsent.set(analytics === "on");
144
+ }
145
+ }
146
+ }
@@ -0,0 +1,26 @@
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
+ export * from "./inversify.config.js";
26
+ export * from "./inversify.types.js";