Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit df0b11a

Browse files
kuzhelovmanajdov
authored and
manajdov
committed
chore: cache results of vulnerability scans (#621)
* implement caching strategy * adjust file name of scan marker * add yarn lock hash to marker file name * add change to build config * fix dir name in build config * improve caching strategy * just restore cache * temporary remove lint and tests * try * fix caching strategy * try * try * try * try epoch * create file on scan * return lint and test steps * introduce comment for the caching approach taken * remove unnecessary function * simplify expression for marker file name
1 parent fa2e9bd commit df0b11a

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

.circleci/config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,18 @@ jobs:
4949
- run:
5050
name: Report coverage
5151
command: bash <(curl -s https://codecov.io/bash)
52+
53+
- restore_cache:
54+
key: v1-vuln-scans-{{ checksum "yarn.lock" }}
5255
- run:
5356
name: Vulnerability Tests
5457
command: yarn test:vulns
58+
# https://discuss.circleci.com/t/add-mechanism-to-update-existing-cache-key/9014/12
59+
- save_cache:
60+
key: v1-vuln-scans-{{ checksum "yarn.lock" }}-{{ epoch }}
61+
paths:
62+
- .vuln-scans
63+
5564
- run:
5665
name: Visual Tests
5766
command: yarn test:visual

build/gulp/tasks/test-vulns.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import * as fs from 'fs'
2+
import { task } from 'gulp'
3+
import * as path from 'path'
4+
import debug from 'debug'
5+
6+
import config from '../../../config'
7+
import sh from '../sh'
8+
9+
const { paths } = config
10+
11+
const SCAN_RESULTS_DIR_NAME = '.vuln-scans'
12+
const SCAN_RESULTS_DIR_PATH = paths.base(SCAN_RESULTS_DIR_NAME)
13+
14+
const log = message => debug.log(message)
15+
log.success = message => debug.log(`✔ ${message}`)
16+
17+
const ensureDirExists = path => {
18+
if (!fs.existsSync(path)) {
19+
sh(`mkdir -p ${path}`)
20+
}
21+
}
22+
23+
const getTodayScanFilePath = () => {
24+
const now = new Date()
25+
26+
const year = now.getUTCFullYear()
27+
const month = now.getUTCMonth() + 1
28+
const date = now.getUTCDate()
29+
30+
const fileName = `snyk-scanned-${year}-${month}-${date}`
31+
32+
return path.resolve(SCAN_RESULTS_DIR_PATH, fileName)
33+
}
34+
35+
const recentlyChecked = () => {
36+
const recentCheckFilePath = getTodayScanFilePath()
37+
return fs.existsSync(recentCheckFilePath)
38+
}
39+
40+
const registerRecentSucessfulScan = async () => {
41+
ensureDirExists(SCAN_RESULTS_DIR_PATH)
42+
43+
const recentScanFilePath = getTodayScanFilePath()
44+
await sh(`touch ${recentScanFilePath}`)
45+
}
46+
47+
/**
48+
* The following strategy is used to perform vulnerabilites scan
49+
* - check if there is marker of recent sucessful scan
50+
* - if this marker exists, skip checks
51+
* - if there is no marker, perform check
52+
* - if check is successful, create successful check marker
53+
*/
54+
task('test:vulns', async () => {
55+
if (recentlyChecked()) {
56+
log.success('Vulnerabilities check was already performed recently, skipping..')
57+
return
58+
}
59+
60+
log('Scanning dependency packages for vulnerabilities..')
61+
await sh(`yarn snyk test`)
62+
log.success('Vulnerability scan is successfully passed.')
63+
64+
registerRecentSucessfulScan()
65+
})

gulpfile.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require('./build/gulp/tasks/screener')
1414
require('./build/gulp/tasks/git')
1515
require('./build/gulp/tasks/test-unit')
1616
require('./build/gulp/tasks/test-projects')
17+
require('./build/gulp/tasks/test-vulns')
1718

1819
// global tasks
1920
task('build', series('dll', parallel('dist', 'build:docs')))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"pretest": "yarn satisfied",
3636
"test": "gulp test",
3737
"test:watch": "gulp test:watch",
38-
"test:vulns": "snyk test",
38+
"test:vulns": "gulp test:vulns",
3939
"test:visual": "gulp screener",
4040
"test:projects": "gulp test:projects",
4141
"generate:component": "gulp generate:component"

0 commit comments

Comments
 (0)