Skip to content

Commit bb5cd20

Browse files
haresh-sureshDavide Schiera
authored andcommitted
SSPROD-2436 : add get scan result (#103)
1 parent 2d8a98f commit bb5cd20

File tree

4 files changed

+125
-2
lines changed

4 files changed

+125
-2
lines changed

examples/get_image_info_by_id.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
#
3+
# Get an image scan result given image id
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_id_sha>' % sys.argv[0])
14+
print('You can find your token at https://secure.sysdig.com/#/settings/user')
15+
sys.exit(1)
16+
17+
18+
#
19+
# Parse arguments
20+
#
21+
if len(sys.argv) != 3:
22+
usage()
23+
24+
sdc_token = sys.argv[1]
25+
image_id_sha = sys.argv[2]
26+
27+
#
28+
# Instantiate the SDC client
29+
#
30+
sdclient = SdScanningClient(sdc_token, 'https://secure.sysdig.com')
31+
32+
ok, res = sdclient.get_image_info_by_id(image_id_sha)
33+
34+
#
35+
# Return the result
36+
#
37+
if ok:
38+
print("Image Info %s" % res)
39+
else:
40+
print(res)
41+
sys.exit(1)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
#
3+
# Get an image scan result given image id
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_id> <full_tag_name>' % sys.argv[0])
14+
print('You can find your token at https://secure.sysdig.com/#/settings/user')
15+
sys.exit(1)
16+
17+
18+
#
19+
# Parse arguments
20+
#
21+
if len(sys.argv) != 4:
22+
usage()
23+
24+
sdc_token = sys.argv[1]
25+
image_id = sys.argv[2]
26+
full_tag_name = sys.argv[3]
27+
28+
#
29+
# Instantiate the SDC client
30+
#
31+
sdclient = SdScanningClient(sdc_token, 'https://secure.sysdig.com')
32+
33+
ok, res = sdclient.get_image_scan_result_by_id(image_id, full_tag_name)
34+
35+
#
36+
# Return the result
37+
#
38+
if ok:
39+
print("Image Scan Result %s" % res)
40+
else:
41+
print(res)
42+
sys.exit(1)

sdcclient/_scanning.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def import_image(self, infile):
321321
m = MultipartEncoder(
322322
fields={'archive_file': (infile, open(infile, 'rb'), 'text/plain')}
323323
)
324-
url = self.url+"/api/scanning/v1/import/images"
324+
url = self.url + "/api/scanning/v1/anchore/import/images"
325325

326326
headers = {'Authorization': 'Bearer ' + self.token, 'Content-Type': m.content_type}
327327
res = requests.post(url, data=m, headers=headers, verify=self.ssl_verify)
@@ -350,6 +350,46 @@ def get_anchore_users_account(self):
350350

351351
return [True, res.json()]
352352

353+
def get_image_scan_result_by_id(self, image_id, full_tag_name):
354+
'''**Description**
355+
Get the anchore image scan result for an image id.
356+
357+
**Arguments**
358+
- image_id: Docker image id of the image whose scan result is to be fetched.
359+
- full_tag_name: The complete tag name of the image for e.g. docker.io/alpine:3.10.
360+
361+
**Success Return Value**
362+
A JSON object containing pass/fail status of image scan policy.
363+
'''
364+
url = "{base_url}/api/scanning/v1/anchore/images/by_id/{image_id}/check?tag={full_tag_name}&detail=false".format(
365+
base_url=self.url,
366+
image_id=image_id,
367+
full_tag_name=full_tag_name)
368+
res = requests.get(url, headers=self.hdrs, verify=self.ssl_verify)
369+
if not self._checkResponse(res):
370+
return [False, self.lasterr]
371+
372+
return [True, res.json()]
373+
374+
def get_image_info_by_id(self, image_id_sha):
375+
'''**Description**
376+
Get the anchore image info for an image id sha.
377+
378+
**Arguments**
379+
- image_id: Image id sha of the image.
380+
381+
**Success Return Value**
382+
A JSON object containing metadata about the image.
383+
'''
384+
url = "{base_url}/api/scanning/v1/anchore/images/{image_id_sha}".format(
385+
base_url=self.url,
386+
image_id_sha=image_id_sha)
387+
res = requests.get(url, headers=self.hdrs, verify=self.ssl_verify)
388+
if not self._checkResponse(res):
389+
return [False, self.lasterr]
390+
391+
return [True, res.json()]
392+
353393
def add_registry(self, registry, registry_user, registry_pass, insecure=False, registry_type="docker_v2", validate=True):
354394
'''**Description**
355395
Add image registry

test/test_secure_apis.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ diff /tmp/falco_rules.yaml /tmp/test_apis_user_rules.yaml
4040
# Delete all policies and then get them. There should be none.
4141
$SCRIPTDIR/../examples/delete_all_policies.py $PYTHON_SDC_TEST_API_TOKEN
4242
OUT=`$SCRIPTDIR/../examples/list_policies.py $PYTHON_SDC_TEST_API_TOKEN`
43-
if [[ $OUT != *"\"policies\": []"* ]]; then
43+
if [[ $OUT != *"[]"* ]]; then
4444
echo "Unexpected output after deleting all policies"
4545
exit 1
4646
fi

0 commit comments

Comments
 (0)