forked from snyk/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added --fail-on argument for snyk test CLI command. When supplied this argument will check whether any vulns are upgradable or patchable before failing. If a vulnerabilities are found be they cannot be upgraded or patched, a sucessful error code is returned instead. Test cases added. Added help text for new arg.
- Loading branch information
Showing
30 changed files
with
4,025 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { CustomError } from './custom-error'; | ||
import { FAIL_ON } from '../snyk-test/common'; | ||
|
||
export class FailOnError extends CustomError { | ||
private static ERROR_MESSAGE = | ||
'Invalid fail on argument, please use one of: ' + | ||
Object.keys(FAIL_ON).join(' | '); | ||
|
||
constructor() { | ||
super(FailOnError.ERROR_MESSAGE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,7 +207,6 @@ function sendTestPayload( | |
} | ||
|
||
body.filesystemPolicy = filesystemPolicy; | ||
|
||
resolve(body); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import * as tap from 'tap'; | ||
import * as cli from '../../src/cli/commands'; | ||
import { fakeServer } from './fake-server'; | ||
import * as version from '../../src/lib/version'; | ||
import * as sinon from 'sinon'; | ||
import * as snyk from '../../src/lib'; | ||
import { getWorkspaceJSON, chdirWorkspaces } from './workspace-helper'; | ||
|
||
const { test, only } = tap; | ||
(tap as any).runOnly = false; // <- for debug. set to true, and replace a test to only(..) | ||
|
||
const port = (process.env.PORT = process.env.SNYK_PORT = '12345'); | ||
process.env.SNYK_API = 'http://localhost:' + port + '/api/v1'; | ||
process.env.SNYK_HOST = 'http://localhost:' + port; | ||
process.env.LOG_LEVEL = '0'; | ||
const apiKey = '123456789'; | ||
let oldkey; | ||
let oldendpoint; | ||
let versionNumber; | ||
const server = fakeServer(process.env.SNYK_API, apiKey); | ||
const before = tap.runOnly ? only : test; | ||
const after = tap.runOnly ? only : test; | ||
|
||
const pinnableVulnsResult = getWorkspaceJSON( | ||
'fail-on', | ||
'pinnable', | ||
'vulns-result.json', | ||
); | ||
|
||
// snyk test stub responses | ||
const pinnableVulns = getWorkspaceJSON('fail-on', 'pinnable', 'vulns.json'); | ||
|
||
// @later: remove this config stuff. | ||
// Was copied straight from ../src/cli-server.js | ||
before('setup', async (t) => { | ||
versionNumber = await version(); | ||
|
||
t.plan(3); | ||
let key = await cli.config('get', 'api'); | ||
oldkey = key; | ||
t.pass('existing user config captured'); | ||
|
||
key = await cli.config('get', 'endpoint'); | ||
oldendpoint = key; | ||
t.pass('existing user endpoint captured'); | ||
|
||
await new Promise((resolve) => { | ||
server.listen(port, resolve); | ||
}); | ||
t.pass('started demo server'); | ||
t.end(); | ||
}); | ||
|
||
// @later: remove this config stuff. | ||
// Was copied straight from ../src/cli-server.js | ||
before('prime config', async (t) => { | ||
await cli.config('set', 'api=' + apiKey); | ||
t.pass('api token set'); | ||
await cli.config('unset', 'endpoint'); | ||
t.pass('endpoint removed'); | ||
t.end(); | ||
}); | ||
|
||
test('test vulnerable project with pinnable and --fail-on=upgradable', async (t) => { | ||
// mocking test results here as CI tooling does not have python installed | ||
const snykTestStub = sinon.stub(snyk, 'test').returns(pinnableVulns); | ||
try { | ||
server.setNextResponse(pinnableVulnsResult); | ||
chdirWorkspaces('fail-on'); | ||
await cli.test('pinnable', { | ||
failOn: 'upgradable', | ||
}); | ||
t.fail('expected test to throw exception'); | ||
} catch (err) { | ||
t.equal(err.code, 'VULNS', 'should throw exception'); | ||
} finally { | ||
snykTestStub.restore(); | ||
} | ||
}); | ||
|
||
test('test vulnerable project with pinnable and --fail-on=upgradable --json', async (t) => { | ||
// mocking test results here as CI tooling does not have python installed | ||
const snykTestStub = sinon.stub(snyk, 'test').returns(pinnableVulns); | ||
try { | ||
server.setNextResponse(pinnableVulnsResult); | ||
chdirWorkspaces('fail-on'); | ||
await cli.test('pinnable', { | ||
failOn: 'upgradable', | ||
json: true, | ||
}); | ||
t.fail('expected test to throw exception'); | ||
} catch (err) { | ||
t.equal(err.code, 'VULNS', 'should throw exception'); | ||
} finally { | ||
snykTestStub.restore(); | ||
} | ||
}); | ||
|
||
// @later: try and remove this config stuff | ||
// Was copied straight from ../src/cli-server.js | ||
after('teardown', async (t) => { | ||
t.plan(4); | ||
|
||
delete process.env.SNYK_API; | ||
delete process.env.SNYK_HOST; | ||
delete process.env.SNYK_PORT; | ||
t.notOk(process.env.SNYK_PORT, 'fake env values cleared'); | ||
|
||
await new Promise((resolve) => { | ||
server.close(resolve); | ||
}); | ||
t.pass('server shutdown'); | ||
let key = 'set'; | ||
let value = 'api=' + oldkey; | ||
if (!oldkey) { | ||
key = 'unset'; | ||
value = 'api'; | ||
} | ||
await cli.config(key, value); | ||
t.pass('user config restored'); | ||
if (oldendpoint) { | ||
await cli.config('endpoint', oldendpoint); | ||
t.pass('user endpoint restored'); | ||
t.end(); | ||
} else { | ||
t.pass('no endpoint'); | ||
t.end(); | ||
} | ||
}); |
Oops, something went wrong.