Skip to content

Commit d5b7a10

Browse files
committed
added examples/demos for alternative blackduck client
1 parent 7e4209a commit d5b7a10

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

test/demo_client.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import os
2+
import requests
3+
from requests.adapters import HTTPAdapter
4+
import logging
5+
6+
logging.basicConfig(
7+
level=logging.INFO,
8+
format='[%(asctime)s] {%(module)s:%(lineno)d} %(levelname)s - %(message)s'
9+
)
10+
11+
# create http adapter with exponential backoff (for unstable and/or slow connections)
12+
http_adapter = HTTPAdapter(
13+
max_retries=requests.packages.urllib3.util.retry.Retry(
14+
total=5,
15+
backoff_factor=10,
16+
status_forcelist=[429,500,502,503,504]
17+
)
18+
)
19+
custom_session = requests.session()
20+
custom_session.mount('http://', http_adapter)
21+
custom_session.mount('https://', http_adapter)
22+
23+
# use os env proxy settings, if any
24+
custom_session.proxies.update({
25+
'http' : os.environ.get('http_proxy',''),
26+
'https' : os.environ.get('http_proxy', '')
27+
})
28+
29+
30+
# Brief demo
31+
from datetime import datetime, timedelta
32+
import blackduck
33+
34+
def vulns_in_all_project_versions_components(bd):
35+
for project in bd.get_projects():
36+
for version in bd.get_resource(project, 'versions'):
37+
for component in bd.get_resource(version, 'components'):
38+
for vulnerability in bd.get_resource(component, 'vulnerabilities'):
39+
print(f"{project.get('name')}-{version.get('versionName')} [{component.get('componentName')}] has {vulnerability.get('severity')} severity vulnerability '{vulnerability.get('name')}'")
40+
41+
def list_project_subresources(bd):
42+
for project in bd.get_projects():
43+
subresources = bd.list_resources(project)
44+
print(f"projects has the following subresources: {', '.join(subresources)}")
45+
return
46+
47+
48+
def projects_added_at_4_week_intervals(bd):
49+
last_count = 0
50+
count = 0
51+
print("Projects added, in 4 week intervals:")
52+
for timestamp in blackduck.Utils.iso8601_timespan(days_ago=365, delta=timedelta(weeks=4)):
53+
last_count=count
54+
count=0
55+
for project in bd.get_projects():
56+
created_at = blackduck.Utils.iso8601_to_date(project.get('createdAt'))
57+
count += (created_at <= blackduck.Utils.iso8601_to_date(timestamp))
58+
59+
print(f"{count-last_count} projects as of {timestamp}")
60+
61+
bd = blackduck.Client(
62+
token=os.environ.get('blackduck_token', 'YOUR TOKEN HERE'),
63+
base_url='https://your.blackduck.url', #!important! no trailing slash
64+
session=custom_session
65+
# verify=False # if required
66+
)
67+
68+
# If disabling warnings, don't do so at the library level:
69+
requests.packages.urllib3.disable_warnings()
70+
71+
# Various examples:
72+
# vulns_in_all_project_versions_components(bd)
73+
projects_added_at_4_week_intervals(bd)
74+
# list_project_subresources(bd)

0 commit comments

Comments
 (0)