Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Linter refactor #78

Merged
merged 12 commits into from
Jan 17, 2019
Merged

Linter refactor #78

merged 12 commits into from
Jan 17, 2019

Conversation

evqna
Copy link
Contributor

@evqna evqna commented Dec 20, 2018

Currently the code that deals with running linters and collecting the results of analysis is scattered around the code base which makes it difficult to change and understand. Additionally, intricacies such as the way gosec interacts with the folder structure have to be accounted in various places which also increases the mental overhead when dealing with simple tasks.

This PR attempts to hide all linter-specific behavior under a common interface Runner that executes the common scanning and reporting logic. The linters folder contains the specifics of each linter runner instance (command line arguments, report parsing logic, ...). I'm not entirely satisfied with the current design (having new Bandit() in javascript is quite ugly IMO), i'm open to suggestions to change it.

Some other notable changes:

  • I dropped the baseline code as it was specific to Bandit and we weren't using it anymore
  • The runner only gives the GitHub report back, not the intermediate scan output so I had to modify all tests

TODO:

  • Switch the main app logic to new runner code
  • Update all tests to use new mechanism
  • Document interface and new code
  • Figure a systematic way to log information
  • Merge report generation code with new linter file? -> Coming in follow up PR

@evqna evqna requested a review from a team December 20, 2018 17:40
cache.js Outdated
} else if (fileType === 'python') {
writeFileCreateDirs(path.join('cache', repoID.toString(), prID.toString(), branchTag, filePath), data)
}
const appTag = (fileType === 'go') ? 'gosec' : 'bandit'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should probably start using a const for 'gosec' and 'bandit'. I've seen the strings more than once. Probably from bandit.js and gosec.js would be appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree but I'd rather tackle that as a follow up.
Opening #103

runner.js Outdated
* @param {string} prID
*/
async function runLinters (files, repoID, prID) {
// TODO: Sync with file download location resolution
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also in the future, it would be nicer if the linters were pluggable and not hardcoded here. I imagine a linter registration model, that the runner would iterate through the loaded linters.

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought the same yes. That would be really cool.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is a rough first draft but it would be nice to have an array of objects and just iterate through it. I'll see what I can do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

linters/index.js now exports a list of code linters, we might add a utility function to register linters dynamically later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that this requires the report generation rewrite, coming in a followup PR

linters/bandit.js Show resolved Hide resolved
runner.js Outdated Show resolved Hide resolved
* @param {any} results Linter results
* @returns GitHub checks report
*/
generateReport (results) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like both bandit.js and gosec.js have this method which just calls another. Do we anticipate future linters doing other work here?

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 now the report generation logic lives in a separate file for bandit and gosec but it may not be necessary as most of it is similar and could be factored away.
This PR is already large enough so right now I'm taking the simplest approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also bringing everything in the same file might make it more difficult to test correctly, though I am open to suggestions.
Of course we can export every helper function but then there is not much point in getting rid of the helper files.

linters/bandit.js Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
test/gosec.spawn.test.js Outdated Show resolved Hide resolved
Copy link
Contributor

@MVrachev MVrachev left a comment

Choose a reason for hiding this comment

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

I don't see where do you use bandit/bandit.js and gosec/gosec.js files.
Also there is something important if you ask me in gosec/gosec.js file which is missing when you spawn Gosec:
image

The reason this is important is that when Gosec fails it throws an exception and the exception usually comes from the fact that the report file doesn't exists and fs.readFileSync throws that exception.
But if you have the logs how Gosec had started it's much more easier for debug as I explain here:
#61

test/gosec.spawn.test.js Show resolved Hide resolved
test/gosec.spawn.test.js Outdated Show resolved Hide resolved
test/gosec.spawn.test.js Show resolved Hide resolved
@evqna evqna added the WIP label Dec 21, 2018
@evqna
Copy link
Contributor Author

evqna commented Dec 21, 2018

@MVrachev Yes I didn't put the logging code back in yet because I wanted to try to use Bunyan (the probot logger) instead of console.log but all things considered this is outside the scope of this PR. I'll copy your changes and we can decide on the logging situation later.

MVrachev pushed a commit that referenced this pull request Jan 10, 2019
The baseline feature is now irrelevant since we rely on GitHub to correctly filter the annotations and only display relevant ones.

This is split off from #78 to help trim it down.
@evqna evqna removed the WIP label Jan 10, 2019
@evqna
Copy link
Contributor Author

evqna commented Jan 10, 2019

Ready for review.

linters/bandit.js Outdated Show resolved Hide resolved
linters/gosec.js Outdated
* relative to the working directory
*/
get reportPath () {
return '../gosec.json'
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here as I mentioned before in the linters/bandit.js file

Copy link
Contributor

Choose a reason for hiding this comment

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

Again I expected change here.
If you have different opinion I will be glad to hear it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same here

test/gosec.spawn.test.js Outdated Show resolved Hide resolved
test/gosec.spawn.test.js Show resolved Hide resolved
test/gosec.spawn.test.js Show resolved Hide resolved
linters/bandit.js Outdated Show resolved Hide resolved
linters/gosec.js Outdated
* relative to the working directory
*/
get reportPath () {
return '../gosec.json'
Copy link
Contributor

Choose a reason for hiding this comment

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

Again I expected change here.
If you have different opinion I will be glad to hear it.

linters/index.js Show resolved Hide resolved
test/gosec.spawn.test.js Show resolved Hide resolved
Antoine Salon added 11 commits January 15, 2019 13:38
Signed-off-by: Antoine Salon <[email protected]>
Signed-off-by: Antoine Salon <[email protected]>
Signed-off-by: Antoine Salon <[email protected]>
Signed-off-by: Antoine Salon <[email protected]>
Signed-off-by: Antoine Salon <[email protected]>
Signed-off-by: Antoine Salon <[email protected]>
Use 'safe' and 'vulnerable' to describe all instances of test files
Copy link
Contributor

@MVrachev MVrachev left a comment

Choose a reason for hiding this comment

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

Now it looks good to me.

@ericwb ericwb merged commit 0dc05ad into securesauce:master Jan 17, 2019
@evqna evqna deleted the linter_refactor branch January 18, 2019 00:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants