From a87e01467a3be9b76ab45e1922d2c48b3d81499b Mon Sep 17 00:00:00 2001 From: Fred Blaise Date: Fri, 8 Jan 2021 18:12:41 +0100 Subject: [PATCH 1/6] initial parser doc --- doc/guide_to_parser_writing.md | 85 ++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 doc/guide_to_parser_writing.md diff --git a/doc/guide_to_parser_writing.md b/doc/guide_to_parser_writing.md new file mode 100644 index 00000000000..7908d89a16c --- /dev/null +++ b/doc/guide_to_parser_writing.md @@ -0,0 +1,85 @@ +# How to write a DefectDojo parser + +> All commands assume that you're located at the root of the django-DefectDojo cloned repo. + +## Pre-requisites +- You have forked https://github.com/DefectDojo/django-DefectDojo and cloned locally. +- Checkout `dev` and make sure you're up to date with the latest changes. + +It is probably easier to use the docker-compose stack (and benefit from the hot-reload capbility for uWSGI). +Set up your environment to use the dev or ptvsd environment, such as: + +`$ docker/setEnv.sh dev` +or +`$ docker/setEnv.sh ptvsd` (allows to set breakpoints in uWSGI) + +Please have a look at [../DOCKER.md] for more details. + +### docker images +You'd want to build your docker images locally, and eventually pass in your local user's `uid` to be able to write to the image (handy for database migration files). Assuming your user's `uid` is `1000`, then: + +`$ docker-compose build --build-arg uid=1000` + +## Which files do you need to modify? + +| File | Purpose +|------- |-------- +|dojo/fixtures/test_type.json | Django fixture for the type of scan. +dojo/templates/dojo/import_scan_results.html | Add the scan to the array presented in the drop-down box +dojo/tools//__init__.py | Empty file for class initialization +dojo/tools//parser.py | The meat. This is where you write your actual parser +dojo/unittests/scans//{many_vulns,no_vuln,one_vuln}.json | Sample files containing meaningful data for unit tests. The minimal set. +dojo/unittests/test__parser.py | The unittest class, holding unit tests definitions +dojo/tools/factory.py | Import there your new parser class and add it to the long "if/else" statement + +## Unit tests + +Each parser must have unit tests, at least to test for 0 vuln, 1 vuln and many vulns. You can take a look at how other parsers have them for starters. The more quality tests, the better. + +### Test database +To test your unit tests locally, you first need to grant some rights: + +``` +MYSQL> grant all privileges on test_defectdojo.* to defectdojo@'%'; +MYSQL> flush privileges; +``` + +### Run your tests + +This local command will launch the unit test for your new parser + +`$ docker-compose exec uwsgi bash -c 'python manage.py test dojo.unittests.. -v2'` + +Example for the blackduck hub parser: + +`$ docker-compose exec uwsgi bash -c 'python manage.py test dojo.unittests.test_blackduck_csv_parser.TestBlackduckHubParser -v2'` + +> If you want to run all unit tests, simply run `$ docker-compose exec uwsgi bash -c 'python manage.py test dojo.unittests -v2'` + +## Other files that could be involved + +### Change to the model +In the event where you'd have to change the model, e.g. to increase a database column size to accomodate a longer string of data to be saved +* Change what you need in `dojo/models.py` +* Create a new migration file in dojo/db_migrations by running and including as part of your PR + + `$ docker-compose exec uwsgi bash -c 'python manage.py makemigrations -v2'` + +### Accept a different type of file to upload +If you want to be able to accept a new type of file for your parser, take a look at `dojo/forms.py` around line 436 (at the time of this writing) or locate the 2 places (for import and re-import) where you find the string `attrs={"accept":`. + +Formats currently accepted: .xml, .csv, .nessus, .json, .html, .js, .zip. + +### A need for more than just the parser.py + +Of course, nothing prevents you from having more files than the `parser.py` file. It's python :-) + +## Example PRs + +If you want to take a look at previous parsers that are now part of DefectDojo, take a look at https://github.com/DefectDojo/django-DefectDojo/pulls?q=is%3Apr+label%3A%22import+scans%22+ + +## Update the readthedocs documentation + +The DefectDojo official documentation lives in another repository, https://github.com/DefectDojo/documentation + +Please update the `docs/integration.rst` with the details of your new parser and create a PR in that repo. Reference the PR in the main DefectDojo repository to establish an automatic link between the two. From 9808506c1fe34d84736d385552768cb4b89e84f2 Mon Sep 17 00:00:00 2001 From: Fred Blaise Date: Fri, 8 Jan 2021 18:20:11 +0100 Subject: [PATCH 2/6] fixes --- doc/guide_to_parser_writing.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/doc/guide_to_parser_writing.md b/doc/guide_to_parser_writing.md index 7908d89a16c..c4f4dc6df00 100644 --- a/doc/guide_to_parser_writing.md +++ b/doc/guide_to_parser_writing.md @@ -5,6 +5,7 @@ ## Pre-requisites - You have forked https://github.com/DefectDojo/django-DefectDojo and cloned locally. - Checkout `dev` and make sure you're up to date with the latest changes. +- It's advised that you create a dedicated branch for your development, such as `git checkout -b parser-name` yet that's up to you. It is probably easier to use the docker-compose stack (and benefit from the hot-reload capbility for uWSGI). Set up your environment to use the dev or ptvsd environment, such as: @@ -13,7 +14,7 @@ Set up your environment to use the dev or ptvsd environment, such as: or `$ docker/setEnv.sh ptvsd` (allows to set breakpoints in uWSGI) -Please have a look at [../DOCKER.md] for more details. +Please have a look at [DOCKER.md](../DOCKER.md) for more details. ### docker images You'd want to build your docker images locally, and eventually pass in your local user's `uid` to be able to write to the image (handy for database migration files). Assuming your user's `uid` is `1000`, then: @@ -24,20 +25,20 @@ You'd want to build your docker images locally, and eventually pass in your loca | File | Purpose |------- |-------- -|dojo/fixtures/test_type.json | Django fixture for the type of scan. -dojo/templates/dojo/import_scan_results.html | Add the scan to the array presented in the drop-down box -dojo/tools//__init__.py | Empty file for class initialization -dojo/tools//parser.py | The meat. This is where you write your actual parser -dojo/unittests/scans//{many_vulns,no_vuln,one_vuln}.json | Sample files containing meaningful data for unit tests. The minimal set. -dojo/unittests/test__parser.py | The unittest class, holding unit tests definitions -dojo/tools/factory.py | Import there your new parser class and add it to the long "if/else" statement +|`dojo/fixtures/test_type.json` | Django fixture for the type of scan. +|`dojo/templates/dojo/import_scan_results.html` | Add the scan to the array presented in the drop-down box +|`dojo/tools//__init__.py` | Empty file for class initialization +|`dojo/tools//parser.py` | The meat. This is where you write your actual parser +|`dojo/unittests/scans//{many_vulns,no_vuln,one_vuln}.json` | Sample files containing meaningful data for unit tests. The minimal set. +|`dojo/unittests/test__parser.py` | The unittest class, holding unit tests definitions +|`dojo/tools/factory.py` | Import there your new parser class and add it to the long "if/else" statement ## Unit tests Each parser must have unit tests, at least to test for 0 vuln, 1 vuln and many vulns. You can take a look at how other parsers have them for starters. The more quality tests, the better. ### Test database -To test your unit tests locally, you first need to grant some rights: +To test your unit tests locally, you first need to grant some rights. Get your MySQL root password from the docker-compose logs, login as root and issue the following commands: ``` MYSQL> grant all privileges on test_defectdojo.* to defectdojo@'%'; From 02629a8c93378098029f8ccb9a37ea3e64a80a65 Mon Sep 17 00:00:00 2001 From: Fred Blaise Date: Fri, 8 Jan 2021 19:10:20 +0100 Subject: [PATCH 3/6] add ref to new parser guide file --- CONTRIBUTING.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 296a0d212e2..2d779f2e4e0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,6 +12,10 @@ Bugs that do not have this information will be closed. Here are a few things to keep in mind when making changes to DefectDojo. +## Writing a new parser + +Please see [this file](doc/guide_to_parser_writing.md) for guidance on how to write a parser. + ## Modifying DefectDojo and Testing Please use [these test scripts](./tests) to test your changes. These are the scripts we run in our [integration tests](DOCKER.md#run-the-tests-with-docker). From 158fa7f181a05e8a758b6acb50d6f231dfc0f3cb Mon Sep 17 00:00:00 2001 From: Fred Blaise Date: Sun, 10 Jan 2021 00:20:19 +0100 Subject: [PATCH 4/6] add note to pr template --- .../PULL_REQUEST_TEMPLATE/pull_request_template.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md b/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md index 10ed0ebb6d7..0c6e0d3682b 100644 --- a/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md +++ b/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md @@ -1,6 +1,7 @@ -**Descriptions** +**Description** -Describe the feature / bug fix implemented by this PR +Describe the feature / bug fix implemented by this PR. +If this is a new parser, [the parser guide](../../doc/guide_to_parser_writing.md) may be worth (re)reading. **Test results** @@ -55,12 +56,12 @@ In case of conflict: git rebase --continue ``` -When everything's fine on your local branch, force push to your `myOrigin` remote: +When everything's fine on your local branch, force push to your `myOrigin` remote: ``` git push myOrigin --force-with-lease ``` -To cancel everything: +To cancel everything: ``` git rebase --abort ``` @@ -74,7 +75,7 @@ git rebase -i origin/dev - Replace `pick` by `reword` on the first commit if you want to change the commit message - Save the file and quit your editor -Force push to your `myOrigin` remote: +Force push to your `myOrigin` remote: ``` git push myOrigin --force-with-lease ``` From dfa1587a882c84ecf2edb13dac9fb932a69239e4 Mon Sep 17 00:00:00 2001 From: Fred Blaise Date: Fri, 8 Jan 2021 20:51:31 +0100 Subject: [PATCH 5/6] ref to KeyError --- doc/guide_to_parser_writing.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/guide_to_parser_writing.md b/doc/guide_to_parser_writing.md index c4f4dc6df00..c497d9d8462 100644 --- a/doc/guide_to_parser_writing.md +++ b/doc/guide_to_parser_writing.md @@ -33,6 +33,12 @@ You'd want to build your docker images locally, and eventually pass in your loca |`dojo/unittests/test__parser.py` | The unittest class, holding unit tests definitions |`dojo/tools/factory.py` | Import there your new parser class and add it to the long "if/else" statement +## Things to pay attention to + +Parsers may have many fields, out of which many of them may be optional. + +Always make sure you include checks to avoid potential `KeyError` errors (e.g. field does not exist), for those fields you are not absolutely certain will always be in file that will get uploaded. These translate to 500 error, and do not look good. + ## Unit tests Each parser must have unit tests, at least to test for 0 vuln, 1 vuln and many vulns. You can take a look at how other parsers have them for starters. The more quality tests, the better. From b59c2c8c511cace93f1b0fd2e79da927f232785c Mon Sep 17 00:00:00 2001 From: Fred Blaise Date: Sun, 10 Jan 2021 16:26:32 +0100 Subject: [PATCH 6/6] add hint for test_type fixture --- doc/guide_to_parser_writing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/guide_to_parser_writing.md b/doc/guide_to_parser_writing.md index c497d9d8462..aa24efb38da 100644 --- a/doc/guide_to_parser_writing.md +++ b/doc/guide_to_parser_writing.md @@ -25,7 +25,7 @@ You'd want to build your docker images locally, and eventually pass in your loca | File | Purpose |------- |-------- -|`dojo/fixtures/test_type.json` | Django fixture for the type of scan. +|`dojo/fixtures/test_type.json` | Django fixture for the type of scan. Take the next available integer if you intend to push upstream. If you're planning to use only in your own fork, you could jump ahead by 1000 to avoid any potential conflicts. |`dojo/templates/dojo/import_scan_results.html` | Add the scan to the array presented in the drop-down box |`dojo/tools//__init__.py` | Empty file for class initialization |`dojo/tools//parser.py` | The meat. This is where you write your actual parser