Skip to content

Commit 332b306

Browse files
petermetzsfuji822
authored andcommitted
fix(cmd-api-server): disallow running on older than Node 12 but provide optional override
1. Node 10 is no longer the LTS version 2. Node 10 does not properly support TLS v1.3 3. The CI script will no longer run against Node 10 just 12 and 14. fix hyperledger-cacti#150 Signed-off-by: Peter Somogyvari <[email protected]>
1 parent 9dd28da commit 332b306

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: node_js
22
node_js:
3-
- 10
43
- 12
54
- 14
65

packages/cactus-cmd-api-server/package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cactus-cmd-api-server/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
"types": "dist/types/main/typescript/index.d.ts",
1717
"scripts": {
1818
"tsc": "tsc --project ./tsconfig.json",
19-
2019
"webpack": "npm-run-all webpack:dev webpack:prod",
21-
2220
"webpack:dev": "npm-run-all webpack:dev:node webpack:dev:web",
2321
"webpack:dev:web": "webpack --env=dev --target=web --config ../../webpack.config.js",
2422
"webpack:dev:node": "webpack --env=dev --target=node --config ../../webpack.config.js",
25-
2623
"webpack:prod": "npm-run-all webpack:prod:node webpack:prod:web",
2724
"webpack:prod:web": "webpack --env=prod --target=web --config ../../webpack.config.js",
2825
"webpack:prod:node": "webpack --env=prod --target=node --config ../../webpack.config.js"
@@ -82,6 +79,7 @@
8279
"js-sha3": "0.8.0",
8380
"node-fetch": "3.0.0-beta.4",
8481
"secp256k1": "4.0.0",
82+
"semver": "7.3.2",
8583
"sha3": "2.1.2",
8684
"typescript-optional": "2.0.1",
8785
"uuid": "7.0.2"
@@ -94,6 +92,7 @@
9492
"@types/joi": "14.3.4",
9593
"@types/multer": "1.4.2",
9694
"@types/secp256k1": "3.5.3",
95+
"@types/semver": "7.3.1",
9796
"@types/uuid": "7.0.2"
9897
}
9998
}

packages/cactus-cmd-api-server/src/main/typescript/api-server.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from "path";
22
import { Server } from "http";
3+
import { gte } from "semver";
34
import express, {
45
Express,
56
Request,
@@ -18,7 +19,10 @@ import {
1819
IPluginWebService,
1920
PluginRegistry,
2021
} from "@hyperledger/cactus-core-api";
21-
import { ICactusApiServerOptions as ICactusApiServerConfig } from "./config/config-service";
22+
import {
23+
ICactusApiServerOptions as ICactusApiServerConfig,
24+
ConfigService,
25+
} from "./config/config-service";
2226
import { CACTUS_OPEN_API_JSON } from "./openapi-spec";
2327
import { Logger, LoggerProvider } from "@hyperledger/cactus-common";
2428
import { Servers } from "./common/servers";
@@ -49,6 +53,7 @@ export class ApiServer {
4953
}
5054

5155
async start(): Promise<void> {
56+
this.checkNodeVersion();
5257
try {
5358
await this.startCockpitFileServer();
5459
await this.startApiServer();
@@ -59,6 +64,25 @@ export class ApiServer {
5964
}
6065
}
6166

67+
/**
68+
* Verifies that the currently running NodeJS process is at least of a certain
69+
* NodeJS version as specified by the configuration.
70+
*
71+
* @throws {Error} if the version contraint is not satisfied by the runtime.
72+
*/
73+
public checkNodeVersion(currentVersion: string = process.version): void {
74+
if (gte(this.options.config.minNodeVersion, currentVersion)) {
75+
const msg =
76+
`ApiServer#checkNodeVersion() detected NodeJS ` +
77+
`v${process.version} that is outdated as per the configuration. ` +
78+
`If you must run on this NodeJS version you can override the minimum ` +
79+
`acceptable version via config parameters of the API server. ` +
80+
`Though doing so may lead to vulnerabilities in your deployment. ` +
81+
`You've been warned.`;
82+
throw new Error(msg);
83+
}
84+
}
85+
6286
public getHttpServerApi(): Server | null {
6387
return this.httpServerApi;
6488
}

packages/cactus-cmd-api-server/src/main/typescript/config/config-service.ts

+13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface ICactusApiServerOptions {
3333
privateKey: string;
3434
keychainSuffixPublicKey: string;
3535
keychainSuffixPrivateKey: string;
36+
minNodeVersion: string;
3637
}
3738

3839
export class ConfigService {
@@ -107,6 +108,17 @@ export class ConfigService {
107108
env: "LOG_LEVEL",
108109
arg: "log-level",
109110
},
111+
minNodeVersion: {
112+
doc:
113+
"Determines the lower bound of NodeJS version that the API " +
114+
"server will be willing to start on. Defaults to v12 because v10 " +
115+
"does not support TLS v1.3. If you must run on Node 10, just set " +
116+
"this configuration parameter to 10.0.0 for example.",
117+
format: ConfigService.formatNonBlankString,
118+
default: "12.0.0",
119+
env: "MIN_NODE_VERSION",
120+
arg: "min-node-version",
121+
},
110122
cockpitHost: {
111123
doc:
112124
"The host to bind the Cockpit webserver to. Secure default is: 127.0.0.1. Use 0.0.0.0 to bind for any host.",
@@ -272,6 +284,7 @@ export class ConfigService {
272284
configFile: ".config.json",
273285
cactusNodeId: uuidV4(),
274286
logLevel: "debug",
287+
minNodeVersion: (schema.minNodeVersion as SchemaObj).default,
275288
publicKey,
276289
privateKey,
277290
apiCorsDomainCsv: (schema.apiCorsDomainCsv as SchemaObj).default,

0 commit comments

Comments
 (0)