Skip to content

Commit

Permalink
fix(config): add package name sanitisation (#212)
Browse files Browse the repository at this point in the history
Attempting to deploy an application that fails to meet openshift's
naming rules will result in misleading error messages being returned.

This commit addresses the issue by verifying the package.name is
compliant with openshift's naming restrictions.

fix linter issues

remove wc file

remove added line

fixes #211
  • Loading branch information
evanshortiss authored and lholmquist committed Apr 10, 2018
1 parent 6f8727c commit 1c18b2a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
6 changes: 5 additions & 1 deletion bin/nodeshift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ function commandHandler (argv) {
})
.catch((err) => {
log.error(err.message);
log.error('Status code', err.statusCode);

if (err && err.statusCode) {
log.error('Status code', err.statusCode);
}

require('util').debuglog('nodeshift.cli')(err.stack);
process.exit(1);
});
Expand Down
4 changes: 4 additions & 0 deletions lib/nodeshift-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ async function setup (options = {}) {
const config = await openshiftConfigLoader(Object.assign({}, {tryServiceAccount: options.tryServiceAccount, configLocation: options.configLocation}));
logger.info(`using namespace ${config.context.namespace} at ${config.cluster}`);

if (!projectPackage.name.match(/^[a-z][0-9a-z-]+[0-9a-z]$/)) {
throw new Error('"name" in package.json can only consist lower-case letters, numbers, and dashes. It must start with a letter and can\'t end with a -.');
}

// Return a new object with the config, the rest client and other data.
return Object.assign({}, config, {
projectPackage,
Expand Down
45 changes: 45 additions & 0 deletions test/nodeshift-config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,51 @@ test('nodeshift-config no package.json', (t) => {
});
});

test('nodeshift-config invalid "name" in package.json', (t) => {
const nodeshiftConfig = proxyquire('../lib/nodeshift-config', {
'openshift-config-loader': () => {
return Promise.resolve({
context: {
namespace: 'test-namespace'
},
cluster: 'http://mock-cluster'
});
},
'openshift-rest-client': () => { return Promise.resolve({}); }
});

const tmpDir = require('os').tmpdir();
const join = require('path').join;
const fs = require('fs');

const options = {
projectLocation: join(tmpDir, 'nodeshift-invalid-package-name-test')
};

if (!fs.existsSync(options.projectLocation)) {
fs.mkdirSync(options.projectLocation);
}

// Create a temp package that has an invalid name, but extends the example JSON
fs.writeFileSync(
join(options.projectLocation, 'package.json'),
JSON.stringify(
Object.assign(
{},
require('../examples/sample-project/package.json'),
{
name: '@invalid-package-name'
}
)
)
);

nodeshiftConfig(options).catch((err) => {
t.equal(err.message.includes('"name" in package.json can only consist lower-case letters, numbers, and dashes. It must start with a letter and can\'t end with a -.'), true);
t.end();
});
});

test('nodeshift-config options for the config loader', (t) => {
const nodeshiftConfig = proxyquire('../lib/nodeshift-config', {
'openshift-config-loader': (options) => {
Expand Down

0 comments on commit 1c18b2a

Please sign in to comment.