Skip to content

Commit cfe47d0

Browse files
authored
SSPROD-2210: Added import_image and anchore_account to SDC CLI (#100)
1 parent acd8ffe commit cfe47d0

File tree

5 files changed

+87
-19
lines changed

5 files changed

+87
-19
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ python:
55
- '2.7'
66
install:
77
- sudo apt-get install linux-headers-$(uname -r) dkms gcc-multilib g++-multilib
8-
- pip install pyyaml requests
8+
- pip install pyyaml requests requests_toolbelt
99
script:
1010
- bash test/start_agent.sh
1111
- bash test/test_monitor_apis.sh
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
#
3+
# Get a specific anchore user account
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>' % 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) != 2:
22+
usage()
23+
24+
sdc_token = sys.argv[1]
25+
26+
#
27+
# Instantiate the SDC client
28+
#
29+
sdclient = SdScanningClient(sdc_token, 'https://secure.sysdig.com')
30+
31+
ok, res = sdclient.get_anchore_users_account()
32+
33+
#
34+
# Return the result
35+
#
36+
if ok:
37+
print("Anchore User Info %s" % res)
38+
else:
39+
print(res)
40+
sys.exit(1)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
requests
22
pyaml
3+
requests_toolbelt

sdcclient/_scanning.py

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import re
55
import requests
6+
from requests_toolbelt.multipart.encoder import MultipartEncoder
67
import time
78

89
try:
@@ -54,23 +55,6 @@ def add_image(self, image, force=False, dockerfile=None, annotations={}, autosub
5455

5556
return [True, res.json()]
5657

57-
def import_image(self, image_data):
58-
'''**Description**
59-
Import an image from the scanner export
60-
61-
**Arguments**
62-
- image_data: A JSON with the image information.
63-
64-
**Success Return Value**
65-
A JSON object representing the image that was imported.
66-
'''
67-
url = self.url + "/api/scanning/v1/anchore/imageimport"
68-
res = requests.post(url, data=json.dumps(image_data), headers=self.hdrs, verify=self.ssl_verify)
69-
if not self._checkResponse(res):
70-
return [False, self.lasterr]
71-
72-
return [True, res.json()]
73-
7458
def get_image(self, image, show_history=False):
7559
'''**Description**
7660
Find the image with the tag <image> and return its json description
@@ -323,6 +307,49 @@ def get_pdf_report(self, image, tag=None, date=None):
323307

324308
return [True, res.content]
325309

310+
def import_image(self, infile):
311+
'''**Description**
312+
Import an image archive
313+
314+
**Arguments**
315+
- infile: An image archive file
316+
317+
**Success Return Value**
318+
A JSON object representing the image that was imported.
319+
'''
320+
try:
321+
m = MultipartEncoder(
322+
fields={'archive_file': (infile, open(infile, 'rb'), 'text/plain')}
323+
)
324+
url = self.url+"/api/scanning/v1/import/images"
325+
326+
headers = {'Authorization': 'Bearer ' + self.token, 'Content-Type': m.content_type}
327+
res = requests.post(url, data=m, headers=headers)
328+
if not self._checkResponse(res):
329+
return [False, self.lasterr]
330+
331+
return [True, res.json()]
332+
333+
except Exception as err:
334+
print(err)
335+
336+
def get_anchore_users_account(self):
337+
'''**Description**
338+
Get the anchore user account.
339+
340+
**Arguments**
341+
- None
342+
343+
**Success Return Value**
344+
A JSON object containing user account information.
345+
'''
346+
url = self.url + "/api/scanning/v1/anchore/account"
347+
res = requests.get(url, headers=self.hdrs, verify=self.ssl_verify)
348+
if not self._checkResponse(res):
349+
return [False, self.lasterr]
350+
351+
return [True, res.json()]
352+
326353
def add_registry(self, registry, registry_user, registry_pass, insecure=False, registry_type="docker_v2", validate=True):
327354
'''**Description**
328355
Add image registry

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
author_email='[email protected]',
99
license='MIT',
1010
packages=['sdcclient'],
11-
install_requires=['requests', 'pyaml'],
11+
install_requires=['requests', 'pyaml', 'requests_toolbelt'],
1212
zip_safe=False)

0 commit comments

Comments
 (0)