-
Notifications
You must be signed in to change notification settings - Fork 44
SSPROD-2210: Added import_image and anchore_account to SDC CLI #100
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <sysdig-token>' % 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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| requests | ||
| pyaml | ||
| requests_toolbelt |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 <image> 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): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can this function be useful? Asking only because I have not much context about it (it may be useful to add more color in the description for other users as well).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This API is needed to fetch the username for running inline-scan. The script uses this API here https://github.com/sysdiglabs/secure-inline-scan/blob/inline-scan/inline_scan.sh#L434
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| '''**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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any example (maybe not just using this function) to show how this functionality can be used? I think it can expose very interesting use cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davideschiera this function is part of the inline-scan solution which is currently in a bash script. This function essentially implements the import image API being used in the script here https://github.com/sysdiglabs/secure-inline-scan/blob/inline-scan/inline_scan.sh#L463
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can an example (similar to examples/get_anchore_users_account.py) show how to import an image and then do something with it (eg. scan it)?
I was imagine something like that, but I'm not sure if it can be meaningful or useful.