This repository was archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
[Suggestion] Add module configuration extendability #126
Open
bekh6ex
wants to merge
5
commits into
hawkeyesec:master
Choose a base branch
from
bekh6ex:module-config
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
720b096
Add module configuration extendability
bekh6ex 68a8f36
Add generic module configuration schema tests
bekh6ex c56b164
Add test that empty object is a valid config for every module
bekh6ex fdd4c18
Improve module configuration help output
bekh6ex 0d3ccaa
Enforce type definition of root level properties
bekh6ex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,13 @@ | ||
| { | ||
| "all": true, | ||
| "modules": ["files-secrets"], | ||
| "failOn": "low", | ||
| "showCode": false, | ||
| "moduleConfig": { | ||
| "files-secrets": { | ||
| "files": [ | ||
| "patterns.json" | ||
| ] | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,43 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| 'use strict' | ||
|
|
||
| const modules = require('../lib/modules') | ||
| require('colors') | ||
| const program = require('commander') | ||
|
|
||
| program | ||
| .arguments('<module>') | ||
| .action(printModuleConfigurationDocumentation); | ||
|
|
||
| program.parse(process.argv); | ||
|
|
||
| function printModuleConfigurationDocumentation(moduleKey) { | ||
| console.log(`${moduleKey} module configuration properties:`.bold) | ||
| console.log('') | ||
|
|
||
| let module = modules().filter(m => m.key === moduleKey)[0]; | ||
| const schema = module.configSchema; | ||
| for (const [k, v] of Object.entries(schema.properties)) { | ||
| let type = v.type; | ||
| if (type === 'array') { | ||
| type = v.items.type + '[]' | ||
| } | ||
| console.log(` ${k.bold} (${type}): ${v.title}`) | ||
| } | ||
|
|
||
| const exampleConfig = { | ||
| moduleConfig:{ | ||
| } | ||
| } | ||
|
|
||
| console.log('') | ||
| console.log('.hawkeyerc examples:'.bold) | ||
| schema.examples.forEach((e) => { | ||
| exampleConfig.moduleConfig[module.key] = e; | ||
| let example = JSON.stringify(exampleConfig, undefined, 2).padStart(4); | ||
| console.log(example) | ||
| console.log('') | ||
| }) | ||
| } | ||
|
|
This file contains hidden or 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,13 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| 'use strict' | ||
|
|
||
| const logger = require('../lib/logger') | ||
| const modules = require('../lib/modules') | ||
| require('colors') | ||
|
|
||
| logger.log('Module Status'.bold) | ||
| modules().forEach(m => { | ||
| logger.log(`${m.enabled ? 'Enabled: '.green : 'Disabled: '.red} ${m.key.bold}`) | ||
| logger.log(' ' + m.description) | ||
| }) |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,14 +1,92 @@ | ||
| 'use strict' | ||
|
|
||
| /* eslint-disable no-unused-expressions */ | ||
|
|
||
| const Ajv = require('ajv') | ||
|
|
||
| const modules = require('../modules') | ||
|
|
||
| describe('Modules', () => { | ||
| modules().forEach(module => { | ||
| describe(module.key, () => { | ||
| it('should have the module signature', () => { | ||
| it('should have required properties', () => { | ||
| let expectMethods = ['key', 'description', 'handles', 'run', 'enabled'].sort() | ||
| expect(Object.keys(module).sort()).to.deep.equal(expectMethods) | ||
| expect(module).to.include.all.keys(expectMethods) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Configurable Modules', () => { | ||
| modules() | ||
| .filter(m => m.configSchema) | ||
| .forEach(module => { | ||
| describe(`${module.key} config schema`, () => { | ||
| it('should be a valid JSON Schema', () => { | ||
| const ajv = new Ajv({ | ||
| strictDefaults: true, | ||
| strictKeywords: true | ||
| }) | ||
|
|
||
| expect(ajv.validateSchema(module.configSchema)).to.be.true | ||
| }) | ||
|
|
||
| it('should expect an object', () => { | ||
| expect(module.configSchema.type).to.be.equal('object') | ||
| }) | ||
|
|
||
| it('should have at least one example', () => { | ||
| expect(module.configSchema.examples).to.have.lengthOf.at.least(1) | ||
| }) | ||
|
|
||
| it('should have valid examples', () => { | ||
| const ajv = new Ajv({ | ||
| strictDefaults: true, | ||
| strictKeywords: true, | ||
| removeAdditional: true | ||
| }) | ||
|
|
||
| if (!module.configSchema.hasOwnProperty('additionalProperties')) { | ||
| module.configSchema.additionalProperties = false | ||
| } | ||
|
|
||
| module.configSchema.examples.forEach((e, i) => { | ||
| const validate = ajv.compile(module.configSchema) | ||
| expect(validate(e), `Example #${i}`).to.be.true | ||
| }) | ||
| }) | ||
|
|
||
| it('should have some title for root level properties', () => { | ||
| for (const [property, definition] of Object.entries(module.configSchema.properties)) { | ||
| expect(definition.title, property).to.not.be.empty | ||
| } | ||
| }) | ||
|
|
||
| it('should have some type defined for root level properties', () => { | ||
| for (const [property, definition] of Object.entries(module.configSchema.properties)) { | ||
| expect(definition.type, property).to.not.be.empty | ||
|
|
||
| if (definition.type === 'array') { | ||
| expect(definition.items.type, `${property} items`).to.not.be.empty | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| it('should accept empty object as valid config', () => { | ||
| const ajv = new Ajv({ | ||
| strictDefaults: true, | ||
| strictKeywords: true, | ||
| removeAdditional: true, | ||
| useDefaults: true | ||
| }) | ||
|
|
||
| if (!module.configSchema.hasOwnProperty('additionalProperties')) { | ||
| module.configSchema.additionalProperties = false | ||
| } | ||
|
|
||
| const validate = ajv.compile(module.configSchema) | ||
| expect(validate({})).to.be.true | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or 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,26 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "type": "object", | ||
| "title": "Schema of Hawkeye module files-secrets", | ||
| "properties": { | ||
| "files": { | ||
| "type": "array", | ||
| "title": "List of files containing patterns of suspicious filenames", | ||
| "default": [], | ||
| "items": { | ||
| "type": "string", | ||
| "title": "Path to file", | ||
| "examples": [ | ||
| "path/to/patterns.json" | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
| "examples": [ | ||
| { | ||
| "files": [ | ||
| "path/to/patterns.json" | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ const modules = require('./modules') | |
| const logger = require('./logger') | ||
| const _ = require('lodash') | ||
| require('colors') | ||
| const Ajv = require('ajv') | ||
|
|
||
| module.exports = async (rc = {}) => { | ||
| logger.log('Target for scan:', rc.target) | ||
|
|
@@ -26,9 +27,14 @@ module.exports = async (rc = {}) => { | |
|
|
||
| const activeModules = [] | ||
| const inactiveModules = [] | ||
| knownModules | ||
| .filter(canBeConfigured) | ||
| .filter(m => rc.moduleConfig.hasOwnProperty(m.key)) | ||
| .forEach(m => validateAndNormalizeModuleConfig(m, rc.moduleConfig[m.key])) | ||
|
|
||
| for (const module of knownModules) { | ||
| logger.log(`Checking ${module.key} for applicability`) | ||
| const isActive = await module.handles(fm) | ||
| const isActive = await module.handles(fm, rc.moduleConfig[module.key]) | ||
| ;(isActive ? activeModules : inactiveModules).push(module) | ||
| } | ||
| inactiveModules.forEach(module => logger.log('Skipping module'.bold, module.key)) | ||
|
|
@@ -41,7 +47,7 @@ module.exports = async (rc = {}) => { | |
| .reduce((prom, { key, run }) => prom.then(async allRes => { | ||
| logger.log('Running module'.bold, key) | ||
| try { | ||
| const res = await run(fm) | ||
| const res = await run(fm, rc.moduleConfig[module.key]) | ||
| return allRes.concat(res) | ||
| } catch (e) { | ||
| logger.error(key, 'returned an error!', e.message) | ||
|
|
@@ -73,3 +79,29 @@ module.exports = async (rc = {}) => { | |
|
|
||
| return results.length ? 1 : 0 | ||
| } | ||
|
|
||
| function canBeConfigured (module) { | ||
| return !!module.configSchema | ||
| } | ||
|
|
||
| /** | ||
| * Not only validates the config, but also removes fields that are not defined in schema and | ||
| * puts default value in if property is undefined | ||
| */ | ||
| function validateAndNormalizeModuleConfig (module, config) { | ||
| const ajv = new Ajv({ | ||
| strictDefaults: true, | ||
| strictKeywords: true, | ||
| removeAdditional: true, | ||
| useDefaults: true | ||
| }) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (!module.configSchema.hasOwnProperty('additionalProperties')) { | ||
| module.configSchema.additionalProperties = false | ||
| } | ||
|
|
||
| const validate = ajv.compile(module.configSchema) | ||
| const valid = validate(config) | ||
| if (!valid) { | ||
| throw new Error(`Config for module '${module.key}' is invalid:\n` + validate.errors.map(JSON.stringify).join('\n')) | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To generate reasonable documentation (help) we can enforce by tests things like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also we can try to use this definition for cli arguments. Not sure how hard it will be if we want to make it good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... or limit cli arguments to what we have now and allow to pass
.hawkeyercas an argument or even read it from stdIn