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

chore: cache results of vulnerability scans #621

Merged
merged 20 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,17 @@ jobs:
- run:
name: Report coverage
command: bash <(curl -s https://codecov.io/bash)

- restore_cache:
key: v1-vuln-scans-{{ checksum "yarn.lock" }}
- run:
name: Vulnerability Tests
command: yarn test:vulns
- save_cache:
key: v1-vuln-scans-{{ checksum "yarn.lock" }}-{{ epoch }}
Copy link
Member

@layershifter layershifter Dec 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay that we restoring a cache with a key without the epoch variable?

Copy link
Contributor Author

@kuzhelov kuzhelov Dec 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is necessary because otherwise Circle CI won't update the cache entry if it has existed before (this is its feature). To avoid this 'rewrite ban' the following strategy was suggested: https://discuss.circleci.com/t/add-mechanism-to-update-existing-cache-key/9014/12

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed, please add a small comment with this link before the key line 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed to introduce comment for that

paths:
- .vuln-scans

- run:
name: Visual Tests
command: yarn test:visual
Expand Down
65 changes: 65 additions & 0 deletions build/gulp/tasks/test-vulns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as fs from 'fs'
import { task } from 'gulp'
import * as path from 'path'
import debug from 'debug'

import config from '../../../config'
import sh from '../sh'

const { paths } = config

const SCAN_RESULTS_DIR_NAME = '.vuln-scans'

const log = message => debug.log(message)
log.success = message => debug.log(`✔ ${message}`)

const ensureDirExists = path => {
if (!fs.existsSync(path)) {
sh(`mkdir -p ${path}`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fs.mkdirSync(path, { recursive: true })?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for that we need Node LTS v10 (and this is, actually, the step we should make). Agreed to defer it to the follow-up PR, to be absolutely sure that all the necessary accompanying adjustments to the code will be made

}
}

const getScanResultsDirPath = () => {
return paths.base(SCAN_RESULTS_DIR_NAME)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need it? As I see it can be a simple variable


const getTodayScanFilePath = () => {
const now = new Date()

const fileName = `snyk-scanned-${now.getUTCFullYear()}-${now.getUTCMonth() +
1}-${now.getUTCDate()}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we split this to multiple variables? To avoid multiline there


return path.resolve(getScanResultsDirPath(), fileName)
}

const recentlyChecked = () => {
const recentCheckFilePath = getTodayScanFilePath()
return fs.existsSync(recentCheckFilePath)
}

const registerRecentSucessfulScan = async () => {
ensureDirExists(getScanResultsDirPath())

const recentScanFilePath = getTodayScanFilePath()
await sh(`touch ${recentScanFilePath}`)
}

/**
* The following strategy is used to perform vulnerabilites scan
* - check if there is marker of recent sucessful scan
* - if this marker exists, skip checks
* - if there is no marker, perform check
* - if check is successful, create successful check marker
*/
task('test:vulns', async () => {
if (recentlyChecked()) {
log.success('Vulnerabilities check was already performed recently, skipping..')
return
}

log('Scanning dependency packages for vulnerabilities..')
await sh(`yarn snyk test`)
log.success('Vulnerability scan is successfully passed.')

registerRecentSucessfulScan()
})
1 change: 1 addition & 0 deletions gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require('./build/gulp/tasks/screener')
require('./build/gulp/tasks/git')
require('./build/gulp/tasks/test-unit')
require('./build/gulp/tasks/test-projects')
require('./build/gulp/tasks/test-vulns')

// global tasks
task('build', series('dll', parallel('dist', 'build:docs')))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"pretest": "yarn satisfied",
"test": "gulp test",
"test:watch": "gulp test:watch",
"test:vulns": "snyk test",
"test:vulns": "gulp test:vulns",
"test:visual": "gulp screener",
"test:projects": "gulp test:projects",
"generate:component": "gulp generate:component"
Expand Down