From 31ee873934e44f1d78e44711b85613aebcfe429f Mon Sep 17 00:00:00 2001 From: Or Kamara Date: Sun, 18 Nov 2018 17:34:08 +0200 Subject: [PATCH] feat: suggest using --docker --- src/cli/commands/monitor.js | 16 +++++++++++----- src/cli/commands/test.js | 17 +++++++++++++---- src/lib/config.js | 5 +++++ src/lib/docker.js | 22 ++++++++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 src/lib/docker.js diff --git a/src/cli/commands/monitor.js b/src/cli/commands/monitor.js index 036383ea68..f49ec7a662 100644 --- a/src/cli/commands/monitor.js +++ b/src/cli/commands/monitor.js @@ -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() { @@ -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}); @@ -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' ? @@ -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, diff --git a/src/cli/commands/test.js b/src/cli/commands/test.js index eb87f3d0c9..402e3fe409 100644 --- a/src/cli/commands/test.js +++ b/src/cli/commands/test.js @@ -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 @@ -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 ? @@ -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) ); } @@ -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( diff --git a/src/lib/config.js b/src/lib/config.js index d6c535d377..f8810c4627 100644 --- a/src/lib/config.js +++ b/src/lib/config.js @@ -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; diff --git a/src/lib/docker.js b/src/lib/docker.js new file mode 100644 index 0000000000..5c6aaa15c5 --- /dev/null +++ b/src/lib/docker.js @@ -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, +};