Skip to content

Commit

Permalink
feat: suggest using --docker
Browse files Browse the repository at this point in the history
  • Loading branch information
orkamara committed Nov 20, 2018
1 parent e014a67 commit 31ee873
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/cli/commands/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var spinner = require('../../lib/spinner');
var detect = require('../../lib/detect');
var plugins = require('../../lib/plugins');
var ModuleInfo = require('../../lib/module-info');

var docker = require('../../lib/docker');
var SEPARATOR = '\n-------------------------------------------------------\n';

function monitor() {
Expand Down Expand Up @@ -115,8 +115,10 @@ function monitor() {

endpoint.pathname = leader + '/monitor/' + res.id;
var output = formatMonitorOutput(
packageManager, res,
manageUrl, options.json
packageManager,
res,
manageUrl,
options
);
// push a good result
results.push({ok: true, data: output, path: path});
Expand Down Expand Up @@ -172,7 +174,7 @@ function monitor() {
});
}

function formatMonitorOutput(packageManager, res, manageUrl, isJson) {
function formatMonitorOutput(packageManager, res, manageUrl, options) {
var issues = res.licensesPolicy ? 'issues' : 'vulnerabilities';
var strOutput = chalk.bold.white('\nMonitoring ' + res.path + '...\n\n') +
(packageManager === 'yarn' ?
Expand All @@ -189,7 +191,11 @@ function formatMonitorOutput(packageManager, res, manageUrl, isJson) {
'View plans here: ' + manageUrl + '\n\n') :
'');

return isJson ?
if (docker.shouldSuggestDocker(options)) {
strOutput += chalk.bold.white(docker.suggestionText);
}

return options.json ?
JSON.stringify(_.assign({}, res, {
manageUrl: manageUrl,
packageManager: packageManager,
Expand Down
17 changes: 13 additions & 4 deletions src/cli/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var apiTokenExists = require('../../lib/api-token').exists;
var SEVERITIES = require('../../lib/snyk-test/common').SEVERITIES;
var WIZARD_SUPPORTED_PMS =
require('../../lib/snyk-test/common').WIZARD_SUPPORTED_PMS;
var docker = require('../../lib/docker');
var SEPARATOR = '\n-------------------------------------------------------\n';

// arguments array is 0 or more `path` strings followed by
Expand Down Expand Up @@ -212,6 +213,11 @@ function displayResult(res, options) {
var testedInfoText =
'Tested ' + pathOrDepsText + ' for known ' + issuesText;

let dockerSuggestion = '';
if (docker.shouldSuggestDocker(options)) {
dockerSuggestion += chalk.bold.white(docker.suggestionText);
}

// OK => no vulns found, return
if (res.ok && res.vulnerabilities.length === 0) {
var vulnPathsText = options.showVulnPaths ?
Expand All @@ -227,7 +233,7 @@ function displayResult(res, options) {
'\n- Run `snyk test` as part of ' +
'your CI/test.';
return (
prefix + meta + summaryOKText + (isCI ? '' : dockerAdvice + nextStepsText)
prefix + meta + summaryOKText + (isCI ? '' : dockerAdvice + nextStepsText + dockerSuggestion)
);
}

Expand Down Expand Up @@ -260,12 +266,15 @@ function displayResult(res, options) {
'\n\nRun `snyk wizard` to address these issues.'
);
}
if (options.docker && !options.file) {

if (options.docker &&
!options.file &&
(!config.disableSuggestions || config.disableSuggestions !== 'true')) {
summary += chalk.bold.white('\n\n Pro tip: use `--file` option to get base image remediation advice.' +
`\n Example: $ snyk test --docker ${options.path} --file=path/to/Dockerfile`);
`\n Example: $ snyk test --docker ${options.path} --file=path/to/Dockerfile` +
'\n\nTo remove this message in the future, please run `snyk config set disableSuggestions=true`');
}


var vulns = res.vulnerabilities || [];
var groupedVulns = groupVulnerabilities(vulns);
var sortedGroupedVulns = _.orderBy(
Expand Down
5 changes: 5 additions & 0 deletions src/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ if (endpoint) {
config.API = endpoint;
}

var disableSuggestions = require('./user-config').get('disableSuggestions');
if (disableSuggestions) {
config.disableSuggestions = disableSuggestions;
}

var org = require('./user-config').get('org');
if (!config.org && org) {
config.org = org;
Expand Down
22 changes: 22 additions & 0 deletions src/lib/docker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var fs = require('fs');
var config = require('./config');

function shouldSuggestDocker(options) {
const dateToStopDockerPromotion = new Date('2019-01-01');

return (!options.docker &&
fs.existsSync('Dockerfile') &&
(!config.disableSuggestions || config.disableSuggestions !== 'true') &&
Date.now() < dateToStopDockerPromotion);
}

const suggestionText =
'\n\nPro tip: We noticed that there is a Dockerfile in the current directory.' +
'\nConsider using `--docker` to scan your docker images.' +
'\n\nTo remove this message in the future, please run `snyk config set disableSuggestions=true`';


module.exports = {
shouldSuggestDocker: shouldSuggestDocker,
suggestionText: suggestionText,
};

0 comments on commit 31ee873

Please sign in to comment.