Skip to content

Commit 3b666e1

Browse files
committed
Data loader example
1 parent f927cf6 commit 3b666e1

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
lines changed

defectdojo_api/defectdojo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ def _request(self, method, url, params=None, data=None, files=None):
670670
print(response.text)
671671

672672
try:
673-
if response.status_code == 201: #Created new ojbect
673+
if response.status_code == 201: #Created new object
674674
object_id = response.headers["Location"].split('/')
675675
key_id = object_id[-2]
676676
try:
@@ -681,7 +681,7 @@ def _request(self, method, url, params=None, data=None, files=None):
681681
return DefectDojoResponse(message="Upload complete", data=data, success=True)
682682
elif response.status_code == 204: #Object updates
683683
return DefectDojoResponse(message="Object updated.", success=True)
684-
elif response.status_code == 404: #Created new ojbect
684+
elif response.status_code == 404: #Object not created
685685
return DefectDojoResponse(message="Object id does not exist.", success=False)
686686
else:
687687
data = response.json()

examples/dojo_populate.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from defectdojo_api import defectdojo
2+
from random import randint
3+
import os
4+
from datetime import datetime, timedelta
5+
6+
"""
7+
Imports test data into Defect DefectDojo
8+
"""
9+
10+
# Setup DefectDojo connection information
11+
host = 'http://localhost:8000'
12+
api_key = os.environ['DOJO_API_KEY']
13+
user = 'admin'
14+
15+
"""
16+
#Optionally, specify a proxy
17+
proxies = {
18+
'http': 'http://localhost:8080',
19+
'https': 'http://localhost:8080',
20+
}
21+
#proxies=proxies
22+
"""
23+
24+
# Instantiate the DefectDojo api wrapper
25+
dd = defectdojo.DefectDojoAPI(host, api_key, user, debug=False)
26+
27+
user_id = 1 #Default user
28+
29+
def create_finding_data(product_id, engagement_id, test_id):
30+
cwe = [352, 22, 676, 863, 134, 759, 798]
31+
cwe_desc = ['Cross-Site Request Forgery (CSRF)', 'Improper Limitation of a Pathname to a Restricted Directory (\'Path Traversal\')',
32+
'Use of Potentially Dangerous Function', 'Incorrect Authorization', 'Uncontrolled Format String',
33+
'Use of a One-Way Hash without a Salt', 'Use of Hard-coded Credentials']
34+
severity=['Low','Medium','High', 'Critical']
35+
user_id = 1
36+
finding_date = datetime.now()
37+
finding_date = finding_date+timedelta(days=randint(-30,0))
38+
finding_cwe = randint(0,6)
39+
40+
finding = dd.create_finding(cwe_desc[finding_cwe], cwe_desc[finding_cwe], severity[randint(0,3)],
41+
cwe[finding_cwe], finding_date.strftime("%Y-%m-%d"), product_id, engagement_id, test_id, user_id,
42+
"None", "true", "true", "References")
43+
44+
def create_load_data(product_name, product_desc, file=None, file_test_type=None):
45+
# Create a product
46+
prod_type = 1 #1 - Research and Development, product type
47+
print "Creating product: " + product_name
48+
product = dd.create_product(product_name, product_desc, prod_type)
49+
if product.success:
50+
# Get the product id
51+
product_id = product.id()
52+
53+
# Create an engagement
54+
start_date = datetime.now()
55+
end_date = start_date+timedelta(days=randint(2,8))
56+
57+
print "Creating engagement: " + "Intial " + product_name + " Engagement"
58+
engagement = dd.create_engagement("Intial " + product_name + " Engagement", product_id, user_id,
59+
"In Progress", start_date.strftime("%Y-%m-%d"), end_date.strftime("%Y-%m-%d"))
60+
engagement_id = engagement.id()
61+
62+
# Create some tests
63+
print "Creating tests"
64+
65+
#Load scanner test data
66+
if file is not None:
67+
print "Loading scanner results from scanner export"
68+
dir_path = os.path.dirname(os.path.realpath(__file__))
69+
upload_scan = dd.upload_scan(engagement_id, "Burp Scan", dir_path + file,
70+
"true", "01/11/2016", "API")
71+
72+
i = 0
73+
while i < 6:
74+
test_type = i+1 #Select some random tests
75+
environment = randint(1,6) #Select random environments
76+
test = dd.create_test(engagement_id, test_type, environment,
77+
start_date.strftime("%Y-%m-%d"), start_date.strftime("%Y-%m-%d"))
78+
test_id = test.id()
79+
80+
f = 0
81+
f_max = randint(4,10)
82+
while f < f_max:
83+
# Load findings
84+
create_finding_data(product_id, engagement_id, test_id)
85+
f = f + 1
86+
87+
i = i + 1
88+
else:
89+
print product.message
90+
91+
##### Create Products, Engagements and Tests ########
92+
create_load_data("BodgeIt", "Product description.", "../tests/scans/Bodgeit-burp.xml", "Burp Scan")
93+
create_load_data("A CRM App", "Product description.")
94+
create_load_data("An Engineering Application", "Product description.")
95+
create_load_data("A Marketing Site", "Product description.")

tests/defectdojo_api_unit_test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ def setUp(self):
1010
api_key = os.environ['DOJO_API_KEY']
1111
user = 'admin'
1212

13-
self.dd = defectdojo.DefectDojoAPI(host, api_key, user, debug=False)
13+
proxies = {
14+
'http': 'http://localhost:8080',
15+
'https': 'http://localhost:8080',
16+
}
17+
18+
self.dd = defectdojo.DefectDojoAPI(host, api_key, user, proxies=proxies, debug=False)
1419

1520
#### USER API TESTS ####
1621
def test_01_get_user(self):

0 commit comments

Comments
 (0)