Skip to content

Commit 7cf09bb

Browse files
authored
feat: Add vulnerability detail endpoint (#144)
This commit adds the `get_vulnerability_details` method for the SdScanningClient
1 parent 82b1780 commit 7cf09bb

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

sdcclient/_scanning.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,3 +1040,23 @@ def _discover_inputimage(self, input_string):
10401040
break
10411041

10421042
return ret_type, input_string, urldigest
1043+
1044+
def get_vulnerability_details(self, id):
1045+
if id is None:
1046+
return [False, "No vulnerability ID provided"]
1047+
1048+
url = self.url + f"/api/scanning/v1/anchore/query/vulnerabilities"
1049+
1050+
params = {
1051+
"id": id,
1052+
}
1053+
1054+
res = requests.get(url, params=params, headers=self.hdrs, verify=self.ssl_verify)
1055+
if not self._checkResponse(res):
1056+
return [False, self.lasterr]
1057+
1058+
json_res = res.json()
1059+
if "vulnerabilities" not in json_res or not json_res["vulnerabilities"]:
1060+
return [False, f"Vulnerability {id} was not found"]
1061+
1062+
return [True, json_res["vulnerabilities"][0]]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
3+
from expects import *
4+
from expects import equal
5+
from mamba import *
6+
7+
from sdcclient import SdScanningClient
8+
from specs import be_successful_api_call
9+
10+
with description("Scanning vulnerability details") as self:
11+
with before.each:
12+
self.client = SdScanningClient(sdc_url=os.getenv("SDC_SECURE_URL", "https://secure.sysdig.com"),
13+
token=os.getenv("SDC_SECURE_TOKEN"))
14+
15+
with context("when retrieving a simple vulnerability"):
16+
with it("retrieves the vulnerability details correctly if exists"):
17+
vuln_id = "VULNDB-140292"
18+
ok, res = self.client.get_vulnerability_details(id=vuln_id)
19+
20+
expect((ok, res)).to(be_successful_api_call)
21+
expect(res).to(
22+
have_keys("description", "severity", "vendor_data", "nvd_data", "references",
23+
"affected_packages", id=equal(vuln_id))
24+
)
25+
26+
with it("fails if it does not exist"):
27+
non_existing_vuln_id = "VULNDB-NOEXISTS"
28+
ok, res = self.client.get_vulnerability_details(id=non_existing_vuln_id)
29+
30+
expect((ok, res)).to_not(be_successful_api_call)
31+
expect(res).to(equal(f"Vulnerability {non_existing_vuln_id} was not found"))
32+
33+
with it("fails if no id was provided"):
34+
non_existing_vuln_id = None
35+
ok, res = self.client.get_vulnerability_details(id=non_existing_vuln_id)
36+
37+
expect((ok, res)).to_not(be_successful_api_call)
38+
expect(res).to(equal(f"No vulnerability ID provided"))

0 commit comments

Comments
 (0)