Skip to content

Commit 6732489

Browse files
Adding method for fetching latest pdf report with image digest (#116)
* Adding method for fetching latest pdf report with image digest + detail boolean flag for image id scan result
1 parent bda4504 commit 6732489

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

examples/get_image_scan_result_by_id.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,28 @@
1010

1111

1212
def usage():
13-
print('usage: %s <sysdig-token> <image_id> <full_tag_name>' % sys.argv[0])
13+
print('usage: %s <sysdig-token> <image_id> <full_tag_name> <detail>' % sys.argv[0])
1414
print('You can find your token at https://secure.sysdig.com/#/settings/user')
1515
sys.exit(1)
1616

1717

1818
#
1919
# Parse arguments
2020
#
21-
if len(sys.argv) != 4:
21+
if len(sys.argv) != 5:
2222
usage()
2323

2424
sdc_token = sys.argv[1]
2525
image_id = sys.argv[2]
2626
full_tag_name = sys.argv[3]
27+
detail = sys.argv[4]
2728

2829
#
2930
# Instantiate the SDC client
3031
#
3132
sdclient = SdScanningClient(sdc_token, 'https://secure.sysdig.com')
3233

33-
ok, res = sdclient.get_image_scan_result_by_id(image_id, full_tag_name)
34+
ok, res = sdclient.get_image_scan_result_by_id(image_id, full_tag_name, detail)
3435

3536
#
3637
# Return the result
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
#
3+
# Get a specific policy
4+
#
5+
6+
import os
7+
import sys
8+
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..'))
9+
from sdcclient import SdScanningClient
10+
11+
12+
def usage():
13+
print('usage: %s <sysdig-token> <image> <output_pdf>' % sys.argv[0])
14+
print('You can find your token at https://secure.sysdig.com/#/settings/user')
15+
sys.exit(1)
16+
17+
#
18+
# Parse arguments
19+
#
20+
if len(sys.argv) != 5:
21+
usage()
22+
23+
sdc_token = sys.argv[1]
24+
image_digest = sys.argv[2]
25+
full_tag = sys.argv[3]
26+
pdf_path = sys.argv[4]
27+
28+
#
29+
# Instantiate the SDC client
30+
#
31+
sdclient = SdScanningClient(sdc_token, 'https://secure.sysdig.com')
32+
33+
ok, res = sdclient.get_latest_pdf_report_by_digest(image_digest, full_tag)
34+
35+
#
36+
# Return the result
37+
#
38+
if ok:
39+
with open(pdf_path, 'wb') as f:
40+
f.write(res)
41+
print("PDF %s saved" % pdf_path)
42+
else:
43+
print(res)
44+
sys.exit(1)

sdcclient/_scanning.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,28 @@ def get_pdf_report(self, image, tag=None, date=None):
307307

308308
return [True, res.content]
309309

310+
def get_latest_pdf_report_by_digest(self, image_digest, full_tag=None):
311+
'''**Description**
312+
Get the latest pdf report of one image digest
313+
314+
**Arguments**
315+
- image_digest: Input image digest should be in the following formats: sha256:134dhgfd65765
316+
- tag: Specify which FULLTAG is evaluated for a given Image Digest: docker.io/alpine:latest
317+
318+
**Success Return Value**
319+
The pdf content
320+
'''
321+
url = "{base_url}/api/scanning/v1/images/{image_digest}/report?tag={tag}".format(
322+
base_url=self.url,
323+
image_digest=image_digest,
324+
tag=full_tag)
325+
326+
res = requests.get(url, headers=self.hdrs, verify=self.ssl_verify)
327+
if not self._checkResponse(res):
328+
return [False, self.lasterr]
329+
330+
return [True, res.content]
331+
310332
def import_image(self, infile, sync=False):
311333
'''**Description**
312334
Import an image archive
@@ -355,21 +377,23 @@ def get_anchore_users_account(self):
355377

356378
return [True, res.json()]
357379

358-
def get_image_scan_result_by_id(self, image_id, full_tag_name):
380+
def get_image_scan_result_by_id(self, image_id, full_tag_name, detail):
359381
'''**Description**
360382
Get the anchore image scan result for an image id.
361383
362384
**Arguments**
363385
- image_id: Docker image id of the image whose scan result is to be fetched.
364386
- full_tag_name: The complete tag name of the image for e.g. docker.io/alpine:3.10.
387+
- detail: Boolean to indicate whether full scan result API is needed.
365388
366389
**Success Return Value**
367390
A JSON object containing pass/fail status of image scan policy.
368391
'''
369-
url = "{base_url}/api/scanning/v1/anchore/images/by_id/{image_id}/check?tag={full_tag_name}&detail=false".format(
392+
url = "{base_url}/api/scanning/v1/anchore/images/by_id/{image_id}/check?tag={full_tag_name}&detail={detail}".format(
370393
base_url=self.url,
371394
image_id=image_id,
372-
full_tag_name=full_tag_name)
395+
full_tag_name=full_tag_name,
396+
detail=detail)
373397
res = requests.get(url, headers=self.hdrs, verify=self.ssl_verify)
374398
if not self._checkResponse(res):
375399
return [False, self.lasterr]

0 commit comments

Comments
 (0)