Skip to content

Commit

Permalink
Allow node patch versions to be higher on runtime (opensearch-project…
Browse files Browse the repository at this point in the history
…#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 <[email protected]>
  • Loading branch information
hashworks authored and kavilla committed Jun 16, 2022
1 parent 94aac78 commit 952570b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 33 deletions.
14 changes: 8 additions & 6 deletions src/setup_node_env/node_version_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
80 changes: 53 additions & 27 deletions src/setup_node_env/node_version_validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
}

0 comments on commit 952570b

Please sign in to comment.