|
| 1 | +--- |
| 2 | +title: "DefectDojo API v2" |
| 3 | +description: "DefectDojo's API lets you automate tasks, e.g. uploading scan reports in CI/CD pipelines." |
| 4 | +draft: false |
| 5 | +weight: 2 |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +DefectDojo\'s API is created using [Django Rest |
| 12 | +Framework](http://www.django-rest-framework.org/). The documentation of |
| 13 | +each endpoint is available within each DefectDojo installation at |
| 14 | +[`/api/v2/doc/`](https://demo.defectdojo.org/api/v2/) and can be accessed by choosing the API v2 |
| 15 | +Docs link on the user drop down menu in the header. |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +The documentation is generated using [drf-spectacular](https://drf-spectacular.readthedocs.io/) at [`/api/v2/oa3/swagger-ui/`](https://demo.defectdojo.org/api/v2/oa3/swagger-ui/), and is |
| 20 | +interactive. On the top of API v2 docs is a link that generates an OpenAPI v3 spec. |
| 21 | + |
| 22 | +To interact with the documentation, a valid Authorization header value |
| 23 | +is needed. Visit the `/api/key-v2` view to generate your |
| 24 | +API Key (`Token <api_key>`) and copy the header value provided. |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +Each section allows you to make calls to the API and view the Request |
| 29 | +URL, Response Body, Response Code and Response Headers. |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +If you're logged in to the Defect Dojo web UI, you do not need to provide the authorization token. |
| 34 | + |
| 35 | +## Authentication |
| 36 | + |
| 37 | +The API uses header authentication with API key. The format of the |
| 38 | +header should be: : |
| 39 | + |
| 40 | + Authorization: Token <api.key> |
| 41 | + |
| 42 | +For example: : |
| 43 | + |
| 44 | + Authorization: Token c8572a5adf107a693aa6c72584da31f4d1f1dcff |
| 45 | + |
| 46 | +### Alternative authentication method |
| 47 | + |
| 48 | +If you use [an alternative authentication method](../social-authentication/) for users, you may want to disable DefectDojo API tokens because it could bypass your authentication concept. \ |
| 49 | +Using of DefectDojo API tokens can be disabled by specifying the environment variable `DD_API_TOKENS_ENABLED` to `False`. |
| 50 | +Or only `api/v2/api-token-auth/` endpoint can be disabled by setting `DD_API_TOKEN_AUTH_ENDPOINT_ENABLED` to `False`. |
| 51 | + |
| 52 | +## Sample Code |
| 53 | + |
| 54 | +Here are some simple python examples and their results produced against |
| 55 | +the `/users` endpoint: : |
| 56 | + |
| 57 | +{{< highlight python >}} |
| 58 | +import requests |
| 59 | + |
| 60 | +url = 'http://127.0.0.1:8000/api/v2/users' |
| 61 | +headers = {'content-type': 'application/json', |
| 62 | + 'Authorization': 'Token c8572a5adf107a693aa6c72584da31f4d1f1dcff'} |
| 63 | +r = requests.get(url, headers=headers, verify=True) # set verify to False if ssl cert is self-signed |
| 64 | + |
| 65 | +for key, value in r.__dict__.items(): |
| 66 | + print(f"'{key}': '{value}'") |
| 67 | + print('------------------') |
| 68 | +{{< /highlight >}} |
| 69 | + |
| 70 | +This code will return the list of all the users defined in DefectDojo. |
| 71 | +The json object result looks like : : |
| 72 | + |
| 73 | +{{< highlight json >}} |
| 74 | + [ |
| 75 | + { |
| 76 | + "first_name": "Tyagi", |
| 77 | + "id": 22, |
| 78 | + "last_login": "2019-06-18T08:05:51.925743", |
| 79 | + "last_name": "Paz", |
| 80 | + "username": "dev7958" |
| 81 | + }, |
| 82 | + { |
| 83 | + "first_name": "saurabh", |
| 84 | + "id": 31, |
| 85 | + "last_login": "2019-06-06T11:44:32.533035", |
| 86 | + "last_name": "", |
| 87 | + "username": "saurabh.paz" |
| 88 | + } |
| 89 | + ] |
| 90 | +{{< /highlight >}} |
| 91 | + |
| 92 | +Here is another example against the `/users` endpoint, this |
| 93 | +time we will filter the results to include only the users whose user |
| 94 | +name includes `jay`: |
| 95 | + |
| 96 | +{{< highlight python >}} |
| 97 | +import requests |
| 98 | + |
| 99 | +url = 'http://127.0.0.1:8000/api/v2/users/?username__contains=jay' |
| 100 | +headers = {'content-type': 'application/json', |
| 101 | + 'Authorization': 'Token c8572a5adf107a693aa6c72584da31f4d1f1dcff'} |
| 102 | +r = requests.get(url, headers=headers, verify=True) # set verify to False if ssl cert is self-signed |
| 103 | + |
| 104 | +for key, value in r.__dict__.items(): |
| 105 | + print(f"'{key}': '{value}'") |
| 106 | + print('------------------') |
| 107 | +{{< /highlight >}} |
| 108 | + |
| 109 | +The json object result is: : |
| 110 | + |
| 111 | +{{< highlight json >}} |
| 112 | +[ |
| 113 | + { |
| 114 | + "first_name": "Jay", |
| 115 | + "id": 22, |
| 116 | + "last_login": "2015-10-28T08:05:51.925743", |
| 117 | + "last_name": "Paz", |
| 118 | + "username": "jay7958" |
| 119 | + }, |
| 120 | + { |
| 121 | + "first_name": "", |
| 122 | + "id": 31, |
| 123 | + "last_login": "2015-10-13T11:44:32.533035", |
| 124 | + "last_name": "", |
| 125 | + "username": "jay.paz" |
| 126 | + } |
| 127 | +] |
| 128 | +{{< /highlight >}} |
| 129 | + |
| 130 | +See [Django Rest Framework\'s documentation on interacting with an |
| 131 | +API](http://www.django-rest-framework.org/topics/api-clients/) for |
| 132 | +additional examples and tips. |
| 133 | + |
| 134 | +## Manually calling the API |
| 135 | + |
| 136 | +Tools like Postman can be used for testing the API. |
| 137 | + |
| 138 | +Example for importing a scan result: |
| 139 | + |
| 140 | +- Verb: POST |
| 141 | +- URI: <http://localhost:8080/api/v2/import-scan/> |
| 142 | +- Headers tab: |
| 143 | + |
| 144 | + add the authentication header |
| 145 | + : - Key: Authorization |
| 146 | + - Value: Token c8572a5adf107a693aa6c72584da31f4d1f1dcff |
| 147 | + |
| 148 | +- Body tab |
| 149 | + |
| 150 | + - select \"form-data\", click \"bulk edit\". Example for a ZAP scan: |
| 151 | + |
| 152 | +<!-- --> |
| 153 | + |
| 154 | + engagement:3 |
| 155 | + verified:true |
| 156 | + active:true |
| 157 | + lead:1 |
| 158 | + tags:test |
| 159 | + scan_type:ZAP Scan |
| 160 | + minimum_severity:Info |
| 161 | + skip_duplicates:true |
| 162 | + close_old_findings:false |
| 163 | + |
| 164 | +- Body tab |
| 165 | + |
| 166 | + - Click \"Key-value\" edit |
| 167 | + - Add a \"file\" parameter of type \"file\". This will trigger |
| 168 | + multi-part form data for sending the file content |
| 169 | + - Browse for the file to upload |
| 170 | + |
| 171 | +- Click send |
| 172 | + |
| 173 | +## Clients / API Wrappers |
| 174 | + |
| 175 | +| Wrapper | Status | Notes | |
| 176 | +| -----------------------------| ------------------------| ------------------------| |
| 177 | +| [Specific python wrapper](https://github.com/DefectDojo/defectdojo_api) | working (2021-01-21) | API Wrapper including scripts for continous CI/CD uploading. Is lagging behind a bit on latest API features as we plan to revamp the API wrapper | |
| 178 | +| [Openapi python wrapper](https://github.com/alles-klar/defectdojo-api-v2-client) | | proof of concept only where we found out the the OpenAPI spec is not perfect yet | |
| 179 | +| [Java library](https://github.com/secureCodeBox/defectdojo-client-java) | working (2021-08-30) | Created by the kind people of [SecureCodeBox](https://github.com/secureCodeBox/secureCodeBox) | |
| 180 | +| [Image using the Java library](https://github.com/SDA-SE/defectdojo-client) | working (2021-08-30) | | |
| 181 | +| [.Net/C# library](https://www.nuget.org/packages/DefectDojo.Api/) | working (2021-06-08) | | |
| 182 | +| [dd-import](https://github.com/MaibornWolff/dd-import) | working (2021-08-24) | dd-import is not directly an API wrapper. It offers some convenience functions to make it easier to import findings and language data from CI/CD pipelines. | |
| 183 | + |
| 184 | +Some of the api wrappers contain quite a bit of logic to ease scanning and importing in CI/CD environments. We are in the process of simplifying this by making the DefectDojo API smarter (so api wrappers / script can be dumber). |
0 commit comments