From decb4e17f6dd066a51a7f6853f1c39f812396a20 Mon Sep 17 00:00:00 2001 From: Yathi Naik Date: Thu, 5 Sep 2019 15:37:25 -0700 Subject: [PATCH] SSPROD-2210: Added import_image and anchore_account to SDC CLI --- .travis.yml | 2 +- examples/get_anchore_users_account.py | 40 ++++++++++++++++++ requirements.txt | 1 + sdcclient/_scanning.py | 61 +++++++++++++++++++-------- setup.py | 2 +- 5 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 examples/get_anchore_users_account.py diff --git a/.travis.yml b/.travis.yml index 7df44101..83e1fd28 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ python: - '2.7' install: - sudo apt-get install linux-headers-$(uname -r) dkms gcc-multilib g++-multilib - - pip install pyyaml requests + - pip install pyyaml requests requests_toolbelt script: - bash test/start_agent.sh - bash test/test_monitor_apis.sh diff --git a/examples/get_anchore_users_account.py b/examples/get_anchore_users_account.py new file mode 100644 index 00000000..93fd20fc --- /dev/null +++ b/examples/get_anchore_users_account.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# +# Get a specific anchore user account +# + +import os +import sys +sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), '..')) +from sdcclient import SdScanningClient + + +def usage(): + print('usage: %s ' % sys.argv[0]) + print('You can find your token at https://secure.sysdig.com/#/settings/user') + sys.exit(1) + + +# +# Parse arguments +# +if len(sys.argv) != 2: + usage() + +sdc_token = sys.argv[1] + +# +# Instantiate the SDC client +# +sdclient = SdScanningClient(sdc_token, 'https://secure.sysdig.com') + +ok, res = sdclient.get_anchore_users_account() + +# +# Return the result +# +if ok: + print("Anchore User Info %s" % res) +else: + print(res) + sys.exit(1) diff --git a/requirements.txt b/requirements.txt index c802313e..6eb853b4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ requests pyaml +requests_toolbelt diff --git a/sdcclient/_scanning.py b/sdcclient/_scanning.py index 46b8c38b..13b61c22 100644 --- a/sdcclient/_scanning.py +++ b/sdcclient/_scanning.py @@ -3,6 +3,7 @@ import json import re import requests +from requests_toolbelt.multipart.encoder import MultipartEncoder import time try: @@ -54,23 +55,6 @@ def add_image(self, image, force=False, dockerfile=None, annotations={}, autosub return [True, res.json()] - def import_image(self, image_data): - '''**Description** - Import an image from the scanner export - - **Arguments** - - image_data: A JSON with the image information. - - **Success Return Value** - A JSON object representing the image that was imported. - ''' - url = self.url + "/api/scanning/v1/anchore/imageimport" - res = requests.post(url, data=json.dumps(image_data), headers=self.hdrs, verify=self.ssl_verify) - if not self._checkResponse(res): - return [False, self.lasterr] - - return [True, res.json()] - def get_image(self, image, show_history=False): '''**Description** Find the image with the tag and return its json description @@ -323,6 +307,49 @@ def get_pdf_report(self, image, tag=None, date=None): return [True, res.content] + def import_image(self, infile): + '''**Description** + Import an image archive + + **Arguments** + - infile: An image archive file + + **Success Return Value** + A JSON object representing the image that was imported. + ''' + try: + m = MultipartEncoder( + fields={'archive_file': (infile, open(infile, 'rb'), 'text/plain')} + ) + url = self.url+"/api/scanning/v1/import/images" + + headers = {'Authorization': 'Bearer ' + self.token, 'Content-Type': m.content_type} + res = requests.post(url, data=m, headers=headers) + if not self._checkResponse(res): + return [False, self.lasterr] + + return [True, res.json()] + + except Exception as err: + print(err) + + def get_anchore_users_account(self): + '''**Description** + Get the anchore user account. + + **Arguments** + - None + + **Success Return Value** + A JSON object containing user account information. + ''' + url = self.url + "/api/scanning/v1/anchore/account" + res = requests.get(url, headers=self.hdrs, verify=self.ssl_verify) + if not self._checkResponse(res): + return [False, self.lasterr] + + return [True, res.json()] + def add_registry(self, registry, registry_user, registry_pass, insecure=False, registry_type="docker_v2", validate=True): '''**Description** Add image registry diff --git a/setup.py b/setup.py index 78be1d34..3f3b6c09 100644 --- a/setup.py +++ b/setup.py @@ -8,5 +8,5 @@ author_email='info@sysdig.com', license='MIT', packages=['sdcclient'], - install_requires=['requests', 'pyaml'], + install_requires=['requests', 'pyaml', 'requests_toolbelt'], zip_safe=False)