From 952570b9848c099a6205289d8d9f7959379a7036 Mon Sep 17 00:00:00 2001 From: Justin Kromlinger Date: Mon, 6 Jun 2022 21:11:47 +0200 Subject: [PATCH] Allow node patch versions to be higher on runtime (#1189) This adjusts the runtime node version check to only compare major and minor version requirements. Systems that don't use the bundled NodeJS might update their node due to security patches, which would currently break OpenSearch Dashboards unnecessarily. ```shell % grep 14.18 ../../package.json "node": "14.18.2", % nvm use 12 Now using node v12.22.8 (npm v6.14.15) % node node_version_validator.js OpenSearch Dashboards does not support the current Node.js version v12.22.8. Please use Node.js ~v14.18. % nvm use 14 Now using node v14.18.3 (npm v6.14.15) % node node_version_validator.js % ``` * Mention Node.js version Dashboards was built with in runtime check error * Use template literals in node version validator * Add additional node validator tests, cleanup * Don't allow node patch versions to be lower on runtime Signed-off-by: Justin Kromlinger --- src/setup_node_env/node_version_validator.js | 14 ++-- .../node_version_validator.test.js | 80 ++++++++++++------- 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/src/setup_node_env/node_version_validator.js b/src/setup_node_env/node_version_validator.js index 4218eeb9e1ea..504d0970ecf6 100644 --- a/src/setup_node_env/node_version_validator.js +++ b/src/setup_node_env/node_version_validator.js @@ -35,16 +35,18 @@ var pkg = require('../../package.json'); var currentVersion = (process && process.version) || null; var rawRequiredVersion = (pkg && pkg.engines && pkg.engines.node) || null; var requiredVersion = rawRequiredVersion ? 'v' + rawRequiredVersion : rawRequiredVersion; -var isVersionValid = !!currentVersion && !!requiredVersion && currentVersion === requiredVersion; +var currentVersionMajorMinorPatch = currentVersion.match(/^v(\d+)\.(\d+)\.(\d+)/); +var requiredVersionMajorMinorPatch = requiredVersion.match(/^v(\d+)\.(\d+)\.(\d+)/); +var isVersionValid = + currentVersionMajorMinorPatch[1] === requiredVersionMajorMinorPatch[1] && + currentVersionMajorMinorPatch[2] === requiredVersionMajorMinorPatch[2] && + parseInt(currentVersionMajorMinorPatch[3], 10) >= parseInt(requiredVersionMajorMinorPatch[3], 10); // Validates current the NodeJS version compatibility when OpenSearch Dashboards starts. if (!isVersionValid) { var errorMessage = - 'OpenSearch Dashboards does not support the current Node.js version ' + - currentVersion + - '. Please use Node.js ' + - requiredVersion + - '.'; + `OpenSearch Dashboards was built with ${requiredVersion} and does not support the current Node.js version ${currentVersion}. ` + + `Please use Node.js ${requiredVersion} or a higher patch version.`; // Actions to apply when validation fails: error report + exit. console.error(errorMessage); diff --git a/src/setup_node_env/node_version_validator.test.js b/src/setup_node_env/node_version_validator.test.js index fc22dce39949..4e95f7219d5a 100644 --- a/src/setup_node_env/node_version_validator.test.js +++ b/src/setup_node_env/node_version_validator.test.js @@ -32,38 +32,64 @@ var exec = require('child_process').exec; var pkg = require('../../package.json'); var REQUIRED_NODE_JS_VERSION = 'v' + pkg.engines.node; -var INVALID_NODE_JS_VERSION = 'v0.10.0'; describe('NodeVersionValidator', function () { - it('should run the script WITH error', function (done) { - var processVersionOverwrite = - "Object.defineProperty(process, 'version', { value: '" + - INVALID_NODE_JS_VERSION + - "', writable: true });"; - var command = - 'node -e "' + processVersionOverwrite + "require('./node_version_validator.js')\""; - - exec(command, { cwd: __dirname }, function (error, stdout, stderr) { - expect(error.code).toBe(1); - expect(stderr).toBeDefined(); - expect(stderr).not.toHaveLength(0); - done(); - }); + it('should run the script WITHOUT error when the version is the same', function (done) { + testValidateNodeVersion(done, REQUIRED_NODE_JS_VERSION); + }); + + it('should run the script WITHOUT error when only the patch version is higher', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, 0, +1)); + }); + + it('should run the script WITH error if the patch version is lower', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, 0, -1), true); + }); + + it('should run the script WITH error if the major version is higher', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(+1, 0, 0), true); + }); + + it('should run the script WITH error if the major version is lower', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(-1, 0, 0), true); + }); + + it('should run the script WITH error if the minor version is higher', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, +1, 0), true); + }); + + it('should run the script WITH error if the minor version is lower', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, -1, 0), true); }); +}); + +function requiredNodeVersionWithDiff(majorDiff, minorDiff, patchDiff) { + var matches = REQUIRED_NODE_JS_VERSION.match(/^v(\d+)\.(\d+)\.(\d+)/); + var major = parseInt(matches[1]) + majorDiff; + var minor = parseInt(matches[2]) + minorDiff; + var patch = parseInt(matches[3]) + patchDiff; - it('should run the script WITHOUT error', function (done) { - var processVersionOverwrite = - "Object.defineProperty(process, 'version', { value: '" + - REQUIRED_NODE_JS_VERSION + - "', writable: true });"; - var command = - 'node -e "' + processVersionOverwrite + "require('./node_version_validator.js')\""; + return `v${major}.${minor}.${patch}`; +} - exec(command, { cwd: __dirname }, function (error, stdout, stderr) { +function testValidateNodeVersion(done, versionToTest, expectError = false) { + var processVersionOverwrite = `Object.defineProperty(process, 'version', { value: '${versionToTest}', writable: true });`; + var command = `node -e "${processVersionOverwrite}require('./node_version_validator.js')"`; + + exec(command, { cwd: __dirname }, function (error, _stdout, stderr) { + expect(stderr).toBeDefined(); + if (expectError) { + expect(error.code).toBe(1); + + var speficicErrorMessage = + `OpenSearch Dashboards was built with ${REQUIRED_NODE_JS_VERSION} and does not support the current Node.js version ${versionToTest}. ` + + `Please use Node.js ${REQUIRED_NODE_JS_VERSION} or a higher patch version.\n`; + + expect(stderr).toStrictEqual(speficicErrorMessage); + } else { expect(error).toBeNull(); - expect(stderr).toBeDefined(); expect(stderr).toHaveLength(0); - done(); - }); + } + done(); }); -}); +}